2014-11-05 14:00:31 +00:00
|
|
|
package eu.siacs.conversations.xmpp.jid;
|
|
|
|
|
|
|
|
import java.net.IDN;
|
|
|
|
|
|
|
|
import gnu.inet.encoding.Stringprep;
|
|
|
|
import gnu.inet.encoding.StringprepException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The `Jid' class provides an immutable representation of a JID.
|
|
|
|
*/
|
|
|
|
public final class Jid {
|
|
|
|
|
|
|
|
private final String localpart;
|
|
|
|
private final String domainpart;
|
|
|
|
private final String resourcepart;
|
|
|
|
|
|
|
|
// It's much more efficient to store the ful JID as well as the parts instead of figuring them
|
|
|
|
// all out every time (since some characters are displayed but aren't used for comparisons).
|
|
|
|
private final String displayjid;
|
|
|
|
|
|
|
|
public String getLocalpart() {
|
|
|
|
return IDN.toUnicode(localpart);
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getDomainpart() {
|
|
|
|
return IDN.toUnicode(domainpart);
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getResourcepart() {
|
|
|
|
return IDN.toUnicode(resourcepart);
|
|
|
|
}
|
|
|
|
|
2014-11-05 14:40:16 +00:00
|
|
|
public Jid fromString(final String jid) throws InvalidJidException {
|
|
|
|
return new Jid(jid);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Jid fromParts(final String localpart,
|
|
|
|
final String domainpart,
|
|
|
|
final String resourcepart) throws InvalidJidException {
|
|
|
|
String out;
|
|
|
|
if (localpart == null || localpart.isEmpty()) {
|
|
|
|
out = domainpart;
|
2014-11-05 14:00:31 +00:00
|
|
|
} else {
|
2014-11-05 14:40:16 +00:00
|
|
|
out = localpart + "@" + domainpart;
|
|
|
|
}
|
|
|
|
if (resourcepart != null && !resourcepart.isEmpty()) {
|
|
|
|
out = out + "/" + resourcepart;
|
2014-11-05 14:00:31 +00:00
|
|
|
}
|
2014-11-05 14:40:16 +00:00
|
|
|
return new Jid(out);
|
2014-11-05 14:00:31 +00:00
|
|
|
}
|
|
|
|
|
2014-11-05 14:40:16 +00:00
|
|
|
private Jid(final String jid) throws InvalidJidException {
|
2014-11-05 14:00:31 +00:00
|
|
|
// Hackish Android way to count the number of chars in a string... should work everywhere.
|
|
|
|
final int atCount = jid.length() - jid.replace("@", "").length();
|
|
|
|
final int slashCount = jid.length() - jid.replace("/", "").length();
|
|
|
|
|
|
|
|
// Throw an error if there's anything obvious wrong with the JID...
|
2014-11-05 14:21:29 +00:00
|
|
|
if (jid.isEmpty() || jid.length() > 3071) {
|
|
|
|
throw new InvalidJidException(InvalidJidException.INVALID_LENGTH);
|
|
|
|
}
|
2014-11-05 14:00:31 +00:00
|
|
|
if (atCount > 1 || slashCount > 1 ||
|
|
|
|
jid.startsWith("@") || jid.endsWith("@") ||
|
|
|
|
jid.startsWith("/") || jid.endsWith("/")) {
|
2014-11-05 14:21:29 +00:00
|
|
|
throw new InvalidJidException(InvalidJidException.INVALID_CHARACTER);
|
2014-11-05 14:00:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String finaljid;
|
|
|
|
|
|
|
|
final int domainpartStart;
|
|
|
|
if (atCount == 1) {
|
|
|
|
final int atLoc = jid.indexOf("@");
|
|
|
|
final String lp = jid.substring(0, atLoc);
|
|
|
|
try {
|
|
|
|
localpart = Stringprep.nodeprep(lp);
|
|
|
|
} catch (final StringprepException e) {
|
2014-11-05 14:21:29 +00:00
|
|
|
throw new InvalidJidException(InvalidJidException.STRINGPREP_FAIL, e);
|
2014-11-05 14:00:31 +00:00
|
|
|
}
|
|
|
|
if (localpart.isEmpty() || localpart.length() > 1023) {
|
2014-11-05 14:21:29 +00:00
|
|
|
throw new InvalidJidException(InvalidJidException.INVALID_PART_LENGTH);
|
2014-11-05 14:00:31 +00:00
|
|
|
}
|
|
|
|
domainpartStart = atLoc;
|
|
|
|
finaljid = lp + "@";
|
|
|
|
} else {
|
|
|
|
localpart = "";
|
|
|
|
finaljid = "";
|
|
|
|
domainpartStart = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
final String dp;
|
|
|
|
if (slashCount == 1) {
|
|
|
|
final int slashLoc = jid.indexOf("/");
|
|
|
|
final String rp = jid.substring(slashLoc + 1, jid.length());
|
|
|
|
try {
|
|
|
|
resourcepart = Stringprep.resourceprep(rp);
|
|
|
|
} catch (final StringprepException e) {
|
2014-11-05 14:21:29 +00:00
|
|
|
throw new InvalidJidException(InvalidJidException.STRINGPREP_FAIL, e);
|
2014-11-05 14:00:31 +00:00
|
|
|
}
|
|
|
|
if (resourcepart.isEmpty() || resourcepart.length() > 1023) {
|
2014-11-05 14:21:29 +00:00
|
|
|
throw new InvalidJidException(InvalidJidException.INVALID_PART_LENGTH);
|
2014-11-05 14:00:31 +00:00
|
|
|
}
|
|
|
|
dp = jid.substring(domainpartStart, slashLoc);
|
|
|
|
finaljid = finaljid + dp + "/" + rp;
|
|
|
|
} else {
|
|
|
|
resourcepart = "";
|
|
|
|
dp = jid.substring(domainpartStart, jid.length());
|
|
|
|
finaljid = finaljid + dp;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove trailling "." before storing the domain part.
|
|
|
|
if (dp.endsWith(".")) {
|
|
|
|
domainpart = IDN.toASCII(dp.substring(0, dp.length() - 1), IDN.USE_STD3_ASCII_RULES);
|
|
|
|
} else {
|
|
|
|
domainpart = IDN.toASCII(dp, IDN.USE_STD3_ASCII_RULES);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Find a proper domain validation library; validate individual parts, separators, etc.
|
|
|
|
if (domainpart.isEmpty() || domainpart.length() > 1023) {
|
2014-11-05 14:21:29 +00:00
|
|
|
throw new InvalidJidException(InvalidJidException.INVALID_PART_LENGTH);
|
2014-11-05 14:00:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.displayjid = finaljid;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Jid getBareJid() {
|
2014-11-05 14:40:16 +00:00
|
|
|
try {
|
|
|
|
return resourcepart.isEmpty() ? this : fromParts(localpart, domainpart, "");
|
|
|
|
} catch (final InvalidJidException e) {
|
|
|
|
// This should never happen due to the contracts we have in place.
|
|
|
|
return null;
|
|
|
|
}
|
2014-11-05 14:00:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return displayjid;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(final Object o) {
|
|
|
|
if (this == o) return true;
|
|
|
|
if (o == null || getClass() != o.getClass()) return false;
|
|
|
|
|
|
|
|
final Jid jid = (Jid) o;
|
|
|
|
|
|
|
|
return jid.hashCode() == this.hashCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
int result = localpart.hashCode();
|
|
|
|
result = 31 * result + domainpart.hashCode();
|
|
|
|
result = 31 * result + resourcepart.hashCode();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|