Class UriResolver

java.lang.Object
org.apache.juneau.UriResolver

public class UriResolver extends Object
Class used to create absolute and root-relative URIs based on your current URI 'location' and rules about how to make such resolutions.

Combines a UriContext instance with rules for resolution (UriResolution and relativity (UriRelativity) to define simple resolve(Object) and append(Appendable, Object) methods.

Three special protocols are used to represent context-root-relative, servlet-relative, and request-path-relative URIs: "context:/", "servlet:/", and "request:/".

The following list shows the protocols of URLs that can be resolved with this class:

  • "foo://foo" - Absolute URI.
  • "/foo" - Root-relative URI.
  • "/" - Root URI.
  • "context:/foo" - Context-root-relative URI.
  • "context:/" - Context-root URI.
  • "servlet:/foo" - Servlet-path-relative URI.
  • "servlet:/" - Servlet-path URI.
  • "request:/foo" - Request-path-relative URI.
  • "request:/" - Request-path URI.
  • "foo" - Path-info-relative URI.
  • "" - Path-info URI.
See Also:
  • Constructor Details

    • UriResolver

      public UriResolver(UriResolution resolution, UriRelativity relativity, UriContext uriContext)
      Constructor.
      Parameters:
      resolution - Rule on how URIs should be resolved.
      relativity - Rule on what relative URIs are relative to.
      uriContext - Current URI context (i.e. the current URI 'location').
  • Method Details

    • of

      public static UriResolver of(UriResolution resolution, UriRelativity relativity, UriContext uriContext)
      Static creator.
      Parameters:
      resolution - Rule on how URIs should be resolved.
      relativity - Rule on what relative URIs are relative to.
      uriContext - Current URI context (i.e. the current URI 'location').
      Returns:
      A new UriResolver object.
    • resolve

      public String resolve(Object uri)
      Converts the specified URI to absolute form based on values in this context.
      Parameters:
      uri - The URI to convert to absolute form. Can be any of the following: URI can be any of the following forms:
      • "foo://foo" - Absolute URI.
      • "/foo" - Root-relative URI.
      • "/" - Root URI.
      • "context:/foo" - Context-root-relative URI.
      • "context:/" - Context-root URI.
      • "servlet:/foo" - Servlet-path-relative URI.
      • "servlet:/" - Servlet-path URI.
      • "request:/foo" - Request-path-relative URI.
      • "request:/" - Request-path URI.
      • "foo" - Path-info-relative URI.
      • "" - Path-info URI.
      Returns:
      The converted URI.
    • relativize

      public String relativize(Object relativeTo, Object uri)
      Relativizes a URI.

      Similar to URI.relativize(URI), except supports special protocols (e.g. "servlet:/") for both the relativeTo and uri parameters.

      For example, to relativize a URI to its servlet-relative form:

      // relativeUri == "path/foo" String relativeUri = resolver.relativize("servlet:/", "/context/servlet/path/foo");

      Parameters:
      relativeTo - The URI to relativize against.
      uri - The URI to relativize.
      Returns:
      The relativized URI.
    • append

      Same as resolve(Object) except appends result to the specified appendable.
      Parameters:
      a - The appendable to append the URL to.
      o - The URI to convert to absolute form.
      Returns:
      The same appendable passed in.