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, or throws.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.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.intstatus()Return the current response status.status(int 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
-
matchedPath
String matchedPath()Returns the matched path as a raw expression, without any parameter substitution.- Returns:
- The matched path as a raw string.
-
attribute
Sets an attribute on the request, accessible to other handlers in the request lifecycle.- Parameters:
key- The attribute key.value- The attribute value.
-
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.
-
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.
-
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.
-
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.
-
cookie
Sets a cookie using the providedContext.Cookieobject.- Parameters:
cookie- The cookie object to set.
-
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.
-
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.
-
routeRoles
Returns a set of roles associated with the current route.- Returns:
- A set of roles.
-
bodyAsBytes
byte[] bodyAsBytes()Returns the request body as a byte array.- Returns:
- The request body as a byte array.
-
bodyAsInputStream
InputStream bodyAsInputStream()Returns the request body as an input stream.- Returns:
- The request body as an input stream.
-
bodyAsClass
Return the request body as bean.- Parameters:
beanType- The bean type
-
bodyAsType
Return the request body as bean.- Parameters:
beanType- The bean type
-
body
String body()Return the request body as String. -
contentLength
long contentLength()Return the request content length. -
contentType
String contentType()Return the request content type. -
contentType
Set the response content type. -
pathParamMap
Return all the path parameters as a map. -
pathParam
Return the path parameter.- Parameters:
name- The path parameter name.
-
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
-
queryParams
Return all the query parameters for the given 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.
-
queryString
String queryString()Return the request query string, or null. -
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. -
formParams
Return the form params for the specified key, or empty list. -
formParamMap
Returns a map with all the form param keys and values. -
exchange
HttpExchange exchange()Return the underlying JDKHttpExchangeobject backing the context -
scheme
String scheme()Return the request scheme. -
url
String url()Return the request url. -
fullUrl
Return the full request url, including query string (if present) -
contextPath
String contextPath()Return the request context path. -
userAgent
Return the request user agent, or null. -
status
Set the status code on the response. -
status
int status()Return the current response status. -
text
Write plain text content to the response. -
html
Write html content to the response. -
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:
stream- The stream of beans to write as json
-
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
-
write
Writes the given string content directly to the response.- Parameters:
content- The string content to write.
-
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.
-
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.
-
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
-
headerMap
Return all the request headers as a map.- Returns:
- all the headers as a single value Map
-
headers
Headers headers()Return underlying request headers.- Returns:
- the request headers
-
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
-
headers
Add the response headers using the provided 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.
-
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.
-
host
String host()Returns the host name of the request.- Returns:
- The host name of the request, or null if not available.
-
ip
String ip()Returns the IP address of the client making the request.- Returns:
- The IP address of the client.
-
method
String method()Returns the HTTP method used in the request (e.g., GET, POST, PUT, DELETE).- Returns:
- The HTTP method of the request.
-
path
String path()Returns the path part of the request URI.- Returns:
- The path part of the request URI.
-
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.
-
basicAuthCredentials
BasicAuthCredentials basicAuthCredentials()Gets basic-auth credentials from the request, or throws.Returns a wrapper object containing the Base64 decoded username and password from the Authorization header, or null if basic-auth is not properly configured
-
responseSent
boolean responseSent()Return true if the response has been sent.
-