Class HttpRequest
java.lang.Object
com.creativityfactory.swiftserver.request.HttpRequest
- All Implemented Interfaces:
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
-
Method Summary
Modifier and TypeMethodDescriptionbody()
Gets the body of the request as a string.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.getAttribute
(String name) Returns the value of the named attribute as an Object, or null if no attribute of the given name exists.long
getDateHeader
(String name) Returns the value of the specified request header as a long value that represents a Date object.Returns the value of the specified request header as a string.hostname()
Returns the hostname of the server that received the current request.jsonBody()
Gets the body of the request as a JSON string.method()
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.Returns the value of the specified request parameter as a String, or null if the parameter does not exist.path()
Returns the path of the current request.pattern()
Returns the path pattern of the current request.int
port()
Returns the port number on which the server is listening for requests.protocol()
Returns the HTTP protocol used for the request (e.g.Returns the value of the specified query parameter as a String, or null if the parameter does not exist.removeAttribute
(String name) Removes the attribute with the given name from this request.jakarta.servlet.http.HttpSession
session()
Returns the HttpSession object associated with this request.void
setAttribute
(String name, Object obj) 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.
-
Constructor Details
-
HttpRequest
- Throws:
IOException
-
-
Method Details
-
body
Description copied from interface:Request
Gets the body of the request as a 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 interfaceRequest
- Returns:
- a map containing the form data in the request body.
-
jsonBody
Description copied from interface:Request
Gets the body of the request as a JSON string. -
urlEncodedFormatBody
Description copied from interface:Request
Gets the body of the request in the URL-encoded format.- Specified by:
urlEncodedFormatBody
in interfaceRequest
- Returns:
- the body of the request in the URL-encoded format.
-
body
Description copied from interface:Request
Parses the body of the request and maps it to an instance of the specified class. -
setBody
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. -
setAttribute
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 interfaceRequest
- Parameters:
name
- the name of the attribute to be stored.obj
- the object to be stored as the attribute value.
-
getAttribute
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 interfaceRequest
- 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
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 interfaceRequest
- 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
Description copied from interface:Request
Returns the HTTP request method (e.g. "GET", "POST", "PUT", etc.). -
protocol
Description copied from interface:Request
Returns the HTTP protocol used for the request (e.g. "HTTP/1.1"). -
params
Description copied from interface:Request
Returns the value of the specified request parameter as a String, or null if the parameter does not exist. -
query
Description copied from interface:Request
Returns the value of the specified query parameter as a String, or null if the parameter does not exist. -
header
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. -
getDateHeader
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 interfaceRequest
- 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
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 interfaceRequest
- Returns:
- a string containing the name of the MIME type of the request, or null if the type is not known
-
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 interfaceRequest
- Returns:
- a string containing the name of the character encoding, or null if the request does not specify a character encoding
-
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 interfaceRequest
- Returns:
- a string containing the context path of the request
-
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". -
hostname
Description copied from 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. -
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. -
pattern
Description copied from interface:Request
Returns the path pattern of the current request. -
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. -
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 interfaceRequest
- 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.
-