public abstract class RequestHelper extends Object
Constructor and Description |
---|
RequestHelper() |
Modifier and Type | Method and Description |
---|---|
static String |
describeRequest(javax.servlet.ServletRequest req)
Return a string describing a servlet request for logging purposes.
|
static Boolean |
getBooleanParameter(javax.servlet.http.HttpServletRequest req,
String key)
Get the value of the servlet parameter
key and parse it
using Boolean.parseBoolean(String) if a value for this servlet parameter
has been found. |
static Double |
getDoubleParameter(javax.servlet.http.HttpServletRequest req,
String key)
Get the value of the servlet parameter
key and parse it
using Double.parseDouble(String) if a value for this servlet parameter
has been found. |
static ForwardedInfo |
getFirstForwarded(javax.servlet.http.HttpServletRequest request)
Get the forwarded information of the first reverse proxy either received by
a new-style
Forwarded header or a legacy X-Forwarded-*
header. |
static List<ForwardedInfo> |
getForwarded(javax.servlet.http.HttpServletRequest request)
Get a list of forwarded information keys to the list of forwarded
infos from a new-style
RFC 7239 conforming
Forwarded header. |
static Integer |
getIntegerParameter(javax.servlet.http.HttpServletRequest req,
String key)
Get the value of the servlet parameter
key and parse it
using Integer.parseInt(String) if a value for this servlet parameter
has been found. |
static Long |
getLongParameter(javax.servlet.http.HttpServletRequest req,
String key)
Get the value of the servlet parameter
key and parse it
using Long.parseLong(String) if a value for this servlet parameter
has been found. |
static URI |
getOriginalRequestUri(javax.servlet.http.HttpServletRequest request)
Return the original request URI as seem by the client on a
best effort basis.
|
static String |
getRealRemoteIP(javax.servlet.http.HttpServletRequest request)
Get the remote IP address of the client taking the
X-Forwarded-for header into account. |
static String |
getRequestUrl(javax.servlet.http.HttpServletRequest req)
The the request URL without host, port and protocol.
|
static String |
getResolvedPath(javax.servlet.http.HttpServletRequest req)
Get the server-side path which may have been resolved by a delegating
servlet.
|
static PasswordAuthentication |
parseBasicAuthenticationHeader(javax.servlet.http.HttpServletRequest req)
Parse the
Authorization request header and extract
HTTP basic authentication credentials. |
static ForwardedInfo |
parseFirstForwarded(String forwarded)
Parse a a new-style
RFC 7239 conforming
Forwarded header and return the first reverse proxy info,
which reflects the original client's request. |
static void |
parseForwarded(List<ForwardedInfo> infos,
String forwarded)
Parse a a new-style
RFC 7239 conforming
Forwarded header and append each reverse proxy info to
the given list of forwarded infos. |
static void |
setResolvedPath(javax.servlet.http.HttpServletRequest req,
String resolvedPath) |
public static void setResolvedPath(javax.servlet.http.HttpServletRequest req, String resolvedPath)
req
- The request to inform about path resolution.resolvedPath
- The resolved server-side path to be propagted to a delegate
servlet.public static String getRequestUrl(javax.servlet.http.HttpServletRequest req)
req
- The servlet request to get the URI from.HttpServletRequest.getRequestURI()
with
the value of HttpServletRequest.getQueryString()
appended alongside with a question mark,
if the query string is not null
.public static String describeRequest(javax.servlet.ServletRequest req)
req
- The servlet request to describe.public static String getResolvedPath(javax.servlet.http.HttpServletRequest req)
req
- The request to query.RESOLVED_PATH_ATTR
attribute
or The result of HttpServletRequest.getPathInfo()
, if
no such attribute is present.public static String getRealRemoteIP(javax.servlet.http.HttpServletRequest request)
Get the remote IP address of the client taking the
X-Forwarded-for
header into account.
If no X-Forwarded-for
header is present,
the value of ServletRequest.getRemoteAddr()
is returned.
If a X-Forwarded-for
header is present,
the leftmost value in the comma-separated list of
X-Forwarded-for
-addresses is returned.
The rationale for returning the leftmost address is, that
this is the original address of the client.
request
- A HTTP servlet request.public static void parseForwarded(List<ForwardedInfo> infos, String forwarded) throws ParseException
Forwarded
header and append each reverse proxy info to
the given list of forwarded infos.infos
- The list of forward info to append new infos to.forwarded
- The value of the HTTP Forwarded
header.ParseException
- if the header does not comply to the specification.public static ForwardedInfo parseFirstForwarded(String forwarded) throws ParseException
Forwarded
header and return the first reverse proxy info,
which reflects the original client's request.forwarded
- The value of the HTTP Forwarded
header.null
, if the header
was effectively empty.ParseException
- if the header does not comply to the specification.public static List<ForwardedInfo> getForwarded(javax.servlet.http.HttpServletRequest request) throws ParseException
Forwarded
header.request
- The HTTP request to inspect.Forwarded
header is found, null
is returned.ParseException
- if the header does not comply to the specification.public static ForwardedInfo getFirstForwarded(javax.servlet.http.HttpServletRequest request) throws ParseException
Forwarded
header or a legacy X-Forwarded-*
header.request
- The HTTP request to inspect.null
,
if the request did not pass any reverse proxy.ParseException
- If a Forwarded
header did not comply to the specification.public static URI getOriginalRequestUri(javax.servlet.http.HttpServletRequest request) throws ParseException, URISyntaxException
request
- The servlet request to inspect.Forwarded
, X_Fowared-*
and Host
header, in order.ParseException
URISyntaxException
public static Double getDoubleParameter(javax.servlet.http.HttpServletRequest req, String key) throws NumberFormatException
Get the value of the servlet parameter key
and parse it
using Double.parseDouble(String)
if a value for this servlet parameter
has been found.
Warning:
For POST requests with a Content-Type application/x-www-form-urlencoded
this method triggers a parsing of the all posted query parameters.
To avoid this, use UrlHelper.parseQuery(String)
together with
UrlHelper.getDoubleParameter(java.util.Map, String)
.
req
- The HTTP servlet request to query.key
- The key of the double parameter to parse.null
if no request parameter with name key
is given.NumberFormatException
- If the value of the servlet parameter is
not a double value as per Double.parseDouble(String)
.public static Long getLongParameter(javax.servlet.http.HttpServletRequest req, String key) throws NumberFormatException
Get the value of the servlet parameter key
and parse it
using Long.parseLong(String)
if a value for this servlet parameter
has been found.
Warning:
For POST requests with a Content-Type application/x-www-form-urlencoded
this method triggers a parsing of the all posted query parameters.
To avoid this, use UrlHelper.parseQuery(String)
together with
UrlHelper.getLongParameter(java.util.Map, String)
.
req
- The HTTP servlet request to query.key
- The key of the long parameter to parse.null
if no request parameter with name key
is given.NumberFormatException
- If the value of the servlet parameter is
not a long value as per Long.parseLong(String)
.public static Integer getIntegerParameter(javax.servlet.http.HttpServletRequest req, String key) throws NumberFormatException
Get the value of the servlet parameter key
and parse it
using Integer.parseInt(String)
if a value for this servlet parameter
has been found.
Warning:
For POST requests with a Content-Type application/x-www-form-urlencoded
this method triggers a parsing of the all posted query parameters.
To avoid this, use UrlHelper.parseQuery(String)
together with
UrlHelper.getIntegerParameter(java.util.Map, String)
.
req
- The HTTP servlet request to query.key
- The key of the integer parameter to parse.null
if no request parameter with name key
is given.NumberFormatException
- If the value of the servlet parameter is
not an integer value as per Integer.parseInt(String)
.public static Boolean getBooleanParameter(javax.servlet.http.HttpServletRequest req, String key)
Get the value of the servlet parameter key
and parse it
using Boolean.parseBoolean(String)
if a value for this servlet parameter
has been found.
Warning:
For POST requests with a Content-Type application/x-www-form-urlencoded
this method triggers a parsing of the all posted query parameters.
To avoid this, use UrlHelper.parseQuery(String)
together with
UrlHelper.getBooleanParameter(java.util.Map, String)
.
req
- The HTTP servlet request to query.key
- The key of the boolean parameter to parse.null
if no request parameter with name key
is given.NumberFormatException
- If the value of the servlet parameter is
not an integer value as per Boolean.parseBoolean(String)
.public static final PasswordAuthentication parseBasicAuthenticationHeader(javax.servlet.http.HttpServletRequest req)
Authorization
request header and extract
HTTP basic authentication credentials.req
- The request to inspect.Authorization: Basic
or null
, if no Authorization
header is given or the header referred to another authorization
scheme.SecurityException
- When the Authorization
header
is malformed.Copyright © 2018 Clazzes.org. All rights reserved.