parent
3fc834c067
commit
4f9dd82457
|
@ -12,25 +12,25 @@ import gnu.inet.encoding.StringprepException;
|
||||||
*/
|
*/
|
||||||
public final class Jid {
|
public final class Jid {
|
||||||
|
|
||||||
private final String localpart;
|
private final String localpart;
|
||||||
private final String domainpart;
|
private final String domainpart;
|
||||||
private final String resourcepart;
|
private final String resourcepart;
|
||||||
|
|
||||||
// It's much more efficient to store the ful JID as well as the parts instead of figuring them
|
// 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).
|
// all out every time (since some characters are displayed but aren't used for comparisons).
|
||||||
private final String displayjid;
|
private final String displayjid;
|
||||||
|
|
||||||
public String getLocalpart() {
|
public String getLocalpart() {
|
||||||
return localpart;
|
return localpart;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDomainpart() {
|
public String getDomainpart() {
|
||||||
return IDN.toUnicode(domainpart);
|
return IDN.toUnicode(domainpart);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getResourcepart() {
|
public String getResourcepart() {
|
||||||
return resourcepart;
|
return resourcepart;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Jid fromSessionID(SessionID id) throws InvalidJidException{
|
public static Jid fromSessionID(SessionID id) throws InvalidJidException{
|
||||||
if (id.getUserID().isEmpty()) {
|
if (id.getUserID().isEmpty()) {
|
||||||
|
@ -40,153 +40,155 @@ public final class Jid {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Jid fromString(final String jid) throws InvalidJidException {
|
public static Jid fromString(final String jid) throws InvalidJidException {
|
||||||
return new Jid(jid);
|
return new Jid(jid);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Jid fromParts(final String localpart,
|
public static Jid fromParts(final String localpart,
|
||||||
final String domainpart,
|
final String domainpart,
|
||||||
final String resourcepart) throws InvalidJidException {
|
final String resourcepart) throws InvalidJidException {
|
||||||
String out;
|
String out;
|
||||||
if (localpart == null || localpart.isEmpty()) {
|
if (localpart == null || localpart.isEmpty()) {
|
||||||
out = domainpart;
|
out = domainpart;
|
||||||
} else {
|
} else {
|
||||||
out = localpart + "@" + domainpart;
|
out = localpart + "@" + domainpart;
|
||||||
}
|
}
|
||||||
if (resourcepart != null && !resourcepart.isEmpty()) {
|
if (resourcepart != null && !resourcepart.isEmpty()) {
|
||||||
out = out + "/" + resourcepart;
|
out = out + "/" + resourcepart;
|
||||||
}
|
}
|
||||||
return new Jid(out);
|
return new Jid(out);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Jid(final String jid) throws InvalidJidException {
|
private Jid(final String jid) throws InvalidJidException {
|
||||||
// Hackish Android way to count the number of chars in a string... should work everywhere.
|
// 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 atCount = jid.length() - jid.replace("@", "").length();
|
||||||
final int slashCount = 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...
|
// Throw an error if there's anything obvious wrong with the JID...
|
||||||
if (jid.isEmpty() || jid.length() > 3071) {
|
if (jid.isEmpty() || jid.length() > 3071) {
|
||||||
throw new InvalidJidException(InvalidJidException.INVALID_LENGTH);
|
throw new InvalidJidException(InvalidJidException.INVALID_LENGTH);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Go ahead and check if the localpart or resourcepart is empty.
|
// Go ahead and check if the localpart or resourcepart is empty.
|
||||||
if (jid.startsWith("@") || jid.endsWith("@") ||
|
if (jid.startsWith("@") || jid.endsWith("@") ||
|
||||||
jid.startsWith("/") || jid.endsWith("/")) {
|
jid.startsWith("/") || jid.endsWith("/")) {
|
||||||
throw new InvalidJidException(InvalidJidException.INVALID_CHARACTER);
|
throw new InvalidJidException(InvalidJidException.INVALID_CHARACTER);
|
||||||
}
|
}
|
||||||
|
|
||||||
String finaljid;
|
String finaljid;
|
||||||
|
|
||||||
final int domainpartStart;
|
final int domainpartStart;
|
||||||
if (atCount >= 1) {
|
final int atLoc = jid.indexOf("@");
|
||||||
final int atLoc = jid.indexOf("@");
|
final int slashLoc = jid.indexOf("/");
|
||||||
final String lp = jid.substring(0, atLoc);
|
// If there is no "@" in the JID (eg. "example.net" or "example.net/resource")
|
||||||
try {
|
// or there are one or more "@" signs but they're all in the resourcepart (eg. "example.net/@/rp@"):
|
||||||
localpart = Stringprep.nodeprep(lp);
|
if (atCount == 0 || (atCount > 0 && slashLoc != -1 && atLoc > slashLoc)) {
|
||||||
} catch (final StringprepException e) {
|
localpart = "";
|
||||||
throw new InvalidJidException(InvalidJidException.STRINGPREP_FAIL, e);
|
finaljid = "";
|
||||||
}
|
domainpartStart = 0;
|
||||||
if (localpart.isEmpty() || localpart.length() > 1023) {
|
} else {
|
||||||
throw new InvalidJidException(InvalidJidException.INVALID_PART_LENGTH);
|
final String lp = jid.substring(0, atLoc);
|
||||||
}
|
try {
|
||||||
domainpartStart = atLoc + 1;
|
localpart = Stringprep.nodeprep(lp);
|
||||||
finaljid = lp + "@";
|
} catch (final StringprepException e) {
|
||||||
} else {
|
throw new InvalidJidException(InvalidJidException.STRINGPREP_FAIL, e);
|
||||||
localpart = "";
|
}
|
||||||
finaljid = "";
|
if (localpart.isEmpty() || localpart.length() > 1023) {
|
||||||
domainpartStart = 0;
|
throw new InvalidJidException(InvalidJidException.INVALID_PART_LENGTH);
|
||||||
}
|
}
|
||||||
|
domainpartStart = atLoc + 1;
|
||||||
|
finaljid = lp + "@";
|
||||||
|
}
|
||||||
|
|
||||||
final String dp;
|
final String dp;
|
||||||
if (slashCount >= 1) {
|
if (slashCount > 0) {
|
||||||
final int slashLoc = jid.indexOf("/");
|
final String rp = jid.substring(slashLoc + 1, jid.length());
|
||||||
final String rp = jid.substring(slashLoc + 1, jid.length());
|
try {
|
||||||
try {
|
resourcepart = Stringprep.resourceprep(rp);
|
||||||
resourcepart = Stringprep.resourceprep(rp);
|
} catch (final StringprepException e) {
|
||||||
} catch (final StringprepException e) {
|
throw new InvalidJidException(InvalidJidException.STRINGPREP_FAIL, e);
|
||||||
throw new InvalidJidException(InvalidJidException.STRINGPREP_FAIL, e);
|
}
|
||||||
}
|
if (resourcepart.isEmpty() || resourcepart.length() > 1023) {
|
||||||
if (resourcepart.isEmpty() || resourcepart.length() > 1023) {
|
throw new InvalidJidException(InvalidJidException.INVALID_PART_LENGTH);
|
||||||
throw new InvalidJidException(InvalidJidException.INVALID_PART_LENGTH);
|
}
|
||||||
}
|
dp = IDN.toUnicode(jid.substring(domainpartStart, slashLoc), IDN.USE_STD3_ASCII_RULES);
|
||||||
dp = IDN.toUnicode(jid.substring(domainpartStart, slashLoc), IDN.USE_STD3_ASCII_RULES);
|
finaljid = finaljid + dp + "/" + rp;
|
||||||
finaljid = finaljid + dp + "/" + rp;
|
} else {
|
||||||
} else {
|
resourcepart = "";
|
||||||
resourcepart = "";
|
dp = IDN.toUnicode(jid.substring(domainpartStart, jid.length()),
|
||||||
dp = IDN.toUnicode(jid.substring(domainpartStart, jid.length()),
|
IDN.USE_STD3_ASCII_RULES);
|
||||||
IDN.USE_STD3_ASCII_RULES);
|
finaljid = finaljid + dp;
|
||||||
finaljid = finaljid + dp;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Remove trailing "." before storing the domain part.
|
// Remove trailing "." before storing the domain part.
|
||||||
if (dp.endsWith(".")) {
|
if (dp.endsWith(".")) {
|
||||||
try {
|
try {
|
||||||
domainpart = IDN.toASCII(dp.substring(0, dp.length() - 1), IDN.USE_STD3_ASCII_RULES);
|
domainpart = IDN.toASCII(dp.substring(0, dp.length() - 1), IDN.USE_STD3_ASCII_RULES);
|
||||||
} catch (final IllegalArgumentException e) {
|
} catch (final IllegalArgumentException e) {
|
||||||
throw new InvalidJidException(e);
|
throw new InvalidJidException(e);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
domainpart = IDN.toASCII(dp, IDN.USE_STD3_ASCII_RULES);
|
domainpart = IDN.toASCII(dp, IDN.USE_STD3_ASCII_RULES);
|
||||||
} catch (final IllegalArgumentException e) {
|
} catch (final IllegalArgumentException e) {
|
||||||
throw new InvalidJidException(e);
|
throw new InvalidJidException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Find a proper domain validation library; validate individual parts, separators, etc.
|
// TODO: Find a proper domain validation library; validate individual parts, separators, etc.
|
||||||
if (domainpart.isEmpty() || domainpart.length() > 1023) {
|
if (domainpart.isEmpty() || domainpart.length() > 1023) {
|
||||||
throw new InvalidJidException(InvalidJidException.INVALID_PART_LENGTH);
|
throw new InvalidJidException(InvalidJidException.INVALID_PART_LENGTH);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.displayjid = finaljid;
|
this.displayjid = finaljid;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Jid toBareJid() {
|
public Jid toBareJid() {
|
||||||
try {
|
try {
|
||||||
return resourcepart.isEmpty() ? this : fromParts(localpart, domainpart, "");
|
return resourcepart.isEmpty() ? this : fromParts(localpart, domainpart, "");
|
||||||
} catch (final InvalidJidException e) {
|
} catch (final InvalidJidException e) {
|
||||||
// This should never happen.
|
// This should never happen.
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Jid toDomainJid() {
|
public Jid toDomainJid() {
|
||||||
try {
|
try {
|
||||||
return resourcepart.isEmpty() && localpart.isEmpty() ? this : fromString(getDomainpart());
|
return resourcepart.isEmpty() && localpart.isEmpty() ? this : fromString(getDomainpart());
|
||||||
} catch (final InvalidJidException e) {
|
} catch (final InvalidJidException e) {
|
||||||
// This should never happen.
|
// This should never happen.
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return displayjid;
|
return displayjid;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(final Object o) {
|
public boolean equals(final Object o) {
|
||||||
if (this == o) return true;
|
if (this == o) return true;
|
||||||
if (o == null || getClass() != o.getClass()) return false;
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
|
||||||
final Jid jid = (Jid) o;
|
final Jid jid = (Jid) o;
|
||||||
|
|
||||||
return jid.hashCode() == this.hashCode();
|
return jid.hashCode() == this.hashCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
int result = localpart.hashCode();
|
int result = localpart.hashCode();
|
||||||
result = 31 * result + domainpart.hashCode();
|
result = 31 * result + domainpart.hashCode();
|
||||||
result = 31 * result + resourcepart.hashCode();
|
result = 31 * result + resourcepart.hashCode();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasLocalpart() {
|
public boolean hasLocalpart() {
|
||||||
return !localpart.isEmpty();
|
return !localpart.isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isBareJid() {
|
public boolean isBareJid() {
|
||||||
return this.resourcepart.isEmpty();
|
return this.resourcepart.isEmpty();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue