public interface Context
Provides access to functions for handling the request and response.
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceThis interface represents a cookie used in HTTP communication. -
Method Summary
Modifier and TypeMethodDescription<T> TGets the attribute with the specified key from the request.Sets an attribute on the request, accessible to other handlers in the request lifecycle.Gets basic-auth credentials from the request.body()Return the request body as String.byte[]Returns the request body as a byte array.<T> TbodyAsClass(Class<T> beanType) Return the request body as bean.Returns the request body as an input stream.<T> TbodyAsType(Type beanType) Return the request body as bean.longReturn the request content length.Return the request content type.default ContextcontentType(ContentType contentType) Set the response content type.contentType(String contentType) Set the response content type.Return the request context path.cookie(Context.Cookie cookie) Sets a cookie using the providedContext.Cookieobject.Returns the value of a cookie with the specified name from the request.Sets a cookie with the specified name and value, with no expiration date.Sets a cookie with the specified name, value, and maximum age in seconds.Returns a map containing all the cookie names and their corresponding values from the request.exchange()Return the underlying JDKHttpExchangeobject backing the contextdefault StringReturn the first form param value for the specified key or null.default StringReturn the first form param value for the specified key or the default value.Returns a map with all the form param keys and values.formParams(String key) Return the form params for the specified key, or empty list.default StringfullUrl()Return the full request url, including query string (if present)Return the request header.Set the response header.Set the response header.Return all the request headers as a map.Sets the response headers using the provided map.headers()Return underlying request headers.default ContextAdd the response headers using the provided map.host()Returns the host name of the request.voidWrite html content to the response.ip()Returns the IP address of the client making the request.voidSet the content type as application/json and write the response.<E> voidjsonStream(Iterator<E> iterator) Write the stream as a JSON stream with new line delimiters application/x-json-stream.<E> voidjsonStream(Stream<E> stream) Write the stream as a JSON stream with new line delimiters application/x-json-stream.Returns the matched path as a raw expression, without any parameter substitution.method()Returns the HTTP method used in the request (e.g., GET, POST, PUT, DELETE).Return the outputStream to write content.path()Returns the path part of the request URI.Return the path parameter.Return all the path parameters as a map.intport()Returns the port number used in the request.protocol()Returns the protocol used in the request (e.g., HTTP/1.1).queryParam(String name) Return the first query parameter value.default StringqueryParam(String name, String defaultValue) Return the first query parameter value or the default value if it does not exist.Return all the query parameters as a map.queryParams(String name) Return all the query parameters for the given parameter name.Return the request query string, or null.voidRedirects the client to the specified location using a 302 (Found) status code.voidRedirects the client to the specified location using the given HTTP status code.removeCookie(String name) Removes a cookie with the specified name.removeCookie(String name, String path) Removes a cookie with the specified name and path.default ContextRender a template typically as html.Render a template typically as html with the given model.responseHeader(String key) Returns the value of the specified response header.booleanReturn true if the response has been sent.Returns a set of roles associated with the current route.scheme()Return the request scheme.Get theSSLSessionfor this exchange.intstatus()Return the current response status.status(int statusCode) Set the status code on the response.default Contextstatus(HttpStatus statusCode) Set the status code on the response.voidWrite plain text content to the response.url()Return the request url.default StringReturn the request user agent, or null.voidwrite(byte[] bytes) Writes the given bytes directly to the response.voidwrite(InputStream is) Writes the content from the given InputStream directly to the response body.voidWrites the given string content directly to the response.
-
Method Details
-
attribute
Gets the attribute with the specified key from the request.- Type Parameters:
T- The type of the attribute.- Parameters:
key- The attribute key.- Returns:
- The attribute value, or null if not found.
-
attribute
Sets an attribute on the request, accessible to other handlers in the request lifecycle.- Parameters:
key- The attribute key.value- The attribute value.
-
basicAuthCredentials
BasicAuthCredentials basicAuthCredentials()Gets basic-auth credentials from the request.- Returns:
- The Base64 decoded username and password from the Authorization header, or null if no header is sent
- Throws:
IllegalStateException- if the Authorization header is malformed
-
body
String body()Return the request body as String. -
bodyAsBytes
byte[] bodyAsBytes()Returns the request body as a byte array.- Returns:
- The request body as a byte array.
-
bodyAsClass
Return the request body as bean.- Parameters:
beanType- The bean type
-
bodyAsInputStream
InputStream bodyAsInputStream()Returns the request body as an input stream.- Returns:
- The request body as an input stream.
-
bodyAsType
Return the request body as bean.- Parameters:
beanType- The bean type
-
contentLength
long contentLength()Return the request content length. -
contentType
String contentType()Return the request content type. -
contentType
Set the response content type. -
contentType
Set the response content type. -
contextPath
String contextPath()Return the request context path. -
cookie
Sets a cookie using the providedContext.Cookieobject.- Parameters:
cookie- The cookie object to set.
-
cookie
Returns the value of a cookie with the specified name from the request.- Parameters:
name- The name of the cookie.- Returns:
- The value of the cookie, or null if the cookie is not found.
-
cookie
Sets a cookie with the specified name and value, with no expiration date.- Parameters:
name- The name of the cookie.value- The value of the cookie.
-
cookie
Sets a cookie with the specified name, value, and maximum age in seconds.- Parameters:
name- The name of the cookie.value- The value of the cookie.maxAge- The maximum age of the cookie in seconds.
-
cookieMap
Returns a map containing all the cookie names and their corresponding values from the request.- Returns:
- A map of cookie names to their values.
-
exchange
HttpExchange exchange()Return the underlying JDKHttpExchangeobject backing the context -
formParam
Return the first form param value for the specified key or null. -
formParam
Return the first form param value for the specified key or the default value. -
formParamMap
Returns a map with all the form param keys and values. -
formParams
Return the form params for the specified key, or empty list. -
fullUrl
Return the full request url, including query string (if present) -
header
Return the request header.- Parameters:
key- The header key
-
header
Set the response header.- Parameters:
key- The header keyvalue- The header value
-
header
Set the response header.- Parameters:
key- The header keyvalue- The header value
-
headerMap
Return all the request headers as a map.- Returns:
- all the headers as a single value Map
-
headerMap
Sets the response headers using the provided map.- Parameters:
headers- A map containing the header names as keys and their corresponding values as lists.- Returns:
- The updated context object.
-
headers
Headers headers()Return underlying request headers.- Returns:
- the request headers
-
headers
Add the response headers using the provided map. -
host
String host()Returns the host name of the request.- Returns:
- The host name of the request, or null if not available.
-
html
Write html content to the response. -
ip
String ip()Returns the IP address of the client making the request.- Returns:
- The IP address of the client.
-
json
Set the content type as application/json and write the response.- Parameters:
bean- the object to serialize and write
-
jsonStream
Write the stream as a JSON stream with new line delimiters application/x-json-stream.- Parameters:
iterator- The iterator of beans to write as json
-
jsonStream
Write the stream as a JSON stream with new line delimiters application/x-json-stream.- Parameters:
stream- The stream of beans to write as json
-
matchedPath
String matchedPath()Returns the matched path as a raw expression, without any parameter substitution.- Returns:
- The matched path as a raw string.
-
method
String method()Returns the HTTP method used in the request (e.g., GET, POST, PUT, DELETE).- Returns:
- The HTTP method of the request.
-
outputStream
OutputStream outputStream()Return the outputStream to write content. It is expected that thecontentType(String)has been set prior to obtaining and writing to the outputStream.- Returns:
- The outputStream to write content to.
-
path
String path()Returns the path part of the request URI.- Returns:
- The path part of the request URI.
-
pathParam
Return the path parameter.- Parameters:
name- The path parameter name.
-
pathParamMap
Return all the path parameters as a map. -
port
int port()Returns the port number used in the request.- Returns:
- The port number of the request.
-
protocol
String protocol()Returns the protocol used in the request (e.g., HTTP/1.1).- Returns:
- The protocol of the request.
-
queryParam
Return the first query parameter value.- Parameters:
name- The query parameter name
-
queryParam
Return the first query parameter value or the default value if it does not exist.- Parameters:
name- The query parameter name
-
queryParamMap
Return all the query parameters as a map.Note this returns the first value for any given key if that key has multiple values.
-
queryParams
Return all the query parameters for the given parameter name. -
queryString
String queryString()Return the request query string, or null. -
redirect
Redirects the client to the specified location using a 302 (Found) status code.- Parameters:
location- The URL to redirect to.
-
redirect
Redirects the client to the specified location using the given HTTP status code.- Parameters:
location- The URL to redirect to.httpStatusCode- The HTTP status code to use for the redirect.
-
removeCookie
Removes a cookie with the specified name.- Parameters:
name- The name of the cookie to remove.
-
removeCookie
Removes a cookie with the specified name and path.- Parameters:
name- The name of the cookie to remove.path- The path of the cookie to remove.
-
render
Render a template typically as html.- Parameters:
name- The template name
-
render
Render a template typically as html with the given model.- Parameters:
name- The template namemodel- The model used with the template
-
responseHeader
Returns the value of the specified response header.- Parameters:
key- The name of the header.- Returns:
- The value of the header, or null if not found.
-
responseSent
boolean responseSent()Return true if the response has been sent. -
routeRoles
Returns a set of roles associated with the current route.- Returns:
- A set of roles.
-
scheme
String scheme()Return the request scheme. -
sslSession
SSLSession sslSession()Get theSSLSessionfor this exchange.- Returns:
- the
SSLSession
-
status
int status()Return the current response status. -
status
Set the status code on the response. -
status
Set the status code on the response. -
text
Write plain text content to the response. -
url
String url()Return the request url. -
userAgent
Return the request user agent, or null. -
write
void write(byte[] bytes) Writes the given bytes directly to the response.- Parameters:
bytes- The byte array to write.
-
write
Writes the content from the given InputStream directly to the response body.- Parameters:
is- The input stream containing the content to write.
-
write
Writes the given string content directly to the response.- Parameters:
content- The string content to write.
-