Class 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

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      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.
      boolean getAttributeAsBoolean​(String attributeName)
      Get a parameter value as a boolean.
      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.
      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.
      List<Boolean> getParameterAsBooleanList​(String parameterName)
      Get a parameter value as a list of 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.
      List<Date> getParameterAsDateList​(String dateParameterName)
      Get a parameter value as a list of date.
      <E extends Enum<E>>
      E
      getParameterAsEnum​(String enumValue, Class<E> enumClass)
      Get an enum instance from one parameter.
      <E extends Enum<E>>
      List<E>
      getParameterAsEnumList​(String parameterName, Class<E> enumClass)
      Get a parameter value as a list of enum.
      Integer getParameterAsInteger​(String parameterName)
      Get a parameter value as a Integer.
      List<Integer> getParameterAsIntegerList​(String parameterName)
      Get a parameter value as a list of integer.
      List<String> getParameterAsList​(String parameterName)
      Get a parameter value as a List of string.
      Long getParameterAsLong​(String parameterName)
      Get a parameter value as a Long.
      List<Long> getParameterAsLongList​(String parameterName)
      Get a parameter value as a list of 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.
      Map<String,​String> getParameterSimpleMap()
      Same as getParameterMap() but with a String as value instead of an array of string.
      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.
      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.
      static boolean isCurrentRequestSecure()
      Is the HTTP request currently processed is secure (that is carried through a TLS connection)?
      boolean isParameterDefined​(String name)
      Is the specified parameter defined?
      boolean isParameterNotNull​(String name)
      Is the specified parameter not null?
      boolean isWithinAnonymousUserSession()
      Is this request within an anonymous user session?
      boolean isWithinUserSession()
      Is this request within an opened user session?
      void mergeSelectedItemsInto​(Collection<String> selectedIds)
      Merges into given collection of identifiers the selected and unselected identifiers extracted from the current request.
      void mergeSelectedItemsInto​(Collection<String> selectedIds, String selectedParamName, String unselectedParamName)
      Merges into given collection of identifiers the selected and unselected identifiers extracted from the current request.
      • Methods inherited from class javax.servlet.http.HttpServletRequestWrapper

        authenticate, changeSessionId, getAuthType, getContextPath, getCookies, getDateHeader, getHeader, getHeaderNames, getHeaders, getHttpServletMapping, getIntHeader, getMethod, getPart, getParts, getPathInfo, getPathTranslated, getQueryString, getRemoteUser, getRequestedSessionId, getRequestURI, getRequestURL, getServletPath, getSession, getSession, getTrailerFields, getUserPrincipal, isRequestedSessionIdFromCookie, isRequestedSessionIdFromUrl, isRequestedSessionIdFromURL, isRequestedSessionIdValid, isTrailerFieldsReady, isUserInRole, login, logout, newPushBuilder, upgrade
      • Methods inherited from class javax.servlet.ServletRequestWrapper

        getAsyncContext, getAttribute, getAttributeNames, getContentLength, getContentLengthLong, getContentType, getDispatcherType, getInputStream, getLocalAddr, getLocale, getLocales, getLocalName, getLocalPort, getProtocol, getReader, getRealPath, getRemoteAddr, getRemoteHost, getRemotePort, getRequest, getRequestDispatcher, getScheme, getServerName, getServerPort, getServletContext, isAsyncStarted, isAsyncSupported, isSecure, isWrapperFor, isWrapperFor, removeAttribute, setAttribute, setCharacterEncoding, setRequest, startAsync, startAsync
      • Methods inherited from interface javax.servlet.ServletRequest

        getAsyncContext, getAttribute, getAttributeNames, getContentLength, getContentLengthLong, getContentType, getDispatcherType, getInputStream, getLocalAddr, getLocale, getLocales, getLocalName, getLocalPort, getProtocol, getReader, getRealPath, getRemoteAddr, getRemoteHost, getRemotePort, getRequestDispatcher, getScheme, getServerName, getServerPort, getServletContext, isAsyncStarted, isAsyncSupported, isSecure, 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.
      • isCurrentRequestSecure

        public static boolean isCurrentRequestSecure()
        Is the HTTP request currently processed is secure (that is carried through a TLS connection)?
        Returns:
        true of the current HTTP request is secure, false otherwise.
      • 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.
      • 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.
      • getAttributeAsBoolean

        public boolean getAttributeAsBoolean​(String attributeName)
        Get a parameter value as a boolean.
        Parameters:
        attributeName - the name of the attribute.
        Returns:
        the value of the attribute as a boolean.
      • 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.
      • getParameterSimpleMap

        public Map<String,​String> getParameterSimpleMap()
        Same as getParameterMap() but with a String as value instead of an array of string.
        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.
      • mergeSelectedItemsInto

        public void mergeSelectedItemsInto​(Collection<String> selectedIds)
        Merges into given collection of identifiers the selected and unselected identifiers extracted from the current request.

        Default parameter names are used:

        • selectedIds: parameter name to retrieve selected identifiers.
        • unselectedIds: parameter name to retrieve unselected identifiers.

        Parameters:
        selectedIds - the collection of selected identifiers.
      • mergeSelectedItemsInto

        public void mergeSelectedItemsInto​(Collection<String> selectedIds,
                                           String selectedParamName,
                                           String unselectedParamName)
        Merges into given collection of identifiers the selected and unselected identifiers extracted from the current request.
        Parameters:
        selectedIds - the collection of selected identifiers.
        selectedParamName - the parameter name of selected identifiers.
        unselectedParamName - the parameter name of unselected identifiers.
      • 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.
      • isParameterDefined

        public boolean isParameterDefined​(String name)
        Is the specified parameter defined?
        Parameters:
        name - the name of the parameter.
        Returns:
        true if the value of the parameter isn't null and not empty.
      • isParameterNotNull

        public boolean isParameterNotNull​(String name)
        Is the specified parameter not null?
        Parameters:
        name - the name of the parameter.
        Returns:
        true if the parameter is valued, even if this value is empty.
      • getParameterAsList

        public List<String> getParameterAsList​(String parameterName)
        Get a parameter value as a List of string.
        Parameters:
        parameterName - the name of the parameter.
        Returns:
        the value of the parameter as a List of string.
      • 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.
      • getParameterAsBooleanList

        public List<Boolean> getParameterAsBooleanList​(String parameterName)
        Get a parameter value as a list of boolean.
        Parameters:
        parameterName - the name of the parameter.
        Returns:
        the value of the parameter as a list of 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.
      • getParameterAsLongList

        public List<Long> getParameterAsLongList​(String parameterName)
        Get a parameter value as a list of long.
        Parameters:
        parameterName - the name of the parameter.
        Returns:
        the value of the parameter as a list of 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.
      • getParameterAsIntegerList

        public List<Integer> getParameterAsIntegerList​(String parameterName)
        Get a parameter value as a list of integer.
        Parameters:
        parameterName - the name of the parameter.
        Returns:
        the value of the parameter as a list of 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.
      • getParameterAsDateList

        public List<Date> getParameterAsDateList​(String dateParameterName)
        Get a parameter value as a list of date.
        Parameters:
        dateParameterName - the name of the parameter.
        Returns:
        the value of the parameter as a list of 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 <E extends Enum<E>> E getParameterAsEnum​(String enumValue,
                                                        Class<E> enumClass)
        Get an enum instance from one parameter.
        Type Parameters:
        E - 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
      • getParameterAsEnumList

        public <E extends Enum<E>> List<E> getParameterAsEnumList​(String parameterName,
                                                                  Class<E> enumClass)
        Get a parameter value as a list of enum.
        Parameters:
        parameterName - the name of the parameter.
        enumClass - the class of the expected enum instance.
        Returns:
        the value of the parameter as a list of enum.
      • 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