/** * Test, if the equals() method works as intended. */ @Test public void testEquals() { BareJid romeo, juliet, guyUnderTheBalcony; try { romeo = JidCreate.bareFrom("romeo@shakespeare.lit"); guyUnderTheBalcony = JidCreate.bareFrom("romeo@shakespeare.lit/underTheBalcony"); juliet = JidCreate.bareFrom("juliet@shakespeare.lit"); } catch (XmppStringprepException e) { Assert.fail(e.getMessage()); return; } OmemoDevice r = new OmemoDevice(romeo, 1); OmemoDevice g = new OmemoDevice(guyUnderTheBalcony, 1); OmemoDevice r2 = new OmemoDevice(romeo, 2); OmemoDevice j = new OmemoDevice(juliet, 3); OmemoDevice j2 = new OmemoDevice(juliet, 1); assertTrue(r.equals(g)); assertFalse(r.equals(r2)); assertFalse(j.equals(j2)); assertFalse(j2.equals(r2)); } }
/** * Construct a new XMPP Stringprep exception with the given causing String and exception. * * @param causingString the String causing the exception. * @param exception the exception. */ public XmppStringprepException(String causingString, Exception exception) { super("XmppStringprepException caused by '" + causingString + "': " + exception); initCause(exception); this.causingString = causingString; }
/** * Setup the libidn Stringprep implementation as active Stringprep implementation. */ public static void setup() { XmppStringPrepUtil.setXmppStringprep(getInstance()); }
protected static void assertNotLongerThan1023BytesOrEmpty(String string) throws XmppStringprepException { char[] bytes = string.toCharArray(); // Better throw XmppStringprepException instead of IllegalArgumentException here, because users don't expect an // IAE and it also makes the error handling for users easier. if (bytes.length > 1023) { throw new XmppStringprepException(string, "Given string is longer then 1023 bytes"); } else if (bytes.length == 0) { throw new XmppStringprepException(string, "Argument can't be the empty string"); } }
@Override public String localprep(String string) throws XmppStringprepException { string = simpleStringprep(string); for (char charFromString : string.toCharArray()) { for (char forbiddenChar : LOCALPART_FURTHER_EXCLUDED_CHARACTERS) { if (charFromString == forbiddenChar) { throw new XmppStringprepException(string, "Localpart must not contain '" + forbiddenChar + "'"); } } } return string; }
/** * Setup the ICU4J Stringprep implementation as active Stringprep implementation. */ public static void setup() { XmppStringPrepUtil.setXmppStringprep(getInstance()); }
/** * Setup Simple XMPP Stringprep as implementation to use. */ public static void setup() { XmppStringPrepUtil.setXmppStringprep(getInstance()); }
/** * Perform localprep on the input String. * * @param string the input String. * @return the localpreped String. * @throws XmppStringprepException if the input String can not be transformed. */ public static String localprep(String string) throws XmppStringprepException { if (xmppStringprep == null) { return string; } // Avoid cache lookup if string is the empty string throwIfEmptyString(string); String res = NODEPREP_CACHE.lookup(string); if (res != null) { return res; } res = xmppStringprep.localprep(string); NODEPREP_CACHE.put(string, res); return res; }
/** * Perform domainprep on the input String. * * @param string the input String. * @return the domainprep String. * @throws XmppStringprepException if the input String can not be transformed. */ public static String domainprep(String string) throws XmppStringprepException { if (xmppStringprep == null) { return string; } // Avoid cache lookup if string is the empty string throwIfEmptyString(string); String res = DOMAINPREP_CACHE.lookup(string); if (res != null) { return res; } res = xmppStringprep.domainprep(string); DOMAINPREP_CACHE.put(string, res); return res; }
/** * Perform resourceprep on the input String. * * @param string the input String. * @return the resourceprep String. * @throws XmppStringprepException if the input String can not be transformed. */ public static String resourceprep(String string) throws XmppStringprepException { if (xmppStringprep == null) { return string; } // Avoid cache lookup if string is the empty string throwIfEmptyString(string); String res = RESOURCEPREP_CACHE.lookup(string); if (res != null) { return res; } res = xmppStringprep.resourceprep(string); RESOURCEPREP_CACHE.put(string, res); return res; }
/** * Get the {@link Resourcepart} representing the input String. * * @param resource the input String. * @return the resource part. * @throws XmppStringprepException if an error occurs. */ public static Resourcepart from(String resource) throws XmppStringprepException { resource = XmppStringPrepUtil.resourceprep(resource); // First prep the String, then assure the limits of the *result* assertNotLongerThan1023BytesOrEmpty(resource); return new Resourcepart(resource); } }
/** * Get the {@link Localpart} representing the input String. * * @param localpart the input String. * @return the localpart. * @throws XmppStringprepException if an error occurs. */ public static Localpart from(String localpart) throws XmppStringprepException { localpart = XmppStringPrepUtil.localprep(localpart); // First prep the String, then assure the limits of the *result* assertNotLongerThan1023BytesOrEmpty(localpart); return new Localpart(localpart); } }
/** * Throws a XMPP Stringprep exception if string is the empty string. * * @param string * @throws XmppStringprepException */ private static void throwIfEmptyString(String string) throws XmppStringprepException { // TODO replace with string.isEmpty() once Smack's min Android SDK level is > 8 if (string.length() == 0) { throw new XmppStringprepException(string, "Argument can't be the empty string"); } } }
@Override public String domainprep(String string) throws XmppStringprepException { try { // Don't allow unassigned because this is a "stored string". See // RFC3453 7, RFC3490 4 1) and RFC6122 2.2 return Stringprep.nameprep(string); } catch (StringprepException e) { throw new XmppStringprepException(string, e); } }
@Override public String localprep(String string) throws XmppStringprepException { try { // Allow unassigned codepoints as of RFC6122 A.2 return Stringprep.nodeprep(string, true); } catch (StringprepException e) { throw new XmppStringprepException(string, e); } }
@Override public String resourceprep(String string) throws XmppStringprepException { try { return RESOURCEPREP.prepare(string, StringPrep.DEFAULT); } catch (StringPrepParseException e) { throw new XmppStringprepException(string, e); } } }
@Override public String resourceprep(String string) throws XmppStringprepException { try { // Allow unassigned codepoints as of RFC6122 B.2 return Stringprep.resourceprep(string, true); } catch (StringprepException e) { throw new XmppStringprepException(string, e); } } }
@Override public String localprep(String string) throws XmppStringprepException { try { return NODEPREP.prepare(string, StringPrep.DEFAULT); } catch (StringPrepParseException e) { throw new XmppStringprepException(string, e); } }
@Override public String domainprep(String string) throws XmppStringprepException { try { return DOMAINPREP.prepare(string, StringPrep.DEFAULT); } catch (StringPrepParseException e) { throw new XmppStringprepException(string, e); } }
/** * Get a {@link EntityFullJid} constructed from the given parts. * * @param localpart a localpart. * @param domainpart a domainpart. * @param resource a resourcepart. * @return a full JID. * @throws XmppStringprepException if an error occurs. */ public static EntityFullJid entityFullFrom(String localpart, String domainpart, String resource) throws XmppStringprepException { EntityFullJid fullJid; try { fullJid = new LocalDomainAndResourcepartJid(localpart, domainpart, resource); } catch (XmppStringprepException e) { throw new XmppStringprepException(localpart + '@' + domainpart + '/' + resource, e); } return fullJid; }