Class RequestParameters


  • public class RequestParameters
    extends Object
    Provides access to query parameters set on servlet requests.

    Here is a snippet demonstrating how to obtain a request parameter value:

     String value = RequestParameters.of(request).getString("myParameterName", "theDefaultValue");
     
    Since:
    1.4.0
    • Method Detail

      • of

        public static RequestParameters of​(HttpServletRequest request)
        Returns an instance that wraps the specified servlet request.
        Parameters:
        request - a servlet request.
        Returns:
        a new RequestParameters instance.
      • getString

        public Optional<String> getString​(String name)
        Returns the string value associated with the specified request parameter name.
        Parameters:
        name - the parameter name.
        Returns:
        an Optional wrapping the string value associated to the parameter name, Optional.empty() if none.
      • getRequiredString

        public String getRequiredString​(String name)
        Returns the string value associated with the specified request parameter name that is required to exist.
        Parameters:
        name - the parameter name.
        Returns:
        the string value associated to the parameter name.
        Throws:
        WebApplicationException - if the parameter is not set in servlet request parameters.
      • getString

        public String getString​(String name,
                                String defaultValue)
        Returns the string value associated with the specified request parameter or default value if parameter is not set.
        Parameters:
        name - the parameter name.
        defaultValue - the default value to use if the request parameter is not set.
        Returns:
        the string value associated to the parameter name or the specified default value if the request parameter is not set.
      • getRequiredBoolean

        public boolean getRequiredBoolean​(String name)
        Returns the boolean value associated with the specified request parameter name that is required to exist.
        Parameters:
        name - the parameter name.
        Returns:
        the boolean value associated to the parameter name.
        Throws:
        WebApplicationException - if the parameter is not set in request parameters.
      • getBoolean

        public boolean getBoolean​(String name,
                                  boolean defaultValue)
        Returns the boolean value associated with the specified request parameter or default value if parameter is not set.
        Parameters:
        name - the parameter name.
        defaultValue - the default value to use if the request parameter is not set.
        Returns:
        the boolean value associated to the parameter name or the specified default value if the request parameter is not set.
      • getURI

        public Optional<URI> getURI​(String name)
        Returns the URI value associated with the specified request parameter name.

        If the parameter value is surrounded by '<' and '>' characters, this method strips them to obtain the URI.

        Parameters:
        name - the parameter name.
        Returns:
        an Optional wrapping the URI value associated to the parameter name, Optional.empty() if none.
        Throws:
        WebApplicationException - if the parameter value is not a valid URI.
      • getRequiredURI

        public URI getRequiredURI​(String name)
        Returns the URI value associated with the specified request parameter name that is required to exist.

        If the parameter value is surrounded by '<' and '>' characters, this method strips them to obtain the URI.

        Parameters:
        name - the parameter name.
        Returns:
        the URI associated to the parameter name.
        Throws:
        WebApplicationException - if the parameter is not set in request parameters or if the value is not a valid URI.
      • requireNonNull

        public static <T> T requireNonNull​(T parameter,
                                           String name)
        Convenient method to ensure a parameter is not null. This shall be used to assert the presence of a value obtained with a QueryParam annotation on a method argument.
        Type Parameters:
        T - parameter's type.
        Parameters:
        parameter - parameter's value.
        name - parameter's name.
        Returns:
        parameter's value.
        Throws:
        WebApplicationException - if parameter is null.
        Since:
        1.14.0