Difference between opaque and hierarchical URI?

What is the difference between an opaque and hierarchical URI in the context of java networking ?

+5
source share
2 answers

A typical example of an opaque uri is the mailto: a@b.com URL mailto: a@b.com . They differ from the hierarchical uri in that they do not describe the path to the resource.

Therefore, an opaque Uri returns null for getPath .

Some examples:

 public static void main(String[] args) { printUriInfo(URI.create("mailto: a@b.com ")); printUriInfo(URI.create("http://example.com")); printUriInfo(URI.create("http://example.com/path")); printUriInfo(URI.create("scheme://example.com")); printUriInfo(URI.create("scheme:example.com")); printUriInfo(URI.create("scheme:example.com/path")); printUriInfo(URI.create("path")); printUriInfo(URI.create("/path")); } private static void printUriInfo(URI uri) { System.out.println(String.format("Uri [%s]", uri)); System.out.println(String.format(" is %sopaque", uri.isOpaque() ? "" : "not ")); System.out.println(String.format(" is %sabsolute", uri.isAbsolute() ? "" : "not ")); System.out.println(String.format(" Path [%s]", uri.getPath())); } 

Print

 Uri [mailto: a@b.com ] is opaque is absolute Path [null] Uri [http://example.com] is not opaque is absolute Path [] Uri [http://example.com/path] is not opaque is absolute Path [/path] Uri [scheme://example.com] is not opaque is absolute Path [] Uri [scheme:example.com] is opaque is absolute Path [null] Uri [scheme:example.com/path] is opaque is absolute Path [null] Uri [path] is not opaque is not absolute Path [path] Uri [/path] is not opaque is not absolute Path [/path] 
+9
source

This is explained by javadoc for the URI class:

"A URI is opaque if and only if it is absolute, and its part, depending on the scheme, does not begin with a slash ('/'). A transparent URI has a scheme, a part specific to the scheme, and possibly a fragment, all other components undefined. "

The "components" to which it refers are the values ​​returned by the various URI getters.

In addition, the “difference” includes the inherent difference between opaque and hierarchical URIs in accordance with the relevant specifications; eg.

These differences are by no means Java specific.

+2
source

All Articles