2003-12-19 19:17:36 +01:00
|
|
|
/**
|
2004-11-03 00:53:30 +01:00
|
|
|
*
|
2007-02-12 01:59:05 +01:00
|
|
|
* Copyright 2003-2007 Jive Software.
|
2004-11-03 00:53:30 +01:00
|
|
|
*
|
2014-02-17 18:57:38 +01:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
2004-11-03 00:53:30 +01:00
|
|
|
* 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.
|
|
|
|
*/
|
2003-12-19 19:17:36 +01:00
|
|
|
|
|
|
|
package org.jivesoftware.smack;
|
|
|
|
|
2014-08-18 09:59:28 +02:00
|
|
|
import java.io.Reader;
|
|
|
|
import java.io.Writer;
|
2013-01-04 12:43:35 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collection;
|
2014-01-16 06:14:39 +01:00
|
|
|
import java.util.Collections;
|
2014-02-23 17:48:07 +01:00
|
|
|
import java.util.HashSet;
|
2013-01-04 12:43:35 +01:00
|
|
|
import java.util.List;
|
2014-02-23 17:48:07 +01:00
|
|
|
import java.util.Set;
|
2013-01-04 12:43:35 +01:00
|
|
|
|
2014-07-21 18:42:44 +02:00
|
|
|
import javax.net.ssl.HostnameVerifier;
|
|
|
|
|
2014-03-10 10:20:52 +01:00
|
|
|
import org.jivesoftware.smack.compression.XMPPInputOutputStream;
|
2014-08-18 09:59:28 +02:00
|
|
|
import org.jivesoftware.smack.debugger.ReflectionDebuggerFactory;
|
2014-08-15 13:28:07 +02:00
|
|
|
import org.jivesoftware.smack.debugger.SmackDebugger;
|
2014-08-18 09:59:28 +02:00
|
|
|
import org.jivesoftware.smack.debugger.SmackDebuggerFactory;
|
2013-10-06 01:14:24 +02:00
|
|
|
import org.jivesoftware.smack.parsing.ExceptionThrowingCallback;
|
2014-01-16 06:14:39 +01:00
|
|
|
import org.jivesoftware.smack.parsing.ParsingExceptionCallback;
|
2003-12-19 19:17:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents the configuration of Smack. The configuration is used for:
|
|
|
|
* <ul>
|
|
|
|
* <li> Initializing classes by loading them at start-up.
|
|
|
|
* <li> Getting the current Smack version.
|
2004-04-18 20:11:26 +02:00
|
|
|
* <li> Getting and setting global library behavior, such as the period of time
|
|
|
|
* to wait for replies to packets from the server. Note: setting these values
|
|
|
|
* via the API will override settings in the configuration file.
|
2003-12-19 19:17:36 +01:00
|
|
|
* </ul>
|
2004-04-18 20:11:26 +02:00
|
|
|
*
|
2014-07-05 12:04:40 +02:00
|
|
|
* Configuration settings are stored in org.jivesoftware.smack/smack-config.xml.
|
2003-12-19 19:17:36 +01:00
|
|
|
*
|
|
|
|
* @author Gaston Dombiak
|
|
|
|
*/
|
|
|
|
public final class SmackConfiguration {
|
2014-02-23 17:48:07 +01:00
|
|
|
|
2014-02-18 15:05:19 +01:00
|
|
|
private static int defaultPacketReplyTimeout = 5000;
|
2014-02-23 17:48:07 +01:00
|
|
|
private static int packetCollectorSize = 5000;
|
|
|
|
|
2014-01-16 06:14:39 +01:00
|
|
|
private static List<String> defaultMechs = new ArrayList<String>();
|
2003-12-19 19:17:36 +01:00
|
|
|
|
2014-09-05 12:17:50 +02:00
|
|
|
static Set<String> disabledSmackClasses = new HashSet<String>();
|
|
|
|
|
|
|
|
final static List<XMPPInputOutputStream> compressionHandlers = new ArrayList<XMPPInputOutputStream>(2);
|
2010-08-15 13:57:11 +02:00
|
|
|
|
2014-09-05 12:17:50 +02:00
|
|
|
static boolean smackInitialized = false;
|
2014-03-10 10:20:52 +01:00
|
|
|
|
2014-08-18 09:59:28 +02:00
|
|
|
private static SmackDebuggerFactory debuggerFactory = new ReflectionDebuggerFactory();
|
|
|
|
|
2014-03-10 10:20:52 +01:00
|
|
|
/**
|
2014-04-03 13:06:43 +02:00
|
|
|
* Value that indicates whether debugging is enabled. When enabled, a debug
|
|
|
|
* window will apear for each new connection that will contain the following
|
|
|
|
* information:<ul>
|
|
|
|
* <li> Client Traffic -- raw XML traffic generated by Smack and sent to the server.
|
|
|
|
* <li> Server Traffic -- raw XML traffic sent by the server to the client.
|
|
|
|
* <li> Interpreted Packets -- shows XML packets from the server as parsed by Smack.
|
|
|
|
* </ul>
|
|
|
|
* <p/>
|
|
|
|
* Debugging can be enabled by setting this field to true, or by setting the Java system
|
|
|
|
* property <tt>smack.debugEnabled</tt> to true. The system property can be set on the
|
|
|
|
* command line such as "java SomeApp -Dsmack.debugEnabled=true".
|
|
|
|
*/
|
|
|
|
public static boolean DEBUG_ENABLED = false;
|
|
|
|
|
2013-06-22 19:01:40 +02:00
|
|
|
/**
|
2013-10-06 01:14:24 +02:00
|
|
|
* The default parsing exception callback is {@link ExceptionThrowingCallback} which will
|
2013-06-22 19:01:40 +02:00
|
|
|
* throw an exception and therefore disconnect the active connection.
|
|
|
|
*/
|
2013-10-06 01:14:24 +02:00
|
|
|
private static ParsingExceptionCallback defaultCallback = new ExceptionThrowingCallback();
|
2013-06-22 19:01:40 +02:00
|
|
|
|
2014-07-21 18:42:44 +02:00
|
|
|
private static HostnameVerifier defaultHostnameVerififer;
|
|
|
|
|
2003-12-19 19:17:36 +01:00
|
|
|
/**
|
2004-08-09 23:13:36 +02:00
|
|
|
* Returns the Smack version information, eg "1.3.0".
|
2003-12-19 19:17:36 +01:00
|
|
|
*
|
2004-03-15 19:59:41 +01:00
|
|
|
* @return the Smack version information.
|
2003-12-19 19:17:36 +01:00
|
|
|
*/
|
2004-03-15 19:59:41 +01:00
|
|
|
public static String getVersion() {
|
2014-09-05 12:17:50 +02:00
|
|
|
return SmackInitialization.SMACK_VERSION;
|
2003-12-19 19:17:36 +01:00
|
|
|
}
|
|
|
|
|
2004-01-02 00:19:37 +01:00
|
|
|
/**
|
|
|
|
* Returns the number of milliseconds to wait for a response from
|
2004-03-15 19:59:41 +01:00
|
|
|
* the server. The default value is 5000 ms.
|
2004-01-02 00:19:37 +01:00
|
|
|
*
|
|
|
|
* @return the milliseconds to wait for a response from the server
|
|
|
|
*/
|
2014-02-18 15:05:19 +01:00
|
|
|
public static int getDefaultPacketReplyTimeout() {
|
2004-09-24 04:12:47 +02:00
|
|
|
// The timeout value must be greater than 0 otherwise we will answer the default value
|
2014-02-18 15:05:19 +01:00
|
|
|
if (defaultPacketReplyTimeout <= 0) {
|
|
|
|
defaultPacketReplyTimeout = 5000;
|
2004-09-24 04:12:47 +02:00
|
|
|
}
|
2014-02-18 15:05:19 +01:00
|
|
|
return defaultPacketReplyTimeout;
|
2004-01-02 00:19:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the number of milliseconds to wait for a response from
|
|
|
|
* the server.
|
|
|
|
*
|
|
|
|
* @param timeout the milliseconds to wait for a response from the server
|
|
|
|
*/
|
2014-02-18 15:05:19 +01:00
|
|
|
public static void setDefaultPacketReplyTimeout(int timeout) {
|
2004-09-24 04:12:47 +02:00
|
|
|
if (timeout <= 0) {
|
|
|
|
throw new IllegalArgumentException();
|
|
|
|
}
|
2014-02-18 15:05:19 +01:00
|
|
|
defaultPacketReplyTimeout = timeout;
|
2004-01-02 00:19:37 +01:00
|
|
|
}
|
|
|
|
|
2011-06-18 20:18:03 +02:00
|
|
|
/**
|
|
|
|
* Gets the default max size of a packet collector before it will delete
|
|
|
|
* the older packets.
|
|
|
|
*
|
|
|
|
* @return The number of packets to queue before deleting older packets.
|
|
|
|
*/
|
|
|
|
public static int getPacketCollectorSize() {
|
2014-02-23 17:48:07 +01:00
|
|
|
return packetCollectorSize;
|
2011-06-18 20:18:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the default max size of a packet collector before it will delete
|
|
|
|
* the older packets.
|
|
|
|
*
|
2014-03-10 18:31:45 +01:00
|
|
|
* @param collectorSize the number of packets to queue before deleting older packets.
|
2011-06-18 20:18:03 +02:00
|
|
|
*/
|
|
|
|
public static void setPacketCollectorSize(int collectorSize) {
|
2014-02-23 17:48:07 +01:00
|
|
|
packetCollectorSize = collectorSize;
|
2011-06-18 20:18:03 +02:00
|
|
|
}
|
|
|
|
|
2007-11-14 17:27:47 +01:00
|
|
|
/**
|
|
|
|
* Add a SASL mechanism to the list to be used.
|
|
|
|
*
|
|
|
|
* @param mech the SASL mechanism to be added
|
|
|
|
*/
|
|
|
|
public static void addSaslMech(String mech) {
|
|
|
|
if(! defaultMechs.contains(mech) ) {
|
|
|
|
defaultMechs.add(mech);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a Collection of SASL mechanisms to the list to be used.
|
|
|
|
*
|
|
|
|
* @param mechs the Collection of SASL mechanisms to be added
|
|
|
|
*/
|
|
|
|
public static void addSaslMechs(Collection<String> mechs) {
|
|
|
|
for(String mech : mechs) {
|
|
|
|
addSaslMech(mech);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-15 13:28:07 +02:00
|
|
|
/**
|
2014-08-18 09:59:28 +02:00
|
|
|
* Sets Smack debugger factory.
|
|
|
|
*
|
|
|
|
* @param debuggerFactory new debugger factory implementation to be used by Smack
|
|
|
|
*/
|
|
|
|
public static void setDebuggerFactory(SmackDebuggerFactory debuggerFactory) {
|
|
|
|
SmackConfiguration.debuggerFactory = debuggerFactory;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return a debugger factory or <code>null</code>
|
2014-08-15 13:28:07 +02:00
|
|
|
*/
|
2014-08-18 09:59:28 +02:00
|
|
|
public static SmackDebuggerFactory getDebuggerFactory() {
|
|
|
|
return debuggerFactory;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates new debugger instance with given arguments as parameters. May
|
|
|
|
* return <code>null</code> if no DebuggerFactory is set or if the factory
|
|
|
|
* did not produce a debugger.
|
|
|
|
*
|
|
|
|
* @param connection
|
|
|
|
* @param writer
|
|
|
|
* @param reader
|
|
|
|
* @return a new debugger or <code>null</code>
|
|
|
|
*/
|
|
|
|
public static SmackDebugger createDebugger(XMPPConnection connection, Writer writer, Reader reader) {
|
|
|
|
SmackDebuggerFactory factory = getDebuggerFactory();
|
|
|
|
if (factory == null) {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return factory.create(connection, writer, reader);
|
|
|
|
}
|
2014-08-15 13:28:07 +02:00
|
|
|
}
|
|
|
|
|
2007-11-14 17:27:47 +01:00
|
|
|
/**
|
|
|
|
* Remove a SASL mechanism from the list to be used.
|
|
|
|
*
|
|
|
|
* @param mech the SASL mechanism to be removed
|
|
|
|
*/
|
|
|
|
public static void removeSaslMech(String mech) {
|
2014-01-16 06:14:39 +01:00
|
|
|
defaultMechs.remove(mech);
|
2007-11-14 17:27:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a Collection of SASL mechanisms to the list to be used.
|
|
|
|
*
|
|
|
|
* @param mechs the Collection of SASL mechanisms to be removed
|
|
|
|
*/
|
|
|
|
public static void removeSaslMechs(Collection<String> mechs) {
|
2014-01-16 06:14:39 +01:00
|
|
|
defaultMechs.removeAll(mechs);
|
2007-11-14 17:27:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the list of SASL mechanisms to be used. If a SASL mechanism is
|
|
|
|
* listed here it does not guarantee it will be used. The server may not
|
|
|
|
* support it, or it may not be implemented.
|
|
|
|
*
|
|
|
|
* @return the list of SASL mechanisms to be used.
|
|
|
|
*/
|
|
|
|
public static List<String> getSaslMechs() {
|
2014-01-16 06:14:39 +01:00
|
|
|
return Collections.unmodifiableList(defaultMechs);
|
2007-11-14 17:27:47 +01:00
|
|
|
}
|
|
|
|
|
2013-06-22 19:01:40 +02:00
|
|
|
/**
|
|
|
|
* Set the default parsing exception callback for all newly created connections
|
|
|
|
*
|
|
|
|
* @param callback
|
|
|
|
* @see ParsingExceptionCallback
|
|
|
|
*/
|
|
|
|
public static void setDefaultParsingExceptionCallback(ParsingExceptionCallback callback) {
|
|
|
|
defaultCallback = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the default parsing exception callback
|
|
|
|
*
|
|
|
|
* @return the default parsing exception callback
|
|
|
|
* @see ParsingExceptionCallback
|
|
|
|
*/
|
|
|
|
public static ParsingExceptionCallback getDefaultParsingExceptionCallback() {
|
|
|
|
return defaultCallback;
|
|
|
|
}
|
|
|
|
|
2014-03-10 10:20:52 +01:00
|
|
|
public static void addCompressionHandler(XMPPInputOutputStream xmppInputOutputStream) {
|
|
|
|
compressionHandlers.add(xmppInputOutputStream);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static List<XMPPInputOutputStream> getCompresionHandlers() {
|
|
|
|
List<XMPPInputOutputStream> res = new ArrayList<XMPPInputOutputStream>(compressionHandlers.size());
|
|
|
|
for (XMPPInputOutputStream ios : compressionHandlers) {
|
|
|
|
if (ios.isSupported()) {
|
|
|
|
res.add(ios);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-07-21 18:42:44 +02:00
|
|
|
/**
|
|
|
|
* Set the default HostnameVerifier that will be used by XMPP connections to verify the hostname
|
|
|
|
* of a TLS certificate. XMPP connections are able to overwrite this settings by supplying a
|
|
|
|
* HostnameVerifier in their ConnecitonConfiguration with
|
|
|
|
* {@link ConnectionConfiguration#setHostnameVerifier(HostnameVerifier)}.
|
|
|
|
*/
|
|
|
|
public static void setDefaultHostnameVerifier(HostnameVerifier verifier) {
|
|
|
|
defaultHostnameVerififer = verifier;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-09-05 12:17:50 +02:00
|
|
|
* Convenience method for {@link #addDisabledSmackClass(String)}.
|
2014-07-21 18:42:44 +02:00
|
|
|
*
|
2014-09-05 12:17:50 +02:00
|
|
|
* @param clz the Smack class to disable
|
2014-07-21 18:42:44 +02:00
|
|
|
*/
|
2014-09-05 12:17:50 +02:00
|
|
|
public static void addDisabledSmackClass(Class<?> clz) {
|
|
|
|
addDisabledSmackClass(clz.getName());
|
2014-07-21 18:42:44 +02:00
|
|
|
}
|
|
|
|
|
2014-09-05 12:17:50 +02:00
|
|
|
/**
|
2014-09-25 09:42:55 +02:00
|
|
|
* Add a class to the disabled smack classes.
|
|
|
|
* <p>
|
|
|
|
* {@code className} can also be a package name, in this case, the entire
|
|
|
|
* package is disabled (but can be manually enabled).
|
|
|
|
* </p>
|
2014-09-05 12:17:50 +02:00
|
|
|
*
|
|
|
|
* @param className
|
|
|
|
*/
|
|
|
|
public static void addDisabledSmackClass(String className) {
|
|
|
|
disabledSmackClasses.add(className);
|
2014-05-15 15:04:46 +02:00
|
|
|
}
|
|
|
|
|
2014-09-05 12:17:50 +02:00
|
|
|
/**
|
|
|
|
* Check if Smack was successfully initialized.
|
|
|
|
*
|
|
|
|
* @return true if smack was initialized, false otherwise
|
|
|
|
*/
|
|
|
|
public static boolean isSmackInitialized() {
|
|
|
|
return smackInitialized;
|
2014-02-23 17:48:07 +01:00
|
|
|
}
|
|
|
|
|
2014-09-05 12:17:50 +02:00
|
|
|
/**
|
|
|
|
* Get the default HostnameVerifier
|
|
|
|
*
|
|
|
|
* @return the default HostnameVerifier or <code>null</code> if none was set
|
|
|
|
*/
|
|
|
|
static HostnameVerifier getDefaultHostnameVerifier() {
|
|
|
|
return defaultHostnameVerififer;
|
2014-02-15 13:01:57 +01:00
|
|
|
}
|
|
|
|
|
2003-12-19 19:17:36 +01:00
|
|
|
}
|