Package de.xima.fc.utils
Class URLUtils
- java.lang.Object
-
- de.xima.fc.utils.URLUtils
-
public class URLUtils extends Object
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
URLUtils.DataUriBinary
POJO for the binary data from a data URI, with the content type.
-
Field Summary
Fields Modifier and Type Field Description protected static org.slf4j.Logger
LOG
-
Constructor Summary
Constructors Constructor Description URLUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static String
addMissingContextPath(String url, javax.servlet.http.HttpServletRequest request)
Prepends the request context path to the URL if it is missing.static String
addNonEmptyPathSegmentsToURL(String baseUri, UnaryOperator<String> sanitizer, Iterable<String> segments)
Encodes each given path segment and adds it to the base URL.static String
addNonEmptyPathSegmentsToURL(String baseUrl, UnaryOperator<String> sanitizer, String... segments)
Encodes each given path segment and adds it to the base URL.static String
addParameterToURL(String url, String paramName, String paramValue)
static String
addParameterToURL(String url, String paramName, String paramValue, boolean useEncoding)
static String
addQueryStringToUrl(String url, String queryString, Predicate<String> predicate)
Adds each query parameter from the query string to which the predicate applies to the given URL.static String
addQueryStringToUrl(String url, Map<String,String[]> queryStringParams, Predicate<String> predicate)
static String
addQueryStringToURL(String url, String queryString)
static String
addQueryStringToURL(String url, String queryString, de.xima.cmn.filter.IGenericFilter<String> predicate)
Deprecated.static String
addQueryStringToURL(String url, Map<String,String[]> queryStringParams)
Adds all query parameters to the given URL.static String
addQueryStringToURL(String url, Map<String,String[]> queryStringParams, de.xima.cmn.filter.IGenericFilter<String> predicate)
Deprecated.static String
changeScheme(String url, String newScheme)
Changes the scheme of a URL (such ashttps
) to the given schema.static List<String>
decodeUrlArrayToList(String[] urlArray)
Decodes a array of urls to a list of all decoded urls.static String
decrypt(String str)
Deprecated.Use other encryption methods such asEncryptionUtils
.static String
encodePathSegment(String value)
Encodes a path segment for a URI so that it can be used a path segment without any characters having a special meaning.static String
encrypt(String str)
Deprecated.Use other encryption methods such asEncryptionUtils
.static URLUtils.DataUriBinary
getDataUriBytes(String dataUri)
Fetches the binary data from a data URI.static String
getFullRequestUri(javax.servlet.http.HttpServletRequest request)
Similar toHttpServletRequest.getRequestURI()
, but also includes the query string parameters.static String
getFullRequestUrl(javax.servlet.http.HttpServletRequest request)
Similar toHttpServletRequest.getRequestURL()
, but also includes the query string parameters.static String
getMultipartFormDataSafeParameter(javax.servlet.http.HttpServletRequest req, String name)
Gets the given parameter for the given request.static Map<String,String[]>
getMultipartFormDataSafeParameterMap(javax.servlet.http.HttpServletRequest req)
static String
getUrlParameter(javax.servlet.http.HttpServletRequest req, String name)
Get the URL parameter value of the given request.static Map<String,String[]>
getUrlParameters(String queryString)
static boolean
isContainsUrlParameter(String url, String paramName)
static boolean
isRelativeUrl(String url)
Checks if the given URL is a relative URL.static List<String>
normalizePath(Iterable<String> pathSegments, boolean allowNavigationOutsideRoot)
Given a list of path segments, normalizes the path by resolving.
and..
navigators.static List<String>
normalizePath(String[] pathSegments, boolean allowNavigationOutsideRoot)
Given a list of path segments, normalizes the path by resolving.
and..
navigators.static String
removeAllParametersAndFragment(String url)
static String
removeParameter(String url, String paramName)
static String
removeParameter(String url, String paramName, boolean useEncoding)
static String
setParameter(String url, String paramName, String paramValue)
static String
setParameter(String url, String paramName, String paramValue, boolean useEncoding)
static List<String>
splitPath(String path, boolean allowNavigationOutsideRoot)
Splits a path into its path segments and resolves path navigators.
-
-
-
Method Detail
-
addNonEmptyPathSegmentsToURL
public static String addNonEmptyPathSegmentsToURL(String baseUri, UnaryOperator<String> sanitizer, Iterable<String> segments) throws InvalidUriPathSegmentNameException
Encodes each given path segment and adds it to the base URL. Empty segments are ignored.- Parameters:
baseUri
- Base URL to process.segments
- Segments to add.sanitizer
- Optional sanitizer applied to each segment before encoding.- Returns:
- The base UL combined with the given segments.
- Throws:
InvalidUriPathSegmentNameException
- When a segment cannot be encoded, seeencodePathSegment(String)
.
-
addMissingContextPath
public static String addMissingContextPath(String url, javax.servlet.http.HttpServletRequest request)
Prepends the request context path to the URL if it is missing. This is only done for relative URLs.- Parameters:
url
- to prepend the missing context path.request
- holding the context path.- Returns:
- the URL prepended with the missing context path if it was missing and if the URL is relative. Returns the input URL unchanged otherwise.
-
addNonEmptyPathSegmentsToURL
public static String addNonEmptyPathSegmentsToURL(String baseUrl, UnaryOperator<String> sanitizer, String... segments) throws InvalidUriPathSegmentNameException
Encodes each given path segment and adds it to the base URL. Empty segments are ignored.- Parameters:
baseUrl
- Base URL to process.segments
- Segments to add.sanitizer
- Optional sanitizer applied to each segment before encoding.- Returns:
- The base UL combined with the given segments.
- Throws:
InvalidUriPathSegmentNameException
- When a segment cannot be encoded, seeencodePathSegment(String)
.
-
addParameterToURL
public static String addParameterToURL(String url, String paramName, String paramValue)
-
addParameterToURL
public static String addParameterToURL(String url, String paramName, String paramValue, boolean useEncoding)
-
addQueryStringToUrl
public static String addQueryStringToUrl(String url, Map<String,String[]> queryStringParams, Predicate<String> predicate)
- Parameters:
url
- URL to process.queryStringParams
- Query parameters to add.predicate
- Predicate that checks whether the parameter should be added. Only parameters to which the predicate applies are added.- Returns:
- The given URL with the parameters to which the predicate applies..
-
addQueryStringToUrl
public static String addQueryStringToUrl(String url, String queryString, Predicate<String> predicate)
Adds each query parameter from the query string to which the predicate applies to the given URL.- Parameters:
url
- URL to process.queryString
- A query string to add to the URL, such as?foo=bar&hello=world
.predicate
- Predicate that checks whether the parameter should be added. Only parameters to which the predicate applies are added.- Returns:
- The given URL with the query parameters to which the predicate applies added.
-
addQueryStringToURL
public static String addQueryStringToURL(String url, Map<String,String[]> queryStringParams)
Adds all query parameters to the given URL.- Parameters:
url
- URL to process.queryStringParams
- Query parameters to add. applies are added.- Returns:
- The given URL with the parameters added.
-
addQueryStringToURL
@Deprecated public static String addQueryStringToURL(String url, Map<String,String[]> queryStringParams, de.xima.cmn.filter.IGenericFilter<String> predicate)
Deprecated.Adds all query parameters to the given URL.- Parameters:
url
- URL to process.queryStringParams
- Query parameters to add.predicate
- Predicate that checks whether the parameter should be added. Only parameters to which the predicate applies are added.- Returns:
- The given URL with the parameters to which the predicate applies.
-
addQueryStringToURL
@Deprecated public static String addQueryStringToURL(String url, String queryString, de.xima.cmn.filter.IGenericFilter<String> predicate)
Deprecated.Adds each query parameter from the query string to which the predicate applies to the given URL.- Parameters:
url
- URL to process.queryString
- A query string to add to the URL, such as?foo=bar&hello=world
.predicate
- Predicate that checks whether the parameter should be added. Only parameters to which the predicate applies are added.- Returns:
- The given URL with the query parameters to which the predicate applies added.
-
changeScheme
public static String changeScheme(String url, String newScheme)
Changes the scheme of a URL (such ashttps
) to the given schema.- Parameters:
url
- URL to modify.newScheme
- New schema for the URL.- Returns:
- The URL with the new scheme.
-
decodeUrlArrayToList
public static List<String> decodeUrlArrayToList(String[] urlArray)
Decodes a array of urls to a list of all decoded urls.- Parameters:
urlArray
- A array of urls.- Returns:
- decodedUrlList Decoded url list.
-
decrypt
@Deprecated public static String decrypt(String str)
Deprecated.Use other encryption methods such asEncryptionUtils
.Methode zum Entschlüsseln eines Strings- Parameters:
str
- String to decrypt- Returns:
- the decrypted String
-
encodePathSegment
public static String encodePathSegment(String value) throws InvalidUriPathSegmentNameException
Encodes a path segment for a URI so that it can be used a path segment without any characters having a special meaning.- Parameters:
value
- A value to encode.- Returns:
- The given segment with URL encoding applied.
- Throws:
InvalidUriPathSegmentNameException
- When he value cannot be encoded, e.g..
and..
always represent path navigation.
-
encrypt
@Deprecated public static String encrypt(String str)
Deprecated.Use other encryption methods such asEncryptionUtils
.Methode zum Verschlüsseln eines Strings- Parameters:
str
- String to encrypt- Returns:
- the encrypted String
-
isRelativeUrl
public static boolean isRelativeUrl(String url)
Checks if the given URL is a relative URL.- Parameters:
url
- to check.- Returns:
true
if the given URL is relative andfalse
otherwise.
-
isContainsUrlParameter
public static boolean isContainsUrlParameter(String url, String paramName)
- Parameters:
url
- URL to check, may be absolute or relative.paramName
- Name of a parameter to check.- Returns:
true
if the URL contains at least one parameter with the given name.false
when the URL does not contain the parameter or when it is an invalid URL.
-
getDataUriBytes
public static URLUtils.DataUriBinary getDataUriBytes(String dataUri)
Fetches the binary data from a data URI. This will always returns the plain bytes, regardless of the encoding specified by the data URI.- Parameters:
dataUri
- A data URI to parse.- Returns:
- The binary data contained in the data URI.
-
getFullRequestUri
public static String getFullRequestUri(javax.servlet.http.HttpServletRequest request)
Similar toHttpServletRequest.getRequestURI()
, but also includes the query string parameters.- Parameters:
request
- A servlet request to process.- Returns:
- The reconstructed full request URI.
-
getFullRequestUrl
public static String getFullRequestUrl(javax.servlet.http.HttpServletRequest request)
Similar toHttpServletRequest.getRequestURL()
, but also includes the query string parameters.- Parameters:
request
- A servlet request to process.- Returns:
- The reconstructed full request URL.
-
getUrlParameters
public static Map<String,String[]> getUrlParameters(String queryString) throws UnsupportedEncodingException
- Throws:
UnsupportedEncodingException
-
normalizePath
public static List<String> normalizePath(Iterable<String> pathSegments, boolean allowNavigationOutsideRoot)
Given a list of path segments, normalizes the path by resolving.
and..
navigators.normalizePath(null, false) --> (empty list) normalizePath(null, true) --> (empty list) normalizePath(asList("", false)) --> (empty list) normalizePath(asList("", true)) --> (empty list) normalizePath(asList("foo", "", "", "bar", false)) --> "foo", "bar" normalizePath(asList("foo", "bar", false)) --> "foo", "bar" normalizePath(asList("foo", bar", true)) --> "foo", "bar" normalizePath(asList("foo", ".", "bar", "baz", false)) --> "foo", "bar", "baz" normalizePath(asList("foo", ".", "bar", "baz", true)) --> "foo", "bar", "baz" normalizePath(asList("foo", "..", "bar", "baz", false)) --> "foo", "baz" normalizePath(asList("foo", "..", "bar", "baz", true)) --> "foo", "baz" normalizePath(asList("foo", "bar", "..", "..", "..", "baz", false)) --> "..", "baz" normalizePath(asList("foo", "bar", "..", "..", "..", "baz", true)) --> "baz" normalizePath(asList("..", "..", false)) --> (empty list) normalizePath(asList("..", "..", true)) --> "..", ".."
- Parameters:
pathSegments
- Path segments to process.allowNavigationOutsideRoot
- Whentrue
, allows the resulting path segments to navigate outside the root dir.- Returns:
- The normalized list of segments.
-
normalizePath
public static List<String> normalizePath(String[] pathSegments, boolean allowNavigationOutsideRoot)
Given a list of path segments, normalizes the path by resolving.
and..
navigators.normalizePath(null, false) --> (empty list) normalizePath(null, true) --> (empty list) normalizePath(asList("", false)) --> (empty list) normalizePath(asList("", true)) --> (empty list) normalizePath(asList("foo", "bar", false)) --> "foo", "bar" normalizePath(asList("foo", bar", true)) --> "foo", "bar" normalizePath(asList("foo", ".", "bar", "baz", false)) --> "foo", "bar", "baz" normalizePath(asList("foo", ".", "bar", "baz", true)) --> "foo", "bar", "baz" normalizePath(asList("foo", "..", "bar", "baz", false)) --> "foo", "baz" normalizePath(asList("foo", "..", "bar", "baz", true)) --> "foo", "baz" normalizePath(asList("foo", "bar", "..", "..", "..", "baz", false)) --> "..", "baz" normalizePath(asList("foo", "bar", "..", "..", "..", "baz", true)) --> "baz" normalizePath(asList("..", "..", false)) --> (empty list) normalizePath(asList("..", "..", true)) --> "..", ".."
- Parameters:
pathSegments
- Path segments to process.allowNavigationOutsideRoot
- Whentrue
, allows the resulting path segments to navigate outside the root dir.- Returns:
- The normalized list of segments.
-
removeAllParametersAndFragment
public static String removeAllParametersAndFragment(String url)
- Parameters:
url
- A URL to process.- Returns:
- The given URL with the fragment and all query parameters removed.
-
removeParameter
public static String removeParameter(String url, String paramName, boolean useEncoding)
-
setParameter
public static String setParameter(String url, String paramName, String paramValue, boolean useEncoding)
-
splitPath
public static List<String> splitPath(String path, boolean allowNavigationOutsideRoot)
Splits a path into its path segments and resolves path navigators.splitPath(null, false) --> (empty list) splitPath(null, true) --> (empty list) splitPath("", false) --> (empty list) splitPath("", true) --> (empty list) splitPath("foo/bar", false) --> "foo", "bar" splitPath("foo/bar", true) --> "foo", "bar" splitPath("foo/./bar/baz", false) --> "foo", "bar", "baz" splitPath("foo/./bar/baz", true) --> "foo", "bar", "baz" splitPath("foo/../bar/baz", false) --> "foo", "baz" splitPath("foo/../bar/baz", true) --> "foo", "baz" splitPath("foo/bar/../../../baz", false) --> "..", "baz" splitPath("foo/bar/../../../baz", true) --> "baz" splitPath("../..", false) --> (empty list) splitPath("../..", true) --> "..", ".."
- Parameters:
path
- Path to split.allowNavigationOutsideRoot
- Whentrue
, allows the resulting path segments to navigate outside the root dir.- Returns:
- A list of path segments from the given path.
-
getMultipartFormDataSafeParameter
public static String getMultipartFormDataSafeParameter(javax.servlet.http.HttpServletRequest req, String name)
Gets the given parameter for the given request. If the request is amultipart/form-data
request then the parameters will not be retrieved by the accessing therequests parameter
directly since this would read the body of themultipart/form-data
request and thereby consuming its input stream. This would be a problem if the body needs to be accessed at a later time. Instead the parameter will be retrieved by accessing the URL rewrite parameters and the requests URL parameters (Query string). Use this method if a parameter ofmutlipart/form-data
requests need to be accessed before the actual body of the request is accessed, e.g. if parameters need to be accessed before the file uploads of the request are processed.- Parameters:
req
- to get the parameter from.name
- the name of the parameter to get from the request.- Returns:
- the value of the request parameter if present.
-
getMultipartFormDataSafeParameterMap
public static Map<String,String[]> getMultipartFormDataSafeParameterMap(javax.servlet.http.HttpServletRequest req)
-
getUrlParameter
public static String getUrlParameter(javax.servlet.http.HttpServletRequest req, String name)
Get the URL parameter value of the given request.- Parameters:
req
- to get the URL parameter fromname
- the name of the URL parameter to get from the request.- Returns:
- the value of the URL parameter if present.
-
-