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.packet.XmlEnvironment; 028import org.jivesoftware.smack.provider.ExtensionElementProvider; 029import org.jivesoftware.smack.util.stringencoder.Base64; 030import org.jivesoftware.smack.xml.XmlPullParser; 031import org.jivesoftware.smack.xml.XmlPullParserException; 032 033import org.jivesoftware.smackx.jiveproperties.JivePropertiesManager; 034import org.jivesoftware.smackx.jiveproperties.packet.JivePropertiesExtension; 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 if an I/O error occurred. 052 * @throws XmlPullParserException if an error in the XML parser occurred. 053 */ 054 @SuppressWarnings("BanSerializableRead") 055 @Override 056 public JivePropertiesExtension parse(XmlPullParser parser, 057 int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, 058 IOException { 059 Map<String, Object> properties = new HashMap<>(); 060 while (true) { 061 XmlPullParser.Event eventType = parser.next(); 062 if (eventType == XmlPullParser.Event.START_ELEMENT && parser.getName().equals("property")) { 063 // Parse a property 064 boolean done = false; 065 String name = null; 066 String type = null; 067 String valueText = null; 068 Object value = null; 069 while (!done) { 070 eventType = parser.next(); 071 if (eventType == XmlPullParser.Event.START_ELEMENT) { 072 String elementName = parser.getName(); 073 if (elementName.equals("name")) { 074 name = parser.nextText(); 075 } 076 else if (elementName.equals("value")) { 077 type = parser.getAttributeValue("", "type"); 078 valueText = parser.nextText(); 079 } 080 } 081 else if (eventType == XmlPullParser.Event.END_ELEMENT) { 082 if (parser.getName().equals("property")) { 083 if ("integer".equals(type)) { 084 value = Integer.valueOf(valueText); 085 } 086 else if ("long".equals(type)) { 087 value = Long.valueOf(valueText); 088 } 089 else if ("float".equals(type)) { 090 value = Float.valueOf(valueText); 091 } 092 else if ("double".equals(type)) { 093 value = Double.valueOf(valueText); 094 } 095 else if ("boolean".equals(type)) { 096 // CHECKSTYLE:OFF 097 value = Boolean.valueOf(valueText); 098 // CHECKSTYLE:ON 099 } 100 else if ("string".equals(type)) { 101 value = valueText; 102 } 103 else if ("java-object".equals(type)) { 104 if (JivePropertiesManager.isJavaObjectEnabled()) { 105 try { 106 byte[] bytes = Base64.decode(valueText); 107 ObjectInputStream in = new ObjectInputStream( 108 new ByteArrayInputStream(bytes)); 109 value = in.readObject(); 110 } 111 catch (Exception e) { 112 LOGGER.log(Level.SEVERE, "Error parsing java object", e); 113 } 114 } 115 else { 116 LOGGER.severe("JavaObject is not enabled. Enable with JivePropertiesManager.setJavaObjectEnabled(true)"); 117 } 118 } 119 if (name != null && value != null) { 120 properties.put(name, value); 121 } 122 done = true; 123 } 124 } 125 } 126 } 127 else if (eventType == XmlPullParser.Event.END_ELEMENT) { 128 if (parser.getName().equals(JivePropertiesExtension.ELEMENT)) { 129 break; 130 } 131 } 132 } 133 return new JivePropertiesExtension(properties); 134 } 135 136}