/** * The default constructor for the In-Band Bytestream Negotiator. * * @param connection The connection which this negotiator works on. */ protected IBBTransferNegotiator(XMPPConnection connection) { super(connection); this.manager = InBandBytestreamManager.getByteStreamManager(connection); }
@Override public void connectionCreated(final XMPPConnection connection) { // create the manager for this connection InBandBytestreamManager.getByteStreamManager(connection); // register shutdown listener connection.addConnectionListener(new AbstractConnectionClosedListener() { @Override public void connectionTerminated() { InBandBytestreamManager.getByteStreamManager(connection).disableService(); } @Override public void connected(XMPPConnection connection) { InBandBytestreamManager.getByteStreamManager(connection); } }); } });
@Override public void initiateIncomingSession(final JingleTransportInitiationCallback callback) { LOGGER.log(Level.INFO, "Await Jingle InBandBytestream session."); InBandBytestreamManager.getByteStreamManager(jingleSession.getConnection()).addIncomingBytestreamListener(new BytestreamListener() { @Override public void incomingBytestreamRequest(BytestreamRequest request) { if (request.getFrom().asFullJidIfPossible().equals(jingleSession.getRemote()) && request.getSessionID().equals(theirProposal.getSessionId())) { BytestreamSession session; try { session = request.accept(); } catch (InterruptedException | SmackException | XMPPException.XMPPErrorException e) { callback.onException(e); return; } callback.onSessionInitiated(session); } } }); }
/** * Test that * {@link InBandBytestreamManager#getByteStreamManager(XMPPConnection)} returns * one bytestream manager for every connection. */ @Test public void shouldHaveOneManagerForEveryConnection() { // mock two connections XMPPConnection connection1 = mock(XMPPConnection.class); XMPPConnection connection2 = mock(XMPPConnection.class); // get bytestream manager for the first connection twice InBandBytestreamManager conn1ByteStreamManager1 = InBandBytestreamManager.getByteStreamManager(connection1); InBandBytestreamManager conn1ByteStreamManager2 = InBandBytestreamManager.getByteStreamManager(connection1); // get bytestream manager for second connection InBandBytestreamManager conn2ByteStreamManager1 = InBandBytestreamManager.getByteStreamManager(connection2); // assertions assertEquals(conn1ByteStreamManager1, conn1ByteStreamManager2); assertNotSame(conn1ByteStreamManager1, conn2ByteStreamManager1); }
@Override public void initiateOutgoingSession(JingleTransportInitiationCallback callback) { LOGGER.log(Level.INFO, "Initiate Jingle InBandBytestream session."); BytestreamSession session; try { session = InBandBytestreamManager.getByteStreamManager(jingleSession.getConnection()) .establishSession(jingleSession.getRemote(), theirProposal.getSessionId()); callback.onSessionInitiated(session); } catch (SmackException.NoResponseException | InterruptedException | SmackException.NotConnectedException | XMPPException.XMPPErrorException e) { callback.onException(e); } }
@Test(expected = IllegalArgumentException.class) public void shouldNotAllowTooBigMaximumBlockSize() { InBandBytestreamManager byteStreamManager = InBandBytestreamManager.getByteStreamManager(connection); byteStreamManager.setMaximumBlockSize(1000000); }
@Test(expected = IllegalArgumentException.class) public void shouldNotAllowTooBigDefaultBlockSize() { InBandBytestreamManager byteStreamManager = InBandBytestreamManager.getByteStreamManager(connection); byteStreamManager.setDefaultBlockSize(1000000); }
/** * Initialize fields used in the tests. */ @Before public void setup() { // mock connection connection = mock(XMPPConnection.class); // initialize InBandBytestreamManager to get the InitiationListener byteStreamManager = InBandBytestreamManager.getByteStreamManager(connection); // create a In-Band Bytestream open packet initBytestream = new Open(sessionID, 4096); initBytestream.setFrom(initiatorJID); initBytestream.setTo(targetJID); }
@Test public void shouldCorrectlySetMaximumBlockSize() { InBandBytestreamManager byteStreamManager = InBandBytestreamManager.getByteStreamManager(connection); byteStreamManager.setMaximumBlockSize(1024); assertEquals(1024, byteStreamManager.getMaximumBlockSize()); }
InBandBytestreamManager.getByteStreamManager(this.connection).getSessions().remove(byteStreamRequest.getSessionID());
@Test public void shouldCorrectlySetDefaultBlockSize() { InBandBytestreamManager byteStreamManager = InBandBytestreamManager.getByteStreamManager(connection); byteStreamManager.setDefaultBlockSize(1024); assertEquals(1024, byteStreamManager.getDefaultBlockSize()); }
/** * Initialize fields used in the tests. */ @Before public void setup() { // mock connection connection = mock(XMPPConnection.class); // initialize InBandBytestreamManager to get the InitiationListener byteStreamManager = InBandBytestreamManager.getByteStreamManager(connection); // get the InitiationListener from InBandByteStreamManager initiationListener = Whitebox.getInternalState(byteStreamManager, InitiationListener.class); // create a In-Band Bytestream open packet initBytestream = new Open(sessionID, 4096); initBytestream.setFrom(initiatorJID); initBytestream.setTo(targetJID); }
@Test public void shouldUseConfiguredStanzaType() throws SmackException, InterruptedException, XMPPException { InBandBytestreamManager byteStreamManager = InBandBytestreamManager.getByteStreamManager(connection); byteStreamManager.setStanza(StanzaType.MESSAGE); protocol.addResponse(null, new Verification<Open, IQ>() { @Override public void verify(Open request, IQ response) { assertEquals(StanzaType.MESSAGE, request.getStanza()); } }); // start In-Band Bytestream byteStreamManager.establishSession(targetJID); protocol.verifyAll(); }
/** * If a close request to an unknown session is received it should be replied * with an <item-not-found/> error. * * @throws Exception should not happen */ @Test public void shouldReplyErrorIfSessionIsUnknown() throws Exception { // mock connection XMPPConnection connection = mock(XMPPConnection.class); // initialize InBandBytestreamManager to get the CloseListener InBandBytestreamManager byteStreamManager = InBandBytestreamManager.getByteStreamManager(connection); // get the CloseListener from InBandByteStreamManager CloseListener closeListener = Whitebox.getInternalState(byteStreamManager, CloseListener.class); Close close = new Close("unknownSessionId"); close.setFrom(initiatorJID); close.setTo(targetJID); closeListener.handleIQRequest(close); // wait because packet is processed in an extra thread Thread.sleep(200); // capture reply to the In-Band Bytestream close request ArgumentCaptor<IQ> argument = ArgumentCaptor.forClass(IQ.class); verify(connection).sendStanza(argument.capture()); // assert that reply is the correct error packet assertEquals(initiatorJID, argument.getValue().getTo()); assertEquals(IQ.Type.error, argument.getValue().getType()); assertEquals(StanzaError.Condition.item_not_found, argument.getValue().getError().getCondition()); }
InBandBytestreamManager byteStreamManager = InBandBytestreamManager.getByteStreamManager(connection);
/** * Initialize fields used in the tests. * @throws XMPPException * @throws SmackException * @throws InterruptedException */ @Before public void setup() throws XMPPException, SmackException, InterruptedException { // build protocol verifier protocol = new Protocol(); // create mocked XMPP connection connection = ConnectionUtils.createMockedConnection(protocol, initiatorJID); // initialize InBandBytestreamManager to get the InitiationListener byteStreamManager = InBandBytestreamManager.getByteStreamManager(connection); // create a In-Band Bytestream open packet initBytestream = new Open(sessionID, blockSize); initBytestream.setFrom(initiatorJID); initBytestream.setTo(targetJID); incrementingSequence = new Verification<Data, IQ>() { long lastSeq = 0; @Override public void verify(Data request, IQ response) { assertEquals(lastSeq++, request.getDataPacketExtension().getSeq()); } }; }
/** * Initialize fields used in the tests. * @throws XMPPException * @throws SmackException * @throws InterruptedException */ @Before public void setup() throws XMPPException, SmackException, InterruptedException { // build protocol verifier protocol = new Protocol(); // create mocked XMPP connection connection = ConnectionUtils.createMockedConnection(protocol, initiatorJID); // initialize InBandBytestreamManager to get the InitiationListener byteStreamManager = InBandBytestreamManager.getByteStreamManager(connection); // create a In-Band Bytestream open packet with message stanza initBytestream = new Open(sessionID, blockSize, StanzaType.MESSAGE); initBytestream.setFrom(initiatorJID); initBytestream.setTo(targetJID); incrementingSequence = new Verification<Message, IQ>() { long lastSeq = 0; @Override public void verify(Message request, IQ response) { DataPacketExtension dpe = request.getExtension( DataPacketExtension.ELEMENT, DataPacketExtension.NAMESPACE); assertEquals(lastSeq++, dpe.getSeq()); } }; }
@Test public void shouldReturnSession() throws Exception { InBandBytestreamManager byteStreamManager = InBandBytestreamManager.getByteStreamManager(connection); IQ success = IBBPacketUtils.createResultIQ(targetJID, initiatorJID); protocol.addResponse(success, Verification.correspondingSenderReceiver, Verification.requestTypeSET); // start In-Band Bytestream InBandBytestreamSession session = byteStreamManager.establishSession(targetJID); assertNotNull(session); assertNotNull(session.getInputStream()); assertNotNull(session.getOutputStream()); protocol.verifyAll(); }
/** * Invoking {@link InBandBytestreamManager#establishSession(org.jxmpp.jid.Jid)} should * throw an exception if the given target does not support in-band * bytestream. * @throws SmackException * @throws XMPPException * @throws InterruptedException */ @Test public void shouldFailIfTargetDoesNotSupportIBB() throws SmackException, XMPPException, InterruptedException { InBandBytestreamManager byteStreamManager = InBandBytestreamManager.getByteStreamManager(connection); try { IQ errorIQ = IBBPacketUtils.createErrorIQ(targetJID, initiatorJID, StanzaError.Condition.feature_not_implemented); protocol.addResponse(errorIQ); // start In-Band Bytestream byteStreamManager.establishSession(targetJID); fail("exception should be thrown"); } catch (XMPPErrorException e) { assertEquals(StanzaError.Condition.feature_not_implemented, e.getStanzaError().getCondition()); } }
public void connectionCreated(Connection connection) { final InBandBytestreamManager manager; manager = InBandBytestreamManager.getByteStreamManager(connection); // register shutdown listener connection.addConnectionListener(new AbstractConnectionListener() { public void connectionClosed() { manager.disableService(); } }); } });