/** * Responds an error with an specific condition. * * @param response the response to send. * @param condition the condition of the error. * @throws NotConnectedException */ private static IQ respondError(AdHocCommandData response, StanzaError.Condition condition) { return respondError(response, StanzaError.getBuilder(condition)); }
public static Builder getBuilder(Condition condition) { return getBuilder().setCondition(condition); }
public static Builder getBuilder(StanzaError xmppError) { return getBuilder().copyFrom(xmppError); }
public static ErrorIQ createErrorResponse(final IQ request, final StanzaError.Condition condition) { return createErrorResponse(request, StanzaError.getBuilder(condition)); }
public static StanzaError.Builder from(Condition condition, String descriptiveText) { StanzaError.Builder builder = getBuilder().setCondition(condition); if (descriptiveText != null) { Map<String, String> descriptiveTexts = new HashMap<>(); descriptiveTexts.put("en", descriptiveText); builder.setDescriptiveTexts(descriptiveTexts); } return builder; }
/** * Convenience method to create a new {@link Type#error IQ.Type.error} IQ * based on a {@link Type#get IQ.Type.get} or {@link Type#set IQ.Type.set} * IQ. The new stanza will be initialized with:<ul> * <li>The sender set to the recipient of the originating IQ. * <li>The recipient set to the sender of the originating IQ. * <li>The type set to {@link Type#error IQ.Type.error}. * <li>The id set to the id of the originating IQ. * <li>The child element contained in the associated originating IQ. * <li>The provided {@link StanzaError XMPPError}. * </ul> * * @param request the {@link Type#get IQ.Type.get} or {@link Type#set IQ.Type.set} IQ packet. * @param error the error to associate with the created IQ packet. * @throws IllegalArgumentException if the IQ stanza does not have a type of * {@link Type#get IQ.Type.get} or {@link Type#set IQ.Type.set}. * @return a new {@link Type#error IQ.Type.error} IQ based on the originating IQ. */ public static ErrorIQ createErrorResponse(final IQ request, final StanzaError error) { return createErrorResponse(request, StanzaError.getBuilder(error)); }
public IQ createErrorTieBreak(Jingle request) { StanzaError.Builder error = StanzaError.getBuilder(); error.setCondition(StanzaError.Condition.conflict) .addExtension(JingleError.TIE_BREAK); return IQ.createErrorResponse(request, error); }
public IQ createErrorUnsupportedInfo(Jingle request) { StanzaError.Builder error = StanzaError.getBuilder(); error.setCondition(StanzaError.Condition.feature_not_implemented) .addExtension(JingleError.UNSUPPORTED_INFO); return IQ.createErrorResponse(request, error); }
public IQ createErrorOutOfOrder(Jingle request) { StanzaError.Builder error = StanzaError.getBuilder(); error.setCondition(StanzaError.Condition.unexpected_request) .addExtension(JingleError.OUT_OF_ORDER); return IQ.createErrorResponse(request, error); }
public IQ createErrorUnknownSession(Jingle request) { StanzaError.Builder error = StanzaError.getBuilder(); error.setCondition(StanzaError.Condition.item_not_found) .addExtension(JingleError.UNKNOWN_SESSION); return IQ.createErrorResponse(request, error); }
/** * Responds an error with an specific condition. * * @param response the response to send. * @param condition the condition of the error. * @param specificCondition the adhoc command error condition. * @throws NotConnectedException */ private static IQ respondError(AdHocCommandData response, StanzaError.Condition condition, AdHocCommand.SpecificErrorCondition specificCondition) { StanzaError.Builder error = StanzaError.getBuilder(condition).addExtension(new AdHocCommandData.SpecificError(specificCondition)); return respondError(response, error); }
/** * Responses to the given packet's sender with an XMPP error that a SOCKS5 Bytestream is not * accepted. * <p> * Specified in XEP-65 5.3.1 (Example 13) * </p> * * @param packet Stanza that should be answered with a not-acceptable error * @throws NotConnectedException * @throws InterruptedException */ protected void replyRejectPacket(IQ packet) throws NotConnectedException, InterruptedException { StanzaError.Builder xmppError = StanzaError.getBuilder(StanzaError.Condition.not_acceptable); IQ errorIQ = IQ.createErrorResponse(packet, xmppError); connection().sendStanza(errorIQ); }
@Test(expected = IllegalArgumentException.class) public void descriptiveTextNullLangPassedMap() throws Exception { final String text = "Dummy descriptive text"; Map<String, String> texts = new HashMap<>(); texts.put(null, text); StanzaError .getBuilder(StanzaError.Condition.internal_server_error) .setDescriptiveTexts(texts) .build(); }
/** * Reject an incoming file transfer. * <p> * Specified in XEP-95 4.2 and 3.2 Example 8 * </p> * @param request * @throws NotConnectedException * @throws InterruptedException */ protected void rejectIncomingFileTransfer(FileTransferRequest request) throws NotConnectedException, InterruptedException { StreamInitiation initiation = request.getStreamInitiation(); // Reject as specified in XEP-95 4.2. Note that this is not to be confused with the Socks 5 // Bytestream rejection as specified in XEP-65 5.3.1 Example 13, which says that // 'not-acceptable' should be returned. This is done by Smack in // Socks5BytestreamManager.replyRejectPacket(IQ). IQ rejection = IQ.createErrorResponse(initiation, StanzaError.getBuilder( StanzaError.Condition.forbidden)); connection().sendStanza(rejection); } }
/** * Returns an error IQ. * * @param from the senders JID * @param to the recipients JID * @param condition the XMPP error condition * @return an error IQ */ public static IQ createErrorIQ(Jid from, Jid to, StanzaError.Condition condition) { StanzaError.Builder xmppError = StanzaError.getBuilder(condition); IQ errorIQ = new ErrorIQ(xmppError); errorIQ.setType(IQ.Type.error); errorIQ.setFrom(from); errorIQ.setTo(to); return errorIQ; }
@Test public void ensureNoEmptyLangInDescriptiveText() throws Exception { final String text = "Dummy descriptive text"; Map<String, String> texts = new HashMap<>(); texts.put("", text); StanzaError error = StanzaError .getBuilder(StanzaError.Condition.internal_server_error) .setDescriptiveTexts(texts) .build(); final String errorXml = XMLBuilder .create(StanzaError.ERROR).a("type", "cancel").up() .element("internal-server-error", StanzaError.ERROR_CONDITION_AND_TEXT_NAMESPACE).up() .element("text", StanzaError.ERROR_CONDITION_AND_TEXT_NAMESPACE).t(text).up() .asString(); XmlUnitUtils.assertSimilar(errorXml, error.toXML(StreamOpen.CLIENT_NAMESPACE)); }
@Override public IQ handleIQRequest(IQ iqRequest) { DiscoverItems discoverItems = (DiscoverItems) iqRequest; DiscoverItems response = new DiscoverItems(); response.setType(IQ.Type.result); response.setTo(discoverItems.getFrom()); response.setStanzaId(discoverItems.getStanzaId()); response.setNode(discoverItems.getNode()); // Add the defined items related to the requested node. Look for // the NodeInformationProvider associated with the requested node. NodeInformationProvider nodeInformationProvider = getNodeInformationProvider(discoverItems.getNode()); if (nodeInformationProvider != null) { // Specified node was found, add node items response.addItems(nodeInformationProvider.getNodeItems()); // Add packet extensions response.addExtensions(nodeInformationProvider.getNodePacketExtensions()); } else if (discoverItems.getNode() != null) { // Return <item-not-found/> error since client doesn't contain // the specified node response.setType(IQ.Type.error); response.setError(StanzaError.getBuilder(StanzaError.Condition.item_not_found)); } return response; } });
/** * According to <a href="http://xmpp.org/rfcs/rfc3920.html#stanzas-semantics-iq" * >RFC3920: IQ Semantics</a> we shouldn't respond to an IQ of type error. * @throws XmppStringprepException */ @Test public void testGeneratingErrorBasedOnError() throws XmppStringprepException { final StanzaError.Builder error = StanzaError.getBuilder(StanzaError.Condition.bad_request); final IQ request = new TestIQ(ELEMENT, NAMESPACE); request.setType(IQ.Type.error); request.setFrom(JidCreate.from("sender@test/Smack")); request.setTo(JidCreate.from("receiver@test/Smack")); request.setError(error); try { IQ.createErrorResponse(request, error); } catch (IllegalArgumentException e) { return; } fail("It shouldn't be possible to generate a response for a error IQ!"); } }
@Test public void getConfigFormWithInsufficientPrivileges() throws XMPPException, SmackException, IOException, InterruptedException { ThreadedDummyConnection con = ThreadedDummyConnection.newInstance(); PubSubManager mgr = new PubSubManager(con, PubSubManagerTest.DUMMY_PUBSUB_SERVICE); DiscoverInfo info = new DiscoverInfo(); info.setType(Type.result); info.setFrom(PubSubManagerTest.DUMMY_PUBSUB_SERVICE); Identity ident = new Identity("pubsub", null, "leaf"); info.addIdentity(ident); con.addIQReply(info); Node node = mgr.getNode("princely_musings"); PubSub errorIq = new PubSub(); errorIq.setType(Type.error); errorIq.setFrom(PubSubManagerTest.DUMMY_PUBSUB_SERVICE); StanzaError.Builder error = StanzaError.getBuilder(Condition.forbidden); errorIq.setError(error); con.addIQReply(errorIq); try { node.getNodeConfiguration(); fail(); } catch (XMPPErrorException e) { Assert.assertEquals(StanzaError.Type.AUTH, e.getStanzaError().getType()); } }
/** * 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()); }