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"); } }
/** * 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; }
/** * Get the unique identifier for a ConferenceMember, which can be used * to see if it corresponds to the name of a ChatRoomMember * * @param member the ConferenceMember whose ID to retrieve * @return the ID of the conference member or null if address cannot be * parsed */ private String getConferenceMemberResourceID(ConferenceMember member) { // assume address is in the form // <room_name>@conference.<jitsi_meet_domain>/<some_unique_id> try { Jid jid = JidCreate.from(member.getAddress()); if(jid.hasResource()) { return jid.getResourceOrThrow().toString(); } } catch (XmppStringprepException e) { e.printStackTrace(); } return null; }
logger.error("Error creating to field for presence packet: " + e.toString());
logger.fatal("Error creating bosh config: " + e.toString()); System.exit(1);
/** * 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 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; }
@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; }
/** * Get a {@link FullJid} constructed from the given parts. * * @param localpart a optional localpart. * @param domainpart a domainpart. * @param resource a resourcepart. * @return a full JID. * @throws XmppStringprepException if an error occurs. */ public static FullJid fullFrom(String localpart, String domainpart, String resource) throws XmppStringprepException { FullJid fullJid; try { if (localpart == null || localpart.length() == 0) { fullJid = new DomainAndResourcepartJid(domainpart, resource); } else { fullJid = new LocalDomainAndResourcepartJid(localpart, domainpart, resource); } } catch (XmppStringprepException e) { throw new XmppStringprepException(localpart + '@' + domainpart + '/' + resource, e); } return fullJid; }
/** * Get the {@link Domainpart} representing the input String. * * @param domain the input String. * @return the domainpart. * @throws XmppStringprepException if an error occurs. */ public static Domainpart from(String domain) throws XmppStringprepException { if (domain == null) { throw new XmppStringprepException(domain, "Input 'domain' must not be null"); } // TODO cache // RFC 6122 § 2.2 "If the domainpart includes a final character considered to be a label // separator (dot) by [IDNA2003] or [DNS], this character MUST be stripped …" if (domain.length() > 0 && domain.charAt(domain.length() - 1) == '.') { domain = domain.substring(0, domain.length() - 1); } domain = XmppStringPrepUtil.domainprep(domain); // First prep the String, then assure the limits of the *result* assertNotLongerThan1023BytesOrEmpty(domain); return new Domainpart(domain); } }
/** * Constructs the jid for the room by taking the last '@' part as domain * and everything before it as the node part. Doing validation on the node * part for allowed chars. * * @param unescapedValue the unescaped jid as received in the iq * @return a bare JID constructed from the given parts. * @throws XmppStringprepException if an error occurs. */ private EntityBareJid getRoomJid(String unescapedValue) throws XmppStringprepException { // the node part of the jid may contain '@' which is not allowed // and passing the correct node value to Localpart.from will check // for all not allowed jid characters int ix = unescapedValue.lastIndexOf("@"); if (ix == -1) throw new XmppStringprepException(unescapedValue, "wrong room name jid format"); String domainPart = unescapedValue.substring(ix + 1); String localPart = unescapedValue.substring(0, ix); return JidCreate.entityBareFrom( Localpart.from(localPart), Domainpart.from(domainPart)); } }
throw new XmppStringprepException("Does not contain a localpart", jidString); throw new XmppStringprepException(jidString, e); domainpart = Domainpart.from(domainpartString); } catch (XmppStringprepException e) { throw new XmppStringprepException(jidString, e); resourcepart = Resourcepart.from(resourceString); } catch (XmppStringprepException e) { throw new XmppStringprepException(jidString, e);