2004-03-29 23:42:32 +02:00
|
|
|
/**
|
2004-11-03 00:53:30 +01:00
|
|
|
* $RCSfile$
|
|
|
|
* $Revision$
|
|
|
|
* $Date$
|
|
|
|
*
|
|
|
|
* Copyright 2003-2004 Jive Software.
|
|
|
|
*
|
|
|
|
* All rights reserved. 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.
|
|
|
|
*/
|
2004-03-29 23:42:32 +02:00
|
|
|
|
|
|
|
package org.jivesoftware.smack.util;
|
|
|
|
|
2005-02-23 04:32:10 +01:00
|
|
|
import org.jivesoftware.smack.packet.*;
|
2004-03-29 23:42:32 +02:00
|
|
|
import org.jivesoftware.smack.provider.PacketExtensionProvider;
|
|
|
|
import org.jivesoftware.smack.provider.ProviderManager;
|
|
|
|
import org.xmlpull.v1.XmlPullParser;
|
|
|
|
|
2006-09-13 22:56:47 +02:00
|
|
|
import java.beans.PropertyDescriptor;
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
import java.io.ObjectInputStream;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2004-03-29 23:42:32 +02:00
|
|
|
/**
|
|
|
|
* Utility class that helps to parse packets. Any parsing packets method that must be shared
|
2006-09-20 18:43:40 +02:00
|
|
|
* between many clients must be placed in this utility class.
|
|
|
|
*
|
2004-03-29 23:42:32 +02:00
|
|
|
* @author Gaston Dombiak
|
|
|
|
*/
|
|
|
|
public class PacketParserUtils {
|
|
|
|
|
2005-02-23 04:32:10 +01:00
|
|
|
/**
|
|
|
|
* Namespace used to store packet properties.
|
|
|
|
*/
|
|
|
|
private static final String PROPERTIES_NAMESPACE =
|
|
|
|
"http://www.jivesoftware.com/xmlns/xmpp/properties";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses a message packet.
|
|
|
|
*
|
|
|
|
* @param parser the XML parser, positioned at the start of a message packet.
|
|
|
|
* @return a Message packet.
|
|
|
|
* @throws Exception if an exception occurs while parsing the packet.
|
|
|
|
*/
|
|
|
|
public static Packet parseMessage(XmlPullParser parser) throws Exception {
|
|
|
|
Message message = new Message();
|
|
|
|
String id = parser.getAttributeValue("", "id");
|
|
|
|
message.setPacketID(id == null ? Packet.ID_NOT_AVAILABLE : id);
|
|
|
|
message.setTo(parser.getAttributeValue("", "to"));
|
|
|
|
message.setFrom(parser.getAttributeValue("", "from"));
|
|
|
|
message.setType(Message.Type.fromString(parser.getAttributeValue("", "type")));
|
|
|
|
|
|
|
|
// Parse sub-elements. We include extra logic to make sure the values
|
|
|
|
// are only read once. This is because it's possible for the names to appear
|
|
|
|
// in arbitrary sub-elements.
|
|
|
|
boolean done = false;
|
|
|
|
String subject = null;
|
|
|
|
String body = null;
|
|
|
|
String thread = null;
|
2006-07-19 21:24:00 +02:00
|
|
|
Map<String, Object> properties = null;
|
2005-02-23 04:32:10 +01:00
|
|
|
while (!done) {
|
|
|
|
int eventType = parser.next();
|
|
|
|
if (eventType == XmlPullParser.START_TAG) {
|
|
|
|
String elementName = parser.getName();
|
|
|
|
String namespace = parser.getNamespace();
|
|
|
|
if (elementName.equals("subject")) {
|
|
|
|
if (subject == null) {
|
|
|
|
subject = parser.nextText();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (elementName.equals("body")) {
|
|
|
|
if (body == null) {
|
|
|
|
body = parser.nextText();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (elementName.equals("thread")) {
|
|
|
|
if (thread == null) {
|
|
|
|
thread = parser.nextText();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (elementName.equals("error")) {
|
|
|
|
message.setError(parseError(parser));
|
|
|
|
}
|
|
|
|
else if (elementName.equals("properties") &&
|
|
|
|
namespace.equals(PROPERTIES_NAMESPACE))
|
|
|
|
{
|
|
|
|
properties = parseProperties(parser);
|
|
|
|
}
|
|
|
|
// Otherwise, it must be a packet extension.
|
|
|
|
else {
|
|
|
|
message.addExtension(
|
|
|
|
PacketParserUtils.parsePacketExtension(elementName, namespace, parser));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (eventType == XmlPullParser.END_TAG) {
|
|
|
|
if (parser.getName().equals("message")) {
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
message.setSubject(subject);
|
|
|
|
message.setBody(body);
|
|
|
|
message.setThread(thread);
|
|
|
|
// Set packet properties.
|
|
|
|
if (properties != null) {
|
2006-07-19 21:24:00 +02:00
|
|
|
for (String name : properties.keySet()) {
|
2005-02-23 04:32:10 +01:00
|
|
|
message.setProperty(name, properties.get(name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses a presence packet.
|
|
|
|
*
|
|
|
|
* @param parser the XML parser, positioned at the start of a presence packet.
|
|
|
|
* @return a Presence packet.
|
|
|
|
* @throws Exception if an exception occurs while parsing the packet.
|
|
|
|
*/
|
|
|
|
public static Presence parsePresence(XmlPullParser parser) throws Exception {
|
2006-07-17 10:39:08 +02:00
|
|
|
Presence.Type type = Presence.Type.available;
|
|
|
|
String typeString = parser.getAttributeValue("", "type");
|
|
|
|
if (typeString != null && !typeString.equals("")) {
|
|
|
|
try {
|
|
|
|
type = Presence.Type.valueOf(typeString);
|
|
|
|
}
|
|
|
|
catch (IllegalArgumentException iae) {
|
|
|
|
System.err.println("Found invalid presence type " + typeString);
|
|
|
|
}
|
|
|
|
}
|
2005-02-23 04:32:10 +01:00
|
|
|
|
|
|
|
Presence presence = new Presence(type);
|
|
|
|
presence.setTo(parser.getAttributeValue("", "to"));
|
|
|
|
presence.setFrom(parser.getAttributeValue("", "from"));
|
|
|
|
String id = parser.getAttributeValue("", "id");
|
|
|
|
presence.setPacketID(id == null ? Packet.ID_NOT_AVAILABLE : id);
|
|
|
|
|
|
|
|
// Parse sub-elements
|
|
|
|
boolean done = false;
|
|
|
|
while (!done) {
|
|
|
|
int eventType = parser.next();
|
|
|
|
if (eventType == XmlPullParser.START_TAG) {
|
|
|
|
String elementName = parser.getName();
|
|
|
|
String namespace = parser.getNamespace();
|
|
|
|
if (elementName.equals("status")) {
|
|
|
|
presence.setStatus(parser.nextText());
|
|
|
|
}
|
|
|
|
else if (elementName.equals("priority")) {
|
|
|
|
try {
|
|
|
|
int priority = Integer.parseInt(parser.nextText());
|
|
|
|
presence.setPriority(priority);
|
|
|
|
}
|
2006-07-17 10:39:08 +02:00
|
|
|
catch (NumberFormatException nfe) {
|
|
|
|
// Ignore.
|
|
|
|
}
|
2005-08-03 04:42:53 +02:00
|
|
|
catch (IllegalArgumentException iae) {
|
|
|
|
// Presence priority is out of range so assume priority to be zero
|
|
|
|
presence.setPriority(0);
|
|
|
|
}
|
2005-02-23 04:32:10 +01:00
|
|
|
}
|
|
|
|
else if (elementName.equals("show")) {
|
2006-07-17 10:39:08 +02:00
|
|
|
String modeText = parser.nextText();
|
|
|
|
try {
|
|
|
|
presence.setMode(Presence.Mode.valueOf(modeText));
|
|
|
|
}
|
|
|
|
catch (IllegalArgumentException iae) {
|
|
|
|
System.err.println("Found invalid presence mode " + modeText);
|
|
|
|
}
|
2005-02-23 04:32:10 +01:00
|
|
|
}
|
|
|
|
else if (elementName.equals("error")) {
|
|
|
|
presence.setError(parseError(parser));
|
|
|
|
}
|
|
|
|
else if (elementName.equals("properties") &&
|
|
|
|
namespace.equals(PROPERTIES_NAMESPACE))
|
|
|
|
{
|
2006-07-19 21:24:00 +02:00
|
|
|
Map<String,Object> properties = parseProperties(parser);
|
2005-02-23 04:32:10 +01:00
|
|
|
// Set packet properties.
|
2006-07-19 21:24:00 +02:00
|
|
|
for (String name : properties.keySet()) {
|
2005-02-23 04:32:10 +01:00
|
|
|
presence.setProperty(name, properties.get(name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Otherwise, it must be a packet extension.
|
|
|
|
else {
|
|
|
|
presence.addExtension(
|
|
|
|
PacketParserUtils.parsePacketExtension(elementName, namespace, parser));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (eventType == XmlPullParser.END_TAG) {
|
|
|
|
if (parser.getName().equals("presence")) {
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return presence;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse a properties sub-packet. If any errors occur while de-serializing Java object
|
|
|
|
* properties, an exception will be printed and not thrown since a thrown
|
|
|
|
* exception will shut down the entire connection. ClassCastExceptions will occur
|
|
|
|
* when both the sender and receiver of the packet don't have identical versions
|
|
|
|
* of the same class.
|
|
|
|
*
|
|
|
|
* @param parser the XML parser, positioned at the start of a properties sub-packet.
|
|
|
|
* @return a map of the properties.
|
|
|
|
* @throws Exception if an error occurs while parsing the properties.
|
|
|
|
*/
|
2006-07-19 21:24:00 +02:00
|
|
|
public static Map<String, Object> parseProperties(XmlPullParser parser) throws Exception {
|
|
|
|
Map<String, Object> properties = new HashMap<String, Object>();
|
2005-02-23 04:32:10 +01:00
|
|
|
while (true) {
|
|
|
|
int eventType = parser.next();
|
|
|
|
if (eventType == XmlPullParser.START_TAG && parser.getName().equals("property")) {
|
|
|
|
// Parse a property
|
|
|
|
boolean done = false;
|
|
|
|
String name = null;
|
|
|
|
String type = null;
|
|
|
|
String valueText = null;
|
|
|
|
Object value = null;
|
|
|
|
while (!done) {
|
|
|
|
eventType = parser.next();
|
|
|
|
if (eventType == XmlPullParser.START_TAG) {
|
|
|
|
String elementName = parser.getName();
|
|
|
|
if (elementName.equals("name")) {
|
|
|
|
name = parser.nextText();
|
|
|
|
}
|
|
|
|
else if (elementName.equals("value")) {
|
|
|
|
type = parser.getAttributeValue("", "type");
|
|
|
|
valueText = parser.nextText();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (eventType == XmlPullParser.END_TAG) {
|
|
|
|
if (parser.getName().equals("property")) {
|
|
|
|
if ("integer".equals(type)) {
|
|
|
|
value = new Integer(valueText);
|
|
|
|
}
|
|
|
|
else if ("long".equals(type)) {
|
|
|
|
value = new Long(valueText);
|
|
|
|
}
|
|
|
|
else if ("float".equals(type)) {
|
|
|
|
value = new Float(valueText);
|
|
|
|
}
|
|
|
|
else if ("double".equals(type)) {
|
|
|
|
value = new Double(valueText);
|
|
|
|
}
|
|
|
|
else if ("boolean".equals(type)) {
|
2006-07-19 21:24:00 +02:00
|
|
|
value = Boolean.valueOf(valueText);
|
2005-02-23 04:32:10 +01:00
|
|
|
}
|
|
|
|
else if ("string".equals(type)) {
|
|
|
|
value = valueText;
|
|
|
|
}
|
|
|
|
else if ("java-object".equals(type)) {
|
|
|
|
try {
|
|
|
|
byte [] bytes = StringUtils.decodeBase64(valueText);
|
|
|
|
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes));
|
|
|
|
value = in.readObject();
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (name != null && value != null) {
|
|
|
|
properties.put(name, value);
|
|
|
|
}
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (eventType == XmlPullParser.END_TAG) {
|
|
|
|
if (parser.getName().equals("properties")) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return properties;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses error sub-packets.
|
|
|
|
*
|
|
|
|
* @param parser the XML parser.
|
|
|
|
* @return an error sub-packet.
|
|
|
|
* @throws Exception if an exception occurs while parsing the packet.
|
|
|
|
*/
|
|
|
|
public static XMPPError parseError(XmlPullParser parser) throws Exception {
|
2006-07-19 21:24:00 +02:00
|
|
|
final String errorNamespace = "urn:ietf:params:xml:ns:xmpp-stanzas";
|
|
|
|
String errorCode = "-1";
|
2006-10-11 00:03:22 +02:00
|
|
|
String type = "";
|
2005-02-23 04:32:10 +01:00
|
|
|
String message = null;
|
2006-07-19 21:24:00 +02:00
|
|
|
String condition = null;
|
|
|
|
List<PacketExtension> extensions = new ArrayList<PacketExtension>();
|
|
|
|
|
|
|
|
// Parse the error header
|
2005-02-23 04:32:10 +01:00
|
|
|
for (int i=0; i<parser.getAttributeCount(); i++) {
|
|
|
|
if (parser.getAttributeName(i).equals("code")) {
|
|
|
|
errorCode = parser.getAttributeValue("", "code");
|
|
|
|
}
|
2006-07-19 21:24:00 +02:00
|
|
|
if (parser.getAttributeName(i).equals("type")) {
|
|
|
|
type = parser.getAttributeValue("", "type");
|
|
|
|
}
|
2005-02-23 04:32:10 +01:00
|
|
|
}
|
2006-07-19 21:24:00 +02:00
|
|
|
boolean done = false;
|
|
|
|
// Parse the text and condition tags
|
|
|
|
while (!done) {
|
|
|
|
int eventType = parser.next();
|
|
|
|
if (eventType == XmlPullParser.START_TAG) {
|
|
|
|
if (parser.getName().equals("text")) {
|
2006-08-25 22:18:54 +02:00
|
|
|
message = parser.nextText();
|
2006-07-19 21:24:00 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Condition tag, it can be xmpp error or an application defined error.
|
|
|
|
String elementName = parser.getName();
|
|
|
|
String namespace = parser.getNamespace();
|
|
|
|
if (errorNamespace.equals(namespace)) {
|
|
|
|
condition = elementName;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
extensions.add(parsePacketExtension(elementName, namespace, parser));
|
|
|
|
}
|
|
|
|
}
|
2005-02-23 04:32:10 +01:00
|
|
|
}
|
2006-07-19 21:24:00 +02:00
|
|
|
else if (eventType == XmlPullParser.END_TAG) {
|
|
|
|
if (parser.getName().equals("error")) {
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
}
|
2005-02-23 04:32:10 +01:00
|
|
|
}
|
2006-09-20 18:43:40 +02:00
|
|
|
// Parse the error type.
|
|
|
|
XMPPError.Type errorType = XMPPError.Type.CANCEL;
|
|
|
|
try {
|
2006-10-17 15:15:35 +02:00
|
|
|
if (type != null) {
|
|
|
|
errorType = XMPPError.Type.valueOf(type.toUpperCase());
|
|
|
|
}
|
2006-09-20 18:43:40 +02:00
|
|
|
}
|
|
|
|
catch (IllegalArgumentException iae) {
|
|
|
|
// Print stack trace. We shouldn't be getting an illegal error type.
|
|
|
|
iae.printStackTrace();
|
|
|
|
}
|
|
|
|
return new XMPPError(Integer.parseInt(errorCode), errorType, condition, message, extensions);
|
2005-02-23 04:32:10 +01:00
|
|
|
}
|
|
|
|
|
2004-03-29 23:42:32 +02:00
|
|
|
/**
|
|
|
|
* Parses a packet extension sub-packet.
|
|
|
|
*
|
|
|
|
* @param elementName the XML element name of the packet extension.
|
|
|
|
* @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.
|
|
|
|
*/
|
|
|
|
public static PacketExtension parsePacketExtension(String elementName, String namespace, XmlPullParser parser)
|
|
|
|
throws Exception
|
|
|
|
{
|
|
|
|
// See if a provider is registered to handle the extension.
|
2006-11-16 20:31:30 +01:00
|
|
|
Object provider = ProviderManager.getInstance().getExtensionProvider(elementName, namespace);
|
2004-03-29 23:42:32 +02:00
|
|
|
if (provider != null) {
|
|
|
|
if (provider instanceof PacketExtensionProvider) {
|
|
|
|
return ((PacketExtensionProvider)provider).parseExtension(parser);
|
|
|
|
}
|
|
|
|
else if (provider instanceof Class) {
|
|
|
|
return (PacketExtension)parseWithIntrospection(
|
|
|
|
elementName, (Class)provider, parser);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// No providers registered, so use a default extension.
|
|
|
|
DefaultPacketExtension extension = new DefaultPacketExtension(elementName, namespace);
|
|
|
|
boolean done = false;
|
|
|
|
while (!done) {
|
|
|
|
int eventType = parser.next();
|
|
|
|
if (eventType == XmlPullParser.START_TAG) {
|
|
|
|
String name = parser.getName();
|
|
|
|
// If an empty element, set the value with the empty string.
|
|
|
|
if (parser.isEmptyElementTag()) {
|
|
|
|
extension.setValue(name,"");
|
|
|
|
}
|
|
|
|
// Otherwise, get the the element text.
|
|
|
|
else {
|
|
|
|
eventType = parser.next();
|
|
|
|
if (eventType == XmlPullParser.TEXT) {
|
|
|
|
String value = parser.getText();
|
|
|
|
extension.setValue(name, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (eventType == XmlPullParser.END_TAG) {
|
|
|
|
if (parser.getName().equals(elementName)) {
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return extension;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Object parseWithIntrospection(String elementName,
|
|
|
|
Class objectClass, XmlPullParser parser) throws Exception
|
|
|
|
{
|
|
|
|
boolean done = false;
|
|
|
|
Object object = objectClass.newInstance();
|
|
|
|
while (!done) {
|
|
|
|
int eventType = parser.next();
|
|
|
|
if (eventType == XmlPullParser.START_TAG) {
|
|
|
|
String name = parser.getName();
|
|
|
|
String stringValue = parser.nextText();
|
|
|
|
PropertyDescriptor descriptor = new PropertyDescriptor(name, objectClass);
|
|
|
|
// Load the class type of the property.
|
|
|
|
Class propertyType = descriptor.getPropertyType();
|
|
|
|
// Get the value of the property by converting it from a
|
|
|
|
// String to the correct object type.
|
|
|
|
Object value = decode(propertyType, stringValue);
|
|
|
|
// Set the value of the bean.
|
2006-07-19 21:24:00 +02:00
|
|
|
descriptor.getWriteMethod().invoke(object, value);
|
2004-03-29 23:42:32 +02:00
|
|
|
}
|
|
|
|
else if (eventType == XmlPullParser.END_TAG) {
|
|
|
|
if (parser.getName().equals(elementName)) {
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decodes a String into an object of the specified type. If the object
|
|
|
|
* type is not supported, null will be returned.
|
|
|
|
*
|
|
|
|
* @param type the type of the property.
|
|
|
|
* @param value the encode String value to decode.
|
|
|
|
* @return the String value decoded into the specified type.
|
2006-11-16 20:31:30 +01:00
|
|
|
* @throws Exception If decoding failed due to an error.
|
2004-03-29 23:42:32 +02:00
|
|
|
*/
|
|
|
|
private static Object decode(Class type, String value) throws Exception {
|
|
|
|
if (type.getName().equals("java.lang.String")) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
if (type.getName().equals("boolean")) {
|
|
|
|
return Boolean.valueOf(value);
|
|
|
|
}
|
|
|
|
if (type.getName().equals("int")) {
|
|
|
|
return Integer.valueOf(value);
|
|
|
|
}
|
|
|
|
if (type.getName().equals("long")) {
|
|
|
|
return Long.valueOf(value);
|
|
|
|
}
|
|
|
|
if (type.getName().equals("float")) {
|
|
|
|
return Float.valueOf(value);
|
|
|
|
}
|
|
|
|
if (type.getName().equals("double")) {
|
|
|
|
return Double.valueOf(value);
|
|
|
|
}
|
|
|
|
if (type.getName().equals("java.lang.Class")) {
|
|
|
|
return Class.forName(value);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2006-07-19 21:24:00 +02:00
|
|
|
}
|