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