Interface Context
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 TypeMethodDescriptiondefault <T> TGets an attribute with the specified type from the request.default <T> ContextSets an attribute on the request, accessible to other handlers in the request lifecycle.<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.default <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.default <T> TbodyStreamAsClass(Class<T> beanType) Return the request body as bean usingbodyAsInputStream().<T> TbodyStreamAsType(Type beanType) Return the request body as bean of the given type usingbodyAsInputStream().longReturn the request content length.default ContextcontentLength(long length) Manually set the response 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 value by name.Set the response header.Set the response header.Return all the request headers as a map.Sets the response headers using the provided map.default ContextAdd the response headers using the provided map.headerValues(String key) Return the request headers.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.default <T> voidjsonb(io.avaje.jsonb.JsonType<T> jsonType, T value) Optimized json write using avaje jsonbReturns the configuredJsonServiceinstance.}<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.default voidrangedWrite(InputStream inputStream) Writes input stream torangedWrite(InputStream, long)with currently available data viaInputStream.available()voidrangedWrite(InputStream inputStream, long totalBytes) Reads HTTP Range headers and determines which part of the provided InputStream to write back.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.Return underlying request headers.responseHeader(String key) Returns the value of the specified response header.Return underlying response headers.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.uri()Return the request uri.default StringReturn the request user agent, or null.default voidwrite(byte[] bytes) Writes the given bytes directly to the response.voidwrite(byte[] bufferBytes, int length) Writes the given length of bytes from this buffer 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
-
attribute
Gets an attribute with the specified type 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
-
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
-
bodyStreamAsClass
Return the request body as bean usingbodyAsInputStream().- Parameters:
beanType- The bean type
-
bodyStreamAsType
Return the request body as bean of the given type usingbodyAsInputStream().- Parameters:
beanType- The bean type
-
contentLength
long contentLength()Return the request content length. -
contentLength
Manually set the response content length. -
contentType
String contentType()Return the request content type. -
contentType
-
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
-
cookie
-
cookie
-
cookieMap
-
exchange
HttpExchange exchange()Return the underlying JDKHttpExchangeobject backing the context -
formParam
-
formParam
-
formParamMap
-
formParams
-
fullUrl
Return the full request url, including query string (if present) -
rangedWrite
Reads HTTP Range headers and determines which part of the provided InputStream to write back.- Parameters:
inputStream- data to writetotalBytes- total size of the data
-
rangedWrite
Writes input stream torangedWrite(InputStream, long)with currently available data viaInputStream.available() -
header
-
headerValues
-
header
-
header
-
headerMap
-
headerMap
-
requestHeaders
-
responseHeaders
-
headers
-
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
-
jsonb
default <T> void jsonb(io.avaje.jsonb.JsonType<T> jsonType, T value) Optimized json write using avaje jsonb- Parameters:
jsonType- the serializer for the value.value- the pojo to serialize
-
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
-
jsonService
JsonService jsonService()Returns the configuredJsonServiceinstance.}- Returns:
- The json service if configured. null otherwise.
-
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
-
pathParam
-
pathParamMap
-
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
-
queryParam
-
queryParamMap
-
queryParams
-
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
-
removeCookie
-
render
-
render
-
responseHeader
-
responseHeaderValues
-
responseSent
boolean responseSent()Return true if the response has been sent. -
routeRoles
-
scheme
String scheme()Return the request scheme. -
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. -
uri
URI uri()Return the request uri. -
userAgent
Return the request user agent, or null. -
write
default void write(byte[] bytes) Writes the given bytes directly to the response.- Parameters:
bytes- The byte array to write.
-
write
void write(byte[] bufferBytes, int length) Writes the given length of bytes from this buffer directly to the response.The bytes written will be from position 0 to length.
- Parameters:
bufferBytes- The byte array to write.length- The number of bytes to write from the buffer.
-
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.
-