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.IOException;
021import java.io.ObjectInputStream;
022import java.util.HashMap;
023import java.util.Map;
024import java.util.logging.Level;
025import java.util.logging.Logger;
026
027import org.jivesoftware.smack.provider.ExtensionElementProvider;
028import org.jivesoftware.smack.util.stringencoder.Base64;
029
030import org.jivesoftware.smackx.jiveproperties.JivePropertiesManager;
031import org.jivesoftware.smackx.jiveproperties.packet.JivePropertiesExtension;
032
033import org.xmlpull.v1.XmlPullParser;
034import org.xmlpull.v1.XmlPullParserException;
035
036public class JivePropertiesExtensionProvider extends ExtensionElementProvider<JivePropertiesExtension> {
037
038    private static final Logger LOGGER = Logger.getLogger(JivePropertiesExtensionProvider.class.getName());
039
040    /**
041     * Parse a properties sub-packet. If any errors occur while de-serializing Java object
042     * properties, an exception will be printed and not thrown since a thrown exception will shut
043     * down the entire connection. ClassCastExceptions will occur when both the sender and receiver
044     * of the stanza don't have identical versions of the same class.
045     * <p>
046     * Note that you have to explicitly enabled Java object deserialization with @{link
047     * {@link JivePropertiesManager#setJavaObjectEnabled(boolean)}
048     *
049     * @param parser the XML parser, positioned at the start of a properties sub-packet.
050     * @return a map of the properties.
051     * @throws IOException
052     * @throws XmlPullParserException
053     */
054    @Override
055    public JivePropertiesExtension parse(XmlPullParser parser,
056                    int initialDepth) throws XmlPullParserException,
057                    IOException {
058        Map<String, Object> properties = new HashMap<>();
059        while (true) {
060            int eventType = parser.next();
061            if (eventType == XmlPullParser.START_TAG && parser.getName().equals("property")) {
062                // Parse a property
063                boolean done = false;
064                String name = null;
065                String type = null;
066                String valueText = null;
067                Object value = null;
068                while (!done) {
069                    eventType = parser.next();
070                    if (eventType == XmlPullParser.START_TAG) {
071                        String elementName = parser.getName();
072                        if (elementName.equals("name")) {
073                            name = parser.nextText();
074                        }
075                        else if (elementName.equals("value")) {
076                            type = parser.getAttributeValue("", "type");
077                            valueText = parser.nextText();
078                        }
079                    }
080                    else if (eventType == XmlPullParser.END_TAG) {
081                        if (parser.getName().equals("property")) {
082                            if ("integer".equals(type)) {
083                                value = Integer.valueOf(valueText);
084                            }
085                            else if ("long".equals(type))  {
086                                value = Long.valueOf(valueText);
087                            }
088                            else if ("float".equals(type)) {
089                                value = Float.valueOf(valueText);
090                            }
091                            else if ("double".equals(type)) {
092                                value = Double.valueOf(valueText);
093                            }
094                            else if ("boolean".equals(type)) {
095                                value = Boolean.valueOf(valueText);
096                            }
097                            else if ("string".equals(type)) {
098                                value = valueText;
099                            }
100                            else if ("java-object".equals(type)) {
101                                if (JivePropertiesManager.isJavaObjectEnabled()) {
102                                    try {
103                                        byte[] bytes = Base64.decode(valueText);
104                                        ObjectInputStream in = new ObjectInputStream(
105                                                        new ByteArrayInputStream(bytes));
106                                        value = in.readObject();
107                                    }
108                                    catch (Exception e) {
109                                        LOGGER.log(Level.SEVERE, "Error parsing java object", e);
110                                    }
111                                }
112                                else {
113                                    LOGGER.severe("JavaObject is not enabled. Enable with JivePropertiesManager.setJavaObjectEnabled(true)");
114                                }
115                            }
116                            if (name != null && value != null) {
117                                properties.put(name, value);
118                            }
119                            done = true;
120                        }
121                    }
122                }
123            }
124            else if (eventType == XmlPullParser.END_TAG) {
125                if (parser.getName().equals(JivePropertiesExtension.ELEMENT)) {
126                    break;
127                }
128            }
129        }
130        return new JivePropertiesExtension(properties);
131    }
132
133}