From 23bb5c56251b69693c5a8880f373da54e3b5f57a Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Wed, 20 Jun 2018 15:55:48 +0200 Subject: [PATCH 1/3] Add XMPPErrorException.getStanzaError() Also deprecate getXMPPError and let StanzaError implement ExtensionElement. --- .../org/jivesoftware/smack/LoginTest.java | 6 ++-- .../smack/packet/PrivacyTest.java | 6 ++-- .../org/jivesoftware/smack/XMPPException.java | 12 +++++++ .../smack/packet/StanzaError.java | 35 +++++++++++++++---- .../smack/packet/StanzaErrorTextElement.java | 2 +- .../smack/util/PacketParserUtils.java | 2 +- .../smack/util/PacketParserUtilsTest.java | 10 +++--- .../smackx/iot/control/IoTControlManager.java | 2 +- .../jivesoftware/smackx/CompressionTest.java | 2 +- .../smackx/LastActivityManagerTest.java | 6 ++-- .../smackx/ServiceDiscoveryManagerTest.java | 2 +- .../smackx/muc/MultiUserChatTest.java | 22 ++++++------ .../pubsub/MultiUserSubscriptionUseCases.java | 4 +-- .../smackx/pubsub/PublisherUseCases.java | 8 ++--- .../smackx/pubsub/TestEvents.java | 2 +- .../smackx/commands/AdHocCommandManager.java | 4 +-- .../filetransfer/OutgoingFileTransfer.java | 2 +- .../smackx/iqprivate/PrivateDataManager.java | 2 +- .../jivesoftware/smackx/ping/PingManager.java | 2 +- .../smackx/pubsub/PubSubManager.java | 14 ++++---- .../ibb/InBandBytestreamManagerTest.java | 2 +- .../socks5/Socks5ByteStreamManagerTest.java | 2 +- .../socks5/Socks5ByteStreamRequestTest.java | 8 ++--- .../socks5/Socks5ClientForInitiatorTest.java | 2 +- .../smackx/pubsub/ConfigureFormTest.java | 2 +- ...SmackIntegrationTestFrameworkUnitTest.java | 4 +-- .../smackx/omemo/OmemoService.java | 2 +- .../smack/sm/packet/StreamManagement.java | 4 +-- .../sm/provider/ParseStreamManagement.java | 2 +- .../smack/tcp/XMPPTCPConnection.java | 2 +- .../provider/ParseStreamManagementTest.java | 8 ++--- 31 files changed, 108 insertions(+), 75 deletions(-) diff --git a/smack-core/src/integration-test/java/org/jivesoftware/smack/LoginTest.java b/smack-core/src/integration-test/java/org/jivesoftware/smack/LoginTest.java index 07fccf8af..18a940667 100644 --- a/smack-core/src/integration-test/java/org/jivesoftware/smack/LoginTest.java +++ b/smack-core/src/integration-test/java/org/jivesoftware/smack/LoginTest.java @@ -120,7 +120,7 @@ public class LoginTest extends SmackTestCase { conn.getAccountManager().createAccount("user_1", "user_1", getAccountCreationParameters()); } catch (XMPPException e) { // Do nothing if the account already exists - if (e.getXMPPError().getCode() != 409) { + if (e.getStanzaError().getCode() != 409) { throw e; } // Else recreate the connection, ins case the server closed it as @@ -141,8 +141,8 @@ public class LoginTest extends SmackTestCase { } } catch (XMPPException e) { - if (e.getXMPPError() != null) { - assertEquals("Wrong error code returned", 406, e.getXMPPError().getCode()); + if (e.getStanzaError() != null) { + assertEquals("Wrong error code returned", 406, e.getStanzaError().getCode()); } else { fail(e.getMessage()); } diff --git a/smack-core/src/integration-test/java/org/jivesoftware/smack/packet/PrivacyTest.java b/smack-core/src/integration-test/java/org/jivesoftware/smack/packet/PrivacyTest.java index 8268d5dbe..5799b28af 100644 --- a/smack-core/src/integration-test/java/org/jivesoftware/smack/packet/PrivacyTest.java +++ b/smack-core/src/integration-test/java/org/jivesoftware/smack/packet/PrivacyTest.java @@ -228,7 +228,7 @@ public class PrivacyTest extends SmackTestCase { // The list should not exist and an error will be raised privacyManager.getDefaultList(); } catch (XMPPException xmppException) { - assertEquals(404, xmppException.getXMPPError().getCode()); + assertEquals(404, xmppException.getStanzaError().getCode()); } assertEquals(null, null); @@ -255,7 +255,7 @@ public class PrivacyTest extends SmackTestCase { // The list should not exist and an error will be raised privacyManager.getActiveList(); } catch (XMPPException xmppException) { - assertEquals(404, xmppException.getXMPPError().getCode()); + assertEquals(404, xmppException.getStanzaError().getCode()); } assertEquals(null, null); @@ -341,7 +341,7 @@ public class PrivacyTest extends SmackTestCase { // The list should not exist and an error will be raised privacyManager.getPrivacyList(listName); } catch (XMPPException xmppException) { - assertEquals(404, xmppException.getXMPPError().getCode()); + assertEquals(404, xmppException.getStanzaError().getCode()); } } catch (Exception e) { e.printStackTrace(); diff --git a/smack-core/src/main/java/org/jivesoftware/smack/XMPPException.java b/smack-core/src/main/java/org/jivesoftware/smack/XMPPException.java index 2ea90aaca..2c9180fcd 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/XMPPException.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/XMPPException.java @@ -123,11 +123,23 @@ public abstract class XMPPException extends Exception { * one. * * @return the XMPPError associated with this exception. + * @deprecated use {@link #getStanzaError()} instead. */ + @Deprecated + // TODO Remove in Smack 4.4. public StanzaError getXMPPError() { return error; } + /** + * Returns the stanza error extension element associated with this exception. + * + * @return the stanza error extension element associated with this exception. + */ + public StanzaError getStanzaError() { + return error; + } + /** * Get the request which triggered the error response causing this exception. * diff --git a/smack-core/src/main/java/org/jivesoftware/smack/packet/StanzaError.java b/smack-core/src/main/java/org/jivesoftware/smack/packet/StanzaError.java index de8d7a446..6ebd523ab 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/packet/StanzaError.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/packet/StanzaError.java @@ -62,9 +62,16 @@ import org.jivesoftware.smack.util.XmlStringBuilder; * @see RFC 6120 - 8.3.2 Syntax: The Syntax of XMPP error stanzas */ // TODO Use StanzaErrorTextElement here. -public class StanzaError extends AbstractError { +public class StanzaError extends AbstractError implements ExtensionElement { + + public static final String ERROR_CONDITION_AND_TEXT_NAMESPACE = "urn:ietf:params:xml:ns:xmpp-stanzas"; + + /** + * TODO describe me. + */ + @Deprecated + public static final String NAMESPACE = ERROR_CONDITION_AND_TEXT_NAMESPACE; - public static final String NAMESPACE = "urn:ietf:params:xml:ns:xmpp-stanzas"; public static final String ERROR = "error"; private static final Logger LOGGER = Logger.getLogger(StanzaError.class.getName()); @@ -117,7 +124,7 @@ public class StanzaError extends AbstractError { */ public StanzaError(Condition condition, String conditionText, String errorGenerator, Type type, Map descriptiveTexts, List extensions, Stanza stanza) { - super(descriptiveTexts, NAMESPACE, extensions); + super(descriptiveTexts, ERROR_CONDITION_AND_TEXT_NAMESPACE, extensions); this.condition = Objects.requireNonNull(condition, "condition must not be null"); this.stanza = stanza; // Some implementations may send the condition as non-empty element containing the empty string, that is @@ -197,20 +204,34 @@ public class StanzaError extends AbstractError { return sb.toString(); } + @Override + public String getElementName() { + return ERROR; + } + + @Override + public String getNamespace() { + return StreamOpen.CLIENT_NAMESPACE; + } + /** * Returns the error as XML. * * @return the error as XML. */ public XmlStringBuilder toXML() { - XmlStringBuilder xml = new XmlStringBuilder(); - xml.halfOpenElement(ERROR); + return toXML(null); + } + + @Override + public XmlStringBuilder toXML(String enclosingNamespace) { + XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace); xml.attribute("type", type.toString()); xml.optAttribute("by", errorGenerator); xml.rightAngleBracket(); xml.halfOpenElement(condition.toString()); - xml.xmlnsAttribute(NAMESPACE); + xml.xmlnsAttribute(ERROR_CONDITION_AND_TEXT_NAMESPACE); if (conditionText != null) { xml.rightAngleBracket(); xml.escape(conditionText); @@ -222,7 +243,7 @@ public class StanzaError extends AbstractError { addDescriptiveTextsAndExtensions(xml); - xml.closeElement(ERROR); + xml.closeElement(this); return xml; } diff --git a/smack-core/src/main/java/org/jivesoftware/smack/packet/StanzaErrorTextElement.java b/smack-core/src/main/java/org/jivesoftware/smack/packet/StanzaErrorTextElement.java index 2af6d85b9..78ab05919 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/packet/StanzaErrorTextElement.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/packet/StanzaErrorTextElement.java @@ -18,7 +18,7 @@ package org.jivesoftware.smack.packet; public class StanzaErrorTextElement extends AbstractTextElement { - public static final String NAMESPACE = StanzaError.NAMESPACE; + public static final String NAMESPACE = StanzaError.ERROR_CONDITION_AND_TEXT_NAMESPACE; public StanzaErrorTextElement(String text, String lang) { super(text, lang); diff --git a/smack-core/src/main/java/org/jivesoftware/smack/util/PacketParserUtils.java b/smack-core/src/main/java/org/jivesoftware/smack/util/PacketParserUtils.java index b5f0a2548..56d63a5e4 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/util/PacketParserUtils.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/util/PacketParserUtils.java @@ -855,7 +855,7 @@ public class PacketParserUtils { String name = parser.getName(); String namespace = parser.getNamespace(); switch (namespace) { - case StanzaError.NAMESPACE: + case StanzaError.ERROR_CONDITION_AND_TEXT_NAMESPACE: switch (name) { case Stanza.TEXT: descriptiveTexts = parseDescriptiveTexts(parser, descriptiveTexts); diff --git a/smack-core/src/test/java/org/jivesoftware/smack/util/PacketParserUtilsTest.java b/smack-core/src/test/java/org/jivesoftware/smack/util/PacketParserUtilsTest.java index e8d525751..4b4ce0681 100644 --- a/smack-core/src/test/java/org/jivesoftware/smack/util/PacketParserUtilsTest.java +++ b/smack-core/src/test/java/org/jivesoftware/smack/util/PacketParserUtilsTest.java @@ -906,10 +906,10 @@ public class PacketParserUtilsTest { .build(); final String errorXml = XMLBuilder .create(StanzaError.ERROR).a("type", "cancel").up() - .element("internal-server-error", StanzaError.NAMESPACE).up() - .element("text", StanzaError.NAMESPACE).t(text).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()); + XmlUnitUtils.assertSimilar(errorXml, error.toXML(StreamOpen.CLIENT_NAMESPACE)); } @Test @@ -917,8 +917,8 @@ public class PacketParserUtilsTest { final String text = "Dummy descriptive text"; final String errorXml = XMLBuilder .create(StanzaError.ERROR).a("type", "cancel").up() - .element("internal-server-error", StanzaError.NAMESPACE).up() - .element("text", StanzaError.NAMESPACE).t(text).up() + .element("internal-server-error", StanzaError.ERROR_CONDITION_AND_TEXT_NAMESPACE).up() + .element("text", StanzaError.ERROR_CONDITION_AND_TEXT_NAMESPACE).t(text).up() .asString(); XmlPullParser parser = TestUtils.getParser(errorXml); StanzaError error = PacketParserUtils.parseError(parser).build(); diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/iot/control/IoTControlManager.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/iot/control/IoTControlManager.java index bc1c4b259..99830f74c 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/iot/control/IoTControlManager.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/iot/control/IoTControlManager.java @@ -90,7 +90,7 @@ public final class IoTControlManager extends IoTManager { try { controlRequest.processRequest(iotSetRequest.getFrom(), iotSetRequest.getSetData()); } catch (XMPPErrorException e) { - return IQ.createErrorResponse(iotSetRequest, e.getXMPPError()); + return IQ.createErrorResponse(iotSetRequest, e.getStanzaError()); } return new IoTSetResponse(iotSetRequest); diff --git a/smack-extensions/src/integration-test/java/org/jivesoftware/smackx/CompressionTest.java b/smack-extensions/src/integration-test/java/org/jivesoftware/smackx/CompressionTest.java index 13c60a122..38ecbe628 100644 --- a/smack-extensions/src/integration-test/java/org/jivesoftware/smackx/CompressionTest.java +++ b/smack-extensions/src/integration-test/java/org/jivesoftware/smackx/CompressionTest.java @@ -94,7 +94,7 @@ public class CompressionTest extends SmackTestCase { setupConnection.getAccountManager().createAccount("user0", "user0"); } catch (XMPPException e) { // Do nothing if the accout already exists - if (e.getXMPPError().getCode() != 409) { + if (e.getStanzaError().getCode() != 409) { throw e; } } diff --git a/smack-extensions/src/integration-test/java/org/jivesoftware/smackx/LastActivityManagerTest.java b/smack-extensions/src/integration-test/java/org/jivesoftware/smackx/LastActivityManagerTest.java index cfb28e128..dd3c926e6 100644 --- a/smack-extensions/src/integration-test/java/org/jivesoftware/smackx/LastActivityManagerTest.java +++ b/smack-extensions/src/integration-test/java/org/jivesoftware/smackx/LastActivityManagerTest.java @@ -84,9 +84,9 @@ public class LastActivityManagerTest extends SmackTestCase { LastActivityManager.getLastActivity(conn0, getFullJID(2)); fail("No error was received from the server. User was able to get info of other user not in his roster."); } catch (XMPPException e) { - assertNotNull("No error was returned from the server", e.getXMPPError()); + assertNotNull("No error was returned from the server", e.getStanzaError()); assertEquals("Forbidden error was not returned from the server", 403, - e.getXMPPError().getCode()); + e.getStanzaError().getCode()); } } @@ -121,7 +121,7 @@ public class LastActivityManagerTest extends SmackTestCase { try { lastActivity = LastActivityManager.getLastActivity(conn0, getHost()); } catch (XMPPException e) { - if (e.getXMPPError().getCode() == 403) { + if (e.getStanzaError().getCode() == 403) { //The test can not be done since the host do not allow this kind of request return; } diff --git a/smack-extensions/src/integration-test/java/org/jivesoftware/smackx/ServiceDiscoveryManagerTest.java b/smack-extensions/src/integration-test/java/org/jivesoftware/smackx/ServiceDiscoveryManagerTest.java index 1a0b76d87..5d56d85ba 100644 --- a/smack-extensions/src/integration-test/java/org/jivesoftware/smackx/ServiceDiscoveryManagerTest.java +++ b/smack-extensions/src/integration-test/java/org/jivesoftware/smackx/ServiceDiscoveryManagerTest.java @@ -77,7 +77,7 @@ public class ServiceDiscoveryManagerTest extends SmackTestCase { fail("Unexpected identities were returned instead of a 404 error"); } catch (XMPPException e) { - assertEquals("Incorrect error", 404, e.getXMPPError().getCode()); + assertEquals("Incorrect error", 404, e.getStanzaError().getCode()); } catch (Exception e) { e.printStackTrace(); diff --git a/smack-extensions/src/integration-test/java/org/jivesoftware/smackx/muc/MultiUserChatTest.java b/smack-extensions/src/integration-test/java/org/jivesoftware/smackx/muc/MultiUserChatTest.java index de59db883..428835ebb 100644 --- a/smack-extensions/src/integration-test/java/org/jivesoftware/smackx/muc/MultiUserChatTest.java +++ b/smack-extensions/src/integration-test/java/org/jivesoftware/smackx/muc/MultiUserChatTest.java @@ -527,7 +527,7 @@ public class MultiUserChatTest extends SmackTestCase { fail("Other user was able to join with other user's reserved nickname"); } catch (XMPPException e) { - XMPPError xmppError = e.getXMPPError(); + XMPPError xmppError = e.getStanzaError(); assertNotNull( "No XMPPError was received when joining with other user's reserved nickname", xmppError); @@ -551,7 +551,7 @@ public class MultiUserChatTest extends SmackTestCase { muc3.sendRegistrationForm(answerForm); } catch (XMPPException e) { - XMPPError xmppError = e.getXMPPError(); + XMPPError xmppError = e.getStanzaError(); assertNotNull( "No XMPPError was received when reserving an already reserved nickname", xmppError); @@ -605,7 +605,7 @@ public class MultiUserChatTest extends SmackTestCase { fail("User2 was allowed to change the room's subject"); } catch (XMPPException e) { - XMPPError xmppError = e.getXMPPError(); + XMPPError xmppError = e.getStanzaError(); assertNotNull( "No XMPPError was received when changing the room's subject", xmppError); @@ -677,7 +677,7 @@ public class MultiUserChatTest extends SmackTestCase { fail("User2 was able to kick a room owner"); } catch (XMPPException e) { - XMPPError xmppError = e.getXMPPError(); + XMPPError xmppError = e.getStanzaError(); assertNotNull("No XMPPError was received when kicking a room owner", xmppError); assertEquals( "A simple participant was able to kick another participant from the room", @@ -754,7 +754,7 @@ public class MultiUserChatTest extends SmackTestCase { fail("User2 was able to ban a room owner"); } catch (XMPPException e) { - XMPPError xmppError = e.getXMPPError(); + XMPPError xmppError = e.getStanzaError(); assertNotNull("No XMPPError was received when banning a room owner", xmppError); assertEquals( "A simple participant was able to ban another participant from the room", @@ -842,7 +842,7 @@ public class MultiUserChatTest extends SmackTestCase { fail("User2 was able to grant voice"); } catch (XMPPException e) { - XMPPError xmppError = e.getXMPPError(); + XMPPError xmppError = e.getStanzaError(); assertNotNull("No XMPPError was received granting voice", xmppError); assertEquals( "A visitor was able to grant voice to another visitor", @@ -947,7 +947,7 @@ public class MultiUserChatTest extends SmackTestCase { fail("User2 was able to grant moderator privileges"); } catch (XMPPException e) { - XMPPError xmppError = e.getXMPPError(); + XMPPError xmppError = e.getStanzaError(); assertNotNull("No XMPPError was received granting moderator privileges", xmppError); assertEquals( "A visitor was able to grant moderator privileges to another visitor", @@ -1078,7 +1078,7 @@ public class MultiUserChatTest extends SmackTestCase { fail("User2 was able to grant membership privileges"); } catch (XMPPException e) { - XMPPError xmppError = e.getXMPPError(); + XMPPError xmppError = e.getStanzaError(); assertNotNull( "No XMPPError was received granting membership privileges", xmppError); @@ -1188,7 +1188,7 @@ public class MultiUserChatTest extends SmackTestCase { fail("User2 was able to grant admin privileges"); } catch (XMPPException e) { - XMPPError xmppError = e.getXMPPError(); + XMPPError xmppError = e.getStanzaError(); assertNotNull("No XMPPError was received granting admin privileges", xmppError); assertEquals( "A visitor was able to grant admin privileges to another visitor", @@ -1363,7 +1363,7 @@ public class MultiUserChatTest extends SmackTestCase { fail("User2 was able to grant ownership privileges"); } catch (XMPPException e) { - XMPPError xmppError = e.getXMPPError(); + XMPPError xmppError = e.getStanzaError(); assertNotNull("No XMPPError was received granting ownership privileges", xmppError); assertEquals( "A visitor was able to grant ownership privileges to another visitor", @@ -1609,7 +1609,7 @@ public class MultiUserChatTest extends SmackTestCase { fail("User2 was able to get the list of owners"); } catch (XMPPException e) { - XMPPError xmppError = e.getXMPPError(); + XMPPError xmppError = e.getStanzaError(); assertNotNull("No XMPPError was received getting the list of owners", xmppError); assertEquals( "A member was able to get the list of owners", diff --git a/smack-extensions/src/integration-test/java/org/jivesoftware/smackx/pubsub/MultiUserSubscriptionUseCases.java b/smack-extensions/src/integration-test/java/org/jivesoftware/smackx/pubsub/MultiUserSubscriptionUseCases.java index 69dd6a483..c49a66378 100644 --- a/smack-extensions/src/integration-test/java/org/jivesoftware/smackx/pubsub/MultiUserSubscriptionUseCases.java +++ b/smack-extensions/src/integration-test/java/org/jivesoftware/smackx/pubsub/MultiUserSubscriptionUseCases.java @@ -73,8 +73,8 @@ public class MultiUserSubscriptionUseCases extends PubSubTestCase } catch (XMPPException exc) { - assertEquals("bad-request", exc.getXMPPError().getCondition()); - assertEquals(XMPPError.Type.MODIFY, exc.getXMPPError().getType()); + assertEquals("bad-request", exc.getStanzaError().getCondition()); + assertEquals(XMPPError.Type.MODIFY, exc.getStanzaError().getType()); } List items = user2Node.getItems(sub1.getId()); assertTrue(items.size() == 5); diff --git a/smack-extensions/src/integration-test/java/org/jivesoftware/smackx/pubsub/PublisherUseCases.java b/smack-extensions/src/integration-test/java/org/jivesoftware/smackx/pubsub/PublisherUseCases.java index 45c521822..17c232228 100644 --- a/smack-extensions/src/integration-test/java/org/jivesoftware/smackx/pubsub/PublisherUseCases.java +++ b/smack-extensions/src/integration-test/java/org/jivesoftware/smackx/pubsub/PublisherUseCases.java @@ -47,7 +47,7 @@ public class PublisherUseCases extends SingleUserTestCase fail("Exception should be thrown when there is no payload"); } catch (XMPPException e) { - XMPPError err = e.getXMPPError(); + XMPPError err = e.getStanzaError(); assertTrue(err.getType().equals(XMPPError.Type.MODIFY)); assertTrue(err.getCondition().equals(Condition.bad_request.toString())); assertNotNull(err.getExtension("payload-required", PubSubNamespace.ERROR.getXmlns())); @@ -59,7 +59,7 @@ public class PublisherUseCases extends SingleUserTestCase fail("Exception should be thrown when there is no payload"); } catch (XMPPException e) { - XMPPError err = e.getXMPPError(); + XMPPError err = e.getStanzaError(); assertTrue(err.getType().equals(XMPPError.Type.MODIFY)); assertTrue(err.getCondition().equals(Condition.bad_request.toString())); assertNotNull(err.getExtension("payload-required", PubSubNamespace.ERROR.getXmlns())); @@ -104,7 +104,7 @@ public class PublisherUseCases extends SingleUserTestCase fail("Exception should be thrown when there is no payload"); } catch (XMPPException e) { - XMPPError err = e.getXMPPError(); + XMPPError err = e.getStanzaError(); assertTrue(err.getType().equals(XMPPError.Type.MODIFY)); assertTrue(err.getCondition().equals(Condition.bad_request.toString())); assertNotNull(err.getExtension("payload-required", PubSubNamespace.ERROR.getXmlns())); @@ -116,7 +116,7 @@ public class PublisherUseCases extends SingleUserTestCase fail("Exception should be thrown when there is no payload"); } catch (XMPPException e) { - XMPPError err = e.getXMPPError(); + XMPPError err = e.getStanzaError(); assertTrue(err.getType().equals(XMPPError.Type.MODIFY)); assertTrue(err.getCondition().equals(Condition.bad_request.toString())); assertNotNull(err.getExtension("payload-required", PubSubNamespace.ERROR.getXmlns())); diff --git a/smack-extensions/src/integration-test/java/org/jivesoftware/smackx/pubsub/TestEvents.java b/smack-extensions/src/integration-test/java/org/jivesoftware/smackx/pubsub/TestEvents.java index 8b8fa6b10..9febc38f4 100644 --- a/smack-extensions/src/integration-test/java/org/jivesoftware/smackx/pubsub/TestEvents.java +++ b/smack-extensions/src/integration-test/java/org/jivesoftware/smackx/pubsub/TestEvents.java @@ -67,7 +67,7 @@ public class TestEvents extends SmackTestCase } catch (XMPPException e) { - if (e.getXMPPError().getType() == Type.CANCEL && e.getXMPPError().getCondition().equals("item-not-found")) + if (e.getStanzaError().getType() == Type.CANCEL && e.getXMPPError().getCondition().equals("item-not-found")) creatorNode = creatorMgr.createNode(nodeId); else throw e; diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/commands/AdHocCommandManager.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/commands/AdHocCommandManager.java index 871d84840..9e1431572 100755 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/commands/AdHocCommandManager.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/commands/AdHocCommandManager.java @@ -460,7 +460,7 @@ public final class AdHocCommandManager extends Manager { // If there is an exception caused by the next, complete, // prev or cancel method, then that error is returned to the // requester. - StanzaError error = e.getXMPPError(); + StanzaError error = e.getStanzaError(); // If the error type is cancel, then the execution is // canceled therefore the status must show that, and the @@ -570,7 +570,7 @@ public final class AdHocCommandManager extends Manager { // If there is an exception caused by the next, complete, // prev or cancel method, then that error is returned to the // requester. - StanzaError error = e.getXMPPError(); + StanzaError error = e.getStanzaError(); // If the error type is cancel, then the execution is // canceled therefore the status must show that, and the diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/OutgoingFileTransfer.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/OutgoingFileTransfer.java index 72197ee5b..6de904d01 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/OutgoingFileTransfer.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/OutgoingFileTransfer.java @@ -344,7 +344,7 @@ public class OutgoingFileTransfer extends FileTransfer { } private void handleXMPPException(XMPPErrorException e) { - StanzaError error = e.getXMPPError(); + StanzaError error = e.getStanzaError(); if (error != null) { switch (error.getCondition()) { case forbidden: diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/iqprivate/PrivateDataManager.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/iqprivate/PrivateDataManager.java index 73c88566d..60a3f0d72 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/iqprivate/PrivateDataManager.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/iqprivate/PrivateDataManager.java @@ -221,7 +221,7 @@ public final class PrivateDataManager extends Manager { return true; } catch (XMPPErrorException e) { - if (e.getXMPPError().getCondition() == Condition.service_unavailable) { + if (e.getStanzaError().getCondition() == Condition.service_unavailable) { return false; } else { diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/ping/PingManager.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/ping/PingManager.java index 276f50fbc..e5da32aeb 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/ping/PingManager.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/ping/PingManager.java @@ -149,7 +149,7 @@ public final class PingManager extends Manager { return true; } - final StanzaError xmppError = xmppErrorException.getXMPPError(); + final StanzaError xmppError = xmppErrorException.getStanzaError(); // We may received an error response from an intermediate service returning an error like // 'remote-server-not-found' or 'remote-server-timeout' to us (which would fake the 'from' address, diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/PubSubManager.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/PubSubManager.java index 1a1dd2783..2b92b1d5d 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/PubSubManager.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/PubSubManager.java @@ -276,12 +276,12 @@ public final class PubSubManager extends Manager { return createNode(id); } catch (XMPPErrorException e1) { - if (e1.getXMPPError().getCondition() == Condition.item_not_found) { + if (e1.getStanzaError().getCondition() == Condition.item_not_found) { try { return createNode(id); } catch (XMPPErrorException e2) { - if (e2.getXMPPError().getCondition() == Condition.conflict) { + if (e2.getStanzaError().getCondition() == Condition.conflict) { // The node was created in the meantime, re-try getNode(). Note that this case should be rare. try { return getNode(id); @@ -294,7 +294,7 @@ public final class PubSubManager extends Manager { throw e2; } } - if (e1.getXMPPError().getCondition() == Condition.service_unavailable) { + if (e1.getStanzaError().getCondition() == Condition.service_unavailable) { // This could be caused by Prosody bug #805 (see https://prosody.im/issues/issue/805). Prosody does not // answer to disco#info requests on the node ID, which makes it undecidable if a node is a leaf or // collection node. @@ -326,7 +326,7 @@ public final class PubSubManager extends Manager { node = getNode(id); } catch (XMPPErrorException e) { - if (e.getXMPPError().getCondition() == Condition.service_unavailable) { + if (e.getStanzaError().getCondition() == Condition.service_unavailable) { // This could be caused by Prosody bug #805 (see https://prosody.im/issues/issue/805). Prosody does not // answer to disco#info requests on the node ID, which makes it undecidable if a node is a leaf or // collection node. @@ -349,7 +349,7 @@ public final class PubSubManager extends Manager { // Try to ensure that this is not a collection node by asking for one item form the node. leafNode.getItems(1); } catch (XMPPErrorException e) { - Condition condition = e.getXMPPError().getCondition(); + Condition condition = e.getStanzaError().getCondition(); if (condition == Condition.feature_not_implemented) { // XEP-0060 ยง 6.5.9.5: Item retrieval not supported, e.g. because node is a collection node throw new PubSubException.NotALeafNodeException(id, pubSubService); @@ -369,7 +369,7 @@ public final class PubSubManager extends Manager { return createNode(id); } catch (XMPPErrorException e1) { - if (e1.getXMPPError().getCondition() == Condition.conflict) { + if (e1.getStanzaError().getCondition() == Condition.conflict) { return getLeafNodeProsodyWorkaround(id); } throw e1; @@ -556,7 +556,7 @@ public final class PubSubManager extends Manager { leafNode = createNode(); } catch (XMPPErrorException e) { - if (e.getXMPPError().getCondition() == StanzaError.Condition.forbidden) { + if (e.getStanzaError().getCondition() == StanzaError.Condition.forbidden) { return false; } throw e; diff --git a/smack-extensions/src/test/java/org/jivesoftware/smackx/bytestreams/ibb/InBandBytestreamManagerTest.java b/smack-extensions/src/test/java/org/jivesoftware/smackx/bytestreams/ibb/InBandBytestreamManagerTest.java index b707836c9..43bede073 100644 --- a/smack-extensions/src/test/java/org/jivesoftware/smackx/bytestreams/ibb/InBandBytestreamManagerTest.java +++ b/smack-extensions/src/test/java/org/jivesoftware/smackx/bytestreams/ibb/InBandBytestreamManagerTest.java @@ -125,7 +125,7 @@ public class InBandBytestreamManagerTest extends InitExtensions { } catch (XMPPErrorException e) { assertEquals(StanzaError.Condition.feature_not_implemented, - e.getXMPPError().getCondition()); + e.getStanzaError().getCondition()); } } diff --git a/smack-extensions/src/test/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5ByteStreamManagerTest.java b/smack-extensions/src/test/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5ByteStreamManagerTest.java index 251c4fa89..aec522ead 100644 --- a/smack-extensions/src/test/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5ByteStreamManagerTest.java +++ b/smack-extensions/src/test/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5ByteStreamManagerTest.java @@ -451,7 +451,7 @@ public class Socks5ByteStreamManagerTest { } catch (XMPPErrorException e) { protocol.verifyAll(); - assertEquals(rejectPacket.getError(), e.getXMPPError()); + assertEquals(rejectPacket.getError(), e.getStanzaError()); } catch (Exception e) { fail(e.getMessage()); diff --git a/smack-extensions/src/test/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5ByteStreamRequestTest.java b/smack-extensions/src/test/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5ByteStreamRequestTest.java index 9811d5f39..37098a646 100644 --- a/smack-extensions/src/test/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5ByteStreamRequestTest.java +++ b/smack-extensions/src/test/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5ByteStreamRequestTest.java @@ -110,7 +110,7 @@ public class Socks5ByteStreamRequestTest { fail("exception should be thrown"); } catch (XMPPErrorException e) { - assertTrue(e.getXMPPError().getDescriptiveText("en").contains("Could not establish socket with any provided host")); + assertTrue(e.getStanzaError().getDescriptiveText("en").contains("Could not establish socket with any provided host")); } // verify targets response @@ -154,7 +154,7 @@ public class Socks5ByteStreamRequestTest { fail("exception should be thrown"); } catch (XMPPErrorException e) { - assertTrue(e.getXMPPError().getDescriptiveText("en").contains("Could not establish socket with any provided host")); + assertTrue(e.getStanzaError().getDescriptiveText("en").contains("Could not establish socket with any provided host")); } // verify targets response @@ -201,7 +201,7 @@ public class Socks5ByteStreamRequestTest { fail("exception should be thrown"); } catch (XMPPErrorException e) { - assertTrue(e.getXMPPError().getDescriptiveText("en").contains( + assertTrue(e.getStanzaError().getDescriptiveText("en").contains( "Could not establish socket with any provided host")); } @@ -293,7 +293,7 @@ public class Socks5ByteStreamRequestTest { fail("exception should be thrown"); } catch (XMPPErrorException e) { - assertTrue(e.getXMPPError().getDescriptiveText("en").contains( + assertTrue(e.getStanzaError().getDescriptiveText("en").contains( "Could not establish socket with any provided host")); } diff --git a/smack-extensions/src/test/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5ClientForInitiatorTest.java b/smack-extensions/src/test/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5ClientForInitiatorTest.java index 393187fe5..3b0a5faf8 100644 --- a/smack-extensions/src/test/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5ClientForInitiatorTest.java +++ b/smack-extensions/src/test/java/org/jivesoftware/smackx/bytestreams/socks5/Socks5ClientForInitiatorTest.java @@ -231,7 +231,7 @@ public class Socks5ClientForInitiatorTest { fail("exception should be thrown"); } catch (XMPPErrorException e) { - assertTrue(StanzaError.Condition.internal_server_error.equals(e.getXMPPError().getCondition())); + assertTrue(StanzaError.Condition.internal_server_error.equals(e.getStanzaError().getCondition())); protocol.verifyAll(); } diff --git a/smack-extensions/src/test/java/org/jivesoftware/smackx/pubsub/ConfigureFormTest.java b/smack-extensions/src/test/java/org/jivesoftware/smackx/pubsub/ConfigureFormTest.java index a5875c9d8..729e2a12d 100644 --- a/smack-extensions/src/test/java/org/jivesoftware/smackx/pubsub/ConfigureFormTest.java +++ b/smack-extensions/src/test/java/org/jivesoftware/smackx/pubsub/ConfigureFormTest.java @@ -77,7 +77,7 @@ public class ConfigureFormTest extends InitExtensions { fail(); } catch (XMPPErrorException e) { - Assert.assertEquals(StanzaError.Type.AUTH, e.getXMPPError().getType()); + Assert.assertEquals(StanzaError.Type.AUTH, e.getStanzaError().getType()); } } diff --git a/smack-integration-test/src/test/java/org/igniterealtime/smack/inttest/unittest/SmackIntegrationTestFrameworkUnitTest.java b/smack-integration-test/src/test/java/org/igniterealtime/smack/inttest/unittest/SmackIntegrationTestFrameworkUnitTest.java index 1d54bce4d..9cd517049 100644 --- a/smack-integration-test/src/test/java/org/igniterealtime/smack/inttest/unittest/SmackIntegrationTestFrameworkUnitTest.java +++ b/smack-integration-test/src/test/java/org/igniterealtime/smack/inttest/unittest/SmackIntegrationTestFrameworkUnitTest.java @@ -85,8 +85,8 @@ public class SmackIntegrationTestFrameworkUnitTest { FailedTest failedTest = failedTests.get(0); assertTrue(failedTest.failureReason instanceof XMPPErrorException); XMPPErrorException ex = (XMPPErrorException) failedTest.failureReason; - assertEquals(StanzaError.Condition.bad_request, ex.getXMPPError().getCondition()); - assertEquals(ThrowsNonFatalExceptionDummyTest.DESCRIPTIVE_TEXT, ex.getXMPPError().getDescriptiveText()); + assertEquals(StanzaError.Condition.bad_request, ex.getStanzaError().getCondition()); + assertEquals(ThrowsNonFatalExceptionDummyTest.DESCRIPTIVE_TEXT, ex.getStanzaError().getDescriptiveText()); } public static class ThrowsNonFatalExceptionDummyTest extends AbstractSmackIntegrationTest { diff --git a/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/OmemoService.java b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/OmemoService.java index d6781fff2..0df2469b6 100644 --- a/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/OmemoService.java +++ b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/OmemoService.java @@ -467,7 +467,7 @@ public abstract class OmemoService textElements = failed.getTextElements(); assertEquals(1, textElements.size()); From 61be4d768d940c6b999fd177c1bba4f34736bbd8 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Wed, 20 Jun 2018 15:58:54 +0200 Subject: [PATCH 2/3] Add javadoc describing the XMPPErrorException class --- .../src/main/java/org/jivesoftware/smack/XMPPException.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/smack-core/src/main/java/org/jivesoftware/smack/XMPPException.java b/smack-core/src/main/java/org/jivesoftware/smack/XMPPException.java index 2c9180fcd..ae3b6c399 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/XMPPException.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/XMPPException.java @@ -69,6 +69,10 @@ public abstract class XMPPException extends Exception { super(message, wrappedThrowable); } + /** + * An exception caused by an XMPP error stanza response on the protocol level. You can examine the underlying + * {@link StanzaError} by calling {@link #getStanzaError()}. + */ public static class XMPPErrorException extends XMPPException { /** * From c73219b79718f6781ec7856e4365a4ef9f68b9c0 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Wed, 20 Jun 2018 18:55:24 +0200 Subject: [PATCH 3/3] Add (To|From)TypeFilter.(FROM|TO)_ANY_JID to filter all stanzas which have a valid jid set as to/from. --- .../org/jivesoftware/smack/filter/AbstractJidTypeFilter.java | 5 ++++- .../java/org/jivesoftware/smack/filter/FromTypeFilter.java | 3 ++- .../java/org/jivesoftware/smack/filter/ToTypeFilter.java | 3 ++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/smack-core/src/main/java/org/jivesoftware/smack/filter/AbstractJidTypeFilter.java b/smack-core/src/main/java/org/jivesoftware/smack/filter/AbstractJidTypeFilter.java index 60bd2df5b..93628b5e9 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/filter/AbstractJidTypeFilter.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/filter/AbstractJidTypeFilter.java @@ -1,6 +1,6 @@ /** * - * Copyright 2017 Florian Schmaus. + * Copyright 2017-2018 Florian Schmaus. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,6 +27,7 @@ public abstract class AbstractJidTypeFilter implements StanzaFilter { entityBare, domainFull, domainBare, + any, ; } @@ -55,6 +56,8 @@ public abstract class AbstractJidTypeFilter implements StanzaFilter { return jid.isDomainFullJid(); case domainBare: return jid.isDomainBareJid(); + case any: + return true; default: throw new AssertionError(); } diff --git a/smack-core/src/main/java/org/jivesoftware/smack/filter/FromTypeFilter.java b/smack-core/src/main/java/org/jivesoftware/smack/filter/FromTypeFilter.java index 27258547f..a571cd791 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/filter/FromTypeFilter.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/filter/FromTypeFilter.java @@ -1,6 +1,6 @@ /** * - * Copyright 2017 Florian Schmaus. + * Copyright 2017-2018 Florian Schmaus. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,6 +26,7 @@ public final class FromTypeFilter extends AbstractJidTypeFilter { public static final FromTypeFilter ENTITY_BARE_JID = new FromTypeFilter(JidType.entityBare); public static final FromTypeFilter DOMAIN_FULL_JID = new FromTypeFilter(JidType.domainFull); public static final FromTypeFilter DOMAIN_BARE_JID = new FromTypeFilter(JidType.domainBare); + public static final FromTypeFilter FROM_ANY_JID = new FromTypeFilter(JidType.any); private FromTypeFilter(JidType jidType) { super(jidType); diff --git a/smack-core/src/main/java/org/jivesoftware/smack/filter/ToTypeFilter.java b/smack-core/src/main/java/org/jivesoftware/smack/filter/ToTypeFilter.java index 0f3ea6835..68b25d4ee 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/filter/ToTypeFilter.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/filter/ToTypeFilter.java @@ -1,6 +1,6 @@ /** * - * Copyright 2017 Florian Schmaus. + * Copyright 2017-2018 Florian Schmaus. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,6 +26,7 @@ public final class ToTypeFilter extends AbstractJidTypeFilter { public static final ToTypeFilter ENTITY_BARE_JID = new ToTypeFilter(JidType.entityBare); public static final ToTypeFilter DOMAIN_FULL_JID = new ToTypeFilter(JidType.domainFull); public static final ToTypeFilter DOMAIN_BARE_JID = new ToTypeFilter(JidType.domainBare); + public static final ToTypeFilter TO_ANY_JID = new ToTypeFilter(JidType.any); public static final StanzaFilter ENTITY_FULL_OR_BARE_JID = new OrFilter(ENTITY_FULL_JID, ENTITY_BARE_JID);