The URI Class

The URI class encapsulates a Uniform Resource Identifier (URI). URIs are similar to URLs.

URLs constitute a subset of URIs. A URI represents a standard way to identify a resource. A URL also describes how to access the resource.


import java.net.URI;
import java.net.URISyntaxException;

public class Main {
  public static void main(String[] args) throws NullPointerException, URISyntaxException {
    URI uri = new URI("http://www.example.org");
    System.out.println("URI      : " + uri);
    System.out.println("Raw Authority : " + uri.getRawAuthority());
    System.out.println("Raw Fragment : " + uri.getRawFragment());
    System.out.println("Fragment : " + uri.getFragment());
    System.out.println("Authority : " + uri.getAuthority());
    System.out.println("Authority : " + uri.getRawPath());
    System.out.println("RawQuery : " + uri.getRawQuery());
    System.out.println("RawSchemeSpecificPart : " + uri.getRawSchemeSpecificPart());
    System.out.println("RawUserInfo : " + uri.getRawUserInfo());

  }
}

Normalization

Normalization is the process of removing unnecessary "." and ".." path segments from a hierarchical URI's path component.

URI declares a URI normalize() method for normalizing a URI.


import java.net.URI;
import java.net.URISyntaxException;

public class Main {
  public static void main(String[] args) throws URISyntaxException {
    URI uri = new URI("http://java2s.com/./");
    System.out.println("Normalized URI = " + uri.normalize());
  }
}

Output:


Normalized URI = http://java2s.com/

Resolution

Resolution is the process of resolving one URI against another URI.

The resulting URI is constructed from components of both URIs. Check http://tools.ietf.org/html/rfc2396 for the details.

Resolution of both absolute and relative URIs, and of both absolute and relative paths in the case of hierarchical URIs, is supported.

URI declares


URI resolve(String str)

and


URI resolve(URI uri)

methods for resolving the original URI argument against the base URI contained in the current URI object.


import java.net.URI;
import java.net.URISyntaxException;

public class Main {
  public static void main(String[] args) throws URISyntaxException {
    URI uri = new URI("http://java2s.com/");
    System.out.println("Resolved URI = " + uri.resolve("/index.htm"));
  }
}

Output:


//Resolved URI = http://java2s.com/index.htm

Relativization

Relativization is the inverse of resolution.

URI declares a URI relativize(URI uri) method for relativizing its uri argument against the URI in the current URI object.


For any two normalized URI instances u and v, u.relativize(u.resolve(v)).equals(v) and u.resolve(u.relativize(v)).equals(v) evaluate to true.

relativize() performs relativization of its URI argument's URI against the URI in the URI object on which this method was called as follows:


import java.net.URI;
import java.net.URISyntaxException;

public class Main {
  public static void main(String[] args) throws URISyntaxException {
    URI uri1 = new URI("http://java2s.com/");
    URI uri2 = new URI("http://java2s.com/index.htm");
    System.out.println("Relativized URI = " + uri1.relativize(uri2));
  }
}

Output:


Relativized URI = index.htm
Home 
  Java Book 
    Networking  

URI:
  1. The URI Class
  2. URI: toURL()