Class SmallRequestUtils
HttpServletRequest
manipulation.- Author:
- Matthew Tropiano
-
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]
getByteData
(javax.servlet.http.HttpServletRequest request) Gets the binary payload of a request.static <R> R
getContentData
(javax.servlet.http.HttpServletRequest request, Class<R> type) Get content data from the request and attempts to return it as the desired type.static String
getFileName
(javax.servlet.http.HttpServletRequest request) Get the base file name parsed out of the request URI.static MultipartParser
getMultipartParser
(javax.servlet.http.HttpServletRequest request) Returns the appropriate multipart parser for a request.static boolean
getParameterBoolean
(javax.servlet.http.HttpServletRequest request, String paramName) Convenience method that callsrequest.getParameter(paramName)
and returns true or false.static boolean
getParameterBoolean
(javax.servlet.http.HttpServletRequest request, String paramName, String trueValue) Convenience method that callsrequest.getParameter(paramName)
and returns true or false, if the string found in the request evaluates totrueValue
, case-insensitively.static byte
getParameterByte
(javax.servlet.http.HttpServletRequest request, String paramName) Convenience method that callsrequest.getParameter(paramName)
.static byte
getParameterByte
(javax.servlet.http.HttpServletRequest request, String paramName, byte def) Convenience method that callsrequest.getParameter(paramName)
.static char
getParameterChar
(javax.servlet.http.HttpServletRequest request, String paramName) Convenience method that callsrequest.getParameter(paramName)
.static char
getParameterChar
(javax.servlet.http.HttpServletRequest request, String paramName, char def) Convenience method that callsrequest.getParameter(paramName)
.static Date
getParameterDate
(javax.servlet.http.HttpServletRequest request, String paramName, String formatString) Method that parses a parameter as a date.static double
getParameterDouble
(javax.servlet.http.HttpServletRequest request, String paramName) Convenience method that callsrequest.getParameter(paramName)
.static double
getParameterDouble
(javax.servlet.http.HttpServletRequest request, String paramName, double def) Convenience method that callsrequest.getParameter(paramName)
.static boolean
getParameterExist
(javax.servlet.http.HttpServletRequest request, String paramName) Convenience method that checks if a parameter exists on a request.static float
getParameterFloat
(javax.servlet.http.HttpServletRequest request, String paramName) Convenience method that callsrequest.getParameter(paramName)
.static float
getParameterFloat
(javax.servlet.http.HttpServletRequest request, String paramName, float def) Convenience method that callsrequest.getParameter(paramName)
.static int
getParameterInt
(javax.servlet.http.HttpServletRequest request, String paramName) Convenience method that callsrequest.getParameter(paramName)
.static int
getParameterInt
(javax.servlet.http.HttpServletRequest request, String paramName, int def) Convenience method that callsrequest.getParameter(paramName)
.static long
getParameterLong
(javax.servlet.http.HttpServletRequest request, String paramName) Convenience method that callsrequest.getParameter(paramName)
.static long
getParameterLong
(javax.servlet.http.HttpServletRequest request, String paramName, long def) Convenience method that callsrequest.getParameter(paramName)
.getParameters
(javax.servlet.http.HttpServletRequest request, String prefix) Gets a group of parameters that start with a specific prefix.static short
getParameterShort
(javax.servlet.http.HttpServletRequest request, String paramName) Convenience method that callsrequest.getParameter(paramName)
.static short
getParameterShort
(javax.servlet.http.HttpServletRequest request, String paramName, short def) Convenience method that callsrequest.getParameter(paramName)
.static String
getParameterString
(javax.servlet.http.HttpServletRequest request, String paramName) Convenience method that callsrequest.getParameter(paramName)
.static String
getParameterString
(javax.servlet.http.HttpServletRequest request, String paramName, String def) Convenience method that callsrequest.getParameter(paramName)
.static String
getPath
(javax.servlet.http.HttpServletRequest request) Get the path parsed out of the request URI.static String
getPathExtension
(javax.servlet.http.HttpServletRequest request) Get the path file extension parsed out of the request URI (includes the dot).static <T> T
getRequestBean
(javax.servlet.http.HttpServletRequest request, Class<T> clazz) Gets and auto-casts an object bean stored at the request level.static <T> T
getRequestBean
(javax.servlet.http.HttpServletRequest request, Class<T> clazz, String name) Gets and auto-casts an object bean stored at the request level.static <T> T
getRequestBean
(javax.servlet.http.HttpServletRequest request, Class<T> clazz, String name, boolean create) Gets and auto-casts an object bean stored at the request level.static <T> T
getSessionBean
(javax.servlet.http.HttpServletRequest request, Class<T> clazz) Gets and auto-casts an object bean stored at the session level.static <T> T
getSessionBean
(javax.servlet.http.HttpServletRequest request, Class<T> clazz, String name) Gets and auto-casts an object bean stored at the session level.static <T> T
getSessionBean
(javax.servlet.http.HttpServletRequest request, Class<T> clazz, String name, boolean create) Gets and auto-casts an object bean stored at the session level.static String
getSessionId
(javax.servlet.http.HttpServletRequest request) Gets the current connection's session id.static SmallResponse
getSmallResponse
(javax.servlet.http.HttpServletRequest request) Convenience method that gets the SmallResponse on the request.static String
getStringData
(javax.servlet.http.HttpServletRequest request) Gets the string data of a request.static boolean
isFormEncoded
(javax.servlet.http.HttpServletRequest request) Checks if this request is a regular form POST.static boolean
isJSON
(javax.servlet.http.HttpServletRequest request) Checks if the request is JSON-formatted.static boolean
isXML
(javax.servlet.http.HttpServletRequest request) Checks if the request is XML-formatted.static <T> T
setModelFields
(javax.servlet.http.HttpServletRequest request, Class<T> type) Sets the fields on a new instance of an object, using its public fields and setters, using the servlet request as a source.static <T> T
setModelFields
(javax.servlet.http.HttpServletRequest request, T target) Sets the fields on an object, using its public fields and setters, using the servlet request as a source.
-
Method Details
-
isJSON
public static boolean isJSON(javax.servlet.http.HttpServletRequest request) Checks if the request is JSON-formatted.- Parameters:
request
- the request object.- Returns:
- true if so, false if not.
-
isXML
public static boolean isXML(javax.servlet.http.HttpServletRequest request) Checks if the request is XML-formatted.- Parameters:
request
- the request object.- Returns:
- true if so, false if not.
-
isFormEncoded
public static boolean isFormEncoded(javax.servlet.http.HttpServletRequest request) Checks if this request is a regular form POST.- Parameters:
request
- the request object.- Returns:
- true if so, false if not.
-
getMultipartParser
Returns the appropriate multipart parser for a request.- Parameters:
request
- the request.- Returns:
- a multipart parser.
-
getPath
Get the path parsed out of the request URI.- Parameters:
request
- the request.- Returns:
- the path string.
-
getPathExtension
Get the path file extension parsed out of the request URI (includes the dot).- Parameters:
request
- the request.- Returns:
- the path extension string or the empty string if none exists.
-
getFileName
Get the base file name parsed out of the request URI.- Parameters:
request
- the request.- Returns:
- the page.
-
getSessionId
Gets the current connection's session id. Meant to be a convenience method forrequest.getSession().getId()
- Parameters:
request
- servlet request object.- Returns:
- the id attached to the session, or null if no session.
-
getSmallResponse
Convenience method that gets the SmallResponse on the request.- Parameters:
request
- servlet request object.- Returns:
- the SmallResponse, if any. Can be null.
- Since:
- 1.6.0
-
getRequestBean
Gets and auto-casts an object bean stored at the request level. The bean is created and stored if it doesn't exist. The name used is the fully-qualified class name prefixed with "$$".- Type Parameters:
T
- the object type.- Parameters:
request
- the source request object.clazz
- the class type of the object that should be returned.- Returns:
- a typecast object on the request, or
null
, if the session is null or the attribute does not exist. - Throws:
IllegalArgumentException
- if the class provided in an anonymous class or array without a component type.BeanCreationException
- if the object cannot be instantiated for any reason.
-
getRequestBean
public static <T> T getRequestBean(javax.servlet.http.HttpServletRequest request, Class<T> clazz, String name) Gets and auto-casts an object bean stored at the request level. The bean is created and stored if it doesn't exist.- Type Parameters:
T
- the object type.- Parameters:
request
- the source request object.name
- the attribute name.clazz
- the class type of the object that should be returned.- Returns:
- a typecast object on the request, or
null
, if the session is null or the attribute does not exist. - Throws:
BeanCreationException
- if the object cannot be instantiated for any reason.
-
getRequestBean
public static <T> T getRequestBean(javax.servlet.http.HttpServletRequest request, Class<T> clazz, String name, boolean create) Gets and auto-casts an object bean stored at the request level.- Type Parameters:
T
- the object type.- Parameters:
request
- the source request object.name
- the attribute name.clazz
- the class type of the object that should be returned.create
- if true, instantiate this class in the request (viaClass.newInstance()
) if it doesn't exist.- Returns:
- a typecast object on the request.
- Throws:
BeanCreationException
- if the object cannot be instantiated for any reason.
-
getSessionBean
Gets and auto-casts an object bean stored at the session level. The bean is created and stored if it doesn't exist. The name used is the fully-qualified class name prefixed with "$$".- Type Parameters:
T
- the object type.- Parameters:
request
- the source request object.clazz
- the class type of the object that should be returned.- Returns:
- a typecast object on the session, or
null
, if the session is null. - Throws:
IllegalArgumentException
- if the class provided in an anonymous class or array without a component type.BeanCreationException
- if the object cannot be instantiated for any reason.
-
getSessionBean
public static <T> T getSessionBean(javax.servlet.http.HttpServletRequest request, Class<T> clazz, String name) Gets and auto-casts an object bean stored at the session level. The bean is created and stored if it doesn't exist.- Type Parameters:
T
- the object type.- Parameters:
request
- the source request object.clazz
- the class type of the object that should be returned.name
- the attribute name.- Returns:
- a typecast object on the session, or
null
, if the session is null. - Throws:
BeanCreationException
- if the object cannot be instantiated for any reason.
-
getSessionBean
public static <T> T getSessionBean(javax.servlet.http.HttpServletRequest request, Class<T> clazz, String name, boolean create) Gets and auto-casts an object bean stored at the session level.- Type Parameters:
T
- the object type.- Parameters:
request
- the source request object.clazz
- the class type of the object that should be returned.name
- the attribute name.create
- if true, instantiate this class in the session (viaClass.newInstance()
) if it doesn't exist.- Returns:
- a typecast object on the session, a new instance if it doesn't exist, or null if the session is null.
- Throws:
BeanCreationException
- if the object cannot be instantiated for any reason.
-
getByteData
Gets the binary payload of a request.- Parameters:
request
- the request.- Returns:
- the resultant byte array of the data.
- Throws:
IOException
- if the data could not be read.
-
getStringData
public static String getStringData(javax.servlet.http.HttpServletRequest request) throws UnsupportedEncodingException, IOException Gets the string data of a request.- Parameters:
request
- the request.- Returns:
- the resultant string.
- Throws:
UnsupportedEncodingException
- if the provided charset name is not a valid charset.IOException
- if
-
getContentData
public static <R> R getContentData(javax.servlet.http.HttpServletRequest request, Class<R> type) throws UnsupportedEncodingException, IOException Get content data from the request and attempts to return it as the desired type. Assumes UTF-8 if the request does not specify an encoding.- Type Parameters:
R
- the return type.- Parameters:
request
- the request to read from.type
- the type to convert to.- Returns:
- the data returned as the requested type.
- Throws:
ClassCastException
- if a type conversion could not be performed.UnsupportedEncodingException
- if the incoming request is not a recognized charset.IOException
- if a read error occurs.
-
getParameters
public static HashMap<String,String[]> getParameters(javax.servlet.http.HttpServletRequest request, String prefix) Gets a group of parameters that start with a specific prefix.- Parameters:
request
- the servlet request.prefix
- the prefix to search for.- Returns:
- a HashMap containing a map of parameter to String value of parameter. The parameters in the map are ones that match the prefix.
-
getParameterExist
public static boolean getParameterExist(javax.servlet.http.HttpServletRequest request, String paramName) Convenience method that checks if a parameter exists on a request.- Parameters:
request
- the servlet request.paramName
- the parameter name.- Returns:
- true if it exists, false otherwise.
-
getParameterBoolean
public static boolean getParameterBoolean(javax.servlet.http.HttpServletRequest request, String paramName) Convenience method that callsrequest.getParameter(paramName)
and returns true or false. This flavor ofgetParameterBoolean(HttpServletRequest, String, String)
assumes that the parameter received is a string that is either "true" or not "true".- Parameters:
request
- the servlet request.paramName
- the parameter name.- Returns:
- true if the parameter value is "true", false otherwise.
-
getParameterBoolean
public static boolean getParameterBoolean(javax.servlet.http.HttpServletRequest request, String paramName, String trueValue) Convenience method that callsrequest.getParameter(paramName)
and returns true or false, if the string found in the request evaluates totrueValue
, case-insensitively. The value oftrueValue
can benull
, meaning that the parameter was not received.- Parameters:
request
- the servlet request.paramName
- the parameter name.trueValue
- the value to equate to true.- Returns:
- true if the parameter value is
trueValue
, false otherwise.
-
getParameterString
public static String getParameterString(javax.servlet.http.HttpServletRequest request, String paramName) Convenience method that callsrequest.getParameter(paramName)
.- Parameters:
request
- the servlet request.paramName
- the parameter name.- Returns:
- the parameter as a string or the empty string if it doesn't exist.
-
getParameterString
public static String getParameterString(javax.servlet.http.HttpServletRequest request, String paramName, String def) Convenience method that callsrequest.getParameter(paramName)
.- Parameters:
request
- the servlet request.paramName
- the parameter name.def
- the default value to use if the parameter is null.- Returns:
- the parameter as a string or
def
if it doesn't exist.
-
getParameterByte
public static byte getParameterByte(javax.servlet.http.HttpServletRequest request, String paramName) Convenience method that callsrequest.getParameter(paramName)
.- Parameters:
request
- the servlet request.paramName
- the parameter name.- Returns:
- the parameter as a byte or 0 if it doesn't exist.
-
getParameterByte
public static byte getParameterByte(javax.servlet.http.HttpServletRequest request, String paramName, byte def) Convenience method that callsrequest.getParameter(paramName)
.- Parameters:
request
- the servlet request.paramName
- the parameter name.def
- the default value to use if the parameter is null.- Returns:
- the parameter as a byte or
def
if it doesn't exist.
-
getParameterShort
public static short getParameterShort(javax.servlet.http.HttpServletRequest request, String paramName) Convenience method that callsrequest.getParameter(paramName)
.- Parameters:
request
- the servlet request.paramName
- the parameter name.- Returns:
- the parameter as a short or 0 if it doesn't exist.
-
getParameterShort
public static short getParameterShort(javax.servlet.http.HttpServletRequest request, String paramName, short def) Convenience method that callsrequest.getParameter(paramName)
.- Parameters:
request
- the servlet request.paramName
- the parameter name.def
- the default value to use if the parameter is null.- Returns:
- the parameter as a short or
def
if it doesn't exist.
-
getParameterChar
public static char getParameterChar(javax.servlet.http.HttpServletRequest request, String paramName) Convenience method that callsrequest.getParameter(paramName)
.- Parameters:
request
- the servlet request.paramName
- the parameter name.- Returns:
- the parameter as a char or
'\0'
if it doesn't exist.
-
getParameterChar
public static char getParameterChar(javax.servlet.http.HttpServletRequest request, String paramName, char def) Convenience method that callsrequest.getParameter(paramName)
.- Parameters:
request
- the servlet request.paramName
- the parameter name.def
- the default value to use if the parameter is null.- Returns:
- the parameter as a char or
def
if it doesn't exist.
-
getParameterInt
Convenience method that callsrequest.getParameter(paramName)
.- Parameters:
request
- the servlet request.paramName
- the parameter name.- Returns:
- the parameter as an integer or 0 if it doesn't exist.
-
getParameterInt
public static int getParameterInt(javax.servlet.http.HttpServletRequest request, String paramName, int def) Convenience method that callsrequest.getParameter(paramName)
.- Parameters:
request
- the servlet request.paramName
- the parameter name.def
- the default value to use if the parameter is null.- Returns:
- the parameter as an integer or
def
if it doesn't exist.
-
getParameterFloat
public static float getParameterFloat(javax.servlet.http.HttpServletRequest request, String paramName) Convenience method that callsrequest.getParameter(paramName)
.- Parameters:
request
- the servlet request.paramName
- the parameter name.- Returns:
- the parameter as an integer or
0f
if it doesn't exist.
-
getParameterFloat
public static float getParameterFloat(javax.servlet.http.HttpServletRequest request, String paramName, float def) Convenience method that callsrequest.getParameter(paramName)
.- Parameters:
request
- the servlet request.paramName
- the parameter name.def
- the default value to use if the parameter is null.- Returns:
- the parameter as an integer or
def
if it doesn't exist.
-
getParameterLong
public static long getParameterLong(javax.servlet.http.HttpServletRequest request, String paramName) Convenience method that callsrequest.getParameter(paramName)
.- Parameters:
request
- the servlet request.paramName
- the parameter name.- Returns:
- the parameter as a long or 0L if it doesn't exist.
-
getParameterLong
public static long getParameterLong(javax.servlet.http.HttpServletRequest request, String paramName, long def) Convenience method that callsrequest.getParameter(paramName)
.- Parameters:
request
- the servlet request.paramName
- the parameter name.def
- the default value to use if the parameter is null.- Returns:
- the parameter as a long or
def
if it doesn't exist.
-
getParameterDouble
public static double getParameterDouble(javax.servlet.http.HttpServletRequest request, String paramName) Convenience method that callsrequest.getParameter(paramName)
.- Parameters:
request
- the servlet request.paramName
- the parameter name.- Returns:
- the parameter as a double or 0.0 if it doesn't exist.
-
getParameterDouble
public static double getParameterDouble(javax.servlet.http.HttpServletRequest request, String paramName, double def) Convenience method that callsrequest.getParameter(paramName)
.- Parameters:
request
- the servlet request.paramName
- the parameter name.def
- the default value to use if the parameter is null.- Returns:
- the parameter as a double or
def
if it doesn't exist.
-
getParameterDate
public static Date getParameterDate(javax.servlet.http.HttpServletRequest request, String paramName, String formatString) Method that parses a parameter as a date.- Parameters:
request
- the HTTP request object.paramName
- the parameter name.formatString
- theSimpleDateFormat
format string.- Returns:
- a Date object representing the parsed date or null if not parsed.
-
setModelFields
Sets the fields on a new instance of an object, using its public fields and setters, using the servlet request as a source. If this method finds fields on the request whose runtime types do not match, namely Strings to primitives or boxed primitives, an attempt is made to convert them.For instance, if there is an attribute in the request called "color", its value will be applied via the public field "color" or the setter "setColor()". Public fields take precedence over setters.
This method does not just merely look in the request scope. If a parameter cannot be found in the Request, the Session attributes are searched next, followed by the Application. If that fails, it is ignored.
- Type Parameters:
T
- the object type.- Parameters:
request
- the servlet request.type
- the type to instantiate.- Returns:
- the new object with fields set using the model.
- Throws:
RuntimeException
- if an exception occurs - notably if the fields or setters on the class cannot be reached (best to use public classes in these cases), or if the object cannot be instantiated.
-
setModelFields
public static <T> T setModelFields(javax.servlet.http.HttpServletRequest request, T target) Sets the fields on an object, using its public fields and setters, using the servlet request as a source. If this method finds fields on the request whose runtime types do not match, namely Strings to primitives or boxed primitives, an attempt is made to convert them.For instance, if there is an attribute in the request called "color", its value will be applied via the public field "color" or the setter "setColor()". Public fields take precedence over setters.
This method does not just merely look in the request scope. If a parameter cannot be found in the Request, the Session attributes are searched next, followed by the Application. If that fails, it is ignored.
- Type Parameters:
T
- the object type.- Parameters:
request
- the servlet request.target
- the target object.- Returns:
- the passed in object.
- Throws:
RuntimeException
- if an exception occurs - notably if the fields or setters on the class cannot be reached (best to use public classes in these cases).
-