001/** 002 * 003 * Copyright 2014 Florian Schmaus. 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; 018 019import java.util.Collection; 020import java.util.Collections; 021import java.util.Map; 022 023import org.jivesoftware.smack.packet.Stanza; 024import org.jivesoftware.smack.packet.StanzaBuilder; 025import org.jivesoftware.smack.packet.StanzaView; 026 027import org.jivesoftware.smackx.jiveproperties.packet.JivePropertiesExtension; 028 029public class JivePropertiesManager { 030 031 private static boolean javaObjectEnabled = false; 032 033 /** 034 * Enables deserialization of Java objects embedded in the 'properties' stanza extension. Since 035 * this is a security sensitive feature, it is disabled per default in Smack. Only enable it if 036 * you are sure that you understand the potential security implications it can cause. 037 * <p> 038 * See also: 039 * </p> 040 * <ul> 041 * <li> <a href="http://stackoverflow.com/questions/19054460/">"What is the security impact of deserializing untrusted data in Java?" on Stackoverflow</a> 042 * </ul> 043 * @param enabled true to enable Java object deserialization 044 */ 045 public static void setJavaObjectEnabled(boolean enabled) { 046 JivePropertiesManager.javaObjectEnabled = enabled; 047 } 048 049 public static boolean isJavaObjectEnabled() { 050 return javaObjectEnabled; 051 } 052 053 /** 054 * Convenience method to add a property to a packet. 055 * 056 * @param packet the stanza to add the property to. 057 * @param name the name of the property to add. 058 * @param value the value of the property to add. 059 * @deprecated use {@link #addProperty(StanzaBuilder, String, Object)} instead. 060 */ 061 @Deprecated 062 // TODO: Remove in Smack 4.5. 063 public static void addProperty(Stanza packet, String name, Object value) { 064 JivePropertiesExtension jpe = (JivePropertiesExtension) packet.getExtension(JivePropertiesExtension.NAMESPACE); 065 if (jpe == null) { 066 jpe = new JivePropertiesExtension(); 067 packet.addExtension(jpe); 068 } 069 jpe.setProperty(name, value); 070 } 071 072 /** 073 * Convenience method to add a property to a stanza. 074 * 075 * @param stanzaBuilder the stanza to add the property to. 076 * @param name the name of the property to add. 077 * @param value the value of the property to add. 078 */ 079 public static void addProperty(StanzaBuilder<?> stanzaBuilder, String name, Object value) { 080 JivePropertiesExtension jpe = (JivePropertiesExtension) stanzaBuilder.getExtension(JivePropertiesExtension.QNAME); 081 if (jpe == null) { 082 jpe = new JivePropertiesExtension(); 083 stanzaBuilder.addExtension(jpe); 084 } 085 jpe.setProperty(name, value); 086 } 087 088 /** 089 * Convenience method to get a property from a packet. Will return null if the stanza contains 090 * not property with the given name. 091 * 092 * @param packet TODO javadoc me please 093 * @param name TODO javadoc me please 094 * @return the property or <code>null</code> if none found. 095 */ 096 public static Object getProperty(StanzaView packet, String name) { 097 Object res = null; 098 JivePropertiesExtension jpe = packet.getExtension(JivePropertiesExtension.class); 099 if (jpe != null) { 100 res = jpe.getProperty(name); 101 } 102 return res; 103 } 104 105 /** 106 * Return a collection of the names of all properties of the given packet. If the packet 107 * contains no properties extension, then an empty collection will be returned. 108 * 109 * @param packet TODO javadoc me please 110 * @return a collection of the names of all properties. 111 */ 112 public static Collection<String> getPropertiesNames(Stanza packet) { 113 JivePropertiesExtension jpe = (JivePropertiesExtension) packet.getExtension(JivePropertiesExtension.NAMESPACE); 114 if (jpe == null) { 115 return Collections.emptyList(); 116 } 117 return jpe.getPropertyNames(); 118 } 119 120 /** 121 * Return a map of all properties of the given packet. If the stanza contains no properties 122 * extension, an empty map will be returned. 123 * 124 * @param packet TODO javadoc me please 125 * @return a map of all properties of the given packet. 126 */ 127 public static Map<String, Object> getProperties(Stanza packet) { 128 JivePropertiesExtension jpe = (JivePropertiesExtension) packet.getExtension(JivePropertiesExtension.NAMESPACE); 129 if (jpe == null) { 130 return Collections.emptyMap(); 131 } 132 return jpe.getProperties(); 133 } 134}