Class HttpRequest

java.lang.Object
com.creativityfactory.swiftserver.request.HttpRequest
All Implemented Interfaces:
Request

public class HttpRequest extends Object implements Request
An implementation of the Request interface that provides methods for getting HTTP request attributes and related stuffs. This class is intended for use in Java EE web applications, where it can be used to get the incoming HTTP request... Example usage:

 Request request = new HttpRequest(req);
 String method = request.method();
 HttpSession session = request.session();
 // ...
 
  • Constructor Summary

    Constructors
    Constructor
    Description
    HttpRequest(jakarta.servlet.http.HttpServletRequest request)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    Gets the body of the request as a string.
    body(Class<?> clazz)
    Parses the body of the request and maps it to an instance of the specified class.
    Returns the name of the character encoding used in the body of this request.
    Returns the MIME type of the body of the request, or null if the type is not known.
    Returns the context path of the request.
    Parses the body of the request as form data and returns it as a map.
    Returns the value of the named attribute as an Object, or null if no attribute of the given name exists.
    long
    Returns the value of the specified request header as a long value that represents a Date object.
    header(String headerName)
    Returns the value of the specified request header as a string.
    Returns the hostname of the server that received the current request.
    Gets the body of the request as a JSON string.
    Returns the HTTP request method (e.g.
    boolean
    next(boolean isContinue)
    Sets the value indicating whether the request should continue to be handled by the next middleware or route.
    params(String name)
    Returns the value of the specified request parameter as a String, or null if the parameter does not exist.
    Returns the path of the current request.
    Returns the path pattern of the current request.
    int
    Returns the port number on which the server is listening for requests.
    Returns the HTTP protocol used for the request (e.g.
    query(String name)
    Returns the value of the specified query parameter as a String, or null if the parameter does not exist.
    Removes the attribute with the given name from this request.
    jakarta.servlet.http.HttpSession
    Returns the HttpSession object associated with this request.
    void
    Stores an attribute in this request.
    void
    Sets the body of this Request.
    boolean
    Returns the value indicating whether the request should continue to be handled by the next middleware or route.
    url()
    Returns the full URL of the current request, including the protocol, hostname, port, context path, servlet path, and path.
    Gets the body of the request in the URL-encoded format.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • HttpRequest

      public HttpRequest(jakarta.servlet.http.HttpServletRequest request) throws IOException
      Throws:
      IOException
  • Method Details

    • body

      public String body()
      Description copied from interface: Request
      Gets the body of the request as a string.
      Specified by:
      body in interface Request
      Returns:
      the body of the request as a string.
    • formDataBody

      public Map<String,String> formDataBody()
      Description copied from interface: Request
      Parses the body of the request as form data and returns it as a map.
      Specified by:
      formDataBody in interface Request
      Returns:
      a map containing the form data in the request body.
    • jsonBody

      public String jsonBody()
      Description copied from interface: Request
      Gets the body of the request as a JSON string.
      Specified by:
      jsonBody in interface Request
      Returns:
      the body of the request as a JSON string.
    • urlEncodedFormatBody

      public String urlEncodedFormatBody()
      Description copied from interface: Request
      Gets the body of the request in the URL-encoded format.
      Specified by:
      urlEncodedFormatBody in interface Request
      Returns:
      the body of the request in the URL-encoded format.
    • body

      public Object body(Class<?> clazz)
      Description copied from interface: Request
      Parses the body of the request and maps it to an instance of the specified class.
      Specified by:
      body in interface Request
      Parameters:
      clazz - the class to map the request body to.
      Returns:
      an instance of the specified class containing the request body data.
    • setBody

      public void setBody(String body)
      Description copied from interface: Request
      Sets the body of this Request. If the given body is null, the effect is the same as calling setBody("") to set an empty body.
      Specified by:
      setBody in interface Request
      Parameters:
      body - a String representing the body of this Request.
    • setAttribute

      public void setAttribute(String name, Object obj)
      Description copied from interface: Request
      Stores an attribute in this request. Attribute names should follow the same conventions as package names. Names beginning with java.*, javax.*, and com.sun.*, are reserved for use by the Servlet specification. If the object passed in is null, the effect is the same as calling removeAttribute(String).
      Specified by:
      setAttribute in interface Request
      Parameters:
      name - the name of the attribute to be stored.
      obj - the object to be stored as the attribute value.
    • getAttribute

      public Object getAttribute(String name)
      Description copied from interface: Request
      Returns the value of the named attribute as an Object, or null if no attribute of the given name exists. Attribute names should follow the same conventions as package names. This specification reserves names matching java.*, javax.*, and sun.*.
      Specified by:
      getAttribute in interface Request
      Parameters:
      name - the name of the attribute to retrieve.
      Returns:
      the value of the named attribute, or null if no attribute of the given name exists.
    • removeAttribute

      public Object removeAttribute(String name)
      Description copied from interface: Request
      Removes the attribute with the given name from this request. If the request does not have an attribute with the given name, this method does nothing.
      Specified by:
      removeAttribute in interface Request
      Parameters:
      name - the name of the attribute to remove.
      Returns:
      the value of the removed attribute, or null if no attribute of the given name existed.
    • method

      public String method()
      Description copied from interface: Request
      Returns the HTTP request method (e.g. "GET", "POST", "PUT", etc.).
      Specified by:
      method in interface Request
      Returns:
      the HTTP request method
    • protocol

      public String protocol()
      Description copied from interface: Request
      Returns the HTTP protocol used for the request (e.g. "HTTP/1.1").
      Specified by:
      protocol in interface Request
      Returns:
      the HTTP protocol
    • params

      public String params(String name)
      Description copied from interface: Request
      Returns the value of the specified request parameter as a String, or null if the parameter does not exist.
      Specified by:
      params in interface Request
      Parameters:
      name - the name of the parameter to retrieve
      Returns:
      the value of the parameter, or null if the parameter does not exist
    • query

      public String query(String name)
      Description copied from interface: Request
      Returns the value of the specified query parameter as a String, or null if the parameter does not exist.
      Specified by:
      query in interface Request
      Parameters:
      name - the name of the query parameter to retrieve
      Returns:
      the value of the query parameter, or null if the parameter does not exist
    • header

      public String header(String headerName)
      Description copied from interface: Request
      Returns the value of the specified request header as a string. If the request did not include a header with the specified name, this method returns null.
      Specified by:
      header in interface Request
      Parameters:
      headerName - the name of the header
      Returns:
      a string containing the value of the specified request header, or null if the request does not have a header with that name
    • getDateHeader

      public long getDateHeader(String name)
      Description copied from interface: Request
      Returns the value of the specified request header as a long value that represents a Date object. Use this method with headers that contain dates, such as If-Modified-Since.
      Specified by:
      getDateHeader in interface Request
      Parameters:
      name - the name of the header
      Returns:
      a long value representing the date specified in the header expressed as the number of milliseconds since January 1, 1970 GMT, or -1 if the named header was not included with the request
    • contentType

      public String contentType()
      Description copied from interface: Request
      Returns the MIME type of the body of the request, or null if the type is not known. This method returns the value of the Content-Type header field if it exists and is accessible; otherwise, it returns null.
      Specified by:
      contentType in interface Request
      Returns:
      a string containing the name of the MIME type of the request, or null if the type is not known
    • bodyEncoding

      public String bodyEncoding()
      Description copied from interface: Request
      Returns the name of the character encoding used in the body of this request. This method returns null if the request does not specify a character encoding.
      Specified by:
      bodyEncoding in interface Request
      Returns:
      a string containing the name of the character encoding, or null if the request does not specify a character encoding
    • contextPath

      public String contextPath()
      Description copied from interface: Request
      Returns the context path of the request. The context path is the portion of the request URI that indicates the context of the request. The context path always comes first in a request URI. The path starts with a "/" character but does not end with a "/" character. If this method is called on a request that was dispatched to a servlet using a RequestDispatcher, the context path returned is the context path of the original request, not the context path of the dispatch target.
      Specified by:
      contextPath in interface Request
      Returns:
      a string containing the context path of the request
    • path

      public String path()
      Description copied from interface: Request
      Returns the path of the current request. The path is the part of the URL after the domain name and context path. For example, if the full URL is "http://example.com/context/servlet-path/path/stuffs", then the path returned by this method would be "/path/stuffs".
      Specified by:
      path in interface Request
      Returns:
      the path of the current request
    • hostname

      public String hostname()
      Description copied from interface: Request
      Returns the hostname of the server that received the current request.
      Specified by:
      hostname in interface Request
      Returns:
      the hostname of the server that received the current request
    • port

      public int port()
      Description copied from interface: Request
      Returns the port number on which the server is listening for requests.
      Specified by:
      port in interface Request
      Returns:
      the port number on which the server is listening for requests
    • url

      public String url()
      Description copied from interface: Request
      Returns the full URL of the current request, including the protocol, hostname, port, context path, servlet path, and path.
      Specified by:
      url in interface Request
      Returns:
      the full URL of the current request
    • pattern

      public String pattern()
      Description copied from interface: Request
      Returns the path pattern of the current request.
      Specified by:
      pattern in interface Request
      Returns:
      the path pattern of the request, for example "/path/:id/src".
    • next

      public boolean next(boolean isContinue)
      Description copied from interface: Request
      Sets the value indicating whether the request should continue to be handled by the next middleware or route.
      Specified by:
      next in interface Request
      Parameters:
      isContinue - true if the request should continue, false otherwise.
    • shouldContinue

      public boolean shouldContinue()
      Description copied from interface: Request
      Returns the value indicating whether the request should continue to be handled by the next middleware or route. By default, this value is false.
      Specified by:
      shouldContinue in interface Request
      Returns:
      true if the request should continue, false otherwise.
    • session

      public jakarta.servlet.http.HttpSession session()
      Description copied from interface: Request
      Returns the HttpSession object associated with this request. If the request does not have a session, this method creates one. This method never returns null and should not create a new session if the request does not have a valid session ID.
      Specified by:
      session in interface Request
      Returns:
      the HttpSession associated with this request