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.     @Override
  48.     public Jid getFrom() {
  49.         return this.byteStreamRequest.getFrom();
  50.     }

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

  60.     /**
  61.      * Accepts the In-Band Bytestream open request and returns the session to
  62.      * send/receive data.
  63.      *
  64.      * @return the session to send/receive data
  65.      * @throws NotConnectedException if the XMPP connection is not connected.
  66.      * @throws InterruptedException if the calling thread was interrupted.
  67.      */
  68.     @Override
  69.     public InBandBytestreamSession accept() throws NotConnectedException, InterruptedException {
  70.         XMPPConnection connection = this.manager.getConnection();

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

  75.         // acknowledge request
  76.         IQ resultIQ = IQ.createResultIQ(this.byteStreamRequest);
  77.         connection.sendStanza(resultIQ);

  78.         return ibbSession;
  79.     }

  80.     /**
  81.      * Rejects the In-Band Bytestream request by sending a reject error to the
  82.      * initiator.
  83.      * @throws NotConnectedException if the XMPP connection is not connected.
  84.      * @throws InterruptedException if the calling thread was interrupted.
  85.      */
  86.     @Override
  87.     public void reject() throws NotConnectedException, InterruptedException {
  88.         this.manager.replyRejectPacket(this.byteStreamRequest);
  89.     }

  90. }