public IQ(IQ iq) { super(iq); type = iq.getType(); this.childElementName = iq.childElementName; this.childElementNamespace = iq.childElementNamespace; }
@Override protected boolean acceptSpecific(IQ iq) { return iq.getType() == type; }
/** * Acknowledge a IQ packet. * * @param iq * The IQ to acknowledge */ public IQ createAck(IQ iq) { IQ result = null; if (iq != null) { // Don't acknowledge ACKs, errors... if (iq.getType().equals(IQ.Type.set)) { IQ ack = IQ.createResultIQ(iq); // No! Don't send it. Let it flow to the normal way IQ results get processed and sent. // getConnection().sendStanza(ack); result = ack; } } return result; }
private boolean changePushNotificationsStatus(IQ iq) throws NotConnectedException, InterruptedException, NoResponseException, XMPPErrorException { final XMPPConnection connection = connection(); IQ responseIQ = connection.createStanzaCollectorAndSend(iq).nextResultOrThrow(); return responseIQ.getType() != Type.error; }
@Override public void verify(IQ request, Stanza response) { assertEquals(IQ.Type.get, request.getType()); }
@Override public void verify(IQ request, Stanza response) { assertEquals(IQ.Type.result, request.getType()); }
@Override public void verify(IQ request, Stanza response) { assertEquals(IQ.Type.set, request.getType()); }
@Override public void verify(IQ request, Stanza response) { assertEquals(IQ.Type.error, request.getType()); }
protected final void initializeAsResultFor(IQ request) { if (!(request.getType() == Type.get || request.getType() == Type.set)) { throw new IllegalArgumentException( "IQ must be of type 'set' or 'get'. Original IQ: " + request.toXML(null)); } setStanzaId(request.getStanzaId()); setFrom(request.getTo()); setTo(request.getFrom()); setType(Type.result); }
@Override public boolean accept(Stanza pin) { if (pin instanceof IQ) { IQ iq = (IQ) pin; if (iq.getType().equals(IQ.Type.set)) { if (iq instanceof Jingle) { Jingle jin = (Jingle) pin; if (jin.getAction().equals(JingleActionEnum.SESSION_INITIATE)) { return true; } } } } return false; } };
/** * Leave the MUCLight. * * @throws NotConnectedException * @throws InterruptedException * @throws NoResponseException * @throws XMPPErrorException */ public void leave() throws NotConnectedException, InterruptedException, NoResponseException, XMPPErrorException { HashMap<Jid, MUCLightAffiliation> affiliations = new HashMap<>(); affiliations.put(connection.getUser(), MUCLightAffiliation.none); MUCLightChangeAffiliationsIQ changeAffiliationsIQ = new MUCLightChangeAffiliationsIQ(room, affiliations); IQ responseIq = connection.createStanzaCollectorAndSend(changeAffiliationsIQ).nextResultOrThrow(); boolean roomLeft = responseIq.getType().equals(IQ.Type.result); if (roomLeft) { removeConnectionCallbacks(); } }
/** * Destroy the MUC Light. Only will work if it is requested by the owner. * * @throws NoResponseException * @throws XMPPErrorException * @throws NotConnectedException * @throws InterruptedException */ public void destroy() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { MUCLightDestroyIQ mucLightDestroyIQ = new MUCLightDestroyIQ(room); IQ responseIq = connection.createStanzaCollectorAndSend(mucLightDestroyIQ).nextResultOrThrow(); boolean roomDestroyed = responseIq.getType().equals(IQ.Type.result); if (roomDestroyed) { removeConnectionCallbacks(); } }
/** * If no listeners are registered for incoming In-Band Bytestream requests, all request should * be rejected with an error. * * @throws Exception should not happen */ @Test public void shouldRespondWithError() throws Exception { // run the listener with the initiation packet initiationListener.handleIQRequest(initBytestream); // wait because packet is processed in an extra thread Thread.sleep(200); // capture reply to the In-Band Bytestream open 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.not_acceptable, argument.getValue().getError().getCondition()); }
/** * Test reject() method. * @throws NotConnectedException * @throws InterruptedException */ @Test public void shouldReplyWithErrorIfRequestIsRejected() throws NotConnectedException, InterruptedException { InBandBytestreamRequest ibbRequest = new InBandBytestreamRequest( byteStreamManager, initBytestream); // reject request ibbRequest.reject(); // capture reply to the In-Band Bytestream open 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.not_acceptable, argument.getValue().getError().getCondition()); }
/** * If no listeners are registered for incoming SOCKS5 Bytestream requests, all request should be * rejected with an error. * * @throws Exception should not happen */ @Test public void shouldRespondWithError() throws Exception { // run the listener with the initiation packet initiationListener.handleIQRequest(initBytestream); // capture reply to the SOCKS5 Bytestream initiation ArgumentCaptor<IQ> argument = ArgumentCaptor.forClass(IQ.class); verify(connection, timeout(TIMEOUT)).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.not_acceptable, argument.getValue().getError().getCondition()); }
/** * Test accept() method. * * @throws Exception should not happen */ @Test public void shouldReturnSessionIfRequestIsAccepted() throws Exception { InBandBytestreamRequest ibbRequest = new InBandBytestreamRequest( byteStreamManager, initBytestream); // accept request InBandBytestreamSession session = ibbRequest.accept(); // capture reply to the In-Band Bytestream open request ArgumentCaptor<IQ> argument = ArgumentCaptor.forClass(IQ.class); verify(connection).sendStanza(argument.capture()); // assert that reply is the correct acknowledgment packet assertEquals(initiatorJID, argument.getValue().getTo()); assertEquals(IQ.Type.result, argument.getValue().getType()); assertNotNull(session); assertNotNull(session.getInputStream()); assertNotNull(session.getOutputStream()); }
@Test public void checkSlotErrorFileToLarge() throws Exception { IQ fileTooLargeErrorIQ = PacketParserUtils.parseStanza(slotErrorFileToLarge); Assert.assertEquals(IQ.Type.error, fileTooLargeErrorIQ.getType()); FileTooLargeError fileTooLargeError = FileTooLargeError.from(fileTooLargeErrorIQ); Assert.assertEquals(20000, fileTooLargeError.getMaxFileSize()); } }
@Test public void checkProvider() throws Exception { // @formatter:off String control = "<iq from='capulet.lit' to='juliet@capulet.lit/balcony' id='s2c1' type='get'>" + "<ping xmlns='urn:xmpp:ping'/>" + "</iq>"; // @formatter:on DummyConnection con = new DummyConnection(); con.connect(); // Enable ping for this connection PingManager.getInstanceFor(con); IQ pingRequest = PacketParserUtils.parseStanza(control); assertTrue(pingRequest instanceof Ping); con.processStanza(pingRequest); Stanza pongPacket = con.getSentPacket(); assertTrue(pongPacket instanceof IQ); IQ pong = (IQ) pongPacket; assertThat("capulet.lit", equalsCharSequence(pong.getTo())); assertEquals("s2c1", pong.getStanzaId()); assertEquals(IQ.Type.result, pong.getType()); }
/** * Test creating a simple and empty IQ response. * @throws XmppStringprepException */ @Test public void testGeneratingSimpleResponse() throws XmppStringprepException { final IQ request = new TestIQ(ELEMENT, NAMESPACE); request.setFrom(JidCreate.from("sender@test/Smack")); request.setTo(JidCreate.from("receiver@test/Smack")); final IQ result = IQ.createResultIQ(request); assertEquals(IQ.Type.result, result.getType()); assertNotNull(result.getStanzaId()); assertEquals(request.getStanzaId(), result.getStanzaId()); assertEquals(request.getFrom(), result.getTo()); assertEquals(request.getTo(), result.getFrom()); assertEquals("", result.getChildElementXML().toString()); }
/** * Test creating a error response based on an IQ request. * @throws XmppStringprepException */ @Test public void testGeneratingValidErrorResponse() throws XmppStringprepException { final StanzaError.Builder error = StanzaError.getBuilder(StanzaError.Condition.bad_request); final IQ request = new TestIQ(ELEMENT, NAMESPACE); request.setType(IQ.Type.set); request.setFrom(JidCreate.from("sender@test/Smack")); request.setTo(JidCreate.from("receiver@test/Smack")); final IQ result = IQ.createErrorResponse(request, error); assertEquals(IQ.Type.error, result.getType()); assertNotNull(result.getStanzaId()); assertEquals(request.getStanzaId(), result.getStanzaId()); assertEquals(request.getFrom(), result.getTo()); assertEquals(error.build().toXML(), result.getError().toXML()); // TODO this test was never valid // assertEquals(CHILD_ELEMENT, result.getChildElementXML()); }