JivePropertiesManager.java

  1. /**
  2.  *
  3.  * Copyright 2014 Florian Schmaus.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.jiveproperties;

  18. import java.util.Collection;
  19. import java.util.Collections;
  20. import java.util.Map;

  21. import org.jivesoftware.smack.packet.Stanza;
  22. import org.jivesoftware.smackx.jiveproperties.packet.JivePropertiesExtension;

  23. public class JivePropertiesManager {

  24.     private static boolean javaObjectEnabled = false;

  25.     /**
  26.      * Enables deserialization of Java objects embedded in the 'properties' packet extension. Since
  27.      * this is a security sensitive feature, it is disabled per default in Smack. Only enable it if
  28.      * you are sure that you understand the potential security implications it can cause.
  29.      * <p>
  30.      * See also:
  31.      * <ul>
  32.      * <li> <a href="http://stackoverflow.com/questions/19054460/">"What is the security impact of deserializing untrusted data in Java?" on Stackoverflow<a>
  33.      * <ul>
  34.      * @param enabled true to enable Java object deserialization
  35.      */
  36.     public static void setJavaObjectEnabled(boolean enabled) {
  37.         JivePropertiesManager.javaObjectEnabled = enabled;
  38.     }

  39.     public static boolean isJavaObjectEnabled() {
  40.         return javaObjectEnabled;
  41.     }

  42.     /**
  43.      * Convenience method to add a property to a packet.
  44.      *
  45.      * @param packet the packet to add the property to.
  46.      * @param name the name of the property to add.
  47.      * @param value the value of the property to add.
  48.      */
  49.     public static void addProperty(Stanza packet, String name, Object value) {
  50.         JivePropertiesExtension jpe = (JivePropertiesExtension) packet.getExtension(JivePropertiesExtension.NAMESPACE);
  51.         if (jpe == null) {
  52.             jpe = new JivePropertiesExtension();
  53.             packet.addExtension(jpe);
  54.         }
  55.         jpe.setProperty(name, value);
  56.     }

  57.     /**
  58.      * Convenience method to get a property from a packet. Will return null if the packet contains
  59.      * not property with the given name.
  60.      *
  61.      * @param packet
  62.      * @param name
  63.      * @return the property or <tt>null</tt> if none found.
  64.      */
  65.     public static Object getProperty(Stanza packet, String name) {
  66.         Object res = null;
  67.         JivePropertiesExtension jpe = (JivePropertiesExtension) packet.getExtension(JivePropertiesExtension.NAMESPACE);
  68.         if (jpe != null) {
  69.             res = jpe.getProperty(name);
  70.         }
  71.         return res;
  72.     }

  73.     /**
  74.      * Return a collection of the names of all properties of the given packet. If the packet
  75.      * contains no properties extension, then an empty collection will be returned.
  76.      *
  77.      * @param packet
  78.      * @return a collection of the names of all properties.
  79.      */
  80.     public static Collection<String> getPropertiesNames(Stanza packet) {
  81.         JivePropertiesExtension jpe = (JivePropertiesExtension) packet.getExtension(JivePropertiesExtension.NAMESPACE);
  82.         if (jpe == null) {
  83.             return Collections.emptyList();
  84.         }
  85.         return jpe.getPropertyNames();
  86.     }

  87.     /**
  88.      * Return a map of all properties of the given packet. If the packet contains no properties
  89.      * extension, an empty map will be returned.
  90.      *
  91.      * @param packet
  92.      * @return a map of all properties of the given packet.
  93.      */
  94.     public static Map<String, Object> getProperties(Stanza packet) {
  95.         JivePropertiesExtension jpe = (JivePropertiesExtension) packet.getExtension(JivePropertiesExtension.NAMESPACE);
  96.         if (jpe == null) {
  97.             return Collections.emptyMap();
  98.         }
  99.         return jpe.getProperties();
  100.     }
  101. }