public interface Context extends Registry
Modifier and Type | Field and Description |
---|---|
static String |
ACCEPT
Constant for
Accept header. |
static ZoneId |
GMT
Constant for GMT.
|
static int |
PORT
Constant for default HTTP port.
|
static DateTimeFormatter |
RFC1123
RFC1123 date formatter.
|
static String |
RFC1123_PATTERN
RFC1123 date pattern.
|
static int |
SECURE_PORT
Constant for default HTTPS port.
|
Modifier and Type | Method and Description |
---|---|
MediaType |
accept(List<MediaType> produceTypes)
Check if the accept type list matches the given produces list and return the most
specific media type from produces list.
|
boolean |
accept(MediaType contentType)
True if the given type matches the `Accept` header.
|
<T> T |
attribute(String key)
Get an attribute by his key.
|
Context |
attribute(String key,
Object value)
Set an application attribute.
|
Body |
body()
HTTP body which provides access to body content.
|
<T> T |
body(Class<T> type)
Convert the HTTP body to the given type.
|
<T> T |
body(Type type)
Convert the HTTP body to the given type.
|
<T> T |
convert(ValueNode value,
Class<T> type)
Converts a value (single or hash) into the given type.
|
Value |
cookie(String name)
Get a cookie matching the given name.
|
Map<String,String> |
cookieMap()
Request cookies.
|
<T> T |
decode(Type type,
MediaType contentType)
Convert the HTTP body to the given type.
|
MessageDecoder |
decoder(MediaType contentType)
Get a decoder for the given content type or get an
StatusCode.UNSUPPORTED_MEDIA_TYPE . |
Context |
detach(Route.Handler next)
Tells context that response will be generated form a different thread.
|
Context |
dispatch(Executor executor,
Runnable action)
Dispatch context to the given executor.
|
Context |
dispatch(Runnable action)
Dispatch context to a worker threads.
|
FileUpload |
file(String name)
A file upload that matches the given field name.
|
List<FileUpload> |
files()
All file uploads.
|
List<FileUpload> |
files(String name)
All file uploads that matches the given field name.
|
FlashMap |
flash()
Flash map.
|
Value |
flash(String name)
Get a flash attribute.
|
Formdata |
form()
Formdata as
ValueNode . |
<T> T |
form(Class<T> type)
Convert formdata to the given type.
|
ValueNode |
form(String name)
Form field that matches the given name.
|
Map<String,String> |
formMap()
Formdata as single-value map.
|
Map<String,List<String>> |
formMultimap()
Formdata as multi-value map.
|
Map<String,Object> |
getAttributes()
Context attributes (a.k.a request attributes).
|
default String |
getContextPath()
Get application context path (a.k.a as base path).
|
String |
getHost()
Return the host that this request was sent to, in general this will be the
value of the Host header, minus the port specifier.
|
String |
getHostAndPort(boolean useProxy)
Return the host and port that this request was sent to, in general this will be the
value of the Host or X-Forwarded-Host header.
|
String |
getMethod()
HTTP method in upper-case form.
|
int |
getPort()
Return the port that this request was sent to.
|
String |
getProtocol()
The name of the protocol the request.
|
String |
getRemoteAddress()
The IP address of the client or last proxy that sent the request.
|
long |
getRequestLength()
Request
Content-Length header or -1 when missing. |
String |
getRequestPath()
Request path without decoding (a.k.a raw Path) without query string.
|
MediaType |
getRequestType()
Request
Content-Type header or null when missing. |
MediaType |
getRequestType(MediaType defaults)
Request
Content-Type header or null when missing. |
String |
getRequestURL()
Recreates full/entire request url using the
Host header. |
String |
getRequestURL(boolean useProxy)
Recreates full/entire request url using the
X-Forwarded-Host when present
or fallback to Host header when missing. |
String |
getRequestURL(String path)
Recreates full/entire request url using the
Host header. |
String |
getRequestURL(String path,
boolean useProxy)
Recreates full/entire request url using the
X-Forwarded-Host when present
or fallback to Host header when missing. |
boolean |
getResetHeadersOnError()
True if response headers are cleared on application error.
|
StatusCode |
getResponseCode()
Get response status code.
|
String |
getResponseHeader(String name)
Get response header.
|
long |
getResponseLength()
Get response content length or
-1 when none was set. |
MediaType |
getResponseType()
Get response content type.
|
Route |
getRoute()
Matching route.
|
Router |
getRouter()
Get the HTTP router (usually this represent an instance of
Jooby . |
String |
getScheme()
HTTP scheme in lower case.
|
String |
getServerHost()
Server host.
|
int |
getServerPort()
Server port for current request.
|
<T> T |
getUser()
Current user or
null if none was set. |
ValueNode |
header()
Request headers as
ValueNode . |
Value |
header(String name)
Get a header that matches the given name.
|
Map<String,String> |
headerMap()
Header as single-value map.
|
Map<String,List<String>> |
headerMultimap()
Header as multi-value map.
|
boolean |
isInIoThread()
True when request runs in IO threads.
|
boolean |
isResponseStarted()
True if response already started.
|
boolean |
isSecure()
Returns a boolean indicating whether this request was made using a secure channel, such as
HTTPS.
|
boolean |
matches(String pattern)
Check if the request path matches the given pattern.
|
Multipart |
multipart()
Get multipart data.
|
<T> T |
multipart(Class<T> type)
Convert multipart data to the given type.
|
ValueNode |
multipart(String name)
Get a multipart field that matches the given name.
|
Map<String,String> |
multipartMap()
Multipart data as single-value map.
|
Map<String,List<String>> |
multipartMultimap()
Multipart data as multi-value map.
|
Context |
onComplete(Route.Complete task)
Add a complete listener.
|
ValueNode |
path()
|
<T> T |
path(Class<T> type)
Convert the
pathMap() to the given type. |
Value |
path(String name)
Path variable.
|
Map<String,String> |
pathMap()
Path map represent all the path keys with their values.
|
String |
pathString()
Deprecated.
Use {
getRequestPath() }. |
QueryString |
query()
Query string as
ValueNode object. |
<T> T |
query(Class<T> type)
Convert the queryString to the given type.
|
ValueNode |
query(String name)
Get a query parameter that matches the given name.
|
Map<String,String> |
queryMap()
Query string as simple map.
|
Map<String,List<String>> |
queryMultimap()
Query string as multi-value map.
|
String |
queryString()
Query string with the leading
? or empty string. |
static Context |
readOnly(Context ctx)
Wrap a HTTP context and make read only.
|
Context |
removeResponseHeader(String name)
Remove a response header.
|
Context |
removeResponseHeaders()
Clear/reset all the headers, including cookies.
|
Context |
render(Object value)
Render a value and send the response to client.
|
Sender |
responseSender()
HTTP response channel as chunker.
|
OutputStream |
responseStream()
HTTP response channel as output stream.
|
OutputStream |
responseStream(MediaType contentType)
HTTP response channel as output stream.
|
Context |
responseStream(MediaType contentType,
SneakyThrows.Consumer<OutputStream> consumer)
HTTP response channel as output stream.
|
Context |
responseStream(SneakyThrows.Consumer<OutputStream> consumer)
HTTP response channel as output stream.
|
PrintWriter |
responseWriter()
HTTP response channel as response writer.
|
PrintWriter |
responseWriter(MediaType contentType)
HTTP response channel as response writer.
|
PrintWriter |
responseWriter(MediaType contentType,
Charset charset)
HTTP response channel as response writer.
|
Context |
responseWriter(MediaType contentType,
Charset charset,
SneakyThrows.Consumer<PrintWriter> consumer)
HTTP response channel as response writer.
|
Context |
responseWriter(MediaType contentType,
SneakyThrows.Consumer<PrintWriter> consumer)
HTTP response channel as response writer.
|
Context |
responseWriter(SneakyThrows.Consumer<PrintWriter> consumer)
HTTP response channel as response writer.
|
Context |
send(AttachedFile file)
Send a file attached response.
|
Context |
send(byte[]... data)
Send response data.
|
Context |
send(byte[] data)
Send response data.
|
Context |
send(ByteBuffer data)
Send response data.
|
Context |
send(ByteBuffer[] data)
Send response data.
|
Context |
send(FileChannel file)
Send a file response.
|
Context |
send(InputStream input)
Send response data.
|
Context |
send(Path file)
Send a file response.
|
Context |
send(ReadableByteChannel channel)
Send response data.
|
Context |
send(StatusCode statusCode)
Send an empty response with the given status code.
|
Context |
send(String data)
Send response data.
|
Context |
send(String data,
Charset charset)
Send response data.
|
Context |
sendError(Throwable cause)
Send an error response.
|
Context |
sendError(Throwable cause,
StatusCode statusCode)
Send an error response.
|
Context |
sendRedirect(StatusCode redirect,
String location)
Send a redirect response.
|
Context |
sendRedirect(String location)
Send a
302 response. |
Session |
session()
Find a session or creates a new session.
|
Value |
session(String name)
Find a session attribute using the given name.
|
Session |
sessionOrNull()
Find an existing session.
|
Context |
setDefaultResponseType(MediaType contentType)
Set the default response content type header.
|
Context |
setPathMap(Map<String,String> pathMap)
Set path map.
|
Context |
setResetHeadersOnError(boolean value)
Set whenever reset/clear headers on application error.
|
Context |
setResponseCode(int statusCode)
Set response status code.
|
Context |
setResponseCode(StatusCode statusCode)
Set response status code.
|
Context |
setResponseCookie(Cookie cookie)
Set/add a cookie to response.
|
Context |
setResponseHeader(String name,
Date value)
Set response header.
|
Context |
setResponseHeader(String name,
Instant value)
Set response header.
|
Context |
setResponseHeader(String name,
Object value)
Set response header.
|
Context |
setResponseHeader(String name,
String value)
Set response header.
|
Context |
setResponseLength(long length)
Set response content length header.
|
Context |
setResponseType(MediaType contentType)
Set response content type header.
|
Context |
setResponseType(MediaType contentType,
Charset charset)
Set response content type header.
|
Context |
setResponseType(String contentType)
Set response content type header.
|
Context |
setRoute(Route route)
Set matching route.
|
Context |
setUser(Object user)
Set current user.
|
Context |
upgrade(ServerSentEmitter.Handler handler)
Perform a server-sent event handshake and upgrade HTTP GET into a Server-Sent protocol.
|
Context |
upgrade(WebSocket.Initializer handler)
Perform a websocket handsahke and upgrade a HTTP GET into a websocket protocol.
|
static Context |
websocket(Context ctx,
WebSocket ws)
Wrap a HTTP context and make it WebSocket friendly.
|
static final int PORT
static final int SECURE_PORT
static final String ACCEPT
Accept
header.static final ZoneId GMT
static final String RFC1123_PATTERN
static final DateTimeFormatter RFC1123
@Nonnull Map<String,Object> getAttributes()
@Nonnull <T> T attribute(@Nonnull String key)
getAttributes()
.
This method look first in current context and fallback to application attributes.T
- Attribute type.key
- Attribute key.@Nonnull Context attribute(@Nonnull String key, Object value)
key
- Attribute key.value
- Attribute value.@Nonnull Router getRouter()
Jooby
.Jooby
.@Nullable <T> T convert(@Nonnull ValueNode value, @Nonnull Class<T> type)
T
- Generic type.value
- Value to convert.type
- Expected type.@Nonnull Value flash(@Nonnull String name)
name
- Attribute's name.@Nonnull Value session(@Nonnull String name)
name
- Attribute's name.@Nullable Session sessionOrNull()
null
.@Nonnull Value cookie(@Nonnull String name)
name
- Cookie's name.@Nonnull String getMethod()
boolean matches(@Nonnull String pattern)
pattern
- Pattern to use.@Nonnull Context setRoute(@Nonnull Route route)
route
- Matching route.@Nonnull default String getContextPath()
@Deprecated @Nonnull String pathString()
getRequestPath()
}.@Nonnull String getRequestPath()
@Nonnull Value path(@Nonnull String name)
name
- Path key.null
reference.@Nonnull <T> T path(@Nonnull Class<T> type)
pathMap()
to the given type.T
- Target type.type
- Target type.@Nonnull Map<String,String> pathMap()
{
get("/:id", ctx -> ctx.pathMap());
}
A call to:
GET /678Produces a path map like:
id: 678
@Nonnull Context setPathMap(@Nonnull Map<String,String> pathMap)
pathMap
- Path map.@Nonnull QueryString query()
ValueNode
object.ValueNode
object.@Nonnull ValueNode query(@Nonnull String name)
{
get("/search", ctx -> {
String q = ctx.query("q").value();
...
});
}
name
- Parameter name.@Nonnull String queryString()
?
or empty string. This is the raw query string,
without decoding it.?
or empty string. This is the raw query
string, without decoding it.@Nonnull <T> T query(@Nonnull Class<T> type)
T
- Target type.type
- Target type.@Nonnull Map<String,String> queryMap()
{q: jooby, sort: name}
@Nonnull Map<String,List<String>> queryMultimap()
{q: [jooby], sort: [name, id]}
@Nonnull ValueNode header()
ValueNode
.ValueNode
.@Nonnull Value header(@Nonnull String name)
name
- Header name. Case insensitive.null
reference.@Nonnull Map<String,String> headerMap()
@Nonnull Map<String,List<String>> headerMultimap()
boolean accept(@Nonnull MediaType contentType)
true
if there is no accept header.contentType
- Content type to match.@Nullable MediaType accept(@Nonnull List<MediaType> produceTypes)
produceTypes
- Produced types.@Nullable MediaType getRequestType()
Content-Type
header or null
when missing.Content-Type
header or null
when missing.@Nonnull MediaType getRequestType(MediaType defaults)
Content-Type
header or null
when missing.defaults
- Default content type to use when the header is missing.Content-Type
header or null
when missing.long getRequestLength()
Content-Length
header or -1
when missing.Content-Length
header or -1
when missing.@Nullable <T> T getUser()
null
if none was set.T
- User type.null
if none was set.@Nonnull Context setUser(@Nullable Object user)
user
- Current user.@Nonnull String getRequestURL()
Host
header.Host
header.@Nonnull String getRequestURL(@Nonnull String path)
Host
header.path
- Path to use.Host
header.@Nonnull String getRequestURL(boolean useProxy)
X-Forwarded-Host
when present
or fallback to Host
header when missing.useProxy
- True to trust/use the X-Forwarded-Host
.X-Forwarded-Host
when present
or fallback to Host
header when missing.@Nonnull String getRequestURL(@Nonnull String path, boolean useProxy)
X-Forwarded-Host
when present
or fallback to Host
header when missing.path
- Path to use.useProxy
- True to trust/use the X-Forwarded-Host
.X-Forwarded-Host
when present
or fallback to Host
header when missing.@Nonnull String getRemoteAddress()
@Nonnull String getHost()
@Nullable String getHostAndPort(boolean useProxy)
useProxy
- When true this method looks for host data in the X-Forwarded-Host header.int getPort()
@Nonnull String getProtocol()
int getServerPort()
boolean isSecure()
@Nonnull String getScheme()
@Nonnull Formdata form()
ValueNode
. This method is for application/form-url-encoded
request.ValueNode
. This method is for application/form-url-encoded
request.@Nonnull Map<String,List<String>> formMultimap()
application/form-url-encoded
request.application/form-url-encoded
request.@Nonnull Map<String,String> formMap()
application/form-url-encoded
request.application/form-url-encoded
request.@Nonnull ValueNode form(@Nonnull String name)
application/form-url-encoded
request.name
- Field name.@Nonnull <T> T form(@Nonnull Class<T> type)
application/form-url-encoded
request.T
- Target type.type
- Target type.@Nonnull Multipart multipart()
multipart/form-data
request..@Nonnull ValueNode multipart(@Nonnull String name)
file(String)
.
Only for multipart/form-data
request.name
- Field name.@Nonnull <T> T multipart(@Nonnull Class<T> type)
multipart/form-data
request.T
- Target type.type
- Target type.@Nonnull Map<String,List<String>> multipartMultimap()
multipart/form-data
request.@Nonnull Map<String,String> multipartMap()
multipart/form-data
request.@Nonnull List<FileUpload> files()
multipart/form-data
request.@Nonnull List<FileUpload> files(@Nonnull String name)
multipart/form-data
request.name
- Field name. Please note this is the form field name, not the actual file name.@Nonnull FileUpload file(@Nonnull String name)
multipart/form-data
request.name
- Field name. Please note this is the form field name, not the actual file name.@Nonnull Body body()
@Nonnull <T> T body(@Nonnull Class<T> type)
T
- Conversion type.type
- Reified type.@Nonnull <T> T body(@Nonnull Type type)
T
- Conversion type.type
- Reified type.@Nonnull <T> T decode(@Nonnull Type type, @Nonnull MediaType contentType)
T
- Conversion type.type
- Generic type.contentType
- Content type to use.@Nonnull MessageDecoder decoder(@Nonnull MediaType contentType)
StatusCode.UNSUPPORTED_MEDIA_TYPE
.contentType
- Content type.boolean isInIoThread()
@Nonnull Context dispatch(@Nonnull Runnable action)
Jooby.setWorker(Executor)
.
Example:
get("/", ctx -> {
return ctx.dispatch(() -> {
// run blocking code
}):
});
action
- Application code.@Nonnull Context dispatch(@Nonnull Executor executor, @Nonnull Runnable action)
Executor executor = ...;
get("/", ctx -> {
return ctx.dispatch(executor, () -> {
// run blocking code
}):
});
executor
- Executor to use.action
- Application code.@Nonnull Context detach(@Nonnull Route.Handler next) throws Exception
dispatch(Runnable)
except there is no thread dispatching here.
This operation integrates easily with third-party libraries like rxJava or others.next
- Application code.Exception
- When detach operation fails.@Nonnull Context upgrade(@Nonnull WebSocket.Initializer handler)
handler
- Web socket initializer.@Nonnull Context upgrade(@Nonnull ServerSentEmitter.Handler handler)
handler
- Server-Sent event handler.@Nonnull Context setResponseHeader(@Nonnull String name, @Nonnull Date value)
name
- Header name.value
- Header value.@Nonnull Context setResponseHeader(@Nonnull String name, @Nonnull Instant value)
name
- Header name.value
- Header value.@Nonnull Context setResponseHeader(@Nonnull String name, @Nonnull Object value)
name
- Header name.value
- Header value.@Nonnull Context setResponseHeader(@Nonnull String name, @Nonnull String value)
name
- Header name.value
- Header value.@Nonnull Context removeResponseHeader(@Nonnull String name)
name
- Header's name.@Nonnull Context removeResponseHeaders()
@Nonnull Context setResponseLength(long length)
length
- Response length.@Nullable String getResponseHeader(@Nonnull String name)
name
- Header's name.null
.long getResponseLength()
-1
when none was set.-1
when none was set.@Nonnull Context setResponseCookie(@Nonnull Cookie cookie)
cookie
- Cookie to add.@Nonnull Context setResponseType(@Nonnull String contentType)
contentType
- Content type.@Nonnull Context setResponseType(@Nonnull MediaType contentType)
contentType
- Content type.@Nonnull Context setResponseType(@Nonnull MediaType contentType, @Nullable Charset charset)
contentType
- Content type.charset
- Charset.@Nonnull Context setDefaultResponseType(@Nonnull MediaType contentType)
contentType
- Content type.@Nonnull MediaType getResponseType()
@Nonnull Context setResponseCode(@Nonnull StatusCode statusCode)
statusCode
- Status code.@Nonnull Context setResponseCode(int statusCode)
statusCode
- Status code.@Nonnull StatusCode getResponseCode()
@Nonnull Context render(@Nonnull Object value)
value
- Object value.@Nonnull OutputStream responseStream()
@Nonnull OutputStream responseStream(@Nonnull MediaType contentType)
contentType
- Media type.@Nonnull Context responseStream(@Nonnull MediaType contentType, @Nonnull SneakyThrows.Consumer<OutputStream> consumer) throws Exception
contentType
- Content type.consumer
- Output stream consumer.Exception
- Is something goes wrong.@Nonnull Context responseStream(@Nonnull SneakyThrows.Consumer<OutputStream> consumer) throws Exception
consumer
- Output stream consumer.Exception
- Is something goes wrong.@Nonnull Sender responseSender()
@Nonnull PrintWriter responseWriter()
@Nonnull PrintWriter responseWriter(@Nonnull MediaType contentType)
contentType
- Content type.@Nonnull PrintWriter responseWriter(@Nonnull MediaType contentType, @Nullable Charset charset)
contentType
- Content type.charset
- Charset.@Nonnull Context responseWriter(@Nonnull SneakyThrows.Consumer<PrintWriter> consumer) throws Exception
consumer
- Writer consumer.Exception
- Is something goes wrong.@Nonnull Context responseWriter(@Nonnull MediaType contentType, @Nonnull SneakyThrows.Consumer<PrintWriter> consumer) throws Exception
contentType
- Content type.consumer
- Writer consumer.Exception
- Is something goes wrong.@Nonnull Context responseWriter(@Nonnull MediaType contentType, @Nullable Charset charset, @Nonnull SneakyThrows.Consumer<PrintWriter> consumer) throws Exception
contentType
- Content type.charset
- Charset.consumer
- Writer consumer.Exception
- Is something goes wrong.@Nonnull Context sendRedirect(@Nonnull String location)
302
response.location
- Location.@Nonnull Context sendRedirect(@Nonnull StatusCode redirect, @Nonnull String location)
redirect
- Redirect status code.location
- Location.@Nonnull Context send(@Nonnull String data)
data
- Response. Use UTF-8 charset.@Nonnull Context send(@Nonnull String data, @Nonnull Charset charset)
data
- Response.charset
- Charset.@Nonnull Context send(@Nonnull byte[] data)
data
- Response.@Nonnull Context send(@Nonnull ByteBuffer data)
data
- Response.@Nonnull Context send(@Nonnull byte[]... data)
data
- Response.@Nonnull Context send(@Nonnull ByteBuffer[] data)
data
- Response.@Nonnull Context send(@Nonnull ReadableByteChannel channel)
channel
- Response input.@Nonnull Context send(@Nonnull InputStream input)
input
- Response.@Nonnull Context send(@Nonnull AttachedFile file)
file
- Attached file.@Nonnull Context send(@Nonnull Path file)
file
- File response.@Nonnull Context send(@Nonnull FileChannel file)
file
- File response.@Nonnull Context send(@Nonnull StatusCode statusCode)
statusCode
- Status code.@Nonnull Context sendError(@Nonnull Throwable cause)
Router.errorCode(Throwable)
.cause
- Error. If this is a fatal error it is going to be rethrow it.@Nonnull Context sendError(@Nonnull Throwable cause, @Nonnull StatusCode statusCode)
cause
- Error. If this is a fatal error it is going to be rethrow it.statusCode
- Status code.boolean isResponseStarted()
boolean getResetHeadersOnError()
RouterOption.RESET_HEADERS_ON_ERROR
.RouterOption.RESET_HEADERS_ON_ERROR
.@Nonnull Context setResetHeadersOnError(boolean value)
value
- True for reset/clear headers.@Nonnull Context onComplete(@Nonnull Route.Complete task)
task
- Task to execute.@Nonnull static Context readOnly(@Nonnull Context ctx)
ctx
- Originating context.@Nonnull static Context websocket(@Nonnull Context ctx, @Nonnull WebSocket ws)
send(byte[])
and send(String)
which
are delegated to the given web socket.
This context is necessary for creating a bridge between MessageEncoder
and WebSocket
.
This method is part of Public API, but direct usage is discourage.ctx
- Originating context.ws
- WebSocket.Copyright © 2020. All rights reserved.