1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-09-10 17:49:38 +02:00

Add XMPPErrorException.getStanzaError()

Also deprecate getXMPPError and let StanzaError implement
ExtensionElement.
This commit is contained in:
Florian Schmaus 2018-06-20 15:55:48 +02:00
parent 651ee7b85e
commit 23bb5c5625
31 changed files with 108 additions and 75 deletions

View file

@ -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());
}

View file

@ -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();

View file

@ -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.
*

View file

@ -62,9 +62,16 @@ import org.jivesoftware.smack.util.XmlStringBuilder;
* @see <a href="http://xmpp.org/rfcs/rfc6120.html#stanzas-error-syntax">RFC 6120 - 8.3.2 Syntax: The Syntax of XMPP error stanzas</a>
*/
// 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<String, String> descriptiveTexts,
List<ExtensionElement> 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;
}

View file

@ -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);

View file

@ -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);

View file

@ -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();