/** * Compares the URI with the given object for equality. If the object is not a <code>URI</code>, * then the method returns false. Otherwise, the following criteria are observed: * * <ul> * <li>The scheme of the URIs must either be null (undefined) in both cases, or equal, ignorant * of case. * <li>The raw fragment of the URIs must either be null (undefined) in both cases, or equal, * ignorant of case. * <li>Both URIs must be of the same type (opaque or hierarchial) * <li><strong>For opaque URIs:</strong> * <ul> * <li>The raw scheme-specific parts must be equal. * </ul> * <li>For hierarchical URIs: * <ul> * <li>The raw paths must be equal, ignorant of case. * <li>The raw queries are either both undefined or both equal, ignorant of case. * <li>The raw authority sections are either both undefined or: * <li><strong>For registry-based authorities:</strong> * <ul> * <li>they are equal. * </ul> * <li><strong>For server-based authorities:</strong> * <ul> * <li>the hosts are equal, ignoring case * <li>the ports are equal * <li>the user information components are equal * </ul> * </ul> * </ul> * * @param obj the obj to compare the URI with. * @return <code>true</code> if the objects are equal, according to the specification above. */ public boolean equals(Object obj) { if (!(obj instanceof URI)) return false; URI uriObj = (URI) obj; if (scheme == null) { if (uriObj.getScheme() != null) return false; } else if (!(scheme.equalsIgnoreCase(uriObj.getScheme()))) return false; if (rawFragment == null) { if (uriObj.getRawFragment() != null) return false; } else if (!(rawFragment.equalsIgnoreCase(uriObj.getRawFragment()))) return false; boolean opaqueThis = isOpaque(); boolean opaqueObj = uriObj.isOpaque(); if (opaqueThis && opaqueObj) return rawSchemeSpecificPart.equals(uriObj.getRawSchemeSpecificPart()); else if (!opaqueThis && !opaqueObj) { boolean common = rawPath.equalsIgnoreCase(uriObj.getRawPath()) && ((rawQuery == null && uriObj.getRawQuery() == null) || rawQuery.equalsIgnoreCase(uriObj.getRawQuery())); if (rawAuthority == null && uriObj.getRawAuthority() == null) return common; if (host == null) return common && rawAuthority.equalsIgnoreCase(uriObj.getRawAuthority()); return common && host.equalsIgnoreCase(uriObj.getHost()) && port == uriObj.getPort() && (rawUserInfo == null ? uriObj.getRawUserInfo() == null : rawUserInfo.equalsIgnoreCase(uriObj.getRawUserInfo())); } else return false; }
@Override public UriBuilder uri(URI uri) { if (uri == null) { throw new IllegalArgumentException("URI parameter is null"); } if (uri.getRawFragment() != null) { fragment = uri.getRawFragment(); } if (uri.isOpaque()) { scheme = uri.getScheme(); ssp = uri.getRawSchemeSpecificPart(); return this; } if (uri.getScheme() == null) { if (ssp != null) { if (uri.getRawSchemeSpecificPart() != null) { ssp = uri.getRawSchemeSpecificPart(); return this; } } } else { scheme = uri.getScheme(); } ssp = null; if (uri.getRawAuthority() != null) { if (uri.getRawUserInfo() == null && uri.getHost() == null && uri.getPort() == -1) { authority = uri.getRawAuthority(); userInfo = null; host = null; port = -1; } else { authority = null; if (uri.getRawUserInfo() != null) { userInfo = uri.getRawUserInfo(); } if (uri.getHost() != null) { host = uri.getHost(); } if (uri.getPort() != -1) { port = uri.getPort(); } } } if (uri.getRawPath() != null && uri.getRawPath().length() > 0) { path.setLength(0); path.append(uri.getRawPath()); } if (uri.getRawQuery() != null && uri.getRawQuery().length() > 0) { query.setLength(0); query.append(uri.getRawQuery()); } return this; }
@Override public UriBuilder schemeSpecificPart(String ssp) { if (ssp == null) { throw new IllegalArgumentException("Scheme specific part parameter is null"); } // TODO encode or validate scheme specific part // This will not work for template variables present in the spp StringBuilder sb = new StringBuilder(); if (scheme != null) { sb.append(scheme).append(':'); } if (ssp != null) { sb.append(ssp); } if (fragment != null && fragment.length() > 0) { sb.append('#').append(fragment); } URI uri = createURI(sb.toString()); if (uri.getRawSchemeSpecificPart() != null && uri.getRawPath() == null) { this.ssp = uri.getRawSchemeSpecificPart(); } else { this.ssp = null; if (uri.getRawAuthority() != null) { if (uri.getRawUserInfo() == null && uri.getHost() == null && uri.getPort() == -1) { authority = uri.getRawAuthority(); userInfo = null; host = null; port = -1; } else { authority = null; userInfo = uri.getRawUserInfo(); host = uri.getHost(); port = uri.getPort(); } } path.setLength(0); path.append(replaceNull(uri.getRawPath())); query.setLength(0); query.append(replaceNull(uri.getRawQuery())); } return this; }
private static String ensureNonemptyUrlPath(String url) { if (url == null) return null; final URI uri = URI.create(url); if (uri.getPath().isEmpty()) { url = uri.getScheme() + "://" + uri.getRawAuthority() + '/'; if (uri.getRawQuery() != null) url += '?' + uri.getQuery(); } return url; }
/** * Relativizes the given URI against this URI. The following algorithm is used: * * <ul> * <li>If either URI is opaque, the given URI is returned. * <li>If the schemes of the URIs differ, the given URI is returned. * <li>If the authority components of the URIs differ, then the given URI is returned. * <li>If the path of this URI is not a prefix of the supplied URI, then the given URI is * returned. * <li>If all the above conditions hold, a new URI is created using the query and fragment * components of the given URI, along with a path computed by removing the path of this URI * from the start of the path of the supplied URI. * </ul> * * @param uri the URI to relativize agsint this URI * @return the resulting URI * @throws NullPointerException if the uri is null */ public URI relativize(URI uri) { if (isOpaque() || uri.isOpaque()) return uri; if (scheme == null && uri.getScheme() != null) return uri; if (scheme != null && !(scheme.equals(uri.getScheme()))) return uri; if (rawAuthority == null && uri.getRawAuthority() != null) return uri; if (rawAuthority != null && !(rawAuthority.equals(uri.getRawAuthority()))) return uri; if (!(uri.getRawPath().startsWith(rawPath))) return uri; try { return new URI( null, null, uri.getRawPath().substring(rawPath.length()), uri.getRawQuery(), uri.getRawFragment()); } catch (URISyntaxException e) { throw (Error) new InternalError("Relativized URI variant could not " + "be constructed").initCause(e); } }
/** * Compare the URI with another object that must also be a URI. Undefined components are taken to * be less than any other component. The following criteria are observed: * * <ul> * <li>Two URIs with different schemes are compared according to their scheme, regardless of * case. * <li>A hierarchical URI is less than an opaque URI with the same scheme. * <li><strong>For opaque URIs:</strong> * <ul> * <li>URIs with differing scheme-specific parts are ordered according to the ordering of * the scheme-specific part. * <li>URIs with the same scheme-specific part are ordered by the raw fragment. * </ul> * <li>For hierarchical URIs: * <ul> * <li>URIs are ordered according to their raw authority sections, if they are unequal. * <li><strong>For registry-based authorities:</strong> * <ul> * <li>they are ordered according to the ordering of the authority component. * </ul> * <li><strong>For server-based authorities:</strong> * <ul> * <li>URIs are ordered according to the raw user information. * <li>URIs with the same user information are ordered by the host, ignoring case. * <lI>URIs with the same host are ordered by the port. * </ul> * <li>URIs with the same authority section are ordered by the raw path. * <li>URIs with the same path are ordered by their raw query. * <li>URIs with the same query are ordered by their raw fragments. * </ul> * </ul> * * @param obj This object to compare this URI with * @return a negative integer, zero or a positive integer depending on whether this URI is less * than, equal to or greater than that supplied, respectively. * @throws ClassCastException if the given object is not a URI */ public int compareTo(Object obj) throws ClassCastException { URI uri = (URI) obj; if (scheme == null && uri.getScheme() != null) return -1; if (scheme != null) { int sCompare = scheme.compareToIgnoreCase(uri.getScheme()); if (sCompare != 0) return sCompare; } boolean opaqueThis = isOpaque(); boolean opaqueObj = uri.isOpaque(); if (opaqueThis && !opaqueObj) return 1; if (!opaqueThis && opaqueObj) return -1; if (opaqueThis) { int ssCompare = rawSchemeSpecificPart.compareTo(uri.getRawSchemeSpecificPart()); if (ssCompare == 0) return compareFragments(uri); else return ssCompare; } if (rawAuthority == null && uri.getRawAuthority() != null) return -1; if (rawAuthority != null) { int aCompare = rawAuthority.compareTo(uri.getRawAuthority()); if (aCompare != 0) { if (host == null) return aCompare; if (rawUserInfo == null && uri.getRawUserInfo() != null) return -1; int uCompare = rawUserInfo.compareTo(uri.getRawUserInfo()); if (uCompare != 0) return uCompare; if (host == null && uri.getHost() != null) return -1; int hCompare = host.compareTo(uri.getHost()); if (hCompare != 0) return hCompare; return new Integer(port).compareTo(new Integer(uri.getPort())); } } if (rawPath == null && uri.getRawPath() != null) return -1; if (rawPath != null) { int pCompare = rawPath.compareTo(uri.getRawPath()); if (pCompare != 0) return pCompare; } if (rawQuery == null && uri.getRawQuery() != null) return -1; if (rawQuery != null) { int qCompare = rawQuery.compareTo(uri.getRawQuery()); if (qCompare != 0) return qCompare; } return compareFragments(uri); }
/** Convert a java.net.URI to a Uri. */ public static Uri fromJavaUri(URI uri) { if (uri.isOpaque()) { throw new IllegalArgumentException("No support for opaque Uris " + uri.toString()); } return new UriBuilder() .setScheme(uri.getScheme()) .setAuthority(uri.getRawAuthority()) .setPath(uri.getRawPath()) .setQuery(uri.getRawQuery()) .setFragment(uri.getRawFragment()) .toUri(); }
private URI toURI(boolean encode) { if (originalUri == null) { logger.debug("setUrl hasn't been invoked. Using http://localhost"); originalUri = DEFAULT_REQUEST_URL; } AsyncHttpProviderUtils.validateSupportedScheme(originalUri); StringBuilder builder = new StringBuilder(); builder.append(originalUri.getScheme()).append("://").append(originalUri.getRawAuthority()); if (isNonEmpty(originalUri.getRawPath())) { builder.append(originalUri.getRawPath()); } else { builder.append("/"); } if (isNonEmpty(queryParams)) { builder.append("?"); for (Iterator<Entry<String, List<String>>> i = queryParams.iterator(); i.hasNext(); ) { Map.Entry<String, List<String>> param = i.next(); String name = param.getKey(); for (Iterator<String> j = param.getValue().iterator(); j.hasNext(); ) { String value = j.next(); if (encode) { UTF8UrlEncoder.appendEncoded(builder, name); } else { builder.append(name); } if (value != null) { builder.append('='); if (encode) { UTF8UrlEncoder.appendEncoded(builder, value); } else { builder.append(value); } } if (j.hasNext()) { builder.append('&'); } } if (i.hasNext()) { builder.append('&'); } } } return URI.create(builder.toString()); }
/** * Returns the raw (encoded) authority compound element. * * @return the authority compound element. */ public String getRawAuthority() { return uri.getRawAuthority(); }