public interface Context
Modifier and Type | Field and Description |
---|---|
static String |
ACCEPT
Constant for
Accept header. |
static ZoneId |
GMT
Constant for GMT.
|
static DateTimeFormatter |
RFC1123
RFC1123 date formatter.
|
static String |
RFC1123_PATTERN
RFC1123 date pattern.
|
Modifier and Type | Method and Description |
---|---|
default 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.
|
default boolean |
accept(MediaType contentType)
True if the given type matches the `Accept` header.
|
default <T> T |
attribute(String key)
Get an attribute by his key.
|
default Context |
attribute(String key,
Object value)
Set an application attribute.
|
Body |
body()
HTTP body which provides access to body content.
|
default <T> T |
body(Class type)
Convert the HTTP body to the given type.
|
default <T> T |
body(Class type,
MediaType contentType)
Convert the HTTP body to the given type.
|
default <T> T |
body(Reified<T> type)
Convert the HTTP body to the given type.
|
default <T> T |
body(Reified<T> type,
MediaType contentType)
Convert the HTTP body to the given type.
|
Context |
detach(Runnable action)
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.
|
default FileUpload |
file(String name)
A file upload that matches the given field name.
|
default List<FileUpload> |
files()
All file uploads.
|
default List<FileUpload> |
files(String name)
All file uploads that matches the given field name.
|
Formdata |
form()
Formdata as
Value . |
default <T> T |
form(Class<T> type)
Convert formdata to the given type.
|
default <T> T |
form(Reified<T> type)
Convert formdata to the given type.
|
default Value |
form(String name)
Form field that matches the given name.
|
default Map<String,String> |
formMap()
Formdata as single-value map.
|
default Map<String,List<String>> |
formMultimap()
Formdata as multi-value map.
|
Map<String,Object> |
getAttributes()
Context attributes (a.k.a request attributes).
|
default String |
getHost()
The fully qualified name of the resource being requested, as obtained from the Host HTTP
header.
|
String |
getMethod()
HTTP method in upper-case form.
|
String |
getProtocol()
The name of the protocol the request.
|
String |
getRemoteAddress()
The IP address of the client or last proxy that sent the request.
|
default long |
getRequestLength()
Request
Content-Length header or -1 when missing. |
default MediaType |
getRequestType()
Request
Content-Type header or null when missing. |
default MediaType |
getRequestType(MediaType defaults)
Request
Content-Type header or null when missing. |
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.
|
StatusCode |
getStatusCode()
Get response status code.
|
default Value |
header(String name)
Get a header that matches the given name.
|
default Map<String,String> |
headerMap()
Header as single-value map.
|
default Map<String,List<String>> |
headerMultimap()
Header as multi-value map.
|
Value |
headers()
Request headers as
Value . |
boolean |
isInIoThread()
True when request runs in IO threads.
|
boolean |
isResponseStarted()
True if response already started.
|
Multipart |
multipart()
Get multipart data.
|
default <T> T |
multipart(Class<T> type)
Convert multipart data to the given type.
|
default <T> T |
multipart(Reified<T> type)
Convert multipart data to the given type.
|
default Value |
multipart(String name)
Get a multipart field that matches the given name.
|
default Map<String,String> |
multipartMap()
Multipart data as single-value map.
|
default Map<String,List<String>> |
multipartMultimap()
Multipart data as multi-value map.
|
default Parser |
parser(MediaType contentType)
Get a parser for the given content type or get an
StatusCode.UNSUPPORTED_MEDIA_TYPE . |
default Value |
path()
|
default <T> T |
path(Class<T> type)
Convert the
pathMap() to the given type. |
default <T> T |
path(Reified<T> type)
Convert the
pathMap() to the given type. |
default Value |
path(String name)
Path variable.
|
Map<String,String> |
pathMap()
Path map represent all the path keys with their values.
|
String |
pathString()
Request path without decoding (a.k.a raw Path) without query string.
|
QueryString |
query()
Query string as
Value object. |
default <T> T |
query(Class<T> type)
Convert the queryString to the given type.
|
default <T> T |
query(Reified<T> type)
Convert the queryString to the given type.
|
default Value |
query(String name)
Get a query parameter that matches the given name.
|
default Map<String,String> |
queryMap()
Query string as simple map.
|
default Map<String,List<String>> |
queryMultimap()
Query string as multi-value map.
|
default String |
queryString()
Query string with the leading
? or empty string. |
default 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.
|
default OutputStream |
responseStream(MediaType contentType)
HTTP response channel as output stream.
|
default Context |
responseStream(MediaType contentType,
Throwing.Consumer<OutputStream> consumer)
HTTP response channel as output stream.
|
default Context |
responseStream(Throwing.Consumer<OutputStream> consumer)
HTTP response channel as output stream.
|
default PrintWriter |
responseWriter()
HTTP response channel as response writer.
|
default PrintWriter |
responseWriter(MediaType contentType)
HTTP response channel as response writer.
|
PrintWriter |
responseWriter(MediaType contentType,
Charset charset)
HTTP response channel as response writer.
|
default Context |
responseWriter(MediaType contentType,
Charset charset,
Throwing.Consumer<PrintWriter> consumer)
HTTP response channel as response writer.
|
default Context |
responseWriter(MediaType contentType,
Throwing.Consumer<PrintWriter> consumer)
HTTP response channel as response writer.
|
default Context |
responseWriter(Throwing.Consumer<PrintWriter> consumer)
HTTP response channel as response writer.
|
default Context |
send(AttachedFile file)
Send a file attached response.
|
Context |
send(byte[] data)
Send response data.
|
default Context |
send(io.netty.buffer.ByteBuf 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.
|
default 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.
|
default Context |
send(String data)
Send response data.
|
Context |
send(String data,
Charset charset)
Send response data.
|
default Context |
sendError(Throwable cause)
Send an error response.
|
default Context |
sendError(Throwable cause,
StatusCode statusCode)
Send an error response.
|
default Context |
sendRedirect(StatusCode redirect,
String location)
Send a redirect response.
|
default Context |
sendRedirect(String location)
Send a
302 response. |
Context |
setDefaultResponseType(MediaType contentType)
Set the default response content type header.
|
default Context |
setHeader(String name,
Date value)
Set response header.
|
default Context |
setHeader(String name,
Instant value)
Set response header.
|
default Context |
setHeader(String name,
Object value)
Set response header.
|
Context |
setHeader(String name,
String value)
Set response header.
|
Context |
setPathMap(Map<String,String> pathMap)
Set path map.
|
Context |
setResponseLength(long length)
Set response content length header.
|
default 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 |
setStatusCode(int statusCode)
Set response status code.
|
default Context |
setStatusCode(StatusCode statusCode)
Set response status code.
|
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 default <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 default Context attribute(@Nonnull String key, Object value)
key
- Attribute key.value
- Attribute value.@Nonnull Router getRouter()
Jooby
.Jooby
.@Nonnull String getMethod()
@Nonnull Context setRoute(@Nonnull Route route)
route
- Matching route.@Nonnull String pathString()
@Nonnull default Value path(@Nonnull String name)
name
- Path key.null
reference.@Nonnull default <T> T path(@Nonnull Reified<T> type)
pathMap()
to the given type.T
- Target type.type
- Reified type.@Nonnull default <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()
Value
object.Value
object.@Nonnull default Value query(@Nonnull String name)
{
get("/search", ctx -> {
String q = ctx.query("q").value();
...
});
}
name
- Parameter name.@Nonnull default 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 default <T> T query(@Nonnull Reified<T> type)
T
- Target type.type
- Reified type.@Nonnull default <T> T query(@Nonnull Class<T> type)
T
- Target type.type
- Target type.@Nonnull default Map<String,String> queryMap()
{q: jooby, sort: name}
@Nonnull default Map<String,List<String>> queryMultimap()
{q: [jooby], sort: [name, id]}
@Nonnull default Value header(@Nonnull String name)
name
- Header name. Case insensitive.null
reference.@Nonnull default Map<String,String> headerMap()
@Nonnull default Map<String,List<String>> headerMultimap()
default boolean accept(@Nonnull MediaType contentType)
true
if there is no accept header.contentType
- Content type to match.default MediaType accept(@Nonnull List<MediaType> produceTypes)
produceTypes
- Produced types.@Nullable default MediaType getRequestType()
Content-Type
header or null
when missing.Content-Type
header or null
when missing.@Nonnull default 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.default long getRequestLength()
Content-Length
header or -1
when missing.Content-Length
header or -1
when missing.@Nonnull String getRemoteAddress()
@Nonnull default String getHost()
@Nonnull String getProtocol()
@Nonnull String getScheme()
@Nonnull Formdata form()
Value
. This method is for application/form-url-encoded
request.Value
. This method is for application/form-url-encoded
request.@Nonnull default Map<String,List<String>> formMultimap()
application/form-url-encoded
request.application/form-url-encoded
request.@Nonnull default Map<String,String> formMap()
application/form-url-encoded
request.application/form-url-encoded
request.@Nonnull default Value form(@Nonnull String name)
application/form-url-encoded
request.name
- Field name.@Nonnull default <T> T form(@Nonnull Reified<T> type)
application/form-url-encoded
request.T
- Target type.type
- Reified type.@Nonnull default <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 default Value multipart(@Nonnull String name)
Value.fileUpload()
or consider using the
file(String)
instead.
Only for multipart/form-data
request.name
- Field name.@Nonnull default <T> T multipart(@Nonnull Reified<T> type)
multipart/form-data
request.T
- Target type.type
- Reified type.@Nonnull default <T> T multipart(@Nonnull Class<T> type)
multipart/form-data
request.T
- Target type.type
- Target type.@Nonnull default Map<String,List<String>> multipartMultimap()
multipart/form-data
request.@Nonnull default Map<String,String> multipartMap()
multipart/form-data
request.@Nonnull default List<FileUpload> files()
multipart/form-data
request.@Nonnull default 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 default 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 default <T> T body(@Nonnull Reified<T> type)
T
- Conversion type.type
- Reified type.@Nonnull default <T> T body(@Nonnull Reified<T> type, @Nonnull MediaType contentType)
T
- Conversion type.type
- Reified type.contentType
- Body content type.@Nonnull default <T> T body(@Nonnull Class type)
T
- Conversion type.type
- Reified type.@Nonnull default <T> T body(@Nonnull Class type, @Nonnull MediaType contentType)
T
- Conversion type.type
- Reified type.contentType
- Body content type.@Nonnull default Parser parser(@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 Runnable action)
dispatch(Runnable)
except there is no thread dispatching here.
This operations integrates easily with third-party libraries liks rxJava or others.action
- Application code.@Nonnull default Context setHeader(@Nonnull String name, @Nonnull Date value)
name
- Header name.value
- Header value.@Nonnull default Context setHeader(@Nonnull String name, @Nonnull Instant value)
name
- Header name.value
- Header value.@Nonnull default Context setHeader(@Nonnull String name, @Nonnull Object value)
name
- Header name.value
- Header value.@Nonnull Context setHeader(@Nonnull String name, @Nonnull String value)
name
- Header name.value
- Header value.@Nonnull Context setResponseLength(long length)
length
- Response length.@Nonnull Context setResponseType(@Nonnull String contentType)
contentType
- Content type.@Nonnull default 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 default Context setStatusCode(@Nonnull StatusCode statusCode)
statusCode
- Status code.@Nonnull Context setStatusCode(int statusCode)
statusCode
- Status code.@Nonnull StatusCode getStatusCode()
@Nonnull default Context render(@Nonnull Object value)
value
- Object value.@Nonnull OutputStream responseStream()
@Nonnull default OutputStream responseStream(@Nonnull MediaType contentType)
contentType
- Media type.@Nonnull default Context responseStream(@Nonnull MediaType contentType, @Nonnull Throwing.Consumer<OutputStream> consumer) throws Exception
contentType
- Content type.consumer
- Output stream consumer.Exception
- Is something goes wrong.@Nonnull default Context responseStream(@Nonnull Throwing.Consumer<OutputStream> consumer) throws Exception
consumer
- Output stream consumer.Exception
- Is something goes wrong.@Nonnull Sender responseSender()
@Nonnull default PrintWriter responseWriter()
@Nonnull default PrintWriter responseWriter(@Nonnull MediaType contentType)
contentType
- Content type.@Nonnull PrintWriter responseWriter(@Nonnull MediaType contentType, @Nullable Charset charset)
contentType
- Content type.charset
- Charset.@Nonnull default Context responseWriter(@Nonnull Throwing.Consumer<PrintWriter> consumer) throws Exception
consumer
- Writer consumer.Exception
- Is something goes wrong.@Nonnull default Context responseWriter(@Nonnull MediaType contentType, @Nonnull Throwing.Consumer<PrintWriter> consumer) throws Exception
contentType
- Content type.consumer
- Writer consumer.Exception
- Is something goes wrong.@Nonnull default Context responseWriter(@Nonnull MediaType contentType, @Nullable Charset charset, @Nonnull Throwing.Consumer<PrintWriter> consumer) throws Exception
contentType
- Content type.charset
- Charset.consumer
- Writer consumer.Exception
- Is something goes wrong.@Nonnull default Context sendRedirect(@Nonnull String location)
302
response.location
- Location.@Nonnull default Context sendRedirect(@Nonnull StatusCode redirect, @Nonnull String location)
redirect
- Redirect status code.location
- Location.@Nonnull default 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 default Context send(@Nonnull io.netty.buffer.ByteBuf data)
data
- Response.@Nonnull Context send(@Nonnull ReadableByteChannel channel)
channel
- Response input.@Nonnull Context send(@Nonnull InputStream input)
input
- Response.@Nonnull default Context send(@Nonnull AttachedFile file)
file
- Attached file.@Nonnull default 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 default Context sendError(@Nonnull Throwable cause)
Router.errorCode(Throwable)
.cause
- Error. If this is a fatal error it is going to be rethrow it.@Nonnull default 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()
Copyright © 2019. All rights reserved.