001/**
002 *
003 * Copyright 2003-2007 Jive Software.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.jiveproperties.provider;
018
019import java.io.ByteArrayInputStream;
020import java.io.ObjectInputStream;
021import java.util.HashMap;
022import java.util.Map;
023import java.util.logging.Level;
024import java.util.logging.Logger;
025
026import org.jivesoftware.smack.packet.PacketExtension;
027import org.jivesoftware.smack.provider.PacketExtensionProvider;
028import org.jivesoftware.smack.util.StringUtils;
029import org.jivesoftware.smackx.jiveproperties.JivePropertiesManager;
030import org.jivesoftware.smackx.jiveproperties.packet.JivePropertiesExtension;
031import org.xmlpull.v1.XmlPullParser;
032
033public class JivePropertiesExtensionProvider implements PacketExtensionProvider {
034
035    private static final Logger LOGGER = Logger.getLogger(JivePropertiesExtensionProvider.class.getName());
036
037    /**
038     * Parse a properties sub-packet. If any errors occur while de-serializing Java object
039     * properties, an exception will be printed and not thrown since a thrown exception will shut
040     * down the entire connection. ClassCastExceptions will occur when both the sender and receiver
041     * of the packet don't have identical versions of the same class.
042     * <p>
043     * Note that you have to explicitly enabled Java object deserialization with @{link
044     * {@link JivePropertiesManager#setJavaObjectEnabled(boolean)}
045     * 
046     * @param parser the XML parser, positioned at the start of a properties sub-packet.
047     * @return a map of the properties.
048     * @throws Exception if an error occurs while parsing the properties.
049     */
050    @Override
051    public PacketExtension parseExtension(XmlPullParser parser) throws Exception {
052        Map<String, Object> properties = new HashMap<String, Object>();
053        while (true) {
054            int eventType = parser.next();
055            if (eventType == XmlPullParser.START_TAG && parser.getName().equals("property")) {
056                // Parse a property
057                boolean done = false;
058                String name = null;
059                String type = null;
060                String valueText = null;
061                Object value = null;
062                while (!done) {
063                    eventType = parser.next();
064                    if (eventType == XmlPullParser.START_TAG) {
065                        String elementName = parser.getName();
066                        if (elementName.equals("name")) {
067                            name = parser.nextText();
068                        }
069                        else if (elementName.equals("value")) {
070                            type = parser.getAttributeValue("", "type");
071                            valueText = parser.nextText();
072                        }
073                    }
074                    else if (eventType == XmlPullParser.END_TAG) {
075                        if (parser.getName().equals("property")) {
076                            if ("integer".equals(type)) {
077                                value = Integer.valueOf(valueText);
078                            }
079                            else if ("long".equals(type))  {
080                                value = Long.valueOf(valueText);
081                            }
082                            else if ("float".equals(type)) {
083                                value = Float.valueOf(valueText);
084                            }
085                            else if ("double".equals(type)) {
086                                value = Double.valueOf(valueText);
087                            }
088                            else if ("boolean".equals(type)) {
089                                value = Boolean.valueOf(valueText);
090                            }
091                            else if ("string".equals(type)) {
092                                value = valueText;
093                            }
094                            else if ("java-object".equals(type)) {
095                                if (JivePropertiesManager.isJavaObjectEnabled()) {
096                                    try {
097                                        byte[] bytes = StringUtils.decodeBase64(valueText);
098                                        ObjectInputStream in = new ObjectInputStream(
099                                                        new ByteArrayInputStream(bytes));
100                                        value = in.readObject();
101                                    }
102                                    catch (Exception e) {
103                                        LOGGER.log(Level.SEVERE, "Error parsing java object", e);
104                                    }
105                                }
106                                else {
107                                    LOGGER.severe("JavaObject is not enabled. Enable with JivePropertiesManager.setJavaObjectEnabled(true)");
108                                }
109                            }
110                            if (name != null && value != null) {
111                                properties.put(name, value);
112                            }
113                            done = true;
114                        }
115                    }
116                }
117            }
118            else if (eventType == XmlPullParser.END_TAG) {
119                if (parser.getName().equals(JivePropertiesExtension.ELEMENT)) {
120                    break;
121                }
122            }
123        }
124        return new JivePropertiesExtension(properties);
125    }
126
127}