mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-11-07 16:55:58 +01:00
SMACK-452 Correctly parse stream error packets
Modified PacketParserUtils.parseStreamError() to take into account that there may be an optional text element and an optional application specific element. git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/branches/smack_3_3_2@13767 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
parent
b253203cfb
commit
bd709dc323
3 changed files with 126 additions and 7 deletions
|
@ -82,13 +82,21 @@ package org.jivesoftware.smack.packet;
|
||||||
*/
|
*/
|
||||||
public class StreamError {
|
public class StreamError {
|
||||||
|
|
||||||
|
public static final String NAMESPACE = "urn:ietf:params:xml:ns:xmpp-streams";
|
||||||
|
|
||||||
private String code;
|
private String code;
|
||||||
|
private String text;
|
||||||
|
|
||||||
public StreamError(String code) {
|
public StreamError(String code) {
|
||||||
super();
|
super();
|
||||||
this.code = code;
|
this.code = code;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public StreamError(String code, String text) {
|
||||||
|
this(code);
|
||||||
|
this.text = text;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the error code.
|
* Returns the error code.
|
||||||
*
|
*
|
||||||
|
@ -98,9 +106,19 @@ public class StreamError {
|
||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the error text, which may be null.
|
||||||
|
*
|
||||||
|
* @return the error text.
|
||||||
|
*/
|
||||||
|
public String getText() {
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder txt = new StringBuilder();
|
StringBuilder txt = new StringBuilder();
|
||||||
txt.append("stream:error (").append(code).append(")");
|
txt.append("stream:error (").append(code).append(")");
|
||||||
|
if (text != null) txt.append(" text: ").append(text);
|
||||||
return txt.toString();
|
return txt.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -695,21 +695,33 @@ public class PacketParserUtils {
|
||||||
*/
|
*/
|
||||||
public static StreamError parseStreamError(XmlPullParser parser) throws IOException,
|
public static StreamError parseStreamError(XmlPullParser parser) throws IOException,
|
||||||
XmlPullParserException {
|
XmlPullParserException {
|
||||||
StreamError streamError = null;
|
final int depth = parser.getDepth();
|
||||||
boolean done = false;
|
boolean done = false;
|
||||||
|
String code = null;
|
||||||
|
String text = null;
|
||||||
while (!done) {
|
while (!done) {
|
||||||
int eventType = parser.next();
|
int eventType = parser.next();
|
||||||
|
|
||||||
if (eventType == XmlPullParser.START_TAG) {
|
if (eventType == XmlPullParser.START_TAG) {
|
||||||
streamError = new StreamError(parser.getName());
|
String namespace = parser.getNamespace();
|
||||||
}
|
if (StreamError.NAMESPACE.equals(namespace)) {
|
||||||
else if (eventType == XmlPullParser.END_TAG) {
|
String name = parser.getName();
|
||||||
if (parser.getName().equals("error")) {
|
if (name.equals("text") && !parser.isEmptyElementTag()) {
|
||||||
done = true;
|
parser.next();
|
||||||
|
text = parser.getText();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// If it's not a text element, that is qualified by the StreamError.NAMESPACE,
|
||||||
|
// then it has to be the stream error code
|
||||||
|
code = name;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else if (eventType == XmlPullParser.END_TAG && depth == parser.getDepth()) {
|
||||||
|
done = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return streamError;
|
return new StreamError(code, text);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
89
test-unit/org/jivesoftware/smack/packet/StreamErrorTest.java
Normal file
89
test-unit/org/jivesoftware/smack/packet/StreamErrorTest.java
Normal file
|
@ -0,0 +1,89 @@
|
||||||
|
package org.jivesoftware.smack.packet;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
import org.jivesoftware.smack.TestUtils;
|
||||||
|
import org.jivesoftware.smack.packet.StreamError;
|
||||||
|
import org.jivesoftware.smack.util.PacketParserUtils;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.xmlpull.v1.XmlPullParser;
|
||||||
|
|
||||||
|
public class StreamErrorTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testParsingOfSimpleStreamError() {
|
||||||
|
StreamError error = null;
|
||||||
|
final String xml =
|
||||||
|
// Usually the stream:stream element has more attributes (to, version, ...)
|
||||||
|
// We omit those, since they are not relevant for testing
|
||||||
|
"<stream:stream from='im.example.com' id='++TR84Sm6A3hnt3Q065SnAbbk3Y=' xmlns:stream='http://etherx.jabber.org/streams'>" +
|
||||||
|
"<stream:error>" +
|
||||||
|
"<conflict xmlns='urn:ietf:params:xml:ns:xmpp-streams' /> +" +
|
||||||
|
"</stream:error>" +
|
||||||
|
"</stream:stream>";
|
||||||
|
try {
|
||||||
|
XmlPullParser parser = TestUtils.getParser(xml, "error");
|
||||||
|
error = PacketParserUtils.parseStreamError(parser);
|
||||||
|
} catch (Exception e) {
|
||||||
|
fail(e.getMessage());
|
||||||
|
}
|
||||||
|
assertNotNull(error);
|
||||||
|
assertEquals("conflict", error.getCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testParsingOfStreamErrorWithText() {
|
||||||
|
StreamError error = null;
|
||||||
|
final String xml =
|
||||||
|
// Usually the stream:stream element has more attributes (to, version, ...)
|
||||||
|
// We omit those, since they are not relevant for testing
|
||||||
|
"<stream:stream from='im.example.com' id='++TR84Sm6A3hnt3Q065SnAbbk3Y=' xmlns:stream='http://etherx.jabber.org/streams'>" +
|
||||||
|
"<stream:error>" +
|
||||||
|
"<conflict xmlns='urn:ietf:params:xml:ns:xmpp-streams' />" +
|
||||||
|
"<text xml:lang='' xmlns='urn:ietf:params:xml:ns:xmpp-streams'>" +
|
||||||
|
"Replaced by new connection" +
|
||||||
|
"</text>" +
|
||||||
|
"</stream:error>" +
|
||||||
|
"</stream:stream>";
|
||||||
|
try {
|
||||||
|
XmlPullParser parser = TestUtils.getParser(xml, "error");
|
||||||
|
error = PacketParserUtils.parseStreamError(parser);
|
||||||
|
} catch (Exception e) {
|
||||||
|
fail(e.getMessage());
|
||||||
|
}
|
||||||
|
assertNotNull(error);
|
||||||
|
assertEquals("conflict", error.getCode());
|
||||||
|
assertEquals("Replaced by new connection", error.getText());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testParsingOfStreamErrorWithTextAndOptionalElement() {
|
||||||
|
StreamError error = null;
|
||||||
|
final String xml =
|
||||||
|
// Usually the stream:stream element has more attributes (to, version, ...)
|
||||||
|
// We omit those, since they are not relevant for testing
|
||||||
|
"<stream:stream from='im.example.com' id='++TR84Sm6A3hnt3Q065SnAbbk3Y=' xmlns:stream='http://etherx.jabber.org/streams'>" +
|
||||||
|
"<stream:error>" +
|
||||||
|
"<conflict xmlns='urn:ietf:params:xml:ns:xmpp-streams' />" +
|
||||||
|
"<text xml:lang='' xmlns='urn:ietf:params:xml:ns:xmpp-streams'>" +
|
||||||
|
"Replaced by new connection" +
|
||||||
|
"</text>" +
|
||||||
|
"<appSpecificElement>" +
|
||||||
|
"Text contents of application-specific condition element: Foo Bar" +
|
||||||
|
"</appSpecificElement>" +
|
||||||
|
"</stream:error>" +
|
||||||
|
"</stream:stream>";
|
||||||
|
try {
|
||||||
|
XmlPullParser parser = TestUtils.getParser(xml, "error");
|
||||||
|
error = PacketParserUtils.parseStreamError(parser);
|
||||||
|
} catch (Exception e) {
|
||||||
|
fail(e.getMessage());
|
||||||
|
}
|
||||||
|
assertNotNull(error);
|
||||||
|
assertEquals("conflict", error.getCode());
|
||||||
|
assertEquals("Replaced by new connection", error.getText());
|
||||||
|
// As of now, Smack ignores application-specific condition elements, so we don't
|
||||||
|
// test them.
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue