org.silverpeas.servlet
Class HttpRequest

java.lang.Object
  extended by javax.servlet.ServletRequestWrapper
      extended by javax.servlet.http.HttpServletRequestWrapper
          extended by org.silverpeas.servlet.HttpRequest
All Implemented Interfaces:
javax.servlet.http.HttpServletRequest, javax.servlet.ServletRequest

public class HttpRequest
extends javax.servlet.http.HttpServletRequestWrapper

An HTTP request decorating an HTTP servlet request with some additional methods and by changing the implementation of some of its methods to take into account some Silverpeas specificities or needs. For example, the getParameter(java.lang.String) method has been modified to take into account also the parameters passed in a multipart/form-data stream.

Author:
mmoquillon

Field Summary
 
Fields inherited from interface javax.servlet.http.HttpServletRequest
BASIC_AUTH, CLIENT_CERT_AUTH, DIGEST_AUTH, FORM_AUTH
 
Method Summary
static HttpRequest decorate(javax.servlet.http.HttpServletRequest request)
          Decorates the specified HTTP servlet request with an HttpRequest instance.
static HttpRequest decorate(javax.servlet.ServletRequest request)
          Decorates the specified servlet request with an HttpRequest instance.
 Long getAttributeAsLong(String attributeName)
          Get a parameter value as a Long.
 String getCharacterEncoding()
           
 org.apache.commons.fileupload.FileItem getFile(String name)
          Processes an RFC 1867 compliant multipart/form-data stream and returns the item whose the type is a file and the name matches the specified one.
 org.apache.commons.fileupload.FileItem getFileItem(String name)
          Processes an RFC 1867 compliant multipart/form-data stream and returns the item whose the name matches the specified one.
 List<org.apache.commons.fileupload.FileItem> getFileItems()
          Processes an RFC 1867 compliant multipart/form-data stream.
 MainSessionController getMainSessionController()
          Gets the main controller in the current user session.
 String getParameter(String name)
          Returns the value of a request parameter as a String, or null if the parameter does not exist.
 boolean getParameterAsBoolean(String parameterName)
          Get a parameter value as a boolean.
 Date getParameterAsDate(String dateParameterName)
          Get a date from a date parameter.
 Date getParameterAsDate(String dateParameterName, String hourParameterName)
          Get a date from one date parameter and one hour parameter.
<ENUM extends Enum>
ENUM
getParameterAsEnum(String enumValue, Class<ENUM> enumClass)
          Get an enum instance from one parameter.
 Integer getParameterAsInteger(String parameterName)
          Get a parameter value as a Integer.
 Long getParameterAsLong(String parameterName)
          Get a parameter value as a Long.
 RequestFile getParameterAsRequestFile(String parameterName)
          Get a parameter value as a RequestFile.
 Map<String,String[]> getParameterMap()
          Returns a java.util.Map of the parameters of this request.
 Enumeration<String> getParameterNames()
          Returns an Enumeration of String objects containing the names of the parameters contained in this request.
 String[] getParameterValues(String name)
          Returns an array of String objects containing all of the values the given request parameter has, or null if the parameter does not exist.
 int getServerPort()
           
 org.apache.commons.fileupload.FileItem getSingleFile()
          Processes an RFC 1867 compliant multipart/form-data stream and returns the item whose the type is a file.
 Collection<UploadedFile> getUploadedFiles()
          Retrieves from HttpServletRequest a collection of UploadedFile.
 String getUserLanguage()
          Gets the language of the user behind this request.
 boolean hasCookie(String name)
          Is this request has a cookie with the specified name?
 boolean isContentInMultipart()
          Is the content in this request is encoded in a multipart stream.
 boolean isSecure()
           
 boolean isWithinAnonymousUserSession()
          Is this request within an anonymous user session?
 boolean isWithinUserSession()
          Is this request within an opened user session?
 
Methods inherited from class javax.servlet.http.HttpServletRequestWrapper
authenticate, getAuthType, getContextPath, getCookies, getDateHeader, getHeader, getHeaderNames, getHeaders, getIntHeader, getMethod, getPart, getParts, getPathInfo, getPathTranslated, getQueryString, getRemoteUser, getRequestedSessionId, getRequestURI, getRequestURL, getServletPath, getSession, getSession, getUserPrincipal, isRequestedSessionIdFromCookie, isRequestedSessionIdFromUrl, isRequestedSessionIdFromURL, isRequestedSessionIdValid, isUserInRole, login, logout
 
Methods inherited from class javax.servlet.ServletRequestWrapper
getAsyncContext, getAttribute, getAttributeNames, getContentLength, getContentType, getDispatcherType, getInputStream, getLocalAddr, getLocale, getLocales, getLocalName, getLocalPort, getProtocol, getReader, getRealPath, getRemoteAddr, getRemoteHost, getRemotePort, getRequest, getRequestDispatcher, getScheme, getServerName, getServletContext, isAsyncStarted, isAsyncSupported, isWrapperFor, isWrapperFor, removeAttribute, setAttribute, setCharacterEncoding, setRequest, startAsync, startAsync
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface javax.servlet.ServletRequest
getAsyncContext, getAttribute, getAttributeNames, getContentLength, getContentType, getDispatcherType, getInputStream, getLocalAddr, getLocale, getLocales, getLocalName, getLocalPort, getProtocol, getReader, getRealPath, getRemoteAddr, getRemoteHost, getRemotePort, getRequestDispatcher, getScheme, getServerName, getServletContext, isAsyncStarted, isAsyncSupported, removeAttribute, setAttribute, setCharacterEncoding, startAsync, startAsync
 

Method Detail

decorate

public static HttpRequest decorate(javax.servlet.http.HttpServletRequest request)
Decorates the specified HTTP servlet request with an HttpRequest instance. If the request is already an HttpRequest instance, then it is simply returned.

Parameters:
request - the Http servlet request to decorate.
Returns:
an HttpRequest instance decorating the specified request.

decorate

public static HttpRequest decorate(javax.servlet.ServletRequest request)
Decorates the specified servlet request with an HttpRequest instance. If the request is already an HttpRequest instance, then it is simply returned.

Parameters:
request - the servlet request to decorate. Must be of type HttpServletRequest.
Returns:
an HttpRequest instance decorating the specified request.

isWithinAnonymousUserSession

public boolean isWithinAnonymousUserSession()
Is this request within an anonymous user session?

Returns:
true if the request is sent in the context of an opened user session and this session is for an anonymous user.

isWithinUserSession

public boolean isWithinUserSession()
Is this request within an opened user session?

Returns:
true if the request is sent in the context of a Silvepreas user session.

getFileItems

public List<org.apache.commons.fileupload.FileItem> getFileItems()
Processes an RFC 1867 compliant multipart/form-data stream.

Returns:
a list of FileItem instances parsed from the request, in the order that they were transmitted.

getFileItem

public org.apache.commons.fileupload.FileItem getFileItem(String name)
Processes an RFC 1867 compliant multipart/form-data stream and returns the item whose the name matches the specified one.

Parameters:
name - the name of the data to fetch.
Returns:
the FileItem instance whose the name matches the specified one or null if no such data exists in the multipart/form-data stream. The file item can be either a file or a parameter.

getFile

public org.apache.commons.fileupload.FileItem getFile(String name)
Processes an RFC 1867 compliant multipart/form-data stream and returns the item whose the type is a file and the name matches the specified one.

Parameters:
name - the name of the file item to fetch.
Returns:
the file item whose the name matches the specified one or null if no such data exists in the multipart stream.

getSingleFile

public org.apache.commons.fileupload.FileItem getSingleFile()
Processes an RFC 1867 compliant multipart/form-data stream and returns the item whose the type is a file. If there is several items that represent a file, then only the first one is returned.

Returns:
the file item representing a file in the multipart stream or null if no such data exists in the multipart stream.

hasCookie

public boolean hasCookie(String name)
Is this request has a cookie with the specified name?

Parameters:
name - the name of the cookie.
Returns:
true if a cookie with the specified name is carried by this request, false otherwise.

getServerPort

public int getServerPort()
Specified by:
getServerPort in interface javax.servlet.ServletRequest
Overrides:
getServerPort in class javax.servlet.ServletRequestWrapper

isSecure

public boolean isSecure()
Specified by:
isSecure in interface javax.servlet.ServletRequest
Overrides:
isSecure in class javax.servlet.ServletRequestWrapper

getMainSessionController

public MainSessionController getMainSessionController()
Gets the main controller in the current user session. If the current session doesn't match an opened user session in Silverpeas, then null is returned.

Returns:
the main session controller mapped with this request.

getUserLanguage

public String getUserLanguage()
Gets the language of the user behind this request.

Returns:
the language of the user as he has chosen in its profile in Silverpeas.

getUploadedFiles

public Collection<UploadedFile> getUploadedFiles()
Retrieves from HttpServletRequest a collection of UploadedFile.

Returns:
collection of UploadedFile. Empty collection if no uploaded file exists.

getAttributeAsLong

public Long getAttributeAsLong(String attributeName)
Get a parameter value as a Long.

Parameters:
attributeName - the name of the attribute.
Returns:
the value of the attribute as a long.

getParameterValues

public String[] getParameterValues(String name)
Returns an array of String objects containing all of the values the given request parameter has, or null if the parameter does not exist. The parameters from a multipart/form-data stream are also considered by this method, unlike of the default behavior of the decorated request. If the parameter has a single value, the array has a length of 1.

Specified by:
getParameterValues in interface javax.servlet.ServletRequest
Overrides:
getParameterValues in class javax.servlet.ServletRequestWrapper
Parameters:
name - the name of the parameter whose value is requested.
Returns:
an array of String objects containing the parameter's values.

getParameterNames

public Enumeration<String> getParameterNames()
Returns an Enumeration of String objects containing the names of the parameters contained in this request. If the request has no parameters, the method returns an empty Enumeration. The parameters from a multipart/form-data stream are also considered by this method, unlike of the default behavior of the decorated request.

Specified by:
getParameterNames in interface javax.servlet.ServletRequest
Overrides:
getParameterNames in class javax.servlet.ServletRequestWrapper
Returns:
an Enumeration of String objects, each String containing the name of a request parameter; or an empty Enumeration if the request has no parameters.

getParameterMap

public Map<String,String[]> getParameterMap()
Returns a java.util.Map of the parameters of this request. Request parameters are extra information sent with the request. For HTTP servlets, parameters are contained in the query string or posted form data. The parameters from a multipart/form-data stream are also considered by this method, unlike of the default behavior of the decorated request.

Specified by:
getParameterMap in interface javax.servlet.ServletRequest
Overrides:
getParameterMap in class javax.servlet.ServletRequestWrapper
Returns:
an immutable java.util.Map containing parameter names as keys and parameter values as map values. The keys in the parameter map are of type String. The values in the parameter map are of type String array.

getParameter

public String getParameter(String name)
Returns the value of a request parameter as a String, or null if the parameter does not exist. Request parameters are extra information sent with the request. For HTTP servlets, parameters are contained in the query string or posted form data. The parameters from a multipart/form-data stream are also considered by this method, unlike of the default behavior of the decorated request. You should only use this method when you are sure the parameter has only one value. If the parameter might have more than one value, use getParameterValues(java.lang.String). If you use this method with a multivalued parameter, the value returned is equal to the first value in the array returned by getParameterValues. If the parameter data was sent in the request body, such as occurs with an HTTP POST request, then reading the body directly via getInputStream() or getReader() can interfere with the execution of this method.

Specified by:
getParameter in interface javax.servlet.ServletRequest
Overrides:
getParameter in class javax.servlet.ServletRequestWrapper
Parameters:
name - the name of the parameter.
Returns:
the single value of the parameter.

getParameterAsRequestFile

public RequestFile getParameterAsRequestFile(String parameterName)
Get a parameter value as a RequestFile.

Parameters:
parameterName - the name of the parameter.
Returns:
the value of the parameter as a RequestFile.

getParameterAsBoolean

public boolean getParameterAsBoolean(String parameterName)
Get a parameter value as a boolean.

Parameters:
parameterName - the name of the parameter.
Returns:
the value of the parameter as a boolean.

getParameterAsLong

public Long getParameterAsLong(String parameterName)
Get a parameter value as a Long.

Parameters:
parameterName - the name of the parameter.
Returns:
the value of the parameter as a long.

getParameterAsInteger

public Integer getParameterAsInteger(String parameterName)
Get a parameter value as a Integer.

Parameters:
parameterName - the name of the parameter.
Returns:
the value of the parameter as an integer.

getParameterAsDate

public Date getParameterAsDate(String dateParameterName)
                        throws ParseException
Get a date from a date parameter.

Parameters:
dateParameterName - the name of the parameter.
Returns:
the value of the parameter as a date.
Throws:
ParseException - if the parameter value isn't a date.

getParameterAsDate

public Date getParameterAsDate(String dateParameterName,
                               String hourParameterName)
                        throws ParseException
Get a date from one date parameter and one hour parameter.

Parameters:
dateParameterName - the name of the date parameter.
hourParameterName - the name of the time parameter.
Returns:
the value of the parameter as a date.
Throws:
ParseException - if the parameter value isn't a date.

getParameterAsEnum

public <ENUM extends Enum> ENUM getParameterAsEnum(String enumValue,
                                                   Class<ENUM> enumClass)
Get an enum instance from one parameter.

Type Parameters:
ENUM - the type of the expected enum instance.
Parameters:
enumValue - the string value of the expected enum instance.
enumClass - the class of the expected enum instance.
Returns:
the expected enum instance or null if enum has not been well decoded

isContentInMultipart

public boolean isContentInMultipart()
Is the content in this request is encoded in a multipart stream.

Returns:
true if the content type of this request is a compilant multipart/form-data stream, false otherwise.

getCharacterEncoding

public String getCharacterEncoding()
Specified by:
getCharacterEncoding in interface javax.servlet.ServletRequest
Overrides:
getCharacterEncoding in class javax.servlet.ServletRequestWrapper


Copyright © 2016 Silverpeas. All Rights Reserved.