1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-22 12:02:05 +01:00

Use Resourcepart as type for the resource

This commit is contained in:
Florian Schmaus 2015-05-04 20:14:45 +02:00
parent 9354e4fb74
commit 36cc05a507
7 changed files with 62 additions and 18 deletions

View file

@ -42,6 +42,7 @@ import org.jivesoftware.smack.sasl.packet.SaslStreamElements.SASLFailure;
import org.jivesoftware.smack.sasl.packet.SaslStreamElements.Success; import org.jivesoftware.smack.sasl.packet.SaslStreamElements.Success;
import org.jivesoftware.smack.util.PacketParserUtils; import org.jivesoftware.smack.util.PacketParserUtils;
import org.jxmpp.jid.DomainBareJid; import org.jxmpp.jid.DomainBareJid;
import org.jxmpp.jid.parts.Resourcepart;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory; import org.xmlpull.v1.XmlPullParserFactory;
import org.igniterealtime.jbosh.AbstractBody; import org.igniterealtime.jbosh.AbstractBody;
@ -210,7 +211,7 @@ public class XMPPBOSHConnection extends AbstractXMPPConnection {
} }
@Override @Override
protected void loginInternal(String username, String password, String resource) throws XMPPException, protected void loginInternal(String username, String password, Resourcepart resource) throws XMPPException,
SmackException, IOException, InterruptedException { SmackException, IOException, InterruptedException {
// Authenticate using SASL // Authenticate using SASL
saslAuthentication.authenticate(username, password); saslAuthentication.authenticate(username, password);

View file

@ -84,6 +84,7 @@ import org.jivesoftware.smack.util.dns.HostAddress;
import org.jxmpp.jid.DomainBareJid; import org.jxmpp.jid.DomainBareJid;
import org.jxmpp.jid.FullJid; import org.jxmpp.jid.FullJid;
import org.jxmpp.jid.Jid; import org.jxmpp.jid.Jid;
import org.jxmpp.jid.parts.Resourcepart;
import org.jxmpp.util.XmppStringUtils; import org.jxmpp.util.XmppStringUtils;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
@ -372,7 +373,12 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
*/ */
protected abstract void connectInternal() throws SmackException, IOException, XMPPException, InterruptedException; protected abstract void connectInternal() throws SmackException, IOException, XMPPException, InterruptedException;
private String usedUsername, usedPassword, usedResource; private String usedUsername, usedPassword;
/**
* The resourcepart used for this connection. May not be the resulting resourcepart if it's null or overridden by the XMPP service.
*/
private Resourcepart usedResource;
/** /**
* Logs in to the server using the strongest SASL mechanism supported by * Logs in to the server using the strongest SASL mechanism supported by
@ -402,12 +408,12 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
// ones from the connection configuration // ones from the connection configuration
CharSequence username = usedUsername != null ? usedUsername : config.getUsername(); CharSequence username = usedUsername != null ? usedUsername : config.getUsername();
String password = usedPassword != null ? usedPassword : config.getPassword(); String password = usedPassword != null ? usedPassword : config.getPassword();
String resource = usedResource != null ? usedResource : config.getResource(); Resourcepart resource = usedResource != null ? usedResource : config.getResource();
login(username, password, resource); login(username, password, resource);
} }
/** /**
* Same as {@link #login(CharSequence, String, String)}, but takes the resource from the connection * Same as {@link #login(CharSequence, String, Resourcepart)}, but takes the resource from the connection
* configuration. * configuration.
* *
* @param username * @param username
@ -436,7 +442,7 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
* @throws InterruptedException * @throws InterruptedException
* @see #login * @see #login
*/ */
public synchronized void login(CharSequence username, String password, String resource) throws XMPPException, public synchronized void login(CharSequence username, String password, Resourcepart resource) throws XMPPException,
SmackException, IOException, InterruptedException { SmackException, IOException, InterruptedException {
if (!config.allowNullOrEmptyUsername) { if (!config.allowNullOrEmptyUsername) {
StringUtils.requireNotNullOrEmpty(username, "Username must not be null or empty"); StringUtils.requireNotNullOrEmpty(username, "Username must not be null or empty");
@ -449,7 +455,7 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
loginInternal(usedUsername, usedPassword, usedResource); loginInternal(usedUsername, usedPassword, usedResource);
} }
protected abstract void loginInternal(String username, String password, String resource) protected abstract void loginInternal(String username, String password, Resourcepart resource)
throws XMPPException, SmackException, IOException, InterruptedException; throws XMPPException, SmackException, IOException, InterruptedException;
@Override @Override
@ -475,7 +481,7 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
return streamId; return streamId;
} }
protected void bindResourceAndEstablishSession(String resource) throws XMPPErrorException, protected void bindResourceAndEstablishSession(Resourcepart resource) throws XMPPErrorException,
SmackException, InterruptedException { SmackException, InterruptedException {
// Wait until either: // Wait until either:

View file

@ -22,6 +22,8 @@ import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.packet.Session; import org.jivesoftware.smack.packet.Session;
import org.jivesoftware.smack.proxy.ProxyInfo; import org.jivesoftware.smack.proxy.ProxyInfo;
@ -31,6 +33,8 @@ import org.jivesoftware.smack.util.CollectionUtil;
import org.jivesoftware.smack.util.Objects; import org.jivesoftware.smack.util.Objects;
import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smack.util.StringUtils;
import org.jxmpp.jid.DomainBareJid; import org.jxmpp.jid.DomainBareJid;
import org.jxmpp.jid.parts.Resourcepart;
import org.jxmpp.stringprep.XmppStringprepException;
import javax.net.SocketFactory; import javax.net.SocketFactory;
import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HostnameVerifier;
@ -50,6 +54,8 @@ public abstract class ConnectionConfiguration {
SmackConfiguration.getVersion(); SmackConfiguration.getVersion();
} }
private static final Logger LOGGER = Logger.getLogger(ConnectionConfiguration.class.getName());
/** /**
* Hostname of the XMPP server. Usually servers use the same service name as the name * Hostname of the XMPP server. Usually servers use the same service name as the name
* of the server. However, there are some servers like google where host would be * of the server. However, there are some servers like google where host would be
@ -76,7 +82,7 @@ public abstract class ConnectionConfiguration {
private final CharSequence username; private final CharSequence username;
private final String password; private final String password;
private final String resource; private final Resourcepart resource;
/** /**
* Initial presence as of RFC 6121 § 4.2 * Initial presence as of RFC 6121 § 4.2
@ -341,7 +347,7 @@ public abstract class ConnectionConfiguration {
* *
* @return the resource to use when trying to reconnect to the server. * @return the resource to use when trying to reconnect to the server.
*/ */
public String getResource() { public Resourcepart getResource() {
return resource; return resource;
} }
@ -401,6 +407,19 @@ public abstract class ConnectionConfiguration {
* @param <C> the resulting connection configuration type parameter. * @param <C> the resulting connection configuration type parameter.
*/ */
public static abstract class Builder<B extends Builder<B, C>, C extends ConnectionConfiguration> { public static abstract class Builder<B extends Builder<B, C>, C extends ConnectionConfiguration> {
private static final Resourcepart DEFAULT_RESOURCE;
static {
Resourcepart resourcepart = null;
try {
resourcepart = Resourcepart.from("Smack");
}
catch (XmppStringprepException e) {
LOGGER.log(Level.WARNING, "Could not create default resourcepart", e);
}
DEFAULT_RESOURCE = resourcepart;
}
private SecurityMode securityMode = SecurityMode.ifpossible; private SecurityMode securityMode = SecurityMode.ifpossible;
private String keystorePath = System.getProperty("javax.net.ssl.keyStore"); private String keystorePath = System.getProperty("javax.net.ssl.keyStore");
private String keystoreType = "jks"; private String keystoreType = "jks";
@ -411,7 +430,7 @@ public abstract class ConnectionConfiguration {
private HostnameVerifier hostnameVerifier; private HostnameVerifier hostnameVerifier;
private CharSequence username; private CharSequence username;
private String password; private String password;
private String resource = "Smack"; private Resourcepart resource = DEFAULT_RESOURCE;
private boolean sendPresence = true; private boolean sendPresence = true;
private boolean legacySessionDisabled = false; private boolean legacySessionDisabled = false;
private ProxyInfo proxy; private ProxyInfo proxy;
@ -466,11 +485,24 @@ public abstract class ConnectionConfiguration {
* @param resource the resource to use. * @param resource the resource to use.
* @return a reference to this builder. * @return a reference to this builder.
*/ */
public B setResource(String resource) { public B setResource(Resourcepart resource) {
this.resource = resource; this.resource = resource;
return getThis(); return getThis();
} }
/**
* Set the resource to use.
*
* @param resource the non-null CharSequence to use a resource.
* @return a reference ot this builder.
* @throws XmppStringprepException if the CharSequence is not a valid resourcepart.
* @see setResource(Resourcepart)
*/
public B setResource(CharSequence resource) throws XmppStringprepException {
Objects.requireNonNull(resource, "resource must not be null");
return setResource(Resourcepart.from(resource.toString()));
}
public B setHost(String host) { public B setHost(String host) {
this.host = host; this.host = host;
return getThis(); return getThis();

View file

@ -18,6 +18,7 @@
package org.jivesoftware.smack.packet; package org.jivesoftware.smack.packet;
import org.jxmpp.jid.FullJid; import org.jxmpp.jid.FullJid;
import org.jxmpp.jid.parts.Resourcepart;
/** /**
* IQ stanza(/packet) used by Smack to bind a resource and to obtain the jid assigned by the server. * IQ stanza(/packet) used by Smack to bind a resource and to obtain the jid assigned by the server.
@ -35,16 +36,16 @@ public class Bind extends IQ {
public static final String ELEMENT = "bind"; public static final String ELEMENT = "bind";
public static final String NAMESPACE = "urn:ietf:params:xml:ns:xmpp-bind"; public static final String NAMESPACE = "urn:ietf:params:xml:ns:xmpp-bind";
private final String resource; private final Resourcepart resource;
private final FullJid jid; private final FullJid jid;
public Bind(String resource, FullJid jid) { public Bind(Resourcepart resource, FullJid jid) {
super(ELEMENT, NAMESPACE); super(ELEMENT, NAMESPACE);
this.resource = resource; this.resource = resource;
this.jid = jid; this.jid = jid;
} }
public String getResource() { public Resourcepart getResource() {
return resource; return resource;
} }
@ -52,7 +53,7 @@ public class Bind extends IQ {
return jid; return jid;
} }
public static Bind newSet(String resource) { public static Bind newSet(Resourcepart resource) {
Bind bind = new Bind(resource, null); Bind bind = new Bind(resource, null);
bind.setType(IQ.Type.set); bind.setType(IQ.Type.set);
return bind; return bind;

View file

@ -22,6 +22,7 @@ import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.packet.Bind; import org.jivesoftware.smack.packet.Bind;
import org.jxmpp.jid.FullJid; import org.jxmpp.jid.FullJid;
import org.jxmpp.jid.impl.JidCreate; import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.jid.parts.Resourcepart;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserException;
@ -39,7 +40,8 @@ public class BindIQProvider extends IQProvider<Bind> {
name = parser.getName(); name = parser.getName();
switch (name) { switch (name) {
case "resource": case "resource":
bind = Bind.newSet(parser.nextText()); String resourceString = parser.nextText();
bind = Bind.newSet(Resourcepart.from(resourceString));
break; break;
case "jid": case "jid":
FullJid fullJid = JidCreate.fullFrom(parser.nextText()); FullJid fullJid = JidCreate.fullFrom(parser.nextText());

View file

@ -31,6 +31,7 @@ import org.jivesoftware.smack.packet.TopLevelStreamElement;
import org.jxmpp.jid.FullJid; import org.jxmpp.jid.FullJid;
import org.jxmpp.jid.JidTestUtil; import org.jxmpp.jid.JidTestUtil;
import org.jxmpp.jid.impl.JidCreate; import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.jid.parts.Resourcepart;
import org.jxmpp.stringprep.XmppStringprepException; import org.jxmpp.stringprep.XmppStringprepException;
/** /**
@ -116,7 +117,7 @@ public class DummyConnection extends AbstractXMPPConnection {
} }
@Override @Override
protected void loginInternal(String username, String password, String resource) protected void loginInternal(String username, String password, Resourcepart resource)
throws XMPPException { throws XMPPException {
user = getUserJid(); user = getUserJid();
authenticated = true; authenticated = true;

View file

@ -75,6 +75,7 @@ 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.jid.impl.JidCreate; import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.jid.parts.Resourcepart;
import org.jxmpp.stringprep.XmppStringprepException; import org.jxmpp.stringprep.XmppStringprepException;
import org.jxmpp.util.XmppStringUtils; import org.jxmpp.util.XmppStringUtils;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
@ -361,7 +362,7 @@ public class XMPPTCPConnection extends AbstractXMPPConnection {
} }
@Override @Override
protected synchronized void loginInternal(String username, String password, String resource) throws XMPPException, protected synchronized void loginInternal(String username, String password, Resourcepart resource) throws XMPPException,
SmackException, IOException, InterruptedException { SmackException, IOException, InterruptedException {
// Authenticate using SASL // Authenticate using SASL
saslAuthentication.authenticate(username, password); saslAuthentication.authenticate(username, password);