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