+ * Technically there are typically at least two parameters required: Some kind of credentials for authentication. And + * the XMPP service domain. The credentials often consists of a username and password use for the SASL authentication. + * But there are also other authentication mechanisms, like client side certificates, which do not require a particular + * username and password. + *
+ *+ * There are some misconceptions about XMPP client-to-server parameters: The first is that the username used for + * authentication will be equal to the localpart of the bound XMPP address after authentication. While this is usually + * true, it is not required. Technically the username used for authentication and the resulting XMPP address are + * completely independent from each other. The second common misconception steers from the terms "XMPP host" and "XMPP + * service domain": An XMPP service host is a system which hosts one or multiple XMPP domains. The "XMPP service domain" + * will be usually the domainpart of the bound JID. This domain is used to verify the remote endpoint, typically using + * TLS. This third misconception is that the XMPP service domain is required to become the domainpart of the bound JID. + * Again, while this is very common to be true, it is not strictly required. + *
* * @author Gaston Dombiak + * @author Florian Schmaus */ public abstract class ConnectionConfiguration { @@ -534,6 +554,39 @@ public abstract class ConnectionConfiguration { } } + /** + * Convenience method to configure the username, password and XMPP service domain. + * + * @param jid the XMPP address of the user. + * @param password the password of the user. + * @return a reference to this builder. + * @throws XmppStringprepException in case the XMPP address is not valid. + * @see #setXmppAddressAndPassword(EntityBareJid, String) + * @since 4.4.0 + */ + public B setXmppAddressAndPassword(CharSequence jid, String password) throws XmppStringprepException { + return setXmppAddressAndPassword(JidCreate.entityBareFrom(jid), password); + } + + /** + * Convenience method to configure the username, password and XMPP service domain. The localpart of the provided + * JID is used as username and the domanipart is used as XMPP service domain. + *+ * Please note that this does and can not configure the client XMPP address. XMPP services are not required to + * assign bound JIDs where the localpart matches the username and the domainpart matches the verified domainpart. + * Although most services will follow that pattern. + *
+ * + * @param jid + * @param password + * @return a reference to this builder. + * @since 4.4.0 + */ + public B setXmppAddressAndPassword(EntityBareJid jid, String password) { + setUsernameAndPassword(jid.getLocalpart(), password); + return setXmppDomain(jid.asDomainBareJid()); + } + /** * Set the XMPP entities username and password. *diff --git a/smack-core/src/main/java/org/jivesoftware/smack/SmackConfiguration.java b/smack-core/src/main/java/org/jivesoftware/smack/SmackConfiguration.java index 01f2995a4..10695df47 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/SmackConfiguration.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/SmackConfiguration.java @@ -365,4 +365,19 @@ public final class SmackConfiguration { public static void setUnknownIqRequestReplyMode(UnknownIqRequestReplyMode unknownIqRequestReplyMode) { SmackConfiguration.unknownIqRequestReplyMode = Objects.requireNonNull(unknownIqRequestReplyMode, "Must set mode"); } + + private static final int defaultConcurrencyLevelLimit; + + static { + int availableProcessors = Runtime.getRuntime().availableProcessors(); + if (availableProcessors < 8) { + defaultConcurrencyLevelLimit = 8; + } else { + defaultConcurrencyLevelLimit = (int) (availableProcessors * 1.1); + } + } + + public static int getDefaultConcurrencyLevelLimit() { + return defaultConcurrencyLevelLimit; + } } diff --git a/smack-core/src/main/java/org/jivesoftware/smack/XMPPConnection.java b/smack-core/src/main/java/org/jivesoftware/smack/XMPPConnection.java index c6c2db78f..cd7c6cf7e 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/XMPPConnection.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/XMPPConnection.java @@ -70,6 +70,27 @@ import org.jxmpp.jid.EntityFullJid; * disconnected and then connected again. Listeners of the XMPPConnection will be retained across * connections. *
+ *+ * Standard callbacks are invoked concurrently, but it is ensured that the same callback is never run concurrently. + * The callback's identity is used as key for that. The events delivered to the callback preserve the order of the + * causing events of the connection. + *
+ *+ * Asynchronous callbacks are run decoupled from the connections main event loop. Hence a callback triggered by + * stanza B may (appear to) invoked before a callback triggered by stanza A, even though stanza A arrived before B. + *
+ *+ * Synchronous callbacks are run synchronous to the main event loop of a connection. Hence they are invoked in the + * exact order of how events happen there, most importantly the arrival order of incoming stanzas. You should only + * use synchronous callbacks in rare situations. + *
* * @author Matt Tucker * @author Guenther Niess diff --git a/smack-core/src/main/java/org/jivesoftware/smack/debugger/ConsoleDebugger.java b/smack-core/src/main/java/org/jivesoftware/smack/debugger/ConsoleDebugger.java index fb5d80a69..def9b310e 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/debugger/ConsoleDebugger.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/debugger/ConsoleDebugger.java @@ -16,12 +16,11 @@ */ package org.jivesoftware.smack.debugger; -import java.io.PrintWriter; -import java.io.StringWriter; import java.text.SimpleDateFormat; import java.util.Date; import org.jivesoftware.smack.XMPPConnection; +import org.jivesoftware.smack.util.ExceptionUtil; /** * Very simple debugger that prints to the console (stdout) the sent and received stanzas. Use @@ -55,12 +54,8 @@ public class ConsoleDebugger extends AbstractDebugger { @Override protected void log(String logMessage, Throwable throwable) { - StringWriter sw = new StringWriter(); - PrintWriter pw = new PrintWriter(sw); - // CHECKSTYLE:OFF - throwable.printStackTrace(pw); - // CHECKSTYLE:ON - log(logMessage + sw); + String stacktrace = ExceptionUtil.getStackTrace(throwable); + log(logMessage + '\n' + stacktrace); } public static final class Factory implements SmackDebuggerFactory { diff --git a/smack-core/src/main/java/org/jivesoftware/smack/packet/AbstractError.java b/smack-core/src/main/java/org/jivesoftware/smack/packet/AbstractError.java index ece30af7b..e75e7a0e9 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/packet/AbstractError.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/packet/AbstractError.java @@ -23,6 +23,7 @@ import java.util.List; import java.util.Locale; import java.util.Map; +import org.jivesoftware.smack.util.ExceptionUtil; import org.jivesoftware.smack.util.Objects; import org.jivesoftware.smack.util.PacketUtil; import org.jivesoftware.smack.util.XmlStringBuilder; @@ -150,6 +151,17 @@ public class AbstractError { return getThis(); } + public B setDescriptiveEnText(String descriptiveEnText, Exception exception) { + StringBuilder sb = new StringBuilder(512); + sb.append(descriptiveEnText) + .append('\n'); + + String stacktrace = ExceptionUtil.getStackTrace(exception); + sb.append(stacktrace); + + return setDescriptiveEnText(sb.toString()); + } + public B setTextNamespace(String textNamespace) { this.textNamespace = textNamespace; return getThis(); diff --git a/smack-core/src/main/java/org/jivesoftware/smack/packet/IQ.java b/smack-core/src/main/java/org/jivesoftware/smack/packet/IQ.java index 3626b9b9b..9b15c9c95 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/packet/IQ.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/packet/IQ.java @@ -105,6 +105,16 @@ public abstract class IQ extends Stanza { } } + /** + * Return true if this IQ is a request, i.e. an IQ of type {@link Type#result} or {@link Type#error}. + * + * @return true if IQ type is 'result' or 'error', false otherwise. + * @since 4.4 + */ + public boolean isResponseIQ() { + return !isRequestIQ(); + } + public final String getChildElementName() { return childElementName; } @@ -287,7 +297,7 @@ public abstract class IQ extends Stanza { * @return a new {@link Type#error IQ.Type.error} IQ based on the originating IQ. */ public static ErrorIQ createErrorResponse(final IQ request, final StanzaError.Builder error) { - if (!(request.getType() == Type.get || request.getType() == Type.set)) { + if (!request.isRequestIQ()) { throw new IllegalArgumentException( "IQ must be of type 'set' or 'get'. Original IQ: " + request.toXML()); } diff --git a/smack-core/src/main/java/org/jivesoftware/smack/util/ExceptionUtil.java b/smack-core/src/main/java/org/jivesoftware/smack/util/ExceptionUtil.java new file mode 100644 index 000000000..1c282381a --- /dev/null +++ b/smack-core/src/main/java/org/jivesoftware/smack/util/ExceptionUtil.java @@ -0,0 +1,38 @@ +/** + * + * Copyright 2019 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.smack.util; + +import java.io.PrintWriter; +import java.io.StringWriter; + +public class ExceptionUtil { + + public static String getStackTrace(Throwable throwable) { + StringWriter stringWriter = new StringWriter(); + PrintWriter printWriter = new PrintWriter(stringWriter); + + // CHECKSTYLE:OFF + throwable.printStackTrace(printWriter); + // CHECKSTYLE:ON + + printWriter.flush(); + + StringBuffer stringBuffer = stringWriter.getBuffer(); + return stringBuffer.toString(); + } + +} diff --git a/smack-core/src/main/java/org/jivesoftware/smack/util/RandomUtil.java b/smack-core/src/main/java/org/jivesoftware/smack/util/RandomUtil.java new file mode 100644 index 000000000..857557869 --- /dev/null +++ b/smack-core/src/main/java/org/jivesoftware/smack/util/RandomUtil.java @@ -0,0 +1,50 @@ +/** + * + * Copyright 2003-2007 Jive Software, 2016-2019 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.smack.util; + +import java.security.SecureRandom; +import java.util.Random; + +public class RandomUtil { + + static final ThreadLocal+ * Note that this test is based on the assumption that it is possible to trigger a full garbage collection run, which is + * not the case. See also this + * stackoverflow + * question. Hence the {@link #triggerGarbageCollection()} method defined in this class is not portable and depends + * on implementation depended Java Virtual Machine behavior. + *
+ * + * @see SMACK-383 Jira Issue + */ +public class MemoryLeakTestUtil { + + private static final Logger LOGGER = Logger.getLogger(MemoryLeakTestUtil.class.getName()); + + public static