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
Constructors -
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.longgetDateHeader(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.booleannext(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.intport()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.HttpSessionsession()Returns the HttpSession object associated with this request.voidsetAttribute(String name, Object obj) Stores an attribute in this request.voidSets the body of this Request.booleanReturns 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:RequestGets the body of the request as a string. -
formDataBody
Description copied from interface:RequestParses the body of the request as form data and returns it as a map.- Specified by:
formDataBodyin interfaceRequest- Returns:
- a map containing the form data in the request body.
-
jsonBody
Description copied from interface:RequestGets the body of the request as a JSON string. -
urlEncodedFormatBody
Description copied from interface:RequestGets the body of the request in the URL-encoded format.- Specified by:
urlEncodedFormatBodyin interfaceRequest- Returns:
- the body of the request in the URL-encoded format.
-
body
Description copied from interface:RequestParses the body of the request and maps it to an instance of the specified class. -
setBody
Description copied from interface:RequestSets 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:RequestStores 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:
setAttributein 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:RequestReturns 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:
getAttributein 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:RequestRemoves 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:
removeAttributein 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:RequestReturns the HTTP request method (e.g. "GET", "POST", "PUT", etc.). -
protocol
Description copied from interface:RequestReturns the HTTP protocol used for the request (e.g. "HTTP/1.1"). -
params
Description copied from interface:RequestReturns the value of the specified request parameter as a String, or null if the parameter does not exist. -
query
Description copied from interface:RequestReturns the value of the specified query parameter as a String, or null if the parameter does not exist. -
header
Description copied from interface:RequestReturns 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:RequestReturns 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:
getDateHeaderin 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:RequestReturns 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:
contentTypein 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:RequestReturns 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:
bodyEncodingin 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:RequestReturns 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:
contextPathin interfaceRequest- Returns:
- a string containing the context path of the request
-
path
Description copied from interface:RequestReturns 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:RequestReturns the hostname of the server that received the current request. -
port
public int port()Description copied from interface:RequestReturns the port number on which the server is listening for requests. -
url
Description copied from interface:RequestReturns the full URL of the current request, including the protocol, hostname, port, context path, servlet path, and path. -
pattern
Description copied from interface:RequestReturns the path pattern of the current request. -
next
public boolean next(boolean isContinue) Description copied from interface:RequestSets 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:RequestReturns 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:
shouldContinuein interfaceRequest- Returns:
- true if the request should continue, false otherwise.
-
session
public jakarta.servlet.http.HttpSession session()Description copied from interface:RequestReturns 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.
-