2014-04-26 15:43:58 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Copyright 2014 Florian Schmaus.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
package org.jivesoftware.smackx.jiveproperties;
|
|
|
|
|
2014-04-28 20:22:46 +02:00
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2015-02-05 11:17:27 +01:00
|
|
|
import org.jivesoftware.smack.packet.Stanza;
|
2017-06-14 17:12:43 +02:00
|
|
|
|
2014-04-26 15:43:58 +02:00
|
|
|
import org.jivesoftware.smackx.jiveproperties.packet.JivePropertiesExtension;
|
|
|
|
|
|
|
|
public class JivePropertiesManager {
|
|
|
|
|
|
|
|
private static boolean javaObjectEnabled = false;
|
|
|
|
|
|
|
|
/**
|
2018-03-31 14:17:30 +02:00
|
|
|
* Enables deserialization of Java objects embedded in the 'properties' stanza extension. Since
|
2014-04-26 15:43:58 +02:00
|
|
|
* this is a security sensitive feature, it is disabled per default in Smack. Only enable it if
|
|
|
|
* you are sure that you understand the potential security implications it can cause.
|
|
|
|
* <p>
|
|
|
|
* See also:
|
2017-12-25 12:51:41 +01:00
|
|
|
* </p>
|
2014-04-26 15:43:58 +02:00
|
|
|
* <ul>
|
2017-12-25 12:51:41 +01:00
|
|
|
* <li> <a href="http://stackoverflow.com/questions/19054460/">"What is the security impact of deserializing untrusted data in Java?" on Stackoverflow</a>
|
|
|
|
* </ul>
|
2014-04-26 15:43:58 +02:00
|
|
|
* @param enabled true to enable Java object deserialization
|
|
|
|
*/
|
|
|
|
public static void setJavaObjectEnabled(boolean enabled) {
|
|
|
|
JivePropertiesManager.javaObjectEnabled = enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isJavaObjectEnabled() {
|
|
|
|
return javaObjectEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convenience method to add a property to a packet.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2018-03-31 14:17:30 +02:00
|
|
|
* @param packet the stanza to add the property to.
|
2014-04-26 15:43:58 +02:00
|
|
|
* @param name the name of the property to add.
|
|
|
|
* @param value the value of the property to add.
|
|
|
|
*/
|
2015-02-05 11:17:27 +01:00
|
|
|
public static void addProperty(Stanza packet, String name, Object value) {
|
2014-04-26 15:43:58 +02:00
|
|
|
JivePropertiesExtension jpe = (JivePropertiesExtension) packet.getExtension(JivePropertiesExtension.NAMESPACE);
|
|
|
|
if (jpe == null) {
|
|
|
|
jpe = new JivePropertiesExtension();
|
|
|
|
packet.addExtension(jpe);
|
|
|
|
}
|
|
|
|
jpe.setProperty(name, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-31 14:17:30 +02:00
|
|
|
* Convenience method to get a property from a packet. Will return null if the stanza contains
|
2014-04-26 15:43:58 +02:00
|
|
|
* not property with the given name.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-04-26 15:43:58 +02:00
|
|
|
* @param packet
|
|
|
|
* @param name
|
|
|
|
* @return the property or <tt>null</tt> if none found.
|
|
|
|
*/
|
2015-02-05 11:17:27 +01:00
|
|
|
public static Object getProperty(Stanza packet, String name) {
|
2014-04-26 15:43:58 +02:00
|
|
|
Object res = null;
|
|
|
|
JivePropertiesExtension jpe = (JivePropertiesExtension) packet.getExtension(JivePropertiesExtension.NAMESPACE);
|
|
|
|
if (jpe != null) {
|
|
|
|
res = jpe.getProperty(name);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2014-04-28 20:22:46 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a collection of the names of all properties of the given packet. If the packet
|
|
|
|
* contains no properties extension, then an empty collection will be returned.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-04-28 20:22:46 +02:00
|
|
|
* @param packet
|
|
|
|
* @return a collection of the names of all properties.
|
|
|
|
*/
|
2015-02-05 11:17:27 +01:00
|
|
|
public static Collection<String> getPropertiesNames(Stanza packet) {
|
2014-04-28 20:22:46 +02:00
|
|
|
JivePropertiesExtension jpe = (JivePropertiesExtension) packet.getExtension(JivePropertiesExtension.NAMESPACE);
|
|
|
|
if (jpe == null) {
|
|
|
|
return Collections.emptyList();
|
|
|
|
}
|
|
|
|
return jpe.getPropertyNames();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-31 14:17:30 +02:00
|
|
|
* Return a map of all properties of the given packet. If the stanza contains no properties
|
2014-04-28 20:22:46 +02:00
|
|
|
* extension, an empty map will be returned.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-04-28 20:22:46 +02:00
|
|
|
* @param packet
|
|
|
|
* @return a map of all properties of the given packet.
|
|
|
|
*/
|
2015-02-05 11:17:27 +01:00
|
|
|
public static Map<String, Object> getProperties(Stanza packet) {
|
2014-04-28 20:22:46 +02:00
|
|
|
JivePropertiesExtension jpe = (JivePropertiesExtension) packet.getExtension(JivePropertiesExtension.NAMESPACE);
|
|
|
|
if (jpe == null) {
|
|
|
|
return Collections.emptyMap();
|
|
|
|
}
|
|
|
|
return jpe.getProperties();
|
|
|
|
}
|
2014-04-26 15:43:58 +02:00
|
|
|
}
|