Send more information with the stream open tag

This commit is contained in:
Florian Schmaus 2015-01-28 09:31:28 +01:00
parent e493500cfe
commit 37081b2810
3 changed files with 35 additions and 5 deletions

View File

@ -17,6 +17,7 @@
package org.jivesoftware.smack.packet; package org.jivesoftware.smack.packet;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smack.util.XmlStringBuilder;
/** /**
@ -59,13 +60,17 @@ public class StreamOpen extends FullStreamElement {
*/ */
private final String contentNamespace; private final String contentNamespace;
public StreamOpen(String to) { public StreamOpen(CharSequence to) {
this(to, null, null, null, StreamContentNamespace.client); this(to, null, null, null, StreamContentNamespace.client);
} }
public StreamOpen(String to, String from, String id, String lang, StreamContentNamespace ns) { public StreamOpen(CharSequence to, CharSequence from, String id) {
this.to = to; this(to, from, id, "en", StreamContentNamespace.client);
this.from = from; }
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.id = id;
this.lang = lang; this.lang = lang;
switch (ns) { switch (ns) {

View File

@ -264,4 +264,17 @@ public class StringUtils {
} }
return cs; 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 <code>cs</code> or null.
*/
public static String maybeToString(CharSequence cs) {
if (cs == null) {
return null;
}
return cs.toString();
}
} }

View File

@ -74,6 +74,7 @@ import org.jivesoftware.smack.util.PacketParserUtils;
import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.TLSUtils; import org.jivesoftware.smack.util.TLSUtils;
import org.jivesoftware.smack.util.dns.HostAddress; import org.jivesoftware.smack.util.dns.HostAddress;
import org.jxmpp.util.XmppStringUtils;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserException;
@ -882,7 +883,18 @@ public class XMPPTCPConnection extends AbstractXMPPConnection {
* @throws SmackException if the parser could not be reset. * @throws SmackException if the parser could not be reset.
*/ */
void openStream() throws SmackException { 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 { try {
packetReader.parser = PacketParserUtils.newXmppParser(reader); packetReader.parser = PacketParserUtils.newXmppParser(reader);
} }