2020-05-15 15:06:16 +00:00
|
|
|
package eu.siacs.conversations.xmpp;
|
2018-12-08 17:32:27 +00:00
|
|
|
|
2020-05-15 15:06:16 +00:00
|
|
|
import org.jxmpp.jid.impl.JidCreate;
|
|
|
|
import org.jxmpp.jid.parts.Domainpart;
|
|
|
|
import org.jxmpp.jid.parts.Localpart;
|
|
|
|
import org.jxmpp.jid.parts.Resourcepart;
|
|
|
|
import org.jxmpp.stringprep.XmppStringprepException;
|
2018-12-08 17:32:27 +00:00
|
|
|
|
|
|
|
import java.io.Serializable;
|
2020-05-16 08:40:26 +00:00
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
2018-12-08 17:32:27 +00:00
|
|
|
|
|
|
|
public interface Jid extends Comparable<Jid>, Serializable, CharSequence {
|
|
|
|
|
2020-05-16 08:40:26 +00:00
|
|
|
Pattern JID = Pattern.compile("^((.*?)@)?([^/@]+)(/(.*))?$");
|
|
|
|
|
2018-12-08 17:32:27 +00:00
|
|
|
static Jid of(CharSequence local, CharSequence domain, CharSequence resource) {
|
2020-05-16 08:40:26 +00:00
|
|
|
if (local == null) {
|
|
|
|
if (resource == null) {
|
|
|
|
return ofDomain(domain);
|
|
|
|
} else {
|
|
|
|
return ofDomainAndResource(domain, resource);
|
|
|
|
}
|
|
|
|
}
|
2020-05-15 15:06:16 +00:00
|
|
|
if (resource == null) {
|
|
|
|
return ofLocalAndDomain(local, domain);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
return new WrappedJid(JidCreate.entityFullFrom(
|
|
|
|
Localpart.fromUnescaped(local.toString()),
|
|
|
|
Domainpart.from(domain.toString()),
|
|
|
|
Resourcepart.from(resource.toString())
|
|
|
|
));
|
|
|
|
} catch (XmppStringprepException e) {
|
|
|
|
throw new IllegalArgumentException(e);
|
|
|
|
}
|
2018-12-08 17:32:27 +00:00
|
|
|
}
|
|
|
|
|
2020-05-15 16:21:45 +00:00
|
|
|
static Jid ofEscaped(CharSequence local, CharSequence domain, CharSequence resource) {
|
|
|
|
try {
|
|
|
|
if (resource == null) {
|
|
|
|
return new WrappedJid(
|
|
|
|
JidCreate.bareFrom(
|
|
|
|
Localpart.from(local.toString()),
|
|
|
|
Domainpart.from(domain.toString())
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return new WrappedJid(JidCreate.entityFullFrom(
|
|
|
|
Localpart.from(local.toString()),
|
|
|
|
Domainpart.from(domain.toString()),
|
|
|
|
Resourcepart.from(resource.toString())
|
|
|
|
));
|
|
|
|
} catch (XmppStringprepException e) {
|
|
|
|
throw new IllegalArgumentException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-16 06:55:13 +00:00
|
|
|
|
2018-12-08 17:32:27 +00:00
|
|
|
static Jid ofDomain(CharSequence domain) {
|
2020-05-15 15:06:16 +00:00
|
|
|
try {
|
|
|
|
return new WrappedJid(JidCreate.domainBareFrom(domain));
|
|
|
|
} catch (XmppStringprepException e) {
|
|
|
|
throw new IllegalArgumentException(e);
|
|
|
|
}
|
2018-12-08 17:32:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static Jid ofLocalAndDomain(CharSequence local, CharSequence domain) {
|
2020-05-15 15:06:16 +00:00
|
|
|
try {
|
|
|
|
return new WrappedJid(
|
|
|
|
JidCreate.bareFrom(
|
|
|
|
Localpart.fromUnescaped(local.toString()),
|
|
|
|
Domainpart.from(domain.toString())
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} catch (XmppStringprepException e) {
|
|
|
|
throw new IllegalArgumentException(e);
|
|
|
|
}
|
2018-12-08 17:32:27 +00:00
|
|
|
}
|
|
|
|
|
2020-05-16 08:40:26 +00:00
|
|
|
static Jid ofDomainAndResource(CharSequence domain, CharSequence resource) {
|
|
|
|
try {
|
2020-05-17 08:24:46 +00:00
|
|
|
return new WrappedJid(
|
|
|
|
JidCreate.domainFullFrom(
|
|
|
|
Domainpart.from(domain.toString()),
|
|
|
|
Resourcepart.from(resource.toString())
|
|
|
|
));
|
|
|
|
} catch (XmppStringprepException e) {
|
|
|
|
throw new IllegalArgumentException(e);
|
|
|
|
}
|
2020-05-16 08:40:26 +00:00
|
|
|
}
|
|
|
|
|
2020-05-16 06:55:13 +00:00
|
|
|
static Jid ofLocalAndDomainEscaped(CharSequence local, CharSequence domain) {
|
|
|
|
try {
|
|
|
|
return new WrappedJid(
|
|
|
|
JidCreate.bareFrom(
|
|
|
|
Localpart.from(local.toString()),
|
|
|
|
Domainpart.from(domain.toString())
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} catch (XmppStringprepException e) {
|
|
|
|
throw new IllegalArgumentException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-08 17:32:27 +00:00
|
|
|
static Jid of(CharSequence jid) {
|
|
|
|
if (jid instanceof Jid) {
|
|
|
|
return (Jid) jid;
|
|
|
|
}
|
2020-05-16 08:40:26 +00:00
|
|
|
Matcher matcher = JID.matcher(jid);
|
|
|
|
if (matcher.matches()) {
|
|
|
|
return of(matcher.group(2), matcher.group(3), matcher.group(5));
|
|
|
|
} else {
|
|
|
|
throw new IllegalArgumentException("Could not parse JID: " + jid);
|
2020-05-15 15:06:16 +00:00
|
|
|
}
|
2018-12-08 17:32:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static Jid ofEscaped(CharSequence jid) {
|
2020-05-15 15:06:16 +00:00
|
|
|
try {
|
|
|
|
return new WrappedJid(JidCreate.from(jid));
|
|
|
|
} catch (XmppStringprepException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
throw new IllegalArgumentException(e);
|
|
|
|
}
|
2018-12-08 17:32:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
boolean isFullJid();
|
|
|
|
|
|
|
|
boolean isBareJid();
|
|
|
|
|
|
|
|
boolean isDomainJid();
|
|
|
|
|
|
|
|
Jid asBareJid();
|
|
|
|
|
|
|
|
Jid withResource(CharSequence resource);
|
|
|
|
|
|
|
|
String getLocal();
|
|
|
|
|
|
|
|
String getEscapedLocal();
|
|
|
|
|
2020-05-17 08:24:46 +00:00
|
|
|
Jid getDomain();
|
2018-12-08 17:32:27 +00:00
|
|
|
|
|
|
|
String getResource();
|
|
|
|
|
|
|
|
String toEscapedString();
|
|
|
|
}
|