public class ServletUtils extends Object
Modifier and Type | Field and Description |
---|---|
static String |
ADMIN_SESSION
Name of the Session attribute to determine if it is an admin session.
|
static String |
ADMIN_URL
Fully qualified name of the main entry method to the administration.
|
Modifier and Type | Method and Description |
---|---|
static void |
addModificationInfo(HttpServletRequest req,
HttpServletResponse resp,
long lastModified,
Locale locale)
Adds some header fields to the response so we can determine modification on the next request.
|
static void |
addModificationInfo(HttpServletRequest req,
HttpServletResponse resp,
long lastModified,
Locale locale,
String ctx)
Adds some header fields to the response so we can determine modification on the next request
adds an ETag and a Last-Modified -Header |
static void |
checkAdminSession(HttpServletRequest req)
check if the session is an admin session and throw exception if it is not.
|
static void |
checkSessionIP(HttpServletRequest req,
HttpServletResponse res,
HttpSession s)
Checks if the remote address of the request matches the address in session attribute "ip".
|
static void |
checkURL(HttpServletRequest req,
String url)
Check the permissibility of the request with the URLChecker.
|
static Locale |
getBrowserLocale(HttpServletRequest req)
Read the locale from the browser.
|
static String |
getContextPath()
Returns the context path of the web application.
|
static String |
getParameterFromQuery(String queryString,
String paramName)
Parse a String in the same manner as a querystring of an HttpServletRequest would be parsed
and extract one parameter from it.
|
static String |
getParameterFromURL(String urlString,
String paramName)
Parse a String in the same manner as an URL of an HttpServletRequest would be parsed
and extract one parameter from it (all elements up to and including the first question mark are removed first).
|
static String |
getServerAddress(HttpServletRequest req)
Returns the a string of the follwing form: <protocol>://<hostname>:<port>/<contextpath>
|
static void |
init()
Initialize the ServletUtils.
|
static Boolean |
isAdminConnector(HttpServletRequest req)
This method return TRUE if admin port or address is specified and the request uses this connector.
|
static boolean |
isAdminSession(HttpServletRequest req)
check if the session is an admin session
|
static boolean |
isAllowed(HttpServletRequest req,
HttpServletResponse res,
List<String> allowedMethods,
String allowedMethodsString,
org.slf4j.Logger aLogger)
Determines if the method of an HttpRequest is allowed.
|
static boolean |
isModifiedSince(HttpServletRequest req,
HttpServletResponse resp,
long lastModified,
Locale l)
returns
true if the requested resource has been modified since the last request. |
static boolean |
isModifiedSince(HttpServletRequest req,
HttpServletResponse resp,
long lastModified,
Locale l,
String ctx)
returns
true if the requested resource has been modified since the last request the following methods are used: check the ETag -header
check the if-modified-since - header
|
static void |
logParameters(HttpServletRequest req,
org.slf4j.Logger aLogger)
Logs the parameters of an HttpRequest at log level INFO.
|
static HttpServletRequest |
logRequest(HttpServletRequest req,
HttpServletResponse res,
org.slf4j.Logger aLogger)
Log the parameters of an HttpRequest.
|
static void |
logRequestProcessed(long startTime,
HttpServletRequest req,
HttpServletResponse resp)
Logs the duration of an HttpRequest.
|
static void |
setExpiresImmediatelyForGuest(HttpServletRequest req,
HttpServletResponse res)
If no user is currently logged in, it's required, that some resources are not cached, so the user gets the new files as soon he loggs in.
|
static Locale |
setLocale(HttpServletRequest req)
Deprecated.
Use
setLocale(HttpServletRequest,Principal) instead |
static Locale |
setLocale(HttpServletRequest req,
Principal u)
Set the threadcontext Locale and return it.
|
static void |
setNoCache(HttpServletResponse res)
Sets appropriate headers on the response to avoid caching.
|
static void |
setNoTransform(HttpServletResponse res)
Sets appropriate headers on the response to avoid caching and to avoid intermediate transformations
(e.g. compression).
|
static void |
setPrivateCache(HttpServletResponse res)
Sets appropriate headers on the response to enable private caching
This must be called before the response is committed.
|
static void |
setThreadTimeZone(HttpServletRequest req,
HttpSession s,
TimeZone tz,
org.slf4j.Logger l)
Sets the time zone in the threadcontext from a number of sources.
|
static void |
writeHeaders(HttpServletResponse res,
Page page)
Write the headers of a page to the response.
|
static void |
writeToResp(HttpServletResponse res,
Page page)
Write a page to the response.
|
static void |
writeToResp(HttpServletResponse res,
String pageStr)
Write a string to the response.
|
public static final String ADMIN_SESSION
public static final String ADMIN_URL
public static void init()
public static String getContextPath()
public static String getServerAddress(HttpServletRequest req)
req
- the current requestpublic static boolean isAdminSession(HttpServletRequest req)
public static Boolean isAdminConnector(HttpServletRequest req)
public static void checkAdminSession(HttpServletRequest req)
public static HttpServletRequest logRequest(HttpServletRequest req, HttpServletResponse res, org.slf4j.Logger aLogger)
req
- the requestres
- the responseaLogger
- the loger to use. If it is null the logger of ServletUtils will be used.public static void logParameters(HttpServletRequest req, org.slf4j.Logger aLogger)
req
- the requestaLogger
- the logger to use.public static void logRequestProcessed(long startTime, HttpServletRequest req, HttpServletResponse resp)
HttpServletResponse.SC_INTERNAL_SERVER_ERROR
, is logged at level WARN,
else loglevel DEBUG is being used.startTime
- the start time of the request.req
- the requestresp
- the responsepublic static boolean isAllowed(HttpServletRequest req, HttpServletResponse res, List<String> allowedMethods, String allowedMethodsString, org.slf4j.Logger aLogger) throws IOException
HttpServletResponse.SC_METHOD_NOT_ALLOWED
.req
- the request to checkres
- the response to send the error backallowedMethods
- a list of Strings containing the permissible methods.allowedMethodsString
- the string of methods to be used foe the "allow" response header.aLogger
- the logger to use. Requests with non permissible methods are logger at level WARN.IOException
public static void checkURL(HttpServletRequest req, String url)
req
- the request as context for the checkurl
- the url to checkpublic static void checkSessionIP(HttpServletRequest req, HttpServletResponse res, HttpSession s)
req
- s
- public static void addModificationInfo(HttpServletRequest req, HttpServletResponse resp, long lastModified, Locale locale)
ETag
and a Last-Modified
-Headerreq
- requestresp
- responselastModified
- last modification timestamplocale
- the current localepublic static void addModificationInfo(HttpServletRequest req, HttpServletResponse resp, long lastModified, Locale locale, String ctx)
ETag
and a Last-Modified
-Headerreq
- requestresp
- responselastModified
- last modification timestamplocale
- the current localepublic static boolean isModifiedSince(HttpServletRequest req, HttpServletResponse resp, long lastModified, Locale l)
true
if the requested resource has been modified since the last request.
the following methods are used:
ETag
-headerif-modified-since
- headerreq
- requestresp
- responselastModified
- last modification timestampl
- the current localetrue
if the resource has been modified since the the last request, false
otherwisepublic static boolean isModifiedSince(HttpServletRequest req, HttpServletResponse resp, long lastModified, Locale l, String ctx)
true
if the requested resource has been modified since the last request ETag
-headerif-modified-since
- headerreq
- requestresp
- responselastModified
- last modification timestampl
- the current localectx
- an optional context-string to be appended to the etagtrue
if the resource has been modified since the the last request, false
otherwisepublic static void setExpiresImmediatelyForGuest(HttpServletRequest req, HttpServletResponse res)
req
- res
- public static void writeToResp(HttpServletResponse res, Page page)
res
- the response to write topage
- the page to write.public static void writeToResp(HttpServletResponse res, String pageStr)
res
- the response to write topageStr
- the string to writepublic static void writeHeaders(HttpServletResponse res, Page page)
res
- the response to write topage
- the page which headers are to be written@Deprecated public static Locale setLocale(HttpServletRequest req)
setLocale(HttpServletRequest,Principal)
insteadpublic static Locale setLocale(HttpServletRequest req, Principal u)
req
- the request to useu
- the user to use (may be nul)public static Locale getBrowserLocale(HttpServletRequest req)
Settings.getAvailableLocales()
).
If no match with the available locales could be made or in case of a missing request header, Locale.getDefault()
is returned.req
- the request to read the initial locale value frompublic static void setThreadTimeZone(HttpServletRequest req, HttpSession s, TimeZone tz, org.slf4j.Logger l) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
req
- the HttpServletRequests
- the HttpSessiontz
- an initial time zonel
- the logger to useIllegalAccessException
InvocationTargetException
NoSuchMethodException
public static void setNoCache(HttpServletResponse res)
res
- the HttpServletResponsepublic static void setNoTransform(HttpServletResponse res)
res
- the HttpServletResponsepublic static void setPrivateCache(HttpServletResponse res)
res
- the HttpServletResponsepublic static String getParameterFromURL(String urlString, String paramName)
urlString
- the string the parameter is to be extracted fromparamName
- the name of the parameter to extract from urlStringpublic static String getParameterFromQuery(String queryString, String paramName)
queryString
- the string the parameter is to be extracted fromparamName
- the name of the parameter to extract from queryString@enterprise 10.0.39049 Copyright © 2024 FREQUENTIS AG. All Rights Reserved.