From 37081b2810e4409987b2a240e667f38022905edc Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Wed, 28 Jan 2015 09:31:28 +0100 Subject: [PATCH] Send more information with the stream open tag --- .../org/jivesoftware/smack/packet/StreamOpen.java | 13 +++++++++---- .../org/jivesoftware/smack/util/StringUtils.java | 13 +++++++++++++ .../jivesoftware/smack/tcp/XMPPTCPConnection.java | 14 +++++++++++++- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/smack-core/src/main/java/org/jivesoftware/smack/packet/StreamOpen.java b/smack-core/src/main/java/org/jivesoftware/smack/packet/StreamOpen.java index 33406e81d..ff5a78537 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/packet/StreamOpen.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/packet/StreamOpen.java @@ -17,6 +17,7 @@ package org.jivesoftware.smack.packet; +import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smack.util.XmlStringBuilder; /** @@ -59,13 +60,17 @@ public class StreamOpen extends FullStreamElement { */ private final String contentNamespace; - public StreamOpen(String to) { + public StreamOpen(CharSequence to) { this(to, null, null, null, StreamContentNamespace.client); } - public StreamOpen(String to, String from, String id, String lang, StreamContentNamespace ns) { - this.to = to; - this.from = from; + public StreamOpen(CharSequence to, CharSequence from, String id) { + this(to, from, id, "en", StreamContentNamespace.client); + } + + public StreamOpen(CharSequence to, CharSequence from, String id, String lang, StreamContentNamespace ns) { + this.to = StringUtils.maybeToString(to); + this.from = StringUtils.maybeToString(from); this.id = id; this.lang = lang; switch (ns) { diff --git a/smack-core/src/main/java/org/jivesoftware/smack/util/StringUtils.java b/smack-core/src/main/java/org/jivesoftware/smack/util/StringUtils.java index 5aba68d91..230ca58e4 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/util/StringUtils.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/util/StringUtils.java @@ -264,4 +264,17 @@ public class StringUtils { } return cs; } + + /** + * Return the String representation of the given char sequence if it is not null. + * + * @param cs the char sequence or null. + * @return the String representation of cs or null. + */ + public static String maybeToString(CharSequence cs) { + if (cs == null) { + return null; + } + return cs.toString(); + } } diff --git a/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnection.java b/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnection.java index df83f80a3..d35f0cf8b 100644 --- a/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnection.java +++ b/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnection.java @@ -74,6 +74,7 @@ import org.jivesoftware.smack.util.PacketParserUtils; import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smack.util.TLSUtils; import org.jivesoftware.smack.util.dns.HostAddress; +import org.jxmpp.util.XmppStringUtils; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; @@ -882,7 +883,18 @@ public class XMPPTCPConnection extends AbstractXMPPConnection { * @throws SmackException if the parser could not be reset. */ void openStream() throws SmackException { - send(new StreamOpen(getServiceName())); + // If possible, provide the receiving entity of the stream open tag, i.e. the server, as much information as + // possible. The 'to' attribute is *always* available. The 'from' attribute if set by the user and no external + // mechanism is used to determine the local entity (user). And the 'id' attribute is available after the first + // response from the server (see e.g. RFC 6120 ยง 9.1.1 Step 2.) + CharSequence to = getServiceName(); + CharSequence from = null; + CharSequence localpart = config.getUsername(); + if (localpart != null) { + from = XmppStringUtils.completeJidFrom(localpart, to); + } + String id = getConnectionID(); + send(new StreamOpen(to, from, id)); try { packetReader.parser = PacketParserUtils.newXmppParser(reader); }