JivePropertiesExtensionProvider.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.jiveproperties.provider;

  18. import java.io.ByteArrayInputStream;
  19. import java.io.IOException;
  20. import java.io.ObjectInputStream;
  21. import java.util.HashMap;
  22. import java.util.Map;
  23. import java.util.logging.Level;
  24. import java.util.logging.Logger;

  25. import org.jivesoftware.smack.provider.ExtensionElementProvider;
  26. import org.jivesoftware.smack.util.stringencoder.Base64;
  27. import org.jivesoftware.smackx.jiveproperties.JivePropertiesManager;
  28. import org.jivesoftware.smackx.jiveproperties.packet.JivePropertiesExtension;
  29. import org.xmlpull.v1.XmlPullParser;
  30. import org.xmlpull.v1.XmlPullParserException;

  31. public class JivePropertiesExtensionProvider extends ExtensionElementProvider<JivePropertiesExtension> {

  32.     private static final Logger LOGGER = Logger.getLogger(JivePropertiesExtensionProvider.class.getName());

  33.     /**
  34.      * Parse a properties sub-packet. If any errors occur while de-serializing Java object
  35.      * properties, an exception will be printed and not thrown since a thrown exception will shut
  36.      * down the entire connection. ClassCastExceptions will occur when both the sender and receiver
  37.      * of the packet don't have identical versions of the same class.
  38.      * <p>
  39.      * Note that you have to explicitly enabled Java object deserialization with @{link
  40.      * {@link JivePropertiesManager#setJavaObjectEnabled(boolean)}
  41.      *
  42.      * @param parser the XML parser, positioned at the start of a properties sub-packet.
  43.      * @return a map of the properties.
  44.      * @throws IOException
  45.      * @throws XmlPullParserException
  46.      */
  47.     @Override
  48.     public JivePropertiesExtension parse(XmlPullParser parser,
  49.                     int initialDepth) throws XmlPullParserException,
  50.                     IOException {
  51.         Map<String, Object> properties = new HashMap<String, Object>();
  52.         while (true) {
  53.             int eventType = parser.next();
  54.             if (eventType == XmlPullParser.START_TAG && parser.getName().equals("property")) {
  55.                 // Parse a property
  56.                 boolean done = false;
  57.                 String name = null;
  58.                 String type = null;
  59.                 String valueText = null;
  60.                 Object value = null;
  61.                 while (!done) {
  62.                     eventType = parser.next();
  63.                     if (eventType == XmlPullParser.START_TAG) {
  64.                         String elementName = parser.getName();
  65.                         if (elementName.equals("name")) {
  66.                             name = parser.nextText();
  67.                         }
  68.                         else if (elementName.equals("value")) {
  69.                             type = parser.getAttributeValue("", "type");
  70.                             valueText = parser.nextText();
  71.                         }
  72.                     }
  73.                     else if (eventType == XmlPullParser.END_TAG) {
  74.                         if (parser.getName().equals("property")) {
  75.                             if ("integer".equals(type)) {
  76.                                 value = Integer.valueOf(valueText);
  77.                             }
  78.                             else if ("long".equals(type))  {
  79.                                 value = Long.valueOf(valueText);
  80.                             }
  81.                             else if ("float".equals(type)) {
  82.                                 value = Float.valueOf(valueText);
  83.                             }
  84.                             else if ("double".equals(type)) {
  85.                                 value = Double.valueOf(valueText);
  86.                             }
  87.                             else if ("boolean".equals(type)) {
  88.                                 value = Boolean.valueOf(valueText);
  89.                             }
  90.                             else if ("string".equals(type)) {
  91.                                 value = valueText;
  92.                             }
  93.                             else if ("java-object".equals(type)) {
  94.                                 if (JivePropertiesManager.isJavaObjectEnabled()) {
  95.                                     try {
  96.                                         byte[] bytes = Base64.decode(valueText);
  97.                                         ObjectInputStream in = new ObjectInputStream(
  98.                                                         new ByteArrayInputStream(bytes));
  99.                                         value = in.readObject();
  100.                                     }
  101.                                     catch (Exception e) {
  102.                                         LOGGER.log(Level.SEVERE, "Error parsing java object", e);
  103.                                     }
  104.                                 }
  105.                                 else {
  106.                                     LOGGER.severe("JavaObject is not enabled. Enable with JivePropertiesManager.setJavaObjectEnabled(true)");
  107.                                 }
  108.                             }
  109.                             if (name != null && value != null) {
  110.                                 properties.put(name, value);
  111.                             }
  112.                             done = true;
  113.                         }
  114.                     }
  115.                 }
  116.             }
  117.             else if (eventType == XmlPullParser.END_TAG) {
  118.                 if (parser.getName().equals(JivePropertiesExtension.ELEMENT)) {
  119.                     break;
  120.                 }
  121.             }
  122.         }
  123.         return new JivePropertiesExtension(properties);
  124.     }

  125. }