public interface MuRequest
An HTTP request from a client.
Call method()
and uri()
to find the method and URL of this request.
To read query string parameters, see query()
.
Request headers can be accessed by the headers()
method.
You can read the body as an input stream with inputStream()
or if you expect a string call readBodyAsString()
.
If form values were posted as the request, see form()
Getting the value of a cookie by name is done by calling cookie(String)
If you need to share request-specific data between handlers, use attribute(String, Object)
to set and attribute(String)
to get.
Modifier and Type | Method and Description |
---|---|
Object |
attribute(String key)
Gets request-specific state that was added with
attribute(String, Object) . |
void |
attribute(String key,
Object value)
Sets the given object as state associated with the given key that is bound to this request which any subsequent handlers can access.
|
Map<String,Object> |
attributes()
Returns the map containing all the attributes.
|
HttpConnection |
connection() |
String |
contentType()
Gets the content type of the request body, for example
application/json or null
if there is no body. |
String |
contextPath()
If this handler was added to a
ContextHandlerBuilder.context(String) then this
will return the value of the context pre-pended with a '/ '. |
Optional<String> |
cookie(String name)
Gets the value of the client-sent cookie with the given name
|
List<Cookie> |
cookies()
Gets all the client-sent cookies
|
RequestParameters |
form()
Gets the form parameters for this request.
|
String |
formValue(String name)
Deprecated.
Use
form().get(name) instead |
List<String> |
formValues(String name)
Deprecated.
Use
form().getAll(name) instead |
AsyncHandle |
handleAsync()
Specifies that you want to handle this response asynchronously.
|
Headers |
headers() |
Optional<InputStream> |
inputStream()
The input stream of the request, if there was a request body.
|
boolean |
isAsync() |
Method |
method() |
String |
parameter(String name)
Deprecated.
Use
query().get(name) instead |
List<String> |
parameters(String name)
Deprecated.
Use
query().getAll(name) instead |
String |
protocol()
Deprecated.
Call
connection() to get the connection and then use HttpConnection.protocol() |
RequestParameters |
query()
Gets the querystring parameters for this request.
|
String |
readBodyAsString()
Returns the request body as a string.
|
String |
relativePath()
The path of this request (without query string) relative to the context.
|
String |
remoteAddress()
Gets the address that the request came from.
|
MuServer |
server() |
URI |
serverURI()
The URI of the request for this server.
|
long |
startTime() |
Object |
state()
Deprecated.
Use
attribute(String) instead |
void |
state(Object value)
Deprecated.
Use
attribute(String, Object) instead with a key name |
UploadedFile |
uploadedFile(String name)
Gets the uploaded file with the given name, or null if there is no upload with that name.
|
List<UploadedFile> |
uploadedFiles(String name)
Gets all the uploaded files with the given name, or an empty list if none are found.
|
URI |
uri()
The URI of the request at the origin.
|
String contentType()
Gets the content type of the request body, for example application/json
or null
if there is no body.
Note: If the Content-Type header included a charset, then this will NOT be returned by this method. In order
to get the charset, use request.headers().contentType()
and access the charset
parameter on that.
Content-Type
request header),
or null
if there is no body.Headers.contentType()
long startTime()
Method method()
URI uri()
If behind a reverse proxy, this URI should be the URI that the client saw when making the request.
URI serverURI()
If behind a reverse proxy, this will be different from uri()
as it is the actual server URI rather
than what the client sees.
Headers headers()
Optional<InputStream> inputStream()
Note: this can only be read once and cannot be used with readBodyAsString()
or form()
.
Optional.empty()
if there is no request body; otherwise the input stream of the request body.String readBodyAsString() throws IOException
This is a blocking call which waits until the whole request is available. If you need the raw bytes, or to stream
the request body, then use the inputStream()
instead.
The content type of the request body is assumed to be UTF-8 if no encoding is specified
Note: this can only be read once and cannot be used with inputStream()
()} or form()
.
IOException
- if there is an exception during reading the request, e.g. if the HTTP connection is stopped during a requestList<UploadedFile> uploadedFiles(String name) throws IOException
name
- The file input name to getIOException
- Thrown when there is an error while reading the file, e.g. if a user closes their
browser before the upload is complete.UploadedFile uploadedFile(String name) throws IOException
Gets the uploaded file with the given name, or null if there is no upload with that name.
If there are multiple files with the same name, the first one is returned.
name
- The querystring parameter name to getIOException
- Thrown when there is an error while reading the file, e.g. if a user closes their
browser before the upload is complete.RequestParameters query()
Gets the querystring parameters for this request.
RequestParameters form() throws IOException
Gets the form parameters for this request.
Note: this cannot be called after a call to inputStream()
or readBodyAsString()
IOException
- Thrown when there is an error while reading the form, e.g. if a user closes their
browser before the form is fully read into memory.@Deprecated String parameter(String name)
query().get(name)
insteadname
- The querystring parameter name to get@Deprecated List<String> parameters(String name)
query().getAll(name)
insteadname
- The querystring parameter name to get@Deprecated String formValue(String name) throws IOException
form().get(name)
insteadname
- The name of the form element to getIOException
- Thrown when there is an error while reading the form, e.g. if a user closes their
browser before the form is fully read into memory.@Deprecated List<String> formValues(String name) throws IOException
form().getAll(name)
insteadname
- The name of the form element to getIOException
- Thrown when there is an error while reading the form, e.g. if a user closes their
browser before the form is fully read into memory.List<Cookie> cookies()
Optional<String> cookie(String name)
name
- The name of the cookieOptional.empty()
if there is no cookie with that name.String contextPath()
If this handler was added to a ContextHandlerBuilder.context(String)
then this
will return the value of the context pre-pended with a '/
'.
For example,
if this handler was added to ContextHandlerBuilder.context("some context")
this this method will return /some%20context
/
' if there is no context.relativePath()
String relativePath()
The path of this request (without query string) relative to the context. If this handler is not
nested within a context then it simply returns the full path of the request, otherwise it is
the path relative to contextPath()
.
For example, if there is a context some context
and there is a request to /some%20context/some%20path
then this method will return
/some%20path
contextPath()
@Deprecated Object state()
attribute(String)
insteadGets request-specific state that was added with state(Object)
.
An example is getting user information in a view handler that was previously set by an authentication handler.
state(Object)
, or null
if it was never set.@Deprecated void state(Object value)
attribute(String, Object)
instead with a key nameSets the given object as state that is bound to this request which any subsequent handlers can access.
An example use case is if you have a authentication handler that uses this method to set user information on
the request, and then a subsequent handler calls state()
to get the user info.
value
- Any object to store as state.Object attribute(String key)
Gets request-specific state that was added with attribute(String, Object)
.
An example is getting user information in a view handler that was previously set by an authentication handler.
key
- The key the object is associated with.attribute(String, Object)
, or null
if it was never set.void attribute(String key, Object value)
Sets the given object as state associated with the given key that is bound to this request which any subsequent handlers can access.
An example use case is if you have a authentication handler that uses this method to set user information on
the request, and then a subsequent handler calls attribute(String)
to get the user info.
key
- The key to associate the value with.value
- Any object to store as state.Map<String,Object> attributes()
Returns the map containing all the attributes.
Any changes made with attribute(String, Object)
to the map will be reflected on this returned map.
attribute(String, Object)
AsyncHandle handleAsync()
Specifies that you want to handle this response asynchronously.
When finished, call AsyncHandle.complete()
If called more than once, then the async handle created from the first call is returned.
String remoteAddress()
This is a convenience method that returns connection().remoteAddress().getHostString()
MuServer server()
boolean isAsync()
handleAsync()
has been called and this is an async response@Deprecated String protocol()
connection()
to get the connection and then use HttpConnection.protocol()
HTTP/1.1
or HTTP/2
HttpConnection connection()
Copyright © 2017–2020. All rights reserved.