Class Uris

java.lang.Object
com.sodius.oslc.core.util.Uris

public class Uris extends Object
Provides facilities to manipulate URIs.
Since:
1.4.0
  • Method Details

    • getHost

      public static URI getHost(HttpServletRequest request)
      Returns the URI that indicates the host of the request.

      The host URI is formed by:

      • the scheme (e.g. https),
      • the server name (the host name or IP address),
      • the port number (-1 if either 80 or 443 is used)
      Parameters:
      request - the servlet request.
      Returns:
      the URI that indicates the host of the request.
    • getHost

      public static URI getHost(URI uri)
      Returns the URI that indicates the host of the resource URI.

      The host URI is formed by:

      • the scheme (e.g. https),
      • the server name (the host name or IP address),
      • the port number (-1 if either 80 or 443 is used)
      Parameters:
      uri - the URI of a resource.
      Returns:
      the URI that indicates the host of the URI.
      Throws:
      IllegalArgumentException - if a host URI cannot be extracted from the given URI
    • getContext

      public static URI getContext(HttpServletRequest request)
      Returns the URI that indicates the context of the request.

      The context URI is formed by:

      • the scheme (e.g. https),
      • the server name (the host name or IP address),
      • the port number (-1 if either 80 or 443 is used)
      • the request context path (e.g. "/myapp")

      The context path starts with a '/' but does not end with a '/' character. For servlets in the default (root) context, the context path returned here is null. It is therefore safe in any case to append a subpath starting with a '/' character to the context URI.

      URIs obtained with this method should not be compared with those obtained with getContext(URI). Use isLocalResource(HttpServletRequest, URI) instead.

      Parameters:
      request - the servlet request.
      Returns:
      the URI that indicates the context of the request.
    • getContext

      public static URI getContext(URI uri)
      Returns the URI that indicates the context of the resource URI.

      The context URI is formed by:

      • the scheme (e.g. https),
      • the server name (the host name or IP address),
      • the port number (-1 if either 80 or 443 is used)
      • the context path (e.g. "/myapp")

      The context path starts with a '/' but does not end with a '/' character. If the URI path is null or '/', the context path returned here is null. It is therefore safe in any case to append a subpath starting with a '/' character to the context URI.

      URIs obtained with this method should not be compared with those obtained with getContext(HttpServletRequest). Use isLocalResource(HttpServletRequest, URI) instead.

      Parameters:
      uri - the URI of a resource.
      Returns:
      the URI that indicates the context of the URI.
      Throws:
      IllegalArgumentException - if a context URI cannot be extracted from the given URI
    • isLocalResource

      public static boolean isLocalResource(HttpServletRequest request, URI uri)
      Checks if a resource belongs to the local server, by comparing its context with the given request.

      A resource is considered local if:

      1. The scheme, server name and port number of the request and resource URI are the same.
      2. The context path of the request is empty, or is equal to the one of the resource URI.
      Note: With the resource URI, it isn't possible to know if an application was deployed on the root of a server, without any context. This is why we trust the context path of the request instead, and why we cannot compare contexts obtained with getContext(HttpServletRequest) and getContext(URI).
      Parameters:
      request - the servlet request.
      uri - the URI of a resource.
      Returns:
      true if the request and URI share the same context.
      Since:
      3.3.0
    • builder

      public static UriBuilder builder(URI uri)
      Creates a URI builder instance initialized from the specified uri. This method ensures the port number is set to -1 if the default ports are used (either 80 or 443).
      Parameters:
      uri - a resource URI
      Returns:
      a URI builder.
    • create

      public static URI create(String value) throws URISyntaxException
      Creates a URI from a String value form. Tries to 'normalize' the URI if the string contains spaces.
      Parameters:
      value - the String Url to normalize
      Returns:
      the normalized URI
      Throws:
      URISyntaxException - if an URI cannot be created from the String Url
      Since:
      2.0.0
    • encode

      public static String encode(URI uri)
      Translates the given URI into application/x-www-form-urlencodedformat using a specific encoding scheme.
      Parameters:
      uri - the URI to encode
      Returns:
      the translated String.
      Since:
      3.0.0
    • getQueryParams

      public static MultivaluedMap<String,String> getQueryParams(URI uri)
      Parses the query part of the given URI and returns a map of key/values pairs.
      Parameters:
      uri - a URI whose query parameters are to determine
      Returns:
      a map of each key and values found as query parameters in the URI
      Since:
      3.8.0
    • getQueryParam

      public static Optional<String> getQueryParam(URI uri, String name)
      Returns the first value for the given query parameter, if any. Rather use getQueryParams(URI) if multiple values may be assigned for the given parameter.

      Notes on empty values for query parameters:

      • If the query is "?param=", this method returns an optional holding an empty string.
      • If the query is "?param", this method returns an empty optional. In this case, rather use Map.containsKey(Object) on getQueryParams(URI) to determine if the query parameter is present.
      Parameters:
      uri - a URI whose query parameter is to determine
      name - a query parameter name to look for
      Returns:
      the first value of the query parameter, if found, an empty optional otherwise.
      Since:
      3.8.0
    • replaceQueryParam

      public static URI replaceQueryParam(URI uri, String name, String value)
      Replaces the value of a given query parameter in a URI.

      Implementation removes all existing parameters of the given name, if any, and then adds the name and value as last parameter of the URI.

      This method guarantees other parameters are left untouched and notably are not reordered, contrary to what implementations of UriBuilder may do. It shall be used when equality of URIs is important, as URI.equals(Object) is sensitive on order of query parameters.

      Parameters:
      uri - the URI in which a parameter is to replace
      name - the query parameter to replace
      value - the new value of the query parameter
      Returns:
      the URI with the replaced query parameter
      Since:
      3.8.0
    • removeQueryParam

      public static URI removeQueryParam(URI uri, String name)
      Removes query parameters of the given name in the URI, if any.

      This method guarantees other parameters are left untouched and notably are not reordered, contrary to what implementations of UriBuilder may do. It shall be used when equality of URIs is important, as URI.equals(Object) is sensitive on order of query parameters.

      Parameters:
      uri - the URI in which a parameter is to remove
      name - the query parameter to remove
      Returns:
      the URI with the removed query parameter
      Since:
      3.8.0
    • isFQDN

      public static boolean isFQDN(URI uri)
      Verifies whether a URI's hostname is fully qualified, ie it is made of words between dots.
      Parameters:
      uri - the URI to verifiy.
      Returns:
      true if it is a FQDN, false otherwise.
      Since:
      3.12.0
    • isFQDN

      public static boolean isFQDN(HttpServletRequest request)
      Verifies whether a HttpServletRequest's hostname is fully qualified, ie it is made of words between dots.
      Parameters:
      request - the HttpServletRequest to verifiy.
      Returns:
      true if it is a FQDN, false otherwise.
      Since:
      3.12.0