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 @Override 055 public JivePropertiesExtension parse(XmlPullParser parser, 056 int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, 057 IOException { 058 Map<String, Object> properties = new HashMap<>(); 059 while (true) { 060 XmlPullParser.Event eventType = parser.next(); 061 if (eventType == XmlPullParser.Event.START_ELEMENT && 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.Event.START_ELEMENT) { 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.Event.END_ELEMENT) { 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 // CHECKSTYLE:OFF 096 value = Boolean.valueOf(valueText); 097 // CHECKSTYLE:ON 098 } 099 else if ("string".equals(type)) { 100 value = valueText; 101 } 102 else if ("java-object".equals(type)) { 103 if (JivePropertiesManager.isJavaObjectEnabled()) { 104 try { 105 byte[] bytes = Base64.decode(valueText); 106 ObjectInputStream in = new ObjectInputStream( 107 new ByteArrayInputStream(bytes)); 108 value = in.readObject(); 109 } 110 catch (Exception e) { 111 LOGGER.log(Level.SEVERE, "Error parsing java object", e); 112 } 113 } 114 else { 115 LOGGER.severe("JavaObject is not enabled. Enable with JivePropertiesManager.setJavaObjectEnabled(true)"); 116 } 117 } 118 if (name != null && value != null) { 119 properties.put(name, value); 120 } 121 done = true; 122 } 123 } 124 } 125 } 126 else if (eventType == XmlPullParser.Event.END_ELEMENT) { 127 if (parser.getName().equals(JivePropertiesExtension.ELEMENT)) { 128 break; 129 } 130 } 131 } 132 return new JivePropertiesExtension(properties); 133 } 134 135}