Skip to content

Commit

Permalink
xmpp-addr: Backfill missing class method for Java 1.7
Browse files Browse the repository at this point in the history
This backfills missing class methods for `java.nio.charset.StandardCharsets`
and `java.util.Objects` for compatibility with platforms which do not support
these (mainly Android SDK versions <= 18).
  • Loading branch information
deuill committed Dec 8, 2018
1 parent ceaa313 commit 69ca58d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 20 deletions.
10 changes: 5 additions & 5 deletions libs/xmpp-addr/src/main/java/rocks/xmpp/addr/AbstractJid.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
package rocks.xmpp.addr;

import java.text.Collator;
import java.util.Objects;
import java.util.Arrays;

/**
* Abstract Jid implementation for both full and bare JIDs.
Expand Down Expand Up @@ -75,14 +75,14 @@ public final boolean equals(Object o) {
}
Jid other = (Jid) o;

return Objects.equals(getLocal(), other.getLocal())
&& Objects.equals(getDomain(), other.getDomain())
&& Objects.equals(getResource(), other.getResource());
return (getLocal() == other.getLocal() || getLocal() != null && getLocal().equals(other.getLocal()))
&& (getDomain() == other.getDomain() || getDomain() != null && getDomain().equals(other.getDomain()))
&& (getResource() == other.getResource() || getResource() != null && getResource().equals(other.getResource()));
}

@Override
public final int hashCode() {
return Objects.hash(getLocal(), getDomain(), getResource());
return Arrays.hashCode(new String[]{getLocal(), getDomain(), getResource()});
}

/**
Expand Down
37 changes: 25 additions & 12 deletions libs/xmpp-addr/src/main/java/rocks/xmpp/addr/FullJid.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@
import rocks.xmpp.util.cache.LruCache;

import java.net.IDN;
import java.nio.charset.StandardCharsets;
import java.nio.charset.Charset;
import java.text.Normalizer;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -109,6 +108,10 @@ private FullJid(final CharSequence local, final CharSequence domain, final CharS

final String unescapedLocalPart;

if (domain == null) {
throw new NullPointerException();
}

if (doUnescape) {
unescapedLocalPart = unescape(local);
} else {
Expand All @@ -126,7 +129,7 @@ private FullJid(final CharSequence local, final CharSequence domain, final CharS
// character MUST be stripped before any other canonicalization steps
// are taken.
// Also validate, that the domain name can be converted to ASCII, i.e. validate the domain name (e.g. must not start with "_").
final String strDomain = IDN.toASCII(LABEL_SEPARATOR_FINAL.matcher(Objects.requireNonNull(domain)).replaceAll(""), IDN.USE_STD3_ASCII_RULES);
final String strDomain = IDN.toASCII(LABEL_SEPARATOR_FINAL.matcher(domain).replaceAll(""), IDN.USE_STD3_ASCII_RULES);
enforcedLocalPart = escapedLocalPart != null ? PrecisProfiles.USERNAME_CASE_MAPPED.enforce(escapedLocalPart) : null;
enforcedResource = resource != null ? PrecisProfiles.OPAQUE_STRING.enforce(resource) : null;
// See https://tools.ietf.org/html/rfc5895#section-2
Expand All @@ -152,23 +155,26 @@ public Jid asBareJid() {

@Override
public Jid withLocal(CharSequence local) {
if (Objects.equals(local, this.getLocal())) {
if (local == this.getLocal() || local != null && local.equals(this.getLocal())) {
return this;
}
return new FullJid(local, getDomain(), getResource(), false, null);
}

@Override
public Jid withResource(CharSequence resource) {
if (Objects.equals(resource, this.getResource())) {
if (resource == this.getResource() || resource != null && resource.equals(this.getResource())) {
return this;
}
return new FullJid(getLocal(), getDomain(), resource, false, asBareJid());
}

@Override
public Jid atSubdomain(CharSequence subdomain) {
return new FullJid(getLocal(), Objects.requireNonNull(subdomain) + "." + getDomain(), getResource(), false, null);
if (subdomain == null) {
throw new NullPointerException();
}
return new FullJid(getLocal(), subdomain + "." + getDomain(), getResource(), false, null);
}

@Override
Expand Down Expand Up @@ -206,7 +212,9 @@ public String getResource() {
* @see <a href="https://xmpp.org/extensions/xep-0106.html">XEP-0106: JID Escaping</a>
*/
static Jid of(String jid, final boolean doUnescape) {
Objects.requireNonNull(jid, "jid must not be null.");
if (jid == null) {
throw new NullPointerException("jid must not be null.");
}

jid = jid.trim();

Expand Down Expand Up @@ -278,7 +286,9 @@ private static String unescape(final CharSequence localPart) {
}

private static void validateDomain(String domain) {
Objects.requireNonNull(domain, "domain must not be null.");
if (domain == null) {
throw new NullPointerException("domain must not be null.");
}
if (domain.contains("@")) {
// Prevent misuse of API.
throw new IllegalArgumentException("domain must not contain a '@' sign");
Expand All @@ -297,7 +307,7 @@ private static void validateLength(CharSequence value, CharSequence part) {
if (value.length() == 0) {
throw new IllegalArgumentException(part + " must not be empty.");
}
if (value.toString().getBytes(StandardCharsets.UTF_8).length > 1023) {
if (value.toString().getBytes(Charset.forName("UTF-8")).length > 1023) {
throw new IllegalArgumentException(part + " must not be greater than 1023 bytes.");
}
}
Expand Down Expand Up @@ -391,7 +401,7 @@ public final String getResource() {
*/
@Override
public final Jid withLocal(CharSequence local) {
if (Objects.equals(local, this.getLocal())) {
if (local == this.getLocal() || local != null && local.equals(this.getLocal())) {
return this;
}
return new FullJid(local, getDomain(), getResource(), false, null);
Expand All @@ -408,7 +418,7 @@ public final Jid withLocal(CharSequence local) {
*/
@Override
public final Jid withResource(CharSequence resource) {
if (Objects.equals(resource, this.getResource())) {
if (resource == this.getResource() || resource != null && resource.equals(this.getResource())) {
return this;
}
return new FullJid(getLocal(), getDomain(), resource, false, asBareJid());
Expand All @@ -424,7 +434,10 @@ public final Jid withResource(CharSequence resource) {
*/
@Override
public final Jid atSubdomain(CharSequence subdomain) {
return new FullJid(getLocal(), Objects.requireNonNull(subdomain) + "." + getDomain(), getResource(), false, null);
if (subdomain != null) {
throw new NullPointerException();
}
return new FullJid(getLocal(), subdomain + "." + getDomain(), getResource(), false, null);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@

package rocks.xmpp.addr;

import java.util.Objects;

/**
* Represents a malformed JID in order to handle the <code>jid-malformed</code> error.
* <p>
Expand Down Expand Up @@ -96,7 +94,10 @@ public Jid withResource(CharSequence resource) {

@Override
public Jid atSubdomain(CharSequence subdomain) {
return new MalformedJid(localPart, Objects.requireNonNull(subdomain) + "." + domainPart, resourcePart, cause);
if (subdomain == null) {
throw new NullPointerException();
}
return new MalformedJid(localPart, subdomain + "." + domainPart, resourcePart, cause);
}

@Override
Expand Down

0 comments on commit 69ca58d

Please sign in to comment.