Improve JID error handling

This commit is contained in:
Sam Whited 2014-11-05 09:21:29 -05:00
parent 8e23b6c272
commit a11878b139
2 changed files with 57 additions and 9 deletions

View File

@ -0,0 +1,48 @@
package eu.siacs.conversations.xmpp.jid;
public class InvalidJidException extends Exception {
// This is probably not the "Java way", but the "Java way" means we'd have a ton of extra tiny,
// annoying classes floating around. I like this.
public final static String INVALID_LENGTH = "JID must be between 0 and 3071 characters";
public final static String INVALID_PART_LENGTH = "JID part must be between 0 and 1023 characters";
public final static String INVALID_CHARACTER = "JID contains an invalid character";
public final static String STRINGPREP_FAIL = "The STRINGPREP operation has failed for the given JID";
/**
* Constructs a new {@code Exception} that includes the current stack trace.
*/
public InvalidJidException() {
}
/**
* Constructs a new {@code Exception} with the current stack trace and the
* specified detail message.
*
* @param detailMessage the detail message for this exception.
*/
public InvalidJidException(final String detailMessage) {
super(detailMessage);
}
/**
* Constructs a new {@code Exception} with the current stack trace, the
* specified detail message and the specified cause.
*
* @param detailMessage the detail message for this exception.
* @param throwable the cause of this exception.
*/
public InvalidJidException(final String detailMessage, final Throwable throwable) {
super(detailMessage, throwable);
}
/**
* Constructs a new {@code Exception} with the current stack trace and the
* specified cause.
*
* @param throwable the cause of this exception.
*/
public InvalidJidException(final Throwable throwable) {
super(throwable);
}
}

View File

@ -10,8 +10,6 @@ import gnu.inet.encoding.StringprepException;
*/ */
public final class Jid { public final class Jid {
public final static class InvalidJidException extends Exception { }
private final String localpart; private final String localpart;
private final String domainpart; private final String domainpart;
private final String resourcepart; private final String resourcepart;
@ -53,11 +51,13 @@ public final class Jid {
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) {
throw new InvalidJidException(InvalidJidException.INVALID_LENGTH);
}
if (atCount > 1 || slashCount > 1 || if (atCount > 1 || slashCount > 1 ||
jid.length() == 0 || jid.length() > 3071 ||
jid.startsWith("@") || jid.endsWith("@") || jid.startsWith("@") || jid.endsWith("@") ||
jid.startsWith("/") || jid.endsWith("/")) { jid.startsWith("/") || jid.endsWith("/")) {
throw new InvalidJidException(); throw new InvalidJidException(InvalidJidException.INVALID_CHARACTER);
} }
String finaljid; String finaljid;
@ -69,10 +69,10 @@ public final class Jid {
try { try {
localpart = Stringprep.nodeprep(lp); localpart = Stringprep.nodeprep(lp);
} catch (final StringprepException e) { } catch (final StringprepException e) {
throw new InvalidJidException(); throw new InvalidJidException(InvalidJidException.STRINGPREP_FAIL, e);
} }
if (localpart.isEmpty() || localpart.length() > 1023) { if (localpart.isEmpty() || localpart.length() > 1023) {
throw new InvalidJidException(); throw new InvalidJidException(InvalidJidException.INVALID_PART_LENGTH);
} }
domainpartStart = atLoc; domainpartStart = atLoc;
finaljid = lp + "@"; finaljid = lp + "@";
@ -89,10 +89,10 @@ public final class Jid {
try { try {
resourcepart = Stringprep.resourceprep(rp); resourcepart = Stringprep.resourceprep(rp);
} catch (final StringprepException e) { } catch (final StringprepException e) {
throw new InvalidJidException(); throw new InvalidJidException(InvalidJidException.STRINGPREP_FAIL, e);
} }
if (resourcepart.isEmpty() || resourcepart.length() > 1023) { if (resourcepart.isEmpty() || resourcepart.length() > 1023) {
throw new InvalidJidException(); throw new InvalidJidException(InvalidJidException.INVALID_PART_LENGTH);
} }
dp = jid.substring(domainpartStart, slashLoc); dp = jid.substring(domainpartStart, slashLoc);
finaljid = finaljid + dp + "/" + rp; finaljid = finaljid + dp + "/" + rp;
@ -111,7 +111,7 @@ public final class Jid {
// 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(); throw new InvalidJidException(InvalidJidException.INVALID_PART_LENGTH);
} }
this.displayjid = finaljid; this.displayjid = finaljid;