IBBTransferNegotiator.java

  1. /**
  2.  *
  3.  * Copyright 2003-2006 Jive Software.
  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.filetransfer;

  18. import java.io.InputStream;
  19. import java.io.OutputStream;

  20. import org.jivesoftware.smack.SmackException.NoResponseException;
  21. import org.jivesoftware.smack.SmackException.NotConnectedException;
  22. import org.jivesoftware.smack.XMPPConnection;
  23. import org.jivesoftware.smack.XMPPException.XMPPErrorException;
  24. import org.jivesoftware.smack.packet.Stanza;
  25. import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamManager;
  26. import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamRequest;
  27. import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamSession;
  28. import org.jivesoftware.smackx.bytestreams.ibb.packet.DataPacketExtension;
  29. import org.jivesoftware.smackx.bytestreams.ibb.packet.Open;
  30. import org.jivesoftware.smackx.si.packet.StreamInitiation;
  31. import org.jxmpp.jid.Jid;

  32. /**
  33.  * The In-Band Bytestream file transfer method, or IBB for short, transfers the
  34.  * file over the same XML Stream used by XMPP. It is the fall-back mechanism in
  35.  * case the SOCKS5 bytestream method of transferring files is not available.
  36.  *
  37.  * @author Alexander Wenckus
  38.  * @author Henning Staib
  39.  * @see <a href="http://xmpp.org/extensions/xep-0047.html">XEP-0047: In-Band
  40.  *      Bytestreams (IBB)</a>
  41.  */
  42. public class IBBTransferNegotiator extends StreamNegotiator {

  43.     private XMPPConnection connection;

  44.     private InBandBytestreamManager manager;

  45.     /**
  46.      * The default constructor for the In-Band Bytestream Negotiator.
  47.      *
  48.      * @param connection The connection which this negotiator works on.
  49.      */
  50.     protected IBBTransferNegotiator(XMPPConnection connection) {
  51.         this.connection = connection;
  52.         this.manager = InBandBytestreamManager.getByteStreamManager(connection);
  53.     }

  54.     public OutputStream createOutgoingStream(String streamID, Jid initiator,
  55.                     Jid target) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
  56.         InBandBytestreamSession session = this.manager.establishSession(target, streamID);
  57.         session.setCloseBothStreamsEnabled(true);
  58.         return session.getOutputStream();
  59.     }

  60.     public InputStream createIncomingStream(StreamInitiation initiation)
  61.                     throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
  62.         /*
  63.          * In-Band Bytestream initiation listener must ignore next in-band bytestream request with
  64.          * given session ID
  65.          */
  66.         this.manager.ignoreBytestreamRequestOnce(initiation.getSessionID());

  67.         Stanza streamInitiation = initiateIncomingStream(this.connection, initiation);
  68.         return negotiateIncomingStream(streamInitiation);
  69.     }

  70.     @Override
  71.     public void newStreamInitiation(Jid from, String streamID) {
  72.         /*
  73.          * this method is always called prior to #negotiateIncomingStream() so
  74.          * the In-Band Bytestream initiation listener must ignore the next
  75.          * In-Band Bytestream request with the given session ID
  76.          */
  77.         this.manager.ignoreBytestreamRequestOnce(streamID);
  78.     }

  79.     public String[] getNamespaces() {
  80.         return new String[] { DataPacketExtension.NAMESPACE };
  81.     }

  82.     InputStream negotiateIncomingStream(Stanza streamInitiation) throws NotConnectedException, InterruptedException {
  83.         // build In-Band Bytestream request
  84.         InBandBytestreamRequest request = new ByteStreamRequest(this.manager,
  85.                         (Open) streamInitiation);

  86.         // always accept the request
  87.         InBandBytestreamSession session = request.accept();
  88.         session.setCloseBothStreamsEnabled(true);
  89.         return session.getInputStream();
  90.     }

  91.     /**
  92.      * Derive from InBandBytestreamRequest to access protected constructor.
  93.      */
  94.     private static class ByteStreamRequest extends InBandBytestreamRequest {

  95.         private ByteStreamRequest(InBandBytestreamManager manager, Open byteStreamRequest) {
  96.             super(manager, byteStreamRequest);
  97.         }

  98.     }

  99. }