@Override public Open parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException { String sessionID = parser.getAttributeValue("", "sid"); int blockSize = Integer.parseInt(parser.getAttributeValue("", "block-size")); String stanzaValue = parser.getAttributeValue("", "stanza"); StanzaType stanza = null; if (stanzaValue == null) { stanza = StanzaType.IQ; } else { stanza = StanzaType.valueOf(stanzaValue.toUpperCase(Locale.US)); } parser.next(); return new Open(sessionID, blockSize, stanza); }
@Test public void shouldUseConfiguredStanzaType() throws SmackException { InBandBytestreamManager byteStreamManager = InBandBytestreamManager.getByteStreamManager(connection); byteStreamManager.setStanza(StanzaType.MESSAGE); protocol.addResponse(null, new Verification<Open, IQ>() { public void verify(Open request, IQ response) { assertEquals(StanzaType.MESSAGE, request.getStanza()); } }); try { // start In-Band Bytestream byteStreamManager.establishSession(targetJID); } catch (XMPPException e) { protocol.verifyAll(); } }
/** * Creates a new In-Band Bytestream open request packet. * <p> * The data sent over this In-Band Bytestream will be fragmented in blocks * with the given block size. The block size should not be greater than * 65535. A recommended default value is 4096. * <p> * The data can be sent using IQ stanzas or message stanzas. * * @param sessionID unique session ID identifying this In-Band Bytestream * @param blockSize block size in which the data will be fragmented * @param stanza stanza type used to encapsulate the data */ public Open(String sessionID, int blockSize, StanzaType stanza) { super(ELEMENT, NAMESPACE); if (sessionID == null || "".equals(sessionID)) { throw new IllegalArgumentException("Session ID must not be null or empty"); } if (blockSize <= 0) { throw new IllegalArgumentException("Block size must be greater than zero"); } this.sessionID = sessionID; this.blockSize = blockSize; this.stanza = stanza; setType(Type.set); }
@Test public void shouldCorrectlyParseIQStanzaAttribute() throws Exception { String control = XMLBuilder.create("open") .a("xmlns", "http://jabber.org/protocol/ibb") .a("block-size", "4096") .a("sid", "i781hf64") .a("stanza", "iq") .asString(outputProperties); OpenIQProvider oip = new OpenIQProvider(); Open open = oip.parse(getParser(control)); assertEquals(StanzaType.IQ, open.getStanza()); }
@Test public void shouldCorrectlyParseMessageStanzaAttribute() throws Exception { String control = XMLBuilder.create("open") .a("xmlns", "http://jabber.org/protocol/ibb") .a("block-size", "4096") .a("sid", "i781hf64") .a("stanza", "message") .asString(outputProperties); OpenIQProvider oip = new OpenIQProvider(); Open open = oip.parse(getParser(control)); assertEquals(StanzaType.MESSAGE, open.getStanza()); }
/** * Initialize fields used in the tests. * @throws XMPPException * @throws SmackException */ @Before public void setup() throws XMPPException, SmackException { // build protocol verifier protocol = new Protocol(); // create mocked XMPP connection connection = ConnectionUtils.createMockedConnection(protocol, initiatorJID, xmppServer); // 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; public void verify(Message request, IQ response) { DataPacketExtension dpe = (DataPacketExtension) request.getExtension( DataPacketExtension.ELEMENT, DataPacketExtension.NAMESPACE); assertEquals(lastSeq++, dpe.getSeq()); } }; }
@Test public void shouldSetAllFieldsCorrectly() { Open open = new Open("sessionID", 4096, StanzaType.MESSAGE); assertEquals("sessionID", open.getSessionID()); assertEquals(4096, open.getBlockSize()); assertEquals(StanzaType.MESSAGE, open.getStanza()); }
public IQ parseIQ(XmlPullParser parser) throws Exception { String sessionID = parser.getAttributeValue("", "sid"); int blockSize = Integer.parseInt(parser.getAttributeValue("", "block-size")); String stanzaValue = parser.getAttributeValue("", "stanza"); StanzaType stanza = null; if (stanzaValue == null) { stanza = StanzaType.IQ; } else { stanza = StanzaType.valueOf(stanzaValue.toUpperCase()); } return new Open(sessionID, blockSize, stanza); }
/** * Creates a new In-Band Bytestream open request packet. * <p> * The data sent over this In-Band Bytestream will be fragmented in blocks * with the given block size. The block size should not be greater than * 65535. A recommended default value is 4096. * <p> * The data can be sent using IQ stanzas or message stanzas. * * @param sessionID unique session ID identifying this In-Band Bytestream * @param blockSize block size in which the data will be fragmented * @param stanza stanza type used to encapsulate the data */ public Open(String sessionID, int blockSize, StanzaType stanza) { if (sessionID == null || "".equals(sessionID)) { throw new IllegalArgumentException("Session ID must not be null or empty"); } if (blockSize <= 0) { throw new IllegalArgumentException("Block size must be greater than zero"); } this.sessionID = sessionID; this.blockSize = blockSize; this.stanza = stanza; setType(Type.SET); }
/** * An In-Band Bytestream should be successfully established using message stanzas. * * @throws Exception should not happen */ public void testInBandBytestreamWithMessageStanzas() throws Exception { XMPPConnection initiatorConnection = getConnection(0); XMPPConnection targetConnection = getConnection(1); // test data Random rand = new Random(); final byte[] data = new byte[dataSize]; rand.nextBytes(data); final SynchronousQueue<byte[]> queue = new SynchronousQueue<byte[]>(); InBandBytestreamManager targetByteStreamManager = InBandBytestreamManager.getByteStreamManager(targetConnection); InBandBytestreamListener incomingByteStreamListener = new InBandBytestreamListener() { public void incomingBytestreamRequest(InBandBytestreamRequest request) { InputStream inputStream; try { inputStream = request.accept().getInputStream(); byte[] receivedData = new byte[dataSize]; int totalRead = 0; while (totalRead < dataSize) { int read = inputStream.read(receivedData, totalRead, dataSize - totalRead); totalRead += read; } queue.put(receivedData); } catch (Exception e) { fail(e.getMessage()); } } }; targetByteStreamManager.addIncomingBytestreamListener(incomingByteStreamListener); InBandBytestreamManager initiatorByteStreamManager = InBandBytestreamManager.getByteStreamManager(initiatorConnection); initiatorByteStreamManager.setStanza(StanzaType.MESSAGE); OutputStream outputStream = initiatorByteStreamManager.establishSession( targetConnection.getUser()).getOutputStream(); // verify stream outputStream.write(data); outputStream.flush(); outputStream.close(); assertEquals("received data not equal to sent data", data, queue.take()); }
@Test public void shouldSetIQStanzaAsDefault() { Open open = new Open("sessionID", 4096); assertEquals(StanzaType.IQ, open.getStanza()); }
@Test public void shouldUseMessageStanzaIfGiven() { Open open = new Open("sessionID", 4096, StanzaType.MESSAGE); assertEquals(StanzaType.MESSAGE, open.getStanza()); }
/** * Creates a new In-Band Bytestream open request packet. * <p> * The data sent over this In-Band Bytestream will be fragmented in blocks * with the given block size. The block size should not be greater than * 65535. A recommended default value is 4096. * <p> * The data can be sent using IQ stanzas or message stanzas. * * @param sessionID * unique session ID identifying this In-Band Bytestream * @param blockSize * block size in which the data will be fragmented * @param stanza * stanza type used to encapsulate the data */ public Open(String sessionID, int blockSize, StanzaType stanza) { if (sessionID == null || "".equals(sessionID)) { throw new IllegalArgumentException( "Session ID must not be null or empty"); } if (blockSize <= 0) { throw new IllegalArgumentException( "Block size must be greater than zero"); } this.sessionID = sessionID; this.blockSize = blockSize; this.stanza = stanza; setType(Type.SET); }
/** * An In-Band Bytestream should be successfully established using message stanzas. * * @throws Exception should not happen */ public void testInBandBytestreamWithMessageStanzas() throws Exception { Connection initiatorConnection = getConnection(0); Connection targetConnection = getConnection(1); // test data Random rand = new Random(); final byte[] data = new byte[dataSize]; rand.nextBytes(data); final SynchronousQueue<byte[]> queue = new SynchronousQueue<byte[]>(); InBandBytestreamManager targetByteStreamManager = InBandBytestreamManager.getByteStreamManager(targetConnection); InBandBytestreamListener incomingByteStreamListener = new InBandBytestreamListener() { public void incomingBytestreamRequest(InBandBytestreamRequest request) { InputStream inputStream; try { inputStream = request.accept().getInputStream(); byte[] receivedData = new byte[dataSize]; int totalRead = 0; while (totalRead < dataSize) { int read = inputStream.read(receivedData, totalRead, dataSize - totalRead); totalRead += read; } queue.put(receivedData); } catch (Exception e) { fail(e.getMessage()); } } }; targetByteStreamManager.addIncomingBytestreamListener(incomingByteStreamListener); InBandBytestreamManager initiatorByteStreamManager = InBandBytestreamManager.getByteStreamManager(initiatorConnection); initiatorByteStreamManager.setStanza(StanzaType.MESSAGE); OutputStream outputStream = initiatorByteStreamManager.establishSession( targetConnection.getUser()).getOutputStream(); // verify stream outputStream.write(data); outputStream.flush(); outputStream.close(); assertEquals("received data not equal to sent data", data, queue.take()); }
/** * Creates a new In-Band Bytestream open request packet. * <p> * The data sent over this In-Band Bytestream will be fragmented in blocks * with the given block size. The block size should not be greater than * 65535. A recommended default value is 4096. * <p> * The data will be sent using IQ stanzas. * * @param sessionID unique session ID identifying this In-Band Bytestream * @param blockSize block size in which the data will be fragmented */ public Open(String sessionID, int blockSize) { this(sessionID, blockSize, StanzaType.IQ); }
/** * Returns the stanza type used to encapsulate the data. * * @return the stanza type used to encapsulate the data */ public StanzaType getStanza() { return stanza; }
/** * Creates a new In-Band Bytestream open request packet. * <p> * The data sent over this In-Band Bytestream will be fragmented in blocks * with the given block size. The block size should not be greater than * 65535. A recommended default value is 4096. * <p> * The data will be sent using IQ stanzas. * * @param sessionID * unique session ID identifying this In-Band Bytestream * @param blockSize * block size in which the data will be fragmented */ public Open(String sessionID, int blockSize) { this(sessionID, blockSize, StanzaType.IQ); }