JivePropertiesManager.java

  1. /**
  2.  *
  3.  * Copyright 2014-2024 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.smack.packet.StanzaBuilder;
  23. import org.jivesoftware.smack.packet.StanzaView;

  24. import org.jivesoftware.smackx.jiveproperties.packet.JivePropertiesExtension;

  25. public class JivePropertiesManager {

  26.     private static boolean javaObjectEnabled = false;

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

  42.     public static boolean isJavaObjectEnabled() {
  43.         return javaObjectEnabled;
  44.     }

  45.     /**
  46.      * Convenience method to add a property to a stanza.
  47.      *
  48.      * @param stanzaBuilder the stanza to add the property to.
  49.      * @param name the name of the property to add.
  50.      * @param value the value of the property to add.
  51.      */
  52.     public static void addProperty(StanzaBuilder<?> stanzaBuilder, String name, Object value) {
  53.         JivePropertiesExtension jpe = (JivePropertiesExtension) stanzaBuilder.getExtension(JivePropertiesExtension.QNAME);
  54.         if (jpe == null) {
  55.             jpe = new JivePropertiesExtension();
  56.             stanzaBuilder.addExtension(jpe);
  57.         }
  58.         jpe.setProperty(name, value);
  59.     }

  60.     /**
  61.      * Convenience method to get a property from a packet. Will return null if the stanza contains
  62.      * not property with the given name.
  63.      *
  64.      * @param packet TODO javadoc me please
  65.      * @param name TODO javadoc me please
  66.      * @return the property or <code>null</code> if none found.
  67.      */
  68.     public static Object getProperty(StanzaView packet, String name) {
  69.         Object res = null;
  70.         JivePropertiesExtension jpe = packet.getExtension(JivePropertiesExtension.class);
  71.         if (jpe != null) {
  72.             res = jpe.getProperty(name);
  73.         }
  74.         return res;
  75.     }

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

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