public interface ByContentSpec
The handler to use will be selected based on parsing the "Accept" header, respecting quality weighting and wildcard matching. The order that types are specified is significant for wildcard matching. The earliest registered type that matches the wildcard will be used.
import ratpack.test.embed.EmbeddedApp;
import ratpack.http.client.ReceivedResponse;
import static org.junit.Assert.*;
public class Example {
public static void main(String[] args) throws Exception {
EmbeddedApp.fromHandler(ctx -> {
String message = "hello!";
ctx.byContent(m -> m
.json(() -> ctx.render("{\"msg\": \"" + message + "\"}"))
.html(() -> ctx.render("<p>" + message + "</p>"))
);
}).test(httpClient -> {
ReceivedResponse response = httpClient.requestSpec(s -> s.getHeaders().add("Accept", "application/json")).get();
assertEquals("{\"msg\": \"hello!\"}", response.getBody().getText());
assertEquals("application/json", response.getBody().getContentType().getType());
response = httpClient.requestSpec(s -> s.getHeaders().add("Accept", "text/plain; q=1.0, text/html; q=0.8, application/json; q=0.7")).get();
assertEquals("<p>hello!</p>", response.getBody().getText());
assertEquals("text/html", response.getBody().getContentType().getType());
});
}
}
If there is no type registered, or if the client does not accept any of the given types, the "noMatch" handler will be used.
By default, the "noMatch" handler will issue a 406
error via Context.clientError(int)
.
If you want a different behavior, use noMatch(ratpack.func.Block)
.
If the request lacks a usable Accept header (header not present or has an empty value), the "unspecified" handler will be used.
By default, the "unspecified" handler will use the handler for the first registered content type.
If you want a different behavior, use unspecified(ratpack.func.Block)
.
Only the last specified handler for a type will be used. That is, adding a subsequent handler for the same type will replace the previous.
Modifier and Type | Method and Description |
---|---|
default ByContentSpec |
html(Block block)
Specifies that the given handler should be used if the client wants content of type "text/html".
|
ByContentSpec |
html(java.lang.Class<? extends Handler> handlerType)
Specifies that the given handler should be used if the client wants content of type "text/html".
|
ByContentSpec |
html(Handler handler)
Specifies that the given handler should be used if the client wants content of type "text/html".
|
default ByContentSpec |
json(Block block)
Specifies that the given handler should be used if the client wants content of type "application/json".
|
ByContentSpec |
json(java.lang.Class<? extends Handler> handlerType)
Specifies that the given handler should be used if the client wants content of type "application/json".
|
ByContentSpec |
json(Handler handler)
Specifies that the given handler should be used if the client wants content of type "application/json".
|
default ByContentSpec |
noMatch(Block block)
Specifies that the given handler should be used if the client's requested content type cannot be matched with any of the other handlers.
|
ByContentSpec |
noMatch(java.lang.Class<? extends Handler> handlerType)
Specifies that the given handler should be used if the client's requested content type cannot be matched with any of the other handlers.
|
ByContentSpec |
noMatch(Handler handler)
Specifies that the given handler should be used if the client's requested content type cannot be matched with any of the other handlers.
|
ByContentSpec |
noMatch(java.lang.String mimeType)
Specifies that the handler for the specified content type should be used if the client's requested content type cannot be matched with any of the other handlers.
|
default ByContentSpec |
plainText(Block block)
Specifies that the given handler should be used if the client wants content of type "text/plain".
|
ByContentSpec |
plainText(java.lang.Class<? extends Handler> handlerType)
Specifies that the given handler should be used if the client wants content of type "text/plain".
|
ByContentSpec |
plainText(Handler handler)
Specifies that the given handler should be used if the client wants content of type "text/plain".
|
default ByContentSpec |
type(java.lang.CharSequence mimeType,
Block block)
Specifies that the given handler should be used if the client wants content of the given MIME type.
|
ByContentSpec |
type(java.lang.CharSequence mimeType,
java.lang.Class<? extends Handler> handlerType)
Specifies that the given handler should be used if the client wants content of the given MIME type.
|
ByContentSpec |
type(java.lang.CharSequence mimeType,
Handler handler)
Specifies that the given handler should be used if the client wants content of the given MIME type.
|
default ByContentSpec |
type(java.lang.String mimeType,
Block block)
Specifies that the given handler should be used if the client wants content of the given MIME type.
|
ByContentSpec |
type(java.lang.String mimeType,
java.lang.Class<? extends Handler> handlerType)
Specifies that the given handler should be used if the client wants content of the given MIME type.
|
ByContentSpec |
type(java.lang.String mimeType,
Handler handler)
Specifies that the given handler should be used if the client wants content of the given MIME type.
|
default ByContentSpec |
unspecified(Block block)
Specifies that the given handler should be used if the client did not provide a usable "Accept" header in the request.
|
ByContentSpec |
unspecified(java.lang.Class<? extends Handler> handlerType)
Specifies that the given handler should be used if the client did not provide a usable "Accept" header in the request.
|
ByContentSpec |
unspecified(Handler handler)
Specifies that the given handler should be used if the client did not provide a usable "Accept" header in the request.
|
ByContentSpec |
unspecified(java.lang.String mimeType)
Specifies that the handler for the specified content type should be used if the client did not provide a usable "Accept" header in the request.
|
default ByContentSpec |
xml(Block block)
Specifies that the given handler should be used if the client wants content of type "application/xml".
|
ByContentSpec |
xml(java.lang.Class<? extends Handler> handlerType)
Specifies that the given handler should be used if the client wants content of type "application/xml".
|
ByContentSpec |
xml(Handler handler)
Specifies that the given handler should be used if the client wants content of type "application/xml".
|
default ByContentSpec type(java.lang.String mimeType, Block block)
mimeType
- the MIME type to register forblock
- the code to invoke if the content type matchesdefault ByContentSpec type(java.lang.CharSequence mimeType, Block block)
mimeType
- the MIME type to register forblock
- the code to invoke if the content type matchesByContentSpec type(java.lang.String mimeType, Handler handler)
mimeType
- the MIME type to register forhandler
- the handler to invoke if the content type matchesByContentSpec type(java.lang.CharSequence mimeType, Handler handler)
mimeType
- the MIME type to register forhandler
- the handler to invoke if the content type matchesByContentSpec type(java.lang.String mimeType, java.lang.Class<? extends Handler> handlerType)
mimeType
- the MIME type to register forhandlerType
- the type of handler to retrieve from the registry and useByContentSpec type(java.lang.CharSequence mimeType, java.lang.Class<? extends Handler> handlerType)
mimeType
- the MIME type to register forhandlerType
- the type of handler to retrieve from the registry and usedefault ByContentSpec plainText(Block block)
block
- the code to invoke if the content type matchesByContentSpec plainText(Handler handler)
handler
- the handler to invoke if the content type matchesByContentSpec plainText(java.lang.Class<? extends Handler> handlerType)
handlerType
- the type of handler to retrieve from the registry and usedefault ByContentSpec html(Block block)
block
- the code to invoke if the content type matchesByContentSpec html(Handler handler)
handler
- the handler to invoke if the content type matchesByContentSpec html(java.lang.Class<? extends Handler> handlerType)
handlerType
- the type of handler to retrieve from the registry and usedefault ByContentSpec json(Block block)
block
- the code to invoke if the content type matchesByContentSpec json(Handler handler)
handler
- the handler to invoke if the content type matchesByContentSpec json(java.lang.Class<? extends Handler> handlerType)
handlerType
- the type of handler to retrieve from the registry and usedefault ByContentSpec xml(Block block)
block
- the code to invoke if the content type matchesByContentSpec xml(Handler handler)
handler
- the handler to invoke if the content type matchesByContentSpec xml(java.lang.Class<? extends Handler> handlerType)
handlerType
- the type of handler to retrieve from the registry and usedefault ByContentSpec noMatch(Block block)
block
- the code to invoke if the content type doesn't matchByContentSpec noMatch(Handler handler)
handler
- the handler to invoke if the content type matchesByContentSpec noMatch(java.lang.Class<? extends Handler> handlerType)
handlerType
- the type of handler to retrieve from the registry and useByContentSpec noMatch(java.lang.String mimeType)
mimeType
- the MIME type to use as a fallback if the requested type can't be matcheddefault ByContentSpec unspecified(Block block)
block
- the code to invoke if no usable "Accept" header is present in the request.ByContentSpec unspecified(Handler handler)
handler
- the handler to invoke if if no usable "Accept" header is present in the request.ByContentSpec unspecified(java.lang.Class<? extends Handler> handlerType)
handlerType
- the type of handler to retrieve from the registry and use if no usable "Accept" header is present in the request.ByContentSpec unspecified(java.lang.String mimeType)
mimeType
- the MIME type to use as a fallback if no type is requested