public static Failed failed(XmlPullParser parser) throws XmlPullParserException, IOException { ParserUtils.assertAtStartTag(parser); String name; XMPPError.Condition condition = null; outerloop: while(true) { int event = parser.next(); switch (event) { case XmlPullParser.START_TAG: name = parser.getName(); String namespace = parser.getNamespace(); if (XMPPError.NAMESPACE.equals(namespace)) { condition = XMPPError.Condition.fromString(name); } break; case XmlPullParser.END_TAG: name = parser.getName(); if (Failed.ELEMENT.equals(name)) { break outerloop; } break; } } ParserUtils.assertAtEndTag(parser); return new Failed(condition); }
@Test public void testParseFailedError() throws Exception { XMPPError.Condition errorCondition = XMPPError.Condition.unexpected_request; String failedStanza = XMLBuilder.create("failed") .a("xmlns", "urn:xmpp:sm:3") .element(errorCondition.toString(), XMPPError.NAMESPACE) .asString(outputProperties); System.err.println(failedStanza); StreamManagement.Failed failedPacket = ParseStreamManagement.failed( PacketParserUtils.getParserFor(failedStanza)); assertThat(failedPacket, is(notNullValue())); assertTrue(failedPacket.getXMPPErrorCondition() == errorCondition); }
/** * Check the parser with an xml with the 404 error. */ public void test404() { // Make the XML to test String xml = "<error code='404' type='cancel'>" + "<item-not-found xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>" + "</error></iq>"; try { // Create the xml parser XmlPullParser parser = getParserFromXML(xml); // Create a packet from the xml XMPPError packet = parseError(parser); assertNotNull(packet); } catch (Exception e) { e.printStackTrace(); fail(e.getMessage()); } }
/** * Check the parser with an xml with the 404 error. */ public void testCancel() { // Make the XML to test String xml = "<error type='cancel'>" + "<conflict xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>" + "</error>"; try { // Create the xml parser XmlPullParser parser = getParserFromXML(xml); // Create a packet from the xml XMPPError error = parseError(parser); assertNotNull(error); } catch (Exception e) { e.printStackTrace(); fail(e.getMessage()); } }
public void testMessageAndApplicationDefinedError() { String xml = "<error type='modify' code='404'>" + "<undefined-condition xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>" + "<text xml:lang='en' xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'>" + "Some special application diagnostic information..." + "</text>" + "<special-application-condition xmlns='application-ns'/>" + "</error>"; try { // Create the xml parser XmlPullParser parser = getParserFromXML(xml); // Create a packet from the xml XMPPError error = parseError(parser); String sendingXML = error.toXML(); assertNotNull(error); assertNotNull(sendingXML); } catch (Exception e) { e.printStackTrace(); fail(e.getMessage()); } }
/** * Check the parser with an xml with the 404 error. */ public void testCancelWithMessage() { // Make the XML to test String xml = "<error type='cancel'>" + "<conflict xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>" + "<text xmlns='urn:ietf:params:xml:ns:xmpp-stanzas' xml:lang='langcode'>" + "Some special application diagnostic information!" + "</text>" + "</error>"; try { // Create the xml parser XmlPullParser parser = getParserFromXML(xml); // Create a packet from the xml XMPPError error = parseError(parser); assertNotNull(error); } catch (Exception e) { e.printStackTrace(); fail(e.getMessage()); } }
/** * Check the parser with an xml with the 404 error. */ public void testCancelWithMessageAndApplicationError() { // Make the XML to test String xml = "<error type='cancel' code='10'>" + "<conflict xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>" + "<text xml:lang='en' xmlns='urn:ietf:params:xml:ns:xmpp-streams'>" + "Some special application diagnostic information!" + "</text>" + "<application-defined-error xmlns='application-ns'/>" + "</error>"; try { // Create the xml parser XmlPullParser parser = getParserFromXML(xml); // Create a packet from the xml XMPPError error = parseError(parser); assertNotNull(error); } catch (Exception e) { e.printStackTrace(); fail(e.getMessage()); } }
private EntityTimeManager(XMPPConnection connection) { super(connection); if (autoEnable) enable(); connection.registerIQRequestHandler(new AbstractIqRequestHandler(Time.ELEMENT, Time.NAMESPACE, Type.get, Mode.async) { @Override public IQ handleIQRequest(IQ iqRequest) { if (enabled) { return Time.createResponse(iqRequest); } else { return IQ.createErrorResponse(iqRequest, new XMPPError(Condition.not_acceptable)); } } }); }
private void handleXMPPException(XMPPErrorException e) { XMPPError error = e.getXMPPError(); if (error != null) { switch (error.getCondition()) { case forbidden: setStatus(Status.refused); return; case bad_request: setStatus(Status.error); setError(Error.not_acceptable); break; default: setStatus(FileTransfer.Status.error); } } setException(e); }
private VersionManager(final XMPPConnection connection) { super(connection); ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection); sdm.addFeature(Version.NAMESPACE); connection.registerIQRequestHandler(new AbstractIqRequestHandler(Version.ELEMENT, Version.NAMESPACE, IQ.Type.get, Mode.async) { @Override public IQ handleIQRequest(IQ iqRequest) { if (ourVersion == null) { return IQ.createErrorResponse(iqRequest, new XMPPError(Condition.not_acceptable)); } return Version.createResultFor(iqRequest, ourVersion); } }); }
public void testGetItemsWithMultiSubscription() throws XMPPException { LeafNode node = getRandomPubnode(getManager(0), true, false); node.send((Item)null); node.send((Item)null); node.send((Item)null); node.send((Item)null); node.send((Item)null); LeafNode user2Node = (LeafNode) getManager(1).getNode(node.getId()); Subscription sub1 = user2Node.subscribe(getBareJID(1)); Subscription sub2 = user2Node.subscribe(getBareJID(1)); try { user2Node.getItems(); } catch (XMPPException exc) { assertEquals("bad-request", exc.getXMPPError().getCondition()); assertEquals(XMPPError.Type.MODIFY, exc.getXMPPError().getType()); } List<Item> items = user2Node.getItems(sub1.getId()); assertTrue(items.size() == 5); }
/** * Target should respond with not-acceptable error if no listeners for incoming In-Band * Bytestream requests are registered. * * @throws XMPPException should not happen */ public void testRespondWithErrorOnInBandBytestreamRequest() throws XMPPException { XMPPConnection targetConnection = getConnection(0); XMPPConnection initiatorConnection = getConnection(1); Open open = new Open("sessionID", 1024); open.setFrom(initiatorConnection.getUser()); open.setTo(targetConnection.getUser()); PacketCollector collector = initiatorConnection.createPacketCollector(new PacketIDFilter( open.getStanzaId())); initiatorConnection.sendStanza(open); Packet result = collector.nextResult(); assertNotNull(result.getError()); assertEquals(XMPPError.Condition.no_acceptable.toString(), result.getError().getCondition()); }
/** * Target should respond with not-acceptable error if no listeners for incoming Socks5 * bytestream requests are registered. * * @throws XMPPException should not happen */ public void testRespondWithErrorOnSocks5BytestreamRequest() throws XMPPException { XMPPConnection targetConnection = getConnection(0); XMPPConnection initiatorConnection = getConnection(1); Bytestream bytestreamInitiation = Socks5PacketUtils.createBytestreamInitiation( initiatorConnection.getUser(), targetConnection.getUser(), "session_id"); bytestreamInitiation.addStreamHost("proxy.localhost", "127.0.0.1", 7777); PacketCollector collector = initiatorConnection.createPacketCollector(new PacketIDFilter( bytestreamInitiation.getStanzaId())); initiatorConnection.sendStanza(bytestreamInitiation); Packet result = collector.nextResult(); assertNotNull(result.getError()); assertEquals(XMPPError.Condition.no_acceptable.toString(), result.getError().getCondition()); }
@Test public void getConfigFormWithInsufficientPriviliges() throws XMPPException, SmackException, IOException { ThreadedDummyConnection con = ThreadedDummyConnection.newInstance(); PubSubManager mgr = new PubSubManager(con); DiscoverInfo info = new DiscoverInfo(); Identity ident = new Identity("pubsub", null, "leaf"); info.addIdentity(ident); con.addIQReply(info); Node node = mgr.getNode("princely_musings"); PubSub errorIq = new PubSub(); XMPPError error = new XMPPError(Condition.forbidden); errorIq.setError(error); con.addIQReply(errorIq); try { node.getNodeConfiguration(); } catch (XMPPErrorException e) { Assert.assertEquals(XMPPError.Type.AUTH, e.getXMPPError().getType()); } }
/** * 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(XMPPError.Condition.not_acceptable, argument.getValue().getError().getCondition()); }
/** * Open request with a block size that exceeds the maximum block size should be replied with an * resource-constraint error. * * @throws Exception should not happen */ @Test public void shouldRejectRequestWithTooBigBlockSize() throws Exception { byteStreamManager.setMaximumBlockSize(1024); // 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(XMPPError.Condition.resource_constraint, argument.getValue().getError().getCondition()); }
/** * Invoking {@link InBandBytestreamManager#establishSession(String)} should * throw an exception if the given target does not support in-band * bytestream. * @throws SmackException * @throws XMPPException */ @Test public void shouldFailIfTargetDoesNotSupportIBB() throws SmackException, XMPPException { InBandBytestreamManager byteStreamManager = InBandBytestreamManager.getByteStreamManager(connection); try { XMPPError xmppError = new XMPPError( XMPPError.Condition.feature_not_implemented); IQ errorIQ = IBBPacketUtils.createErrorIQ(targetJID, initiatorJID, xmppError); protocol.addResponse(errorIQ); // start In-Band Bytestream byteStreamManager.establishSession(targetJID); fail("exception should be thrown"); } catch (XMPPErrorException e) { assertEquals(XMPPError.Condition.feature_not_implemented, e.getXMPPError().getCondition()); } }
/** * Test reject() method. * @throws NotConnectedException */ @Test public void shouldReplyWithErrorIfRequestIsRejected() throws NotConnectedException { 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(XMPPError.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); // wait because packet is processed in an extra thread Thread.sleep(200); // capture reply to the SOCKS5 Bytestream initiation 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(XMPPError.Condition.not_acceptable, argument.getValue().getError().getCondition()); }
/** * Complete and send an error. Complete all the null fields in an IQ error * reponse, using the sesssion information we have or some info from the * incoming packet. * * @param iq * The Jingle stanza(/packet) we are responing to * @param jingleError * the IQ stanza(/packet) we want to complete and send */ public IQ createJingleError(IQ iq, JingleError jingleError) { IQ errorPacket = null; if (jingleError != null) { // TODO This is wrong according to XEP-166 § 10, but this jingle implementation is deprecated anyways XMPPError error = new XMPPError(XMPPError.Condition.undefined_condition, jingleError); errorPacket = IQ.createErrorResponse(iq, error); // Fill in the fields with the info from the Jingle packet errorPacket.setStanzaId(iq.getStanzaId()); errorPacket.setError(error); // errorPacket.addExtension(jingleError); // NO! Let the normal state machinery do all of the sending. // getConnection().sendStanza(perror); LOGGER.severe("Error sent: " + errorPacket.toXML()); } return errorPacket; }
public String resolveRoom(XMPPConnection connection) throws XMPPException, SmackException { ObjectHelper.notEmpty(room, "room"); if (room.indexOf('@', 0) != -1) { return room; } Iterator<String> iterator = MultiUserChat.getServiceNames(connection).iterator(); if (!iterator.hasNext()) { throw new XMPPErrorException("Cannot find Multi User Chat service", new XMPPError(new XMPPError.Condition("Cannot find Multi User Chat service on connection: " + getConnectionMessage(connection)))); } String chatServer = iterator.next(); LOG.debug("Detected chat server: {}", chatServer); return room + "@" + chatServer; }
private void handleXMPPException(XMPPException e) { XMPPError error = e.getXMPPError(); if (error != null) { int code = error.getCode(); if (code == 403) { setStatus(Status.refused); return; } else if (code == 400) { setStatus(Status.error); setError(Error.not_acceptable); } else { setStatus(FileTransfer.Status.error); } } setException(e); }
private void doLoad(Connection connection, String user) throws XMPPException { setType(Type.GET); PacketCollector collector = connection.createPacketCollector( new PacketIDFilter(getPacketID())); connection.sendPacket(this); VCard result = null; try { result = (VCard) collector.nextResult(SmackConfiguration.getPacketReplyTimeout()); if (result == null) { String errorMessage = "Timeout getting VCard information"; throw new XMPPException(errorMessage, new XMPPError( XMPPError.Condition.request_timeout, errorMessage)); } if (result.getError() != null) { throw new XMPPException(result.getError()); } } catch (ClassCastException e) { System.out.println("No VCard for " + user); } copyFieldsFrom(result); }
@Override public void processStanza(Stanza packet) { XMPPConnection conn = getConnection(); conn.removeAsyncStanzaListener(this); IQ iq = (IQ) packet; if (iq.getType() != IQ.Type.result) { finish(XMPPError.Condition.internal_server_error); return; } DataForm response = iq.getExtension("x", "jabber:x:data"); if (response == null) { finish(XMPPError.Condition.internal_server_error); return; } String token = null; FormField field = response.getField("token"); if (field != null) token = field.getValues().get(0); EndpointServer from = Preferences.getEndpointServer(getContext()); finish(token, from != null ? from.toString() : conn.getXMPPServiceDomain().toString()); }
private void doLoad(Connection connection, String user) throws XMPPException { setType(Type.GET); PacketCollector collector = connection .createPacketCollector(new PacketIDFilter(getPacketID())); connection.sendPacket(this); VCard result = null; try { result = (VCard) collector.nextResult(SmackConfiguration .getPacketReplyTimeout()); if (result == null) { String errorMessage = "Timeout getting VCard information"; throw new XMPPException(errorMessage, new XMPPError( XMPPError.Condition.request_timeout, errorMessage)); } if (result.getError() != null) { throw new XMPPException(result.getError()); } } catch (ClassCastException e) { System.out.println("No VCard for " + user); } copyFieldsFrom(result); }
/** * Complete and send an error. Complete all the null fields in an IQ error * reponse, using the sesssion information we have or some info from the * incoming packet. * * @param iq * The Jingle packet we are responing to * @param error * the IQ packet we want to complete and send */ public IQ createJingleError(IQ iq, JingleError jingleError) { IQ errorPacket = null; if (jingleError != null) { errorPacket = createIQ(getSid(), iq.getFrom(), iq.getTo(), IQ.Type.ERROR); List<PacketExtension> extList = new ArrayList<PacketExtension>(); extList.add(jingleError); XMPPError error = new XMPPError(0, XMPPError.Type.CANCEL, jingleError.toString(), "", extList); // Fill in the fields with the info from the Jingle packet errorPacket.setPacketID(iq.getPacketID()); errorPacket.setError(error); // errorPacket.addExtension(jingleError); // NO! Let the normal state machinery do all of the sending. // getConnection().sendPacket(perror); LOGGER.error("Error sent: " + errorPacket.toXML()); } return errorPacket; }
/** * Target should respond with not-acceptable error if no listeners for incoming In-Band * Bytestream requests are registered. * * @throws XMPPException should not happen */ public void testRespondWithErrorOnInBandBytestreamRequest() throws XMPPException { Connection targetConnection = getConnection(0); Connection initiatorConnection = getConnection(1); Open open = new Open("sessionID", 1024); open.setFrom(initiatorConnection.getUser()); open.setTo(targetConnection.getUser()); PacketCollector collector = initiatorConnection.createPacketCollector(new PacketIDFilter( open.getPacketID())); initiatorConnection.sendPacket(open); Packet result = collector.nextResult(); assertNotNull(result.getError()); assertEquals(XMPPError.Condition.no_acceptable.toString(), result.getError().getCondition()); }
/** * Target should respond with not-acceptable error if no listeners for incoming Socks5 * bytestream requests are registered. * * @throws XMPPException should not happen */ public void testRespondWithErrorOnSocks5BytestreamRequest() throws XMPPException { Connection targetConnection = getConnection(0); Connection initiatorConnection = getConnection(1); Bytestream bytestreamInitiation = Socks5PacketUtils.createBytestreamInitiation( initiatorConnection.getUser(), targetConnection.getUser(), "session_id"); bytestreamInitiation.addStreamHost("proxy.localhost", "127.0.0.1", 7777); PacketCollector collector = initiatorConnection.createPacketCollector(new PacketIDFilter( bytestreamInitiation.getPacketID())); initiatorConnection.sendPacket(bytestreamInitiation); Packet result = collector.nextResult(); assertNotNull(result.getError()); assertEquals(XMPPError.Condition.no_acceptable.toString(), result.getError().getCondition()); }