FileDocCategorySizeDatePackage
URIUtils.javaAPI DocAndroid 1.5 API7376Wed May 06 22:41:10 BST 2009org.apache.http.client.utils

URIUtils

public class URIUtils extends Object
A collection of utilities for {@link URI URIs}, to workaround bugs within the class or for ease-of-use features.

Fields Summary
Constructors Summary
private URIUtils()
This class should not be instantiated.

    
Methods Summary
public static java.net.URIcreateURI(java.lang.String scheme, java.lang.String host, int port, java.lang.String path, java.lang.String query, java.lang.String fragment)
Constructs a {@link URI} using all the parameters. This should be used instead of {@link URI#URI(String, String, String, int, String, String, String)} or any of the other URI multi-argument URI constructors. See HTTPCLIENT-730 for more information.

param
scheme Scheme name
param
host Host name
param
port Port number
param
path Path
param
query Query
param
fragment Fragment
throws
URISyntaxException If both a scheme and a path are given but the path is relative, if the URI string constructed from the given components violates RFC 2396, or if the authority component of the string is present but cannot be parsed as a server-based authority

        
        StringBuilder buffer = new StringBuilder();
        if (host != null) {
            if (scheme != null) {
                buffer.append(scheme);
                buffer.append("://");
            }
            buffer.append(host);
            if (port > 0) {
                buffer.append(':");
                buffer.append(port);
            }
        }
        if (path == null || !path.startsWith("/")) {
            buffer.append('/");
        }
        if (path != null) {
            buffer.append(path);
        }
        if (query != null) {
            buffer.append('?");
            buffer.append(query);
        }
        if (fragment != null) {
            buffer.append('#");
            buffer.append(fragment);
        }
        return new URI(buffer.toString());
    
public static java.net.URIresolve(java.net.URI baseURI, java.lang.String reference)
Resolves a URI reference against a base URI. Work-around for bug in java.net.URI ()

param
baseURI the base URI
param
reference the URI reference
return
the resulting URI

        return URIUtils.resolve(baseURI, URI.create(reference));
    
public static java.net.URIresolve(java.net.URI baseURI, java.net.URI reference)
Resolves a URI reference against a base URI. Work-around for bug in java.net.URI ()

param
baseURI the base URI
param
reference the URI reference
return
the resulting URI

        if (baseURI == null) {
            throw new IllegalArgumentException("Base URI may nor be null");
        }
        if (reference == null) {
            throw new IllegalArgumentException("Reference URI may nor be null");
        }
        boolean emptyReference = reference.toString().length() == 0;
        if (emptyReference) {
            reference = URI.create("#");
        }
        URI resolved = baseURI.resolve(reference);
        if (emptyReference) {
            String resolvedString = resolved.toString();
            resolved = URI.create(resolvedString.substring(0,
                resolvedString.indexOf('#")));
        }
        return resolved;
    
public static java.net.URIrewriteURI(java.net.URI uri, org.apache.http.HttpHost target, boolean dropFragment)
A convenience method for creating a new {@link URI} whose scheme, host and port are taken from the target host, but whose path, query and fragment are taken from the existing URI. The fragment is only used if dropFragment is false.

param
uri Contains the path, query and fragment to use.
param
target Contains the scheme, host and port to use.
param
dropFragment True if the fragment should not be copied.
throws
URISyntaxException If the resulting URI is invalid.

        if (uri == null) {
            throw new IllegalArgumentException("URI may nor be null");
        }
        if (target != null) {
            return URIUtils.createURI(
                    target.getSchemeName(), 
                    target.getHostName(), 
                    target.getPort(), 
                    uri.getRawPath(), 
                    uri.getRawQuery(), 
                    dropFragment ? null : uri.getRawFragment());
        } else {
            return URIUtils.createURI(
                    null, 
                    null, 
                    -1, 
                    uri.getRawPath(), 
                    uri.getRawQuery(), 
                    dropFragment ? null : uri.getRawFragment());
        }
    
public static java.net.URIrewriteURI(java.net.URI uri, org.apache.http.HttpHost target)
A convenience method for {@link URIUtils#rewriteURI(URI, HttpHost, boolean)} that always keeps the fragment.

        return rewriteURI(uri, target, false);