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 Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static UriBuilder
builder(URI uri)
Creates a URI builder instance initialized from the specified uri.static URI
create(String value)
Creates a URI from a String value form.static String
encode(URI uri)
Translates the given URI intoapplication/x-www-form-urlencodedformat
using a specific encoding scheme.static URI
getContext(URI uri)
Returns the URI that indicates the context of the resource URI.static URI
getContext(HttpServletRequest request)
Returns the URI that indicates the context of the request.static URI
getHost(URI uri)
Returns the URI that indicates the context of the resource URI.static URI
getHost(HttpServletRequest request)
Returns the URI that indicates the host of the request.static Optional<String>
getQueryParam(URI uri, String name)
Returns the first value for the given query parameter, if any.static MultivaluedMap<String,String>
getQueryParams(URI uri)
Parses the query part of the given URI and returns a map of key/values pairs.static boolean
isLocalResource(HttpServletRequest request, URI uri)
Checks if a resource belongs to the local server, by comparing its context with the given request.static URI
removeQueryParam(URI uri, String name)
Removes query parameters of the given name in the URI, if any.static URI
replaceQueryParam(URI uri, String name, String value)
Replaces the value of a given query parameter in a URI.
-
-
-
Method Detail
-
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.
- the scheme (e.g.
-
getHost
public static URI getHost(URI uri)
Returns the URI that indicates the context 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.
- the scheme (e.g.
-
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 isnull
. 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)
. UseisLocalResource(HttpServletRequest, URI)
instead.- Parameters:
request
- the servlet request.- Returns:
- the URI that indicates the context of the request.
- the scheme (e.g.
-
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 isnull
or'/'
, the context path returned here isnull
. 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)
. UseisLocalResource(HttpServletRequest, URI)
instead.- Parameters:
uri
- the URI of a resource.- Returns:
- the URI that indicates the context of the URI.
- the scheme (e.g.
-
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:
- The scheme, server name and port number of the request and resource URI are the same.
- The context path of the request is empty, or is equal to the one of the resource URI.
getContext(HttpServletRequest)
andgetContext(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 intoapplication/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 usegetQueryParams(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 useMap.containsKey(Object)
ongetQueryParams(URI)
to determine if the query parameter is present.
- Parameters:
uri
- a URI whose query parameter is to determinename
- 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
- If the query is
-
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, asURI.equals(Object)
is sensitive on order of query parameters.- Parameters:
uri
- the URI in which a parameter is to replacename
- the query parameter to replacevalue
- 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, asURI.equals(Object)
is sensitive on order of query parameters.- Parameters:
uri
- the URI in which a parameter is to removename
- the query parameter to remove- Returns:
- the URI with the removed query parameter
- Since:
- 3.8.0
-
-