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 boolean
isLocalResource(HttpServletRequest request, URI uri)
Checks if a resource belongs to the local server, by comparing its context with the given request.
-
-
-
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
-
-