1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-12-22 20:47:57 +01:00

Improve parsing. Add method for de-serializing Strings

Move some duplicate code from XMPP(TCP|BOSH)Connection to
PacketParserUtils. Remove TestUtils as the method now part of Smack's
public API in PacketParserUtils.
This commit is contained in:
Florian Schmaus 2014-05-25 23:31:58 +02:00
parent beecb8a675
commit 3647a7fce5
24 changed files with 215 additions and 291 deletions

View file

@ -19,6 +19,7 @@ package org.jivesoftware.smack.bosh;
import java.io.StringReader;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.sasl.SASLMechanism.Challenge;
import org.jivesoftware.smack.sasl.SASLMechanism.SASLFailure;
import org.jivesoftware.smack.sasl.SASLMechanism.Success;
@ -75,14 +76,9 @@ public class BOSHPacketReader implements BOSHClientResponseListener {
do {
eventType = parser.next();
if (eventType == XmlPullParser.START_TAG) {
if (parser.getName().equals("body")) {
// ignore the container root element
} else if (parser.getName().equals("message")) {
connection.processPacket(PacketParserUtils.parseMessage(parser));
} else if (parser.getName().equals("iq")) {
connection.processPacket(PacketParserUtils.parseIQ(parser, connection));
} else if (parser.getName().equals("presence")) {
connection.processPacket(PacketParserUtils.parsePresence(parser));
Packet packet = PacketParserUtils.parseStanza(parser, connection);
if (packet != null) {
connection.processPacket(packet);
} else if (parser.getName().equals("challenge")) {
// The server is challenging the SASL authentication
// made by the client

View file

@ -17,6 +17,9 @@
package org.jivesoftware.smack.util;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
@ -44,6 +47,7 @@ import org.jivesoftware.smack.provider.ProviderManager;
import org.jivesoftware.smack.sasl.SASLMechanism.SASLFailure;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
/**
* Utility class that helps to parse packets. Any parsing packets method that must be shared
@ -54,6 +58,82 @@ import org.xmlpull.v1.XmlPullParserException;
public class PacketParserUtils {
private static final Logger LOGGER = Logger.getLogger(PacketParserUtils.class.getName());
public static XmlPullParser getParserFor(String stanza) throws XmlPullParserException, IOException {
return getParserFor(new StringReader(stanza));
}
public static XmlPullParser getParserFor(Reader reader) throws XmlPullParserException, IOException {
XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
parser.setInput(reader);
// Wind the parser forward to the first start tag
int event = parser.getEventType();
while (event != XmlPullParser.START_TAG) {
if (event == XmlPullParser.END_DOCUMENT) {
throw new IllegalArgumentException("Document contains no start tag");
}
event = parser.next();
}
return parser;
}
public static XmlPullParser getParserFor(String stanza, String startTag)
throws XmlPullParserException, IOException {
XmlPullParser parser = getParserFor(stanza);
while (true) {
int event = parser.getEventType();
String name = parser.getName();
if (event == XmlPullParser.START_TAG && name.equals(startTag)) {
break;
}
else if (event == XmlPullParser.END_DOCUMENT) {
throw new IllegalArgumentException("Could not find start tag '" + startTag
+ "' in stanza: " + stanza);
}
parser.next();
}
return parser;
}
public static Packet parseStanza(String stanza) throws Exception {
return parseStanza(getParserFor(stanza));
}
public static Packet parseStanza(XmlPullParser parser) throws Exception {
return parseStanza(parser, null);
}
/**
* Tries to parse and return either a Message, IQ or Presence stanza.
*
* connection is optional and is used to return feature-not-implemented errors for unknown IQ stanzas.
*
* @param parser
* @param connection
* @return a packet which is either a Message, IQ or Presence.
* @throws Exception
*/
public static Packet parseStanza(XmlPullParser parser, XMPPConnection connection) throws Exception {
final int eventType = parser.getEventType();
if (eventType != XmlPullParser.START_TAG) {
throw new IllegalArgumentException("Parser not at start tag");
}
final String name = parser.getName();
switch (name) {
case "message":
return parseMessage(parser);
case "iq":
return parseIQ(parser, connection);
case "presence":
return parsePresence(parser);
default:
return null;
}
}
/**
* Parses a message packet.
*
@ -253,6 +333,7 @@ public class PacketParserUtils {
* Parses an IQ packet.
*
* @param parser the XML parser, positioned at the start of an IQ packet.
* @param connection the optional XMPPConnection used to send feature-not-implemented replies.
* @return an IQ object.
* @throws Exception if an exception occurs while parsing the packet.
*/
@ -315,7 +396,7 @@ public class PacketParserUtils {
}
// Decide what to do when an IQ packet was not understood
if (iqPacket == null) {
if (IQ.Type.GET == type || IQ.Type.SET == type ) {
if (connection != null && (IQ.Type.GET == type || IQ.Type.SET == type)) {
// If the IQ stanza is of type "get" or "set" containing a child element qualified
// by a namespace with no registered Smack provider, then answer an IQ of type
// "error" with code 501 ("feature-not-implemented")
@ -655,11 +736,10 @@ public class PacketParserUtils {
* @param namespace the XML namespace of the packet extension.
* @param parser the XML parser, positioned at the starting element of the extension.
* @return a PacketExtension.
* @throws Exception if a parsing error occurs.
* @throws Exception
*/
public static PacketExtension parsePacketExtension(String elementName, String namespace, XmlPullParser parser)
throws Exception
{
public static PacketExtension parsePacketExtension(String elementName, String namespace,
XmlPullParser parser) throws Exception {
// See if a provider is registered to handle the extension.
Object provider = ProviderManager.getExtensionProvider(elementName, namespace);
if (provider != null) {
@ -712,9 +792,11 @@ public class PacketParserUtils {
return null;
}
public static Object parseWithIntrospection(String elementName,
Class<?> objectClass, XmlPullParser parser) throws Exception
{
public static Object parseWithIntrospection(String elementName, Class<?> objectClass,
XmlPullParser parser) throws NoSuchMethodException, SecurityException,
InstantiationException, IllegalAccessException, XmlPullParserException,
IOException, IllegalArgumentException, InvocationTargetException,
ClassNotFoundException {
boolean done = false;
Object object = objectClass.newInstance();
while (!done) {
@ -748,9 +830,9 @@ public class PacketParserUtils {
* @param type the type of the property.
* @param value the encode String value to decode.
* @return the String value decoded into the specified type.
* @throws Exception If decoding failed due to an error.
* @throws ClassNotFoundException
*/
private static Object decode(Class<?> type, String value) throws Exception {
private static Object decode(Class<?> type, String value) throws ClassNotFoundException {
if (type.getName().equals("java.lang.String")) {
return value;
}

View file

@ -19,7 +19,6 @@ package org.jivesoftware.smack.packet;
import static org.junit.Assert.*;
import org.jivesoftware.smack.packet.StreamError;
import org.jivesoftware.smack.test.util.TestUtils;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.junit.Test;
import org.xmlpull.v1.XmlPullParser;
@ -38,7 +37,7 @@ public class StreamErrorTest {
"</stream:error>" +
"</stream:stream>";
try {
XmlPullParser parser = TestUtils.getParser(xml, "error");
XmlPullParser parser = PacketParserUtils.getParserFor(xml, "error");
error = PacketParserUtils.parseStreamError(parser);
} catch (Exception e) {
fail(e.getMessage());
@ -62,7 +61,7 @@ public class StreamErrorTest {
"</stream:error>" +
"</stream:stream>";
try {
XmlPullParser parser = TestUtils.getParser(xml, "error");
XmlPullParser parser = PacketParserUtils.getParserFor(xml, "error");
error = PacketParserUtils.parseStreamError(parser);
} catch (Exception e) {
fail(e.getMessage());
@ -90,7 +89,7 @@ public class StreamErrorTest {
"</stream:error>" +
"</stream:stream>";
try {
XmlPullParser parser = TestUtils.getParser(xml, "error");
XmlPullParser parser = PacketParserUtils.getParserFor(xml, "error");
error = PacketParserUtils.parseStreamError(parser);
} catch (Exception e) {
fail(e.getMessage());

View file

@ -23,7 +23,6 @@ import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.provider.PacketExtensionProvider;
import org.jivesoftware.smack.provider.ProviderManager;
import org.jivesoftware.smack.test.util.TestUtils;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.junit.After;
import org.junit.Before;
@ -52,7 +51,7 @@ public class ParsingExceptionTest {
@Test
public void consumeUnparsedInput() throws Exception {
XmlPullParser parser = TestUtils.getMessageParser(
XmlPullParser parser = PacketParserUtils.getParserFor(
"<message from='user@server.example' to='francisco@denmark.lit' id='foo'>" +
"<" + ThrowException.ELEMENT + " xmlns='" + ThrowException.NAMESPACE + "'>" +
"<nothingInHere>" +

View file

@ -1,66 +0,0 @@
/**
*
* Copyright 2013 Robin Collier
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack.test.util;
import java.io.IOException;
import java.io.StringReader;
import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
final public class TestUtils {
private TestUtils() {
}
public static XmlPullParser getIQParser(String stanza) {
return getParser(stanza, "iq");
}
public static XmlPullParser getMessageParser(String stanza) {
return getParser(stanza, "message");
}
public static XmlPullParser getPresenceParser(String stanza) {
return getParser(stanza, "presence");
}
public static XmlPullParser getParser(String stanza, String startTag) {
XmlPullParser parser;
try {
parser = XmlPullParserFactory.newInstance().newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
parser.setInput(new StringReader(stanza));
boolean found = false;
while (!found) {
if ((parser.next() == XmlPullParser.START_TAG) && parser.getName().equals(startTag))
found = true;
}
if (!found)
throw new IllegalArgumentException("Cannot parse start tag [" + startTag + "] from stanza [" + stanza
+ "]");
} catch (XmlPullParserException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
return parser;
}
}

View file

@ -33,7 +33,6 @@ import org.custommonkey.xmlunit.examples.RecursiveElementNameAndTextQualifier;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.test.util.TestUtils;
import org.junit.Test;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
@ -69,7 +68,7 @@ public class PacketParserUtilsTest {
.asString(outputProperties);
Message message = (Message) PacketParserUtils
.parseMessage(TestUtils.getMessageParser(control));
.parseMessage(PacketParserUtils.getParserFor(control));
assertEquals(defaultLanguage, message.getBody());
assertTrue(message.getBodyLanguages().isEmpty());
@ -88,7 +87,7 @@ public class PacketParserUtilsTest {
.t(otherLanguage)
.asString(outputProperties);
message = (Message) PacketParserUtils.parseMessage(TestUtils.getMessageParser(control));
message = (Message) PacketParserUtils.parseMessage(PacketParserUtils.getParserFor(control));
assertEquals(otherLanguage, message.getBody());
assertTrue(message.getBodyLanguages().isEmpty());
@ -106,7 +105,7 @@ public class PacketParserUtilsTest {
.t(defaultLanguage)
.asString(outputProperties);
message = (Message) PacketParserUtils.parseMessage(TestUtils.getMessageParser(control));
message = (Message) PacketParserUtils.parseMessage(PacketParserUtils.getParserFor(control));
assertEquals(defaultLanguage, message.getBody());
assertTrue(message.getBodyLanguages().isEmpty());
@ -125,7 +124,7 @@ public class PacketParserUtilsTest {
.t(defaultLanguage)
.asString(outputProperties);
message = (Message) PacketParserUtils.parseMessage(TestUtils.getMessageParser(control));
message = (Message) PacketParserUtils.parseMessage(PacketParserUtils.getParserFor(control));
assertEquals(defaultLanguage, message.getBody());
assertTrue(message.getBodyLanguages().isEmpty());
@ -146,7 +145,7 @@ public class PacketParserUtilsTest {
.t(otherLanguage)
.asString(outputProperties);
message = (Message) PacketParserUtils.parseMessage(TestUtils.getMessageParser(control));
message = (Message) PacketParserUtils.parseMessage(PacketParserUtils.getParserFor(control));
assertNull(message.getBody());
assertFalse(message.getBodyLanguages().isEmpty());
@ -167,7 +166,7 @@ public class PacketParserUtilsTest {
.t(otherLanguage)
.asString(outputProperties);
message = (Message) PacketParserUtils.parseMessage(TestUtils.getMessageParser(control));
message = (Message) PacketParserUtils.parseMessage(PacketParserUtils.getParserFor(control));
assertNull(message.getBody());
assertFalse(message.getBodyLanguages().isEmpty());
@ -188,7 +187,7 @@ public class PacketParserUtilsTest {
.t(defaultLanguage)
.asString(outputProperties);
message = (Message) PacketParserUtils.parseMessage(TestUtils.getMessageParser(control));
message = (Message) PacketParserUtils.parseMessage(PacketParserUtils.getParserFor(control));
assertNull(message.getBody());
assertFalse(message.getBodyLanguages().isEmpty());
@ -218,7 +217,7 @@ public class PacketParserUtilsTest {
.asString(outputProperties);
Message message = (Message) PacketParserUtils
.parseMessage(TestUtils.getMessageParser(control));
.parseMessage(PacketParserUtils.getParserFor(control));
assertEquals(defaultLanguage, message.getSubject());
assertTrue(message.getSubjectLanguages().isEmpty());
@ -237,7 +236,7 @@ public class PacketParserUtilsTest {
.t(otherLanguage)
.asString(outputProperties);
message = (Message) PacketParserUtils.parseMessage(TestUtils.getMessageParser(control));
message = (Message) PacketParserUtils.parseMessage(PacketParserUtils.getParserFor(control));
assertEquals(otherLanguage, message.getSubject());
assertTrue(message.getSubjectLanguages().isEmpty());
@ -255,7 +254,7 @@ public class PacketParserUtilsTest {
.t(defaultLanguage)
.asString(outputProperties);
message = (Message) PacketParserUtils.parseMessage(TestUtils.getMessageParser(control));
message = (Message) PacketParserUtils.parseMessage(PacketParserUtils.getParserFor(control));
assertEquals(defaultLanguage, message.getSubject());
assertTrue(message.getSubjectLanguages().isEmpty());
@ -274,7 +273,7 @@ public class PacketParserUtilsTest {
.t(defaultLanguage)
.asString(outputProperties);
message = (Message) PacketParserUtils.parseMessage(TestUtils.getMessageParser(control));
message = (Message) PacketParserUtils.parseMessage(PacketParserUtils.getParserFor(control));
assertEquals(defaultLanguage, message.getSubject());
assertTrue(message.getSubjectLanguages().isEmpty());
@ -295,7 +294,7 @@ public class PacketParserUtilsTest {
.t(otherLanguage)
.asString(outputProperties);
message = (Message) PacketParserUtils.parseMessage(TestUtils.getMessageParser(control));
message = (Message) PacketParserUtils.parseMessage(PacketParserUtils.getParserFor(control));
assertNull(message.getSubject());
assertFalse(message.getSubjectLanguages().isEmpty());
@ -316,7 +315,7 @@ public class PacketParserUtilsTest {
.t(otherLanguage)
.asString(outputProperties);
message = (Message) PacketParserUtils.parseMessage(TestUtils.getMessageParser(control));
message = (Message) PacketParserUtils.parseMessage(PacketParserUtils.getParserFor(control));
assertNull(message.getSubject());
assertFalse(message.getSubjectLanguages().isEmpty());
@ -337,7 +336,7 @@ public class PacketParserUtilsTest {
.t(defaultLanguage)
.asString(outputProperties);
message = (Message) PacketParserUtils.parseMessage(TestUtils.getMessageParser(control));
message = (Message) PacketParserUtils.parseMessage(PacketParserUtils.getParserFor(control));
assertNull(message.getSubject());
assertFalse(message.getSubjectLanguages().isEmpty());
@ -372,7 +371,7 @@ public class PacketParserUtilsTest {
.asString(outputProperties);
message = (Message) PacketParserUtils
.parseMessage(TestUtils.getMessageParser(control));
.parseMessage(PacketParserUtils.getParserFor(control));
assertEquals(defaultLanguage, message.getBody());
assertEquals(otherLanguage, message.getBody(otherLanguage));
@ -397,7 +396,7 @@ public class PacketParserUtilsTest {
.asString(outputProperties);
message = (Message) PacketParserUtils
.parseMessage(TestUtils.getMessageParser(control));
.parseMessage(PacketParserUtils.getParserFor(control));
assertEquals(defaultLanguage, message.getBody());
assertEquals(defaultLanguage, message.getBody(defaultLanguage));
@ -421,7 +420,7 @@ public class PacketParserUtilsTest {
.asString(outputProperties);
message = (Message) PacketParserUtils
.parseMessage(TestUtils.getMessageParser(control));
.parseMessage(PacketParserUtils.getParserFor(control));
assertEquals(otherLanguage, message.getBody());
assertEquals(defaultLanguage, message.getBody(defaultLanguage));
@ -445,7 +444,7 @@ public class PacketParserUtilsTest {
.asString(outputProperties);
message = (Message) PacketParserUtils
.parseMessage(TestUtils.getMessageParser(control));
.parseMessage(PacketParserUtils.getParserFor(control));
assertEquals(defaultLanguage, message.getBody());
assertEquals(defaultLanguage, message.getBody(defaultLanguage));
@ -468,7 +467,7 @@ public class PacketParserUtilsTest {
.asString(outputProperties);
message = (Message) PacketParserUtils
.parseMessage(TestUtils.getMessageParser(control));
.parseMessage(PacketParserUtils.getParserFor(control));
assertEquals(defaultLanguage, message.getBody());
assertEquals(defaultLanguage, message.getBody(defaultLanguage));
@ -491,7 +490,7 @@ public class PacketParserUtilsTest {
.asString(outputProperties);
message = (Message) PacketParserUtils
.parseMessage(TestUtils.getMessageParser(control));
.parseMessage(PacketParserUtils.getParserFor(control));
assertEquals(defaultLanguage, message.getBody());
assertEquals(defaultLanguage, message.getBody(defaultLanguage));
@ -525,7 +524,7 @@ public class PacketParserUtilsTest {
.asString(outputProperties);
message = (Message) PacketParserUtils
.parseMessage(TestUtils.getMessageParser(control));
.parseMessage(PacketParserUtils.getParserFor(control));
assertEquals(defaultLanguage, message.getSubject());
assertEquals(otherLanguage, message.getSubject(otherLanguage));
@ -550,7 +549,7 @@ public class PacketParserUtilsTest {
.asString(outputProperties);
message = (Message) PacketParserUtils
.parseMessage(TestUtils.getMessageParser(control));
.parseMessage(PacketParserUtils.getParserFor(control));
assertEquals(defaultLanguage, message.getSubject());
assertEquals(defaultLanguage, message.getSubject(defaultLanguage));
@ -574,7 +573,7 @@ public class PacketParserUtilsTest {
.asString(outputProperties);
message = (Message) PacketParserUtils
.parseMessage(TestUtils.getMessageParser(control));
.parseMessage(PacketParserUtils.getParserFor(control));
assertEquals(otherLanguage, message.getSubject());
assertEquals(defaultLanguage, message.getSubject(defaultLanguage));
@ -598,7 +597,7 @@ public class PacketParserUtilsTest {
.asString(outputProperties);
message = (Message) PacketParserUtils
.parseMessage(TestUtils.getMessageParser(control));
.parseMessage(PacketParserUtils.getParserFor(control));
assertEquals(defaultLanguage, message.getSubject());
assertEquals(defaultLanguage, message.getSubject(defaultLanguage));
@ -621,7 +620,7 @@ public class PacketParserUtilsTest {
.asString(outputProperties);
message = (Message) PacketParserUtils
.parseMessage(TestUtils.getMessageParser(control));
.parseMessage(PacketParserUtils.getParserFor(control));
assertEquals(defaultLanguage, message.getSubject());
assertEquals(defaultLanguage, message.getSubject(defaultLanguage));
@ -644,7 +643,7 @@ public class PacketParserUtilsTest {
.asString(outputProperties);
message = (Message) PacketParserUtils
.parseMessage(TestUtils.getMessageParser(control));
.parseMessage(PacketParserUtils.getParserFor(control));
assertEquals(defaultLanguage, message.getSubject());
assertEquals(defaultLanguage, message.getSubject(defaultLanguage));
@ -670,7 +669,7 @@ public class PacketParserUtilsTest {
.asString(outputProperties);
try {
Message message = (Message) PacketParserUtils.parseMessage(TestUtils.getMessageParser(control));
Message message = (Message) PacketParserUtils.parseMessage(PacketParserUtils.getParserFor(control));
String body = "<span style=\"font-weight: bold;\">"
+ "Bad Message Body</span>";
assertEquals(body, message.getBody());
@ -702,7 +701,7 @@ public class PacketParserUtilsTest {
String invalidControl = validControl.replace("Good Message Body", "Bad </span> Body");
try {
PacketParserUtils.parseMessage(TestUtils.getMessageParser(invalidControl));
PacketParserUtils.parseMessage(PacketParserUtils.getParserFor(invalidControl));
fail("Exception should be thrown");
} catch(XmlPullParserException e) {
assertTrue(e.getMessage().contains("end tag name </span>"));
@ -711,7 +710,7 @@ public class PacketParserUtilsTest {
invalidControl = validControl.replace("Good Message Body", "Bad </body> Body");
try {
PacketParserUtils.parseMessage(TestUtils.getMessageParser(invalidControl));
PacketParserUtils.parseMessage(PacketParserUtils.getParserFor(invalidControl));
fail("Exception should be thrown");
} catch(XmlPullParserException e) {
assertTrue(e.getMessage().contains("end tag name </body>"));
@ -720,7 +719,7 @@ public class PacketParserUtilsTest {
invalidControl = validControl.replace("Good Message Body", "Bad </message> Body");
try {
PacketParserUtils.parseMessage(TestUtils.getMessageParser(invalidControl));
PacketParserUtils.parseMessage(PacketParserUtils.getParserFor(invalidControl));
fail("Exception should be thrown");
} catch(XmlPullParserException e) {
assertTrue(e.getMessage().contains("end tag name </message>"));
@ -748,7 +747,7 @@ public class PacketParserUtilsTest {
.t("This is a test of the emergency broadcast system, 3.")
.asString(outputProperties);
Packet message = PacketParserUtils.parseMessage(TestUtils.getMessageParser(control));
Packet message = PacketParserUtils.parseStanza(control);
Diff xmlDiff = new Diff(control, message.toXML().toString());
xmlDiff.overrideElementQualifier(new RecursiveElementNameAndTextQualifier());
assertTrue(xmlDiff.similar());
@ -758,7 +757,7 @@ public class PacketParserUtilsTest {
public void validateSimplePresence() throws Exception {
String stanza = "<presence from='juliet@example.com/balcony' to='romeo@example.net'/>";
Presence presence = PacketParserUtils.parsePresence(TestUtils.getPresenceParser(stanza));
Presence presence = PacketParserUtils.parsePresence(PacketParserUtils.getParserFor(stanza));
assertXMLEqual(stanza, presence.toXML().toString());
}
@ -767,7 +766,7 @@ public class PacketParserUtilsTest {
public void validatePresenceProbe() throws Exception {
String stanza = "<presence from='mercutio@example.com' id='xv291f38' to='juliet@example.com' type='unsubscribed'/>";
Presence presence = PacketParserUtils.parsePresence(TestUtils.getPresenceParser(stanza));
Presence presence = PacketParserUtils.parsePresence(PacketParserUtils.getParserFor(stanza));
assertXMLEqual(stanza, presence.toXML().toString());
assertEquals(Presence.Type.unsubscribed, presence.getType());
@ -781,7 +780,7 @@ public class PacketParserUtilsTest {
+ "<priority>1</priority>"
+ "</presence>";
Presence presence = PacketParserUtils.parsePresence(TestUtils.getPresenceParser(stanza));
Presence presence = PacketParserUtils.parsePresence(PacketParserUtils.getParserFor(stanza));
assertXMLEqual(stanza, presence.toXML().toString());
assertEquals(Presence.Type.unsubscribed, presence.getType());
assertEquals("dnd", presence.getMode().name());
@ -793,7 +792,7 @@ public class PacketParserUtilsTest {
@Test
public void parseContentDepthTest() throws Exception {
final String stanza = "<iq type='result' to='foo@bar.com' from='baz.com' id='42'/>";
XmlPullParser parser = TestUtils.getParser(stanza, "iq");
XmlPullParser parser = PacketParserUtils.getParserFor(stanza, "iq");
String content = PacketParserUtils.parseContentDepth(parser, 1);
assertEquals("", content);
}

View file

@ -21,7 +21,7 @@ import static org.junit.Assert.assertEquals;
import java.util.Properties;
import org.jivesoftware.smack.provider.ProviderManager;
import org.jivesoftware.smack.test.util.TestUtils;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.jivesoftware.smackx.carbons.packet.CarbonExtension;
import org.jivesoftware.smackx.carbons.provider.CarbonManagerProvider;
import org.jivesoftware.smackx.forward.Forwarded;
@ -58,7 +58,7 @@ public class CarbonTest {
.a("from", "romeo@montague.com")
.asString(outputProperties);
parser = TestUtils.getParser(control, "sent");
parser = PacketParserUtils.getParserFor(control);
cc = (CarbonExtension) new CarbonManagerProvider().parseExtension(parser);
fwd = cc.getForwarded();
@ -89,7 +89,7 @@ public class CarbonTest {
.a("from", "romeo@montague.com")
.asString(outputProperties);
parser = TestUtils.getParser(control, "received");
parser = PacketParserUtils.getParserFor(control);
cc = (CarbonExtension) new CarbonManagerProvider().parseExtension(parser);
assertEquals(CarbonExtension.Direction.received, cc.getDirection());
@ -108,7 +108,7 @@ public class CarbonTest {
.a("xmlns", "urn:xmpp:forwarded:0")
.asString(outputProperties);
parser = TestUtils.getParser(control, "sent");
parser = PacketParserUtils.getParserFor(control);
new CarbonManagerProvider().parseExtension(parser);
}
}

View file

@ -17,7 +17,7 @@
package org.jivesoftware.smackx.hoxt.provider;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.test.util.TestUtils;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.jivesoftware.smackx.hoxt.packet.AbstractHttpOverXmpp;
import org.jivesoftware.smackx.hoxt.packet.HttpOverXmppReq;
import org.jivesoftware.smackx.hoxt.packet.HttpOverXmppResp;
@ -53,7 +53,7 @@ public class AbstractHttpOverXmppProviderTest {
expectedHeaders.put("Content-Length", "0");
AbstractHttpOverXmppProvider provider = new HttpOverXmppRespProvider();
XmlPullParser parser = TestUtils.getParser(string, "resp");
XmlPullParser parser = PacketParserUtils.getParserFor(string);
IQ iq = provider.parseIQ(parser);
assertTrue(iq instanceof HttpOverXmppResp);
@ -73,7 +73,7 @@ public class AbstractHttpOverXmppProviderTest {
expectedHeaders.put("Host", "clayster.com");
AbstractHttpOverXmppProvider provider = new HttpOverXmppReqProvider();
XmlPullParser parser = TestUtils.getParser(string, "req");
XmlPullParser parser = PacketParserUtils.getParserFor(string);
IQ iq = provider.parseIQ(parser);
assertTrue(iq instanceof HttpOverXmppReq);
@ -182,7 +182,7 @@ public class AbstractHttpOverXmppProviderTest {
private AbstractHttpOverXmpp.AbstractBody parseAbstractBody(String string, String tag) throws Exception {
AbstractHttpOverXmppProvider provider = new HttpOverXmppRespProvider();
XmlPullParser parser = TestUtils.getParser(string, tag);
XmlPullParser parser = PacketParserUtils.getParserFor(string, tag);
IQ iq = provider.parseIQ(parser);
assertTrue(iq instanceof HttpOverXmppResp);

View file

@ -17,7 +17,7 @@
package org.jivesoftware.smackx.hoxt.provider;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.test.util.TestUtils;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.jivesoftware.smackx.hoxt.packet.Base64BinaryChunk;
import org.junit.Test;
import org.xmlpull.v1.XmlPullParser;
@ -35,7 +35,7 @@ public class Base64BinaryChunkProviderTest {
String string = "<chunk xmlns='urn:xmpp:http' streamId='Stream0001'>" + base64Text + "</chunk>";
Base64BinaryChunkProvider provider = new Base64BinaryChunkProvider();
XmlPullParser parser = TestUtils.getParser(string, "chunk");
XmlPullParser parser = PacketParserUtils.getParserFor(string);
PacketExtension extension = provider.parseExtension(parser);
assertTrue(extension instanceof Base64BinaryChunk);
@ -52,7 +52,7 @@ public class Base64BinaryChunkProviderTest {
String string = "<chunk xmlns='urn:xmpp:http' streamId='Stream0001' last='true'>" + base64Text + "</chunk>";
Base64BinaryChunkProvider provider = new Base64BinaryChunkProvider();
XmlPullParser parser = TestUtils.getParser(string, "chunk");
XmlPullParser parser = PacketParserUtils.getParserFor(string);
PacketExtension extension = provider.parseExtension(parser);
assertTrue(extension instanceof Base64BinaryChunk);

View file

@ -17,7 +17,7 @@
package org.jivesoftware.smackx.hoxt.provider;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.test.util.TestUtils;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.jivesoftware.smackx.hoxt.packet.HttpMethod;
import org.jivesoftware.smackx.hoxt.packet.HttpOverXmppReq;
import org.junit.Test;
@ -67,7 +67,7 @@ public class HttpOverXmppReqProviderTest {
private HttpOverXmppReq.Req parseReq(String string) throws Exception {
HttpOverXmppReqProvider provider = new HttpOverXmppReqProvider();
XmlPullParser parser = TestUtils.getParser(string, "req");
XmlPullParser parser = PacketParserUtils.getParserFor(string);
IQ iq = provider.parseIQ(parser);
assertTrue(iq instanceof HttpOverXmppReq);
HttpOverXmppReq castedIq = (HttpOverXmppReq) iq;

View file

@ -17,7 +17,7 @@
package org.jivesoftware.smackx.hoxt.provider;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.test.util.TestUtils;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.jivesoftware.smackx.hoxt.packet.HttpOverXmppResp;
import org.junit.Test;
import org.xmlpull.v1.XmlPullParser;
@ -33,7 +33,7 @@ public class HttpOverXmppRespProviderTest {
public void areAllRespAttributesCorrectlyParsed() throws Exception {
String string = "<resp xmlns='urn:xmpp:http' version='1.1' statusCode='200' statusMessage='OK'/>";
HttpOverXmppRespProvider provider = new HttpOverXmppRespProvider();
XmlPullParser parser = TestUtils.getParser(string, "resp");
XmlPullParser parser = PacketParserUtils.getParserFor(string);
IQ iq = provider.parseIQ(parser);
assertTrue(iq instanceof HttpOverXmppResp);
@ -49,7 +49,7 @@ public class HttpOverXmppRespProviderTest {
public void areRespAttributesWothoutMessageCorrectlyParsed() throws Exception {
String string = "<resp xmlns='urn:xmpp:http' version='1.1' statusCode='200'/>";
HttpOverXmppRespProvider provider = new HttpOverXmppRespProvider();
XmlPullParser parser = TestUtils.getParser(string, "resp");
XmlPullParser parser = PacketParserUtils.getParserFor(string);
IQ iq = provider.parseIQ(parser);
assertTrue(iq instanceof HttpOverXmppResp);

View file

@ -22,20 +22,13 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.provider.IQProvider;
import org.jivesoftware.smack.util.Base32Encoder;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.jivesoftware.smack.util.StringEncoder;
import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
import org.jivesoftware.smackx.disco.provider.DiscoverInfoProvider;
import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
/**
* Simple implementation of an EntityCapsPersistentCache that uses a directory
@ -106,7 +99,7 @@ public class SimpleDirectoryPersistentCache implements EntityCapsPersistentCache
try {
info = restoreInfoFromFile(nodeFile);
}
catch (IOException e) {
catch (Exception e) {
LOGGER.log(Level.WARNING, "Coud not restore info from file", e);
}
return info;
@ -147,58 +140,21 @@ public class SimpleDirectoryPersistentCache implements EntityCapsPersistentCache
*
* @param file
* @return the restored DiscoverInfo
* @throws IOException
* @throws Exception
*/
private static DiscoverInfo restoreInfoFromFile(File file) throws IOException {
private static DiscoverInfo restoreInfoFromFile(File file) throws Exception {
DataInputStream dis = new DataInputStream(new FileInputStream(file));
String fileContent = null;
String id;
String from;
String to;
try {
fileContent = dis.readUTF();
} finally {
dis.close();
}
if (fileContent == null)
return null;
Reader reader = new StringReader(fileContent);
XmlPullParser parser;
try {
parser = XmlPullParserFactory.newInstance().newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
parser.setInput(reader);
} catch (XmlPullParserException xppe) {
LOGGER.log(Level.SEVERE, "Exception initializing parser", xppe);
if (fileContent == null) {
return null;
}
DiscoverInfo info = (DiscoverInfo) PacketParserUtils.parseStanza(fileContent);
DiscoverInfo iqPacket;
IQProvider provider = new DiscoverInfoProvider();
// Parse the IQ, we only need the id
try {
parser.next();
id = parser.getAttributeValue("", "id");
from = parser.getAttributeValue("", "from");
to = parser.getAttributeValue("", "to");
parser.next();
} catch (XmlPullParserException e1) {
return null;
}
try {
iqPacket = (DiscoverInfo) provider.parseIQ(parser);
} catch (Exception e) {
return null;
}
iqPacket.setPacketID(id);
iqPacket.setFrom(from);
iqPacket.setTo(to);
iqPacket.setType(IQ.Type.RESULT);
return iqPacket;
return info;
}
}

View file

@ -29,6 +29,7 @@ import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.util.Base32Encoder;
import org.jivesoftware.smack.util.Base64FileUrlEncoder;
import org.jivesoftware.smack.util.StringEncoder;
import org.jivesoftware.smackx.InitExtensions;
import org.jivesoftware.smackx.caps.EntityCapsManager;
import org.jivesoftware.smackx.caps.cache.EntityCapsPersistentCache;
import org.jivesoftware.smackx.caps.cache.SimpleDirectoryPersistentCache;
@ -38,7 +39,7 @@ import org.jivesoftware.smackx.xdata.packet.DataForm;
import org.junit.Test;
public class EntityCapsManagerTest {
public class EntityCapsManagerTest extends InitExtensions {
/**
* <a href="http://xmpp.org/extensions/xep-0115.html#ver-gen-complex">XEP-

View file

@ -28,7 +28,6 @@ import java.util.Properties;
import java.util.TimeZone;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.test.util.TestUtils;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.jivesoftware.smack.util.XmppDateTime;
import org.jivesoftware.smackx.InitExtensions;
@ -65,7 +64,7 @@ public class DelayInformationTest extends InitExtensions {
.t("Offline Storage")
.asString(outputProperties);
parser = TestUtils.getParser(control, "x");
parser = PacketParserUtils.getParserFor(control);
delayInfo = (DelayInformation) p.parseExtension(parser);
assertEquals("capulet.com", delayInfo.getFrom());
@ -81,7 +80,7 @@ public class DelayInformationTest extends InitExtensions {
.a("stamp", "2002-09-10T23:08:25Z")
.asString(outputProperties);
parser = TestUtils.getParser(control, "x");
parser = PacketParserUtils.getParserFor(control);
delayInfo = (DelayInformation) p.parseExtension(parser);
assertEquals("capulet.com", delayInfo.getFrom());
@ -110,7 +109,7 @@ public class DelayInformationTest extends InitExtensions {
.t("Offline Storage")
.asString(outputProperties);
parser = TestUtils.getParser(control, "delay");
parser = PacketParserUtils.getParserFor(control);
delayInfo = (DelayInfo) p.parseExtension(parser);
assertEquals("capulet.com", delayInfo.getFrom());
@ -126,7 +125,7 @@ public class DelayInformationTest extends InitExtensions {
.a("stamp", "2002-09-10T23:08:25Z")
.asString(outputProperties);
parser = TestUtils.getParser(control, "delay");
parser = PacketParserUtils.getParserFor(control);
delayInfo = (DelayInfo) p.parseExtension(parser);
assertEquals("capulet.com", delayInfo.getFrom());
@ -153,7 +152,7 @@ public class DelayInformationTest extends InitExtensions {
.a("stamp", "2002-09-10T23:08:25.12Z")
.asString(outputProperties);
delayInfo = (DelayInfo) p.parseExtension(TestUtils.getParser(control, "delay"));
delayInfo = (DelayInfo) p.parseExtension(PacketParserUtils.getParserFor(control));
GregorianCalendar cal = (GregorianCalendar) calendar.clone();
cal.add(Calendar.MILLISECOND, 12);
@ -166,7 +165,7 @@ public class DelayInformationTest extends InitExtensions {
.a("stamp", "2002-09-10T23:08:25Z")
.asString(outputProperties);
delayInfo = (DelayInfo) p.parseExtension(TestUtils.getParser(control, "delay"));
delayInfo = (DelayInfo) p.parseExtension(PacketParserUtils.getParserFor(control));
assertEquals(calendar.getTime(), delayInfo.getStamp());
@ -177,7 +176,7 @@ public class DelayInformationTest extends InitExtensions {
.a("stamp", "2002-9-10T23:08:25Z")
.asString(outputProperties);
delayInfo = (DelayInfo) p.parseExtension(TestUtils.getParser(control, "delay"));
delayInfo = (DelayInfo) p.parseExtension(PacketParserUtils.getParserFor(control));
assertEquals(calendar.getTime(), delayInfo.getStamp());
@ -188,7 +187,7 @@ public class DelayInformationTest extends InitExtensions {
.a("stamp", "20020910T23:08:25")
.asString(outputProperties);
delayInfo = (DelayInfo) p.parseExtension(TestUtils.getParser(control, "delay"));
delayInfo = (DelayInfo) p.parseExtension(PacketParserUtils.getParserFor(control));
assertEquals(calendar.getTime(), delayInfo.getStamp());
@ -208,7 +207,7 @@ public class DelayInformationTest extends InitExtensions {
.a("stamp", dateFormat.format(dateInPast.getTime()))
.asString(outputProperties);
delayInfo = (DelayInfo) p.parseExtension(TestUtils.getParser(control, "delay"));
delayInfo = (DelayInfo) p.parseExtension(PacketParserUtils.getParserFor(control));
assertEquals(dateInPast.getTime(), delayInfo.getStamp());
@ -219,7 +218,7 @@ public class DelayInformationTest extends InitExtensions {
.a("stamp", "200868T09:16:20")
.asString(outputProperties);
delayInfo = (DelayInfo) p.parseExtension(TestUtils.getParser(control, "delay"));
delayInfo = (DelayInfo) p.parseExtension(PacketParserUtils.getParserFor(control));
Date controlDate = XmppDateTime.parseDate("2008-06-08T09:16:20.0Z");
assertEquals(controlDate, delayInfo.getStamp());
@ -231,7 +230,7 @@ public class DelayInformationTest extends InitExtensions {
.a("stamp", "yesterday")
.asString(outputProperties);
delayInfo = (DelayInfo) p.parseExtension(TestUtils.getParser(control, "delay"));
delayInfo = (DelayInfo) p.parseExtension(PacketParserUtils.getParserFor(control));
assertNotNull(delayInfo.getStamp());
@ -242,7 +241,7 @@ public class DelayInformationTest extends InitExtensions {
String stanza = "<presence from='mercutio@example.com' to='juliet@example.com'>"
+ "<delay xmlns='urn:xmpp:delay' stamp='2002-09-10T23:41:07Z'/></presence>";
Presence presence = PacketParserUtils.parsePresence(TestUtils.getPresenceParser(stanza));
Presence presence = PacketParserUtils.parsePresence(PacketParserUtils.getParserFor(stanza));
DelayInformation delay = (DelayInformation) presence.getExtension("urn:xmpp:delay");
assertNotNull(delay);
@ -255,7 +254,7 @@ public class DelayInformationTest extends InitExtensions {
String stanza = "<presence from='mercutio@example.com' to='juliet@example.com'>"
+ "<x xmlns='jabber:x:delay' stamp='20020910T23:41:07'/></presence>";
Presence presence = PacketParserUtils.parsePresence(TestUtils.getPresenceParser(stanza));
Presence presence = PacketParserUtils.parsePresence(PacketParserUtils.getParserFor(stanza));
DelayInformation delay = (DelayInformation) presence.getExtension("jabber:x:delay");
assertNotNull(delay);
@ -271,7 +270,7 @@ public class DelayInformationTest extends InitExtensions {
String stanza = "<presence from='mercutio@example.com' to='juliet@example.com'>"
+ "<x xmlns='jabber:x:delay'/></presence>";
Presence presence = PacketParserUtils.parsePresence(TestUtils.getPresenceParser(stanza));
Presence presence = PacketParserUtils.parsePresence(PacketParserUtils.getParserFor(stanza));
DelayInformation delay = (DelayInformation) presence.getExtension("urn:xmpp:delay");
assertNull((Object)delay);
}

View file

@ -19,11 +19,13 @@ package org.jivesoftware.smackx.forward;
import static org.junit.Assert.*;
import java.util.Properties;
import org.jivesoftware.smack.test.util.TestUtils;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.jivesoftware.smackx.forward.Forwarded;
import org.jivesoftware.smackx.forward.provider.ForwardedProvider;
import org.junit.Test;
import org.xmlpull.v1.XmlPullParser;
import com.jamesmurty.utils.XMLBuilder;
public class ForwardedTest {
@ -45,7 +47,7 @@ public class ForwardedTest {
.a("from", "romeo@montague.com")
.asString(outputProperties);
parser = TestUtils.getParser(control, "forwarded");
parser = PacketParserUtils.getParserFor(control);
fwd = (Forwarded) new ForwardedProvider().parseExtension(parser);
// no delay in packet
@ -69,7 +71,7 @@ public class ForwardedTest {
.a("xmlns", "urn:xmpp:forwarded:0")
.asString(outputProperties);
parser = TestUtils.getParser(control, "forwarded");
parser = PacketParserUtils.getParserFor(control);
new ForwardedProvider().parseExtension(parser);
}
}

View file

@ -22,7 +22,6 @@ import static org.junit.Assert.assertTrue;
import org.jivesoftware.smack.DummyConnection;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.test.util.TestUtils;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.jivesoftware.smackx.InitExtensions;
import org.jivesoftware.smackx.iqlast.packet.LastActivity;
@ -43,7 +42,7 @@ public class LastActivityTest extends InitExtensions {
.namespace(LastActivity.NAMESPACE);
DummyConnection c = new DummyConnection();
IQ lastRequest = PacketParserUtils.parseIQ(TestUtils.getIQParser(xml.asString()), c);
IQ lastRequest = (IQ) PacketParserUtils.parseStanza(xml.asString());
assertTrue(lastRequest instanceof LastActivity);
c.processPacket(lastRequest);;

View file

@ -22,7 +22,6 @@ import static org.junit.Assert.assertTrue;
import org.jivesoftware.smack.DummyConnection;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.test.util.TestUtils;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.jivesoftware.smackx.iqversion.packet.Version;
import org.junit.Test;
@ -39,7 +38,7 @@ public class VersionTest {
// Enable version replys for this connection
VersionManager.getInstanceFor(con).setVersion(new Version("Test", "0.23", "DummyOS"));
IQ versionRequest = PacketParserUtils.parseIQ(TestUtils.getIQParser(control), con);
IQ versionRequest = (IQ) PacketParserUtils.parseStanza(control);
assertTrue(versionRequest instanceof Version);

View file

@ -20,7 +20,6 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.test.util.TestUtils;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.jivesoftware.smackx.InitExtensions;
import org.jivesoftware.smackx.jiveproperties.packet.JivePropertiesExtension;
@ -54,7 +53,7 @@ public class JivePropertiesExtensionTest extends InitExtensions {
+ "</message>";
// @formatter:on
Message message = PacketParserUtils.parseMessage(TestUtils.getMessageParser(properties));
Message message = (Message) PacketParserUtils.parseStanza(properties);
JivePropertiesExtension jpe = (JivePropertiesExtension) message.getExtension(JivePropertiesExtension.NAMESPACE);
assertNotNull(jpe);

View file

@ -28,7 +28,6 @@ import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.ThreadedDummyConnection;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.test.util.TestUtils;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.jivesoftware.smackx.InitExtensions;
import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
@ -57,7 +56,7 @@ public class PingTest extends InitExtensions {
// Enable ping for this connection
PingManager.getInstanceFor(con);
IQ pingRequest = PacketParserUtils.parseIQ(TestUtils.getIQParser(control), con);
IQ pingRequest = (IQ) PacketParserUtils.parseStanza(control);
assertTrue(pingRequest instanceof Ping);
@ -133,7 +132,7 @@ public class PingTest extends InitExtensions {
"</error>" +
"</iq>";
//@formatter:on
IQ serviceUnavailable = PacketParserUtils.parseIQ(TestUtils.getIQParser(reply), threadedCon);
IQ serviceUnavailable = (IQ) PacketParserUtils.parseStanza(reply);
threadedCon.addIQReply(serviceUnavailable);
PingManager pinger = PingManager.getInstanceFor(threadedCon);
@ -169,7 +168,7 @@ public class PingTest extends InitExtensions {
"</error>" +
"</iq>";
//@formatter:on
IQ serviceUnavailable = PacketParserUtils.parseIQ(TestUtils.getIQParser(reply), con);
IQ serviceUnavailable = (IQ) PacketParserUtils.parseStanza(reply);
con.addIQReply(serviceUnavailable);
PingManager pinger = PingManager.getInstanceFor(con);
@ -201,7 +200,7 @@ public class PingTest extends InitExtensions {
"<feature var='urn:xmpp:ping'/>" +
"</query></iq>";
//@formatter:on
IQ discoReply = PacketParserUtils.parseIQ(TestUtils.getIQParser(reply), con);
IQ discoReply = (IQ) PacketParserUtils.parseStanza(reply);
con.addIQReply(discoReply);
PingManager pinger = PingManager.getInstanceFor(con);
@ -223,7 +222,7 @@ public class PingTest extends InitExtensions {
"<feature var='urn:xmpp:noping'/>" +
"</query></iq>";
//@formatter:on
IQ discoReply = PacketParserUtils.parseIQ(TestUtils.getIQParser(reply), con);
IQ discoReply = (IQ) PacketParserUtils.parseStanza(reply);
con.addIQReply(discoReply);
PingManager pinger = PingManager.getInstanceFor(con);

View file

@ -21,9 +21,7 @@ import static org.junit.Assert.assertTrue;
import java.util.List;
import org.jivesoftware.smack.DummyConnection;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.test.util.TestUtils;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.jivesoftware.smackx.InitExtensions;
import org.jivesoftware.smackx.privacy.packet.Privacy;
@ -34,8 +32,6 @@ public class PrivacyProviderTest extends InitExtensions {
@Test
public void parsePrivacyList() throws Exception {
DummyConnection connection = new DummyConnection();
// @formatter:off
final String xmlPrivacyList =
"<iq type='result' id='getlist2' to='romeo@example.net/orchard'>"
@ -50,7 +46,7 @@ public class PrivacyProviderTest extends InitExtensions {
+ "</query>"
+ "</iq>";
// @formatter:on
IQ iqPrivacyList = PacketParserUtils.parseIQ(TestUtils.getIQParser(xmlPrivacyList), connection);
IQ iqPrivacyList = (IQ) PacketParserUtils.parseStanza(xmlPrivacyList);
assertTrue(iqPrivacyList instanceof Privacy);
Privacy privacyList = (Privacy) iqPrivacyList;

View file

@ -23,7 +23,6 @@ import static org.junit.Assert.assertTrue;
import org.jivesoftware.smack.ThreadedDummyConnection;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.test.util.TestUtils;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.jivesoftware.smackx.InitExtensions;
import org.jivesoftware.smackx.pubsub.packet.PubSubNamespace;
@ -95,7 +94,7 @@ public class ItemValidationTest extends InitExtensions {
@Test
public void parseBasicItem() throws Exception
{
XmlPullParser parser = TestUtils.getMessageParser(
XmlPullParser parser = PacketParserUtils.getParserFor(
"<message from='pubsub.myserver.com' to='francisco@denmark.lit' id='foo'>" +
"<event xmlns='http://jabber.org/protocol/pubsub#event'>" +
"<items node='testNode'>" +
@ -124,7 +123,7 @@ public class ItemValidationTest extends InitExtensions {
{
String itemContent = "<foo xmlns='smack:test'>Some text</foo>";
XmlPullParser parser = TestUtils.getMessageParser(
XmlPullParser parser = PacketParserUtils.getParserFor(
"<message from='pubsub.myserver.com' to='francisco@denmark.lit' id='foo'>" +
"<event xmlns='http://jabber.org/protocol/pubsub#event'>" +
"<items node='testNode'>" +
@ -171,7 +170,7 @@ public class ItemValidationTest extends InitExtensions {
"<updated>2003-12-13T18:30:02Z</updated>" +
"</entry>";
XmlPullParser parser = TestUtils.getMessageParser(
XmlPullParser parser = PacketParserUtils.getParserFor(
"<message from='pubsub.myserver.com' to='francisco@denmark.lit' id='foo'>" +
"<event xmlns='http://jabber.org/protocol/pubsub#event'>" +
"<items node='testNode'>" +
@ -204,7 +203,7 @@ public class ItemValidationTest extends InitExtensions {
{
String itemContent = "<foo xmlns='smack:test'><bar/></foo>";
XmlPullParser parser = TestUtils.getMessageParser(
XmlPullParser parser = PacketParserUtils.getParserFor(
"<message from='pubsub.myserver.com' to='francisco@denmark.lit' id='foo'>" +
"<event xmlns='http://jabber.org/protocol/pubsub#event'>" +
"<items node='testNode'>" +

View file

@ -26,7 +26,6 @@ import java.util.Properties;
import org.jivesoftware.smack.DummyConnection;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.test.util.TestUtils;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.jivesoftware.smackx.InitExtensions;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
@ -53,7 +52,7 @@ public class DeliveryReceiptTest extends InitExtensions {
.a("xmlns", "urn:xmpp:receipts")
.asString(outputProperties);
parser = TestUtils.getMessageParser(control);
parser = PacketParserUtils.getParserFor(control);
Packet p = PacketParserUtils.parseMessage(parser);
DeliveryReceiptRequest drr = (DeliveryReceiptRequest)p.getExtension(

View file

@ -23,9 +23,7 @@ import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import org.jivesoftware.smack.DummyConnection;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.test.util.TestUtils;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.jivesoftware.smackx.InitExtensions;
import org.junit.Test;
@ -63,8 +61,6 @@ public class TimeTest extends InitExtensions {
@Test
public void parseTimeWithIntrospectionTest() throws Exception {
DummyConnection connection = new DummyConnection();
// @formatter:off
final String request =
"<iq type='get'"
@ -74,7 +70,7 @@ public class TimeTest extends InitExtensions {
+ "<time xmlns='urn:xmpp:time'/>"
+ "</iq>";
// @formatter:on
IQ iqRequest = PacketParserUtils.parseIQ(TestUtils.getIQParser(request), connection);
IQ iqRequest = (IQ) PacketParserUtils.parseStanza(request);
assertTrue(iqRequest instanceof Time);
// @formatter:off
@ -89,7 +85,7 @@ public class TimeTest extends InitExtensions {
+ "</time>"
+ "</iq>";
// @formatter:on
IQ iqResponse = PacketParserUtils.parseIQ(TestUtils.getIQParser(response), connection);
IQ iqResponse = (IQ) PacketParserUtils.parseStanza(response);
assertTrue(iqResponse instanceof Time);
Time time = (Time) iqResponse;
assertEquals("-06:00", time.getTzo());

View file

@ -19,9 +19,7 @@ package org.jivesoftware.smack.tcp;
import java.io.IOException;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.parsing.ParsingExceptionCallback;
import org.jivesoftware.smack.parsing.UnparsablePacket;
import org.jivesoftware.smack.sasl.SASLMechanism.Challenge;
@ -151,52 +149,25 @@ class PacketReader {
do {
if (eventType == XmlPullParser.START_TAG) {
int parserDepth = parser.getDepth();
String name = parser.getName();
ParsingExceptionCallback callback = connection.getParsingExceptionCallback();
if (parser.getName().equals("message")) {
Packet packet;
try {
packet = PacketParserUtils.parseMessage(parser);
} catch (Exception e) {
String content = PacketParserUtils.parseContentDepth(parser, parserDepth);
UnparsablePacket message = new UnparsablePacket(content, e);
if (callback != null) {
callback.handleUnparsablePacket(message);
}
continue;
Packet packet;
try {
packet = PacketParserUtils.parseStanza(parser, connection);
} catch (Exception e) {
String content = PacketParserUtils.parseContentDepth(parser, parserDepth);
UnparsablePacket message = new UnparsablePacket(content, e);
if (callback != null) {
callback.handleUnparsablePacket(message);
}
continue;
}
if (packet != null) {
connection.processPacket(packet);
}
else if (parser.getName().equals("iq")) {
IQ iq;
try {
iq = PacketParserUtils.parseIQ(parser, connection);
} catch (Exception e) {
String content = PacketParserUtils.parseContentDepth(parser, parserDepth);
UnparsablePacket message = new UnparsablePacket(content, e);
if (callback != null) {
callback.handleUnparsablePacket(message);
}
continue;
}
connection.processPacket(iq);
}
else if (parser.getName().equals("presence")) {
Presence presence;
try {
presence = PacketParserUtils.parsePresence(parser);
} catch (Exception e) {
String content = PacketParserUtils.parseContentDepth(parser, parserDepth);
UnparsablePacket message = new UnparsablePacket(content, e);
if (callback != null) {
callback.handleUnparsablePacket(message);
}
continue;
}
connection.processPacket(presence);
}
// We found an opening stream. Record information about it, then notify
// the connectionID lock so that the packet reader startup can finish.
else if (parser.getName().equals("stream")) {
else if (name.equals("stream")) {
// Ensure the correct jabber:client namespace is being used.
if ("jabber:client".equals(parser.getNamespace(null))) {
// Get the connection id.
@ -212,20 +183,20 @@ class PacketReader {
}
}
}
else if (parser.getName().equals("error")) {
else if (name.equals("error")) {
throw new StreamErrorException(PacketParserUtils.parseStreamError(parser));
}
else if (parser.getName().equals("features")) {
else if (name.equals("features")) {
parseFeatures(parser);
}
else if (parser.getName().equals("proceed")) {
else if (name.equals("proceed")) {
// Secure the connection by negotiating TLS
connection.proceedTLSReceived();
// Reset the state of the parser since a new stream element is going
// to be sent by the server
resetParser();
}
else if (parser.getName().equals("failure")) {
else if (name.equals("failure")) {
String namespace = parser.getNamespace(null);
if ("urn:ietf:params:xml:ns:xmpp-tls".equals(namespace)) {
// TLS negotiation has failed. The server will close the connection
@ -245,13 +216,13 @@ class PacketReader {
connection.getSASLAuthentication().authenticationFailed(failure);
}
}
else if (parser.getName().equals("challenge")) {
else if (name.equals("challenge")) {
// The server is challenging the SASL authentication made by the client
String challengeData = parser.nextText();
connection.processPacket(new Challenge(challengeData));
connection.getSASLAuthentication().challengeReceived(challengeData);
}
else if (parser.getName().equals("success")) {
else if (name.equals("success")) {
connection.processPacket(new Success(parser.nextText()));
// We now need to bind a resource for the connection
// Open a new stream and wait for the response
@ -263,7 +234,7 @@ class PacketReader {
// will be to bind the resource
connection.getSASLAuthentication().authenticated();
}
else if (parser.getName().equals("compressed")) {
else if (name.equals("compressed")) {
// Server confirmed that it's possible to use stream compression. Start
// stream compression
connection.startStreamCompression();