001/** 002 * 003 * Copyright 2003-2007 Jive Software, 2018-2022 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 */ 017 018package org.jivesoftware.smack; 019 020import java.net.MalformedURLException; 021import java.net.URI; 022import java.net.URL; 023import java.util.ArrayList; 024import java.util.Collection; 025import java.util.Collections; 026import java.util.HashSet; 027import java.util.List; 028import java.util.Set; 029 030import javax.net.ssl.HostnameVerifier; 031 032import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionConfiguration; 033import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionModuleDescriptor; 034import org.jivesoftware.smack.compression.XMPPInputOutputStream; 035import org.jivesoftware.smack.debugger.ReflectionDebuggerFactory; 036import org.jivesoftware.smack.debugger.SmackDebuggerFactory; 037import org.jivesoftware.smack.parsing.ExceptionThrowingCallback; 038import org.jivesoftware.smack.parsing.ExceptionThrowingCallbackWithHint; 039import org.jivesoftware.smack.parsing.ParsingExceptionCallback; 040import org.jivesoftware.smack.util.Objects; 041 042/** 043 * Represents the configuration of Smack. The configuration is used for: 044 * <ul> 045 * <li> Initializing classes by loading them at start-up. 046 * <li> Getting the current Smack version. 047 * <li> Getting and setting global library behavior, such as the period of time 048 * to wait for replies to packets from the server. Note: setting these values 049 * via the API will override settings in the configuration file. 050 * </ul> 051 * 052 * Configuration settings are stored in org.jivesoftware.smack/smack-config.xml. 053 * 054 * @author Gaston Dombiak 055 */ 056public final class SmackConfiguration { 057 058 public static final String SMACK_URL_STRING = "https://igniterealtime.org/projects/smack"; 059 060 public static final URL SMACK_URL; 061 062 static { 063 try { 064 SMACK_URL = URI.create(SMACK_URL_STRING).toURL(); 065 } catch (MalformedURLException e) { 066 throw new IllegalStateException(e); 067 } 068 } 069 070 private static int defaultPacketReplyTimeout = 5000; 071 private static int packetCollectorSize = 5000; 072 073 private static List<String> defaultMechs = new ArrayList<>(); 074 075 static Set<String> disabledSmackClasses = new HashSet<>(); 076 077 static final List<XMPPInputOutputStream> compressionHandlers = new ArrayList<>(2); 078 079 static boolean smackInitialized = false; 080 081 /** 082 * Value that indicates whether debugging is enabled. When enabled, a debug 083 * window will appear for each new connection that will contain the following 084 * information:<ul> 085 * <li> Client Traffic -- raw XML traffic generated by Smack and sent to the server. 086 * <li> Server Traffic -- raw XML traffic sent by the server to the client. 087 * <li> Interpreted Packets -- shows XML packets from the server as parsed by Smack. 088 * </ul> 089 * Debugging can be enabled by setting this field to true, or by setting the Java system 090 * property <code>smack.debugEnabled</code> to true. The system property can be set on the 091 * command line such as "java SomeApp -Dsmack.debugEnabled=true". 092 */ 093 public static boolean DEBUG = false; 094 095 private static SmackDebuggerFactory DEFAULT_DEBUGGER_FACTORY = ReflectionDebuggerFactory.INSTANCE; 096 097 /** 098 * The default parsing exception callback is {@link ExceptionThrowingCallback} which will 099 * throw an exception and therefore disconnect the active connection. 100 */ 101 private static ParsingExceptionCallback defaultCallback = new ExceptionThrowingCallbackWithHint(); 102 103 private static HostnameVerifier defaultHostnameVerififer; 104 105 /** 106 * Returns the Smack version information, e.g."1.3.0". 107 * 108 * @return the Smack version information. 109 * @deprecated use {@link Smack#getVersion()} instead. 110 */ 111 @Deprecated 112 // TODO: Remove in Smack 4.6 113 public static String getVersion() { 114 return SmackInitialization.SMACK_VERSION; 115 } 116 117 /** 118 * Returns the number of milliseconds to wait for a response from 119 * the server. The default value is 5000 ms. 120 * 121 * @return the milliseconds to wait for a response from the server 122 */ 123 public static int getDefaultReplyTimeout() { 124 // The timeout value must be greater than 0 otherwise we will answer the default value 125 if (defaultPacketReplyTimeout <= 0) { 126 defaultPacketReplyTimeout = 5000; 127 } 128 return defaultPacketReplyTimeout; 129 } 130 131 /** 132 * Sets the number of milliseconds to wait for a response from 133 * the server. 134 * 135 * @param timeout the milliseconds to wait for a response from the server 136 */ 137 public static void setDefaultReplyTimeout(int timeout) { 138 if (timeout <= 0) { 139 throw new IllegalArgumentException(); 140 } 141 defaultPacketReplyTimeout = timeout; 142 } 143 144 public static void setDefaultSmackDebuggerFactory(SmackDebuggerFactory debuggerFactory) { 145 DEFAULT_DEBUGGER_FACTORY = Objects.requireNonNull(debuggerFactory, "Debugger factory must not be null"); 146 } 147 148 public static SmackDebuggerFactory getDefaultSmackDebuggerFactory() { 149 return DEFAULT_DEBUGGER_FACTORY; 150 } 151 152 /** 153 * Gets the default max size of a stanza collector before it will delete 154 * the older packets. 155 * 156 * @return The number of packets to queue before deleting older packets. 157 */ 158 public static int getStanzaCollectorSize() { 159 return packetCollectorSize; 160 } 161 162 /** 163 * Sets the default max size of a stanza collector before it will delete 164 * the older packets. 165 * 166 * @param collectorSize the number of packets to queue before deleting older packets. 167 */ 168 public static void setStanzaCollectorSize(int collectorSize) { 169 packetCollectorSize = collectorSize; 170 } 171 172 /** 173 * Add a SASL mechanism to the list to be used. 174 * 175 * @param mech the SASL mechanism to be added 176 */ 177 public static void addSaslMech(String mech) { 178 if (!defaultMechs.contains(mech)) { 179 defaultMechs.add(mech); 180 } 181 } 182 183 /** 184 * Add a Collection of SASL mechanisms to the list to be used. 185 * 186 * @param mechs the Collection of SASL mechanisms to be added 187 */ 188 public static void addSaslMechs(Collection<String> mechs) { 189 for (String mech : mechs) { 190 addSaslMech(mech); 191 } 192 } 193 194 /** 195 * Remove a SASL mechanism from the list to be used. 196 * 197 * @param mech the SASL mechanism to be removed 198 */ 199 public static void removeSaslMech(String mech) { 200 defaultMechs.remove(mech); 201 } 202 203 /** 204 * Remove a Collection of SASL mechanisms to the list to be used. 205 * 206 * @param mechs the Collection of SASL mechanisms to be removed 207 */ 208 public static void removeSaslMechs(Collection<String> mechs) { 209 defaultMechs.removeAll(mechs); 210 } 211 212 /** 213 * Returns the list of SASL mechanisms to be used. If a SASL mechanism is 214 * listed here it does not guarantee it will be used. The server may not 215 * support it, or it may not be implemented. 216 * 217 * @return the list of SASL mechanisms to be used. 218 */ 219 public static List<String> getSaslMechs() { 220 return Collections.unmodifiableList(defaultMechs); 221 } 222 223 /** 224 * Set the default parsing exception callback for all newly created connections. 225 * 226 * @param callback TODO javadoc me please 227 * @see ParsingExceptionCallback 228 */ 229 public static void setDefaultParsingExceptionCallback(ParsingExceptionCallback callback) { 230 defaultCallback = callback; 231 } 232 233 /** 234 * Returns the default parsing exception callback. 235 * 236 * @return the default parsing exception callback 237 * @see ParsingExceptionCallback 238 */ 239 public static ParsingExceptionCallback getDefaultParsingExceptionCallback() { 240 return defaultCallback; 241 } 242 243 public static void addCompressionHandler(XMPPInputOutputStream xmppInputOutputStream) { 244 compressionHandlers.add(xmppInputOutputStream); 245 } 246 247 /** 248 * Get compression handlers. 249 * 250 * @return a list of compression handlers. 251 */ 252 public static List<XMPPInputOutputStream> getCompressionHandlers() { 253 List<XMPPInputOutputStream> res = new ArrayList<>(compressionHandlers.size()); 254 for (XMPPInputOutputStream ios : compressionHandlers) { 255 if (ios.isSupported()) { 256 res.add(ios); 257 } 258 } 259 return res; 260 } 261 262 /** 263 * Set the default HostnameVerifier that will be used by XMPP connections to verify the hostname 264 * of a TLS certificate. XMPP connections are able to overwrite this settings by supplying a 265 * HostnameVerifier in their ConnectionConfiguration with 266 * {@link ConnectionConfiguration.Builder#setHostnameVerifier(HostnameVerifier)}. 267 * 268 * @param verifier HostnameVerifier 269 */ 270 public static void setDefaultHostnameVerifier(HostnameVerifier verifier) { 271 defaultHostnameVerififer = verifier; 272 } 273 274 /** 275 * Convenience method for {@link #addDisabledSmackClass(String)}. 276 * 277 * @param clz the Smack class to disable 278 */ 279 public static void addDisabledSmackClass(Class<?> clz) { 280 addDisabledSmackClass(clz.getName()); 281 } 282 283 /** 284 * Add a class to the disabled smack classes. 285 * <p> 286 * {@code className} can also be a package name, in this case, the entire 287 * package is disabled (but can be manually enabled). 288 * </p> 289 * 290 * @param className TODO javadoc me please 291 */ 292 public static void addDisabledSmackClass(String className) { 293 disabledSmackClasses.add(className); 294 } 295 296 /** 297 * Add the given class names to the list of disabled Smack classes. 298 * 299 * @param classNames the Smack classes to disable. 300 * @see #addDisabledSmackClass(String) 301 */ 302 public static void addDisabledSmackClasses(String... classNames) { 303 for (String className : classNames) { 304 addDisabledSmackClass(className); 305 } 306 } 307 308 public static boolean isDisabledSmackClass(String className) { 309 for (String disabledClassOrPackage : disabledSmackClasses) { 310 if (disabledClassOrPackage.equals(className)) { 311 return true; 312 } 313 int lastDotIndex = disabledClassOrPackage.lastIndexOf('.'); 314 // Security check to avoid NPEs if someone entered 'foo.bar.' 315 if (disabledClassOrPackage.length() > lastDotIndex 316 // disabledClassOrPackage is not an Class 317 && !Character.isUpperCase(disabledClassOrPackage.charAt(lastDotIndex + 1)) 318 // classToLoad startsWith the package disabledClassOrPackage disables 319 && className.startsWith(disabledClassOrPackage)) { 320 // Skip the class because the whole package was disabled 321 return true; 322 } 323 } 324 return false; 325 } 326 327 /** 328 * Check if Smack was successfully initialized. 329 * 330 * @return true if smack was initialized, false otherwise 331 */ 332 public static boolean isSmackInitialized() { 333 return smackInitialized; 334 } 335 336 /** 337 * Get the default HostnameVerifier 338 * 339 * @return the default HostnameVerifier or <code>null</code> if none was set 340 */ 341 static HostnameVerifier getDefaultHostnameVerifier() { 342 return defaultHostnameVerififer; 343 } 344 345 public enum UnknownIqRequestReplyMode { 346 doNotReply, 347 reply, 348 } 349 350 private static UnknownIqRequestReplyMode unknownIqRequestReplyMode = UnknownIqRequestReplyMode.reply; 351 352 public static UnknownIqRequestReplyMode getUnknownIqRequestReplyMode() { 353 return unknownIqRequestReplyMode; 354 } 355 356 public static void setUnknownIqRequestReplyMode(UnknownIqRequestReplyMode unknownIqRequestReplyMode) { 357 SmackConfiguration.unknownIqRequestReplyMode = Objects.requireNonNull(unknownIqRequestReplyMode, "Must set mode"); 358 } 359 360 private static final int defaultConcurrencyLevelLimit; 361 362 static { 363 int availableProcessors = Runtime.getRuntime().availableProcessors(); 364 if (availableProcessors < 8) { 365 defaultConcurrencyLevelLimit = 8; 366 } else { 367 defaultConcurrencyLevelLimit = (int) (availableProcessors * 1.1); 368 } 369 } 370 371 public static int getDefaultConcurrencyLevelLimit() { 372 return defaultConcurrencyLevelLimit; 373 } 374 375 private static final Set<Class<? extends ModularXmppClientToServerConnectionModuleDescriptor>> KNOWN_MODULES = new HashSet<>(); 376 377 public static boolean addModule(Class<? extends ModularXmppClientToServerConnectionModuleDescriptor> moduleDescriptor) { 378 synchronized (KNOWN_MODULES) { 379 return KNOWN_MODULES.add(moduleDescriptor); 380 } 381 } 382 383 public static void addAllKnownModulesTo(ModularXmppClientToServerConnectionConfiguration.Builder builder) { 384 synchronized (KNOWN_MODULES) { 385 for (Class<? extends ModularXmppClientToServerConnectionModuleDescriptor> moduleDescriptor : KNOWN_MODULES) { 386 builder.addModule(moduleDescriptor); 387 } 388 } 389 } 390 391}