001/**
002 *
003 * Copyright 2014-2024 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 stanza.
055     *
056     * @param stanzaBuilder 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     */
060    public static void addProperty(StanzaBuilder<?> stanzaBuilder, String name, Object value) {
061        JivePropertiesExtension jpe = (JivePropertiesExtension) stanzaBuilder.getExtension(JivePropertiesExtension.QNAME);
062        if (jpe == null) {
063            jpe = new JivePropertiesExtension();
064            stanzaBuilder.addExtension(jpe);
065        }
066        jpe.setProperty(name, value);
067    }
068
069    /**
070     * Convenience method to get a property from a packet. Will return null if the stanza contains
071     * not property with the given name.
072     *
073     * @param packet TODO javadoc me please
074     * @param name TODO javadoc me please
075     * @return the property or <code>null</code> if none found.
076     */
077    public static Object getProperty(StanzaView packet, String name) {
078        Object res = null;
079        JivePropertiesExtension jpe = packet.getExtension(JivePropertiesExtension.class);
080        if (jpe != null) {
081            res = jpe.getProperty(name);
082        }
083        return res;
084    }
085
086    /**
087     * Return a collection of the names of all properties of the given packet. If the packet
088     * contains no properties extension, then an empty collection will be returned.
089     *
090     * @param packet TODO javadoc me please
091     * @return a collection of the names of all properties.
092     */
093    public static Collection<String> getPropertiesNames(Stanza packet) {
094        JivePropertiesExtension jpe = (JivePropertiesExtension) packet.getExtension(JivePropertiesExtension.NAMESPACE);
095        if (jpe == null) {
096            return Collections.emptyList();
097        }
098        return jpe.getPropertyNames();
099    }
100
101    /**
102     * Return a map of all properties of the given packet. If the stanza contains no properties
103     * extension, an empty map will be returned.
104     *
105     * @param packet TODO javadoc me please
106     * @return a map of all properties of the given packet.
107     */
108    public static Map<String, Object> getProperties(Stanza packet) {
109        JivePropertiesExtension jpe = (JivePropertiesExtension) packet.getExtension(JivePropertiesExtension.NAMESPACE);
110        if (jpe == null) {
111            return Collections.emptyMap();
112        }
113        return jpe.getProperties();
114    }
115}