InBandBytestreamRequest.java

  1. /**
  2.  *
  3.  * Copyright the original author or authors
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.bytestreams.ibb;

  18. import org.jivesoftware.smack.SmackException.NotConnectedException;
  19. import org.jivesoftware.smack.XMPPConnection;
  20. import org.jivesoftware.smack.packet.IQ;
  21. import org.jivesoftware.smackx.bytestreams.BytestreamRequest;
  22. import org.jivesoftware.smackx.bytestreams.ibb.packet.Open;
  23. import org.jxmpp.jid.Jid;

  24. /**
  25.  * InBandBytestreamRequest class handles incoming In-Band Bytestream requests.
  26.  *
  27.  * @author Henning Staib
  28.  */
  29. public class InBandBytestreamRequest implements BytestreamRequest {

  30.     /* the bytestream initialization request */
  31.     private final Open byteStreamRequest;

  32.     /*
  33.      * In-Band Bytestream manager containing the XMPP connection and helper
  34.      * methods
  35.      */
  36.     private final InBandBytestreamManager manager;

  37.     protected InBandBytestreamRequest(InBandBytestreamManager manager,
  38.                     Open byteStreamRequest) {
  39.         this.manager = manager;
  40.         this.byteStreamRequest = byteStreamRequest;
  41.     }

  42.     /**
  43.      * Returns the sender of the In-Band Bytestream open request.
  44.      *
  45.      * @return the sender of the In-Band Bytestream open request
  46.      */
  47.     public Jid getFrom() {
  48.         return this.byteStreamRequest.getFrom();
  49.     }

  50.     /**
  51.      * Returns the session ID of the In-Band Bytestream open request.
  52.      *
  53.      * @return the session ID of the In-Band Bytestream open request
  54.      */
  55.     public String getSessionID() {
  56.         return this.byteStreamRequest.getSessionID();
  57.     }

  58.     /**
  59.      * Accepts the In-Band Bytestream open request and returns the session to
  60.      * send/receive data.
  61.      *
  62.      * @return the session to send/receive data
  63.      * @throws NotConnectedException
  64.      * @throws InterruptedException
  65.      */
  66.     public InBandBytestreamSession accept() throws NotConnectedException, InterruptedException {
  67.         XMPPConnection connection = this.manager.getConnection();

  68.         // create In-Band Bytestream session and store it
  69.         InBandBytestreamSession ibbSession = new InBandBytestreamSession(connection,
  70.                         this.byteStreamRequest, this.byteStreamRequest.getFrom());
  71.         this.manager.getSessions().put(this.byteStreamRequest.getSessionID(), ibbSession);

  72.         // acknowledge request
  73.         IQ resultIQ = IQ.createResultIQ(this.byteStreamRequest);
  74.         connection.sendStanza(resultIQ);

  75.         return ibbSession;
  76.     }

  77.     /**
  78.      * Rejects the In-Band Bytestream request by sending a reject error to the
  79.      * initiator.
  80.      * @throws NotConnectedException
  81.      * @throws InterruptedException
  82.      */
  83.     public void reject() throws NotConnectedException, InterruptedException {
  84.         this.manager.replyRejectPacket(this.byteStreamRequest);
  85.     }

  86. }