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.smackx.jiveproperties.packet.JivePropertiesExtension; 025 026public class JivePropertiesManager { 027 028 private static boolean javaObjectEnabled = false; 029 030 /** 031 * Enables deserialization of Java objects embedded in the 'properties' stanza(/packet) extension. Since 032 * this is a security sensitive feature, it is disabled per default in Smack. Only enable it if 033 * you are sure that you understand the potential security implications it can cause. 034 * <p> 035 * See also: 036 * <ul> 037 * <li> <a href="http://stackoverflow.com/questions/19054460/">"What is the security impact of deserializing untrusted data in Java?" on Stackoverflow<a> 038 * <ul> 039 * @param enabled true to enable Java object deserialization 040 */ 041 public static void setJavaObjectEnabled(boolean enabled) { 042 JivePropertiesManager.javaObjectEnabled = enabled; 043 } 044 045 public static boolean isJavaObjectEnabled() { 046 return javaObjectEnabled; 047 } 048 049 /** 050 * Convenience method to add a property to a packet. 051 * 052 * @param packet the stanza(/packet) to add the property to. 053 * @param name the name of the property to add. 054 * @param value the value of the property to add. 055 */ 056 public static void addProperty(Stanza packet, String name, Object value) { 057 JivePropertiesExtension jpe = (JivePropertiesExtension) packet.getExtension(JivePropertiesExtension.NAMESPACE); 058 if (jpe == null) { 059 jpe = new JivePropertiesExtension(); 060 packet.addExtension(jpe); 061 } 062 jpe.setProperty(name, value); 063 } 064 065 /** 066 * Convenience method to get a property from a packet. Will return null if the stanza(/packet) contains 067 * not property with the given name. 068 * 069 * @param packet 070 * @param name 071 * @return the property or <tt>null</tt> if none found. 072 */ 073 public static Object getProperty(Stanza packet, String name) { 074 Object res = null; 075 JivePropertiesExtension jpe = (JivePropertiesExtension) packet.getExtension(JivePropertiesExtension.NAMESPACE); 076 if (jpe != null) { 077 res = jpe.getProperty(name); 078 } 079 return res; 080 } 081 082 /** 083 * Return a collection of the names of all properties of the given packet. If the packet 084 * contains no properties extension, then an empty collection will be returned. 085 * 086 * @param packet 087 * @return a collection of the names of all properties. 088 */ 089 public static Collection<String> getPropertiesNames(Stanza packet) { 090 JivePropertiesExtension jpe = (JivePropertiesExtension) packet.getExtension(JivePropertiesExtension.NAMESPACE); 091 if (jpe == null) { 092 return Collections.emptyList(); 093 } 094 return jpe.getPropertyNames(); 095 } 096 097 /** 098 * Return a map of all properties of the given packet. If the stanza(/packet) contains no properties 099 * extension, an empty map will be returned. 100 * 101 * @param packet 102 * @return a map of all properties of the given packet. 103 */ 104 public static Map<String, Object> getProperties(Stanza packet) { 105 JivePropertiesExtension jpe = (JivePropertiesExtension) packet.getExtension(JivePropertiesExtension.NAMESPACE); 106 if (jpe == null) { 107 return Collections.emptyMap(); 108 } 109 return jpe.getProperties(); 110 } 111}