forked from jooby-project/jooby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResponseHandler.java
More file actions
74 lines (70 loc) · 1.92 KB
/
ResponseHandler.java
File metadata and controls
74 lines (70 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby;
import javax.annotation.Nonnull;
import java.lang.reflect.Type;
/**
* Bind a route response type to a custom handler. The response handler works like a map function,
* which applies to a specific response type.
*
* For example if your route produces a <code>Foo</code> type as response. You can write a
* FooHandler that knows how to render the <code>Foo</code> object.
*
* Mapping is done efficiently, it doesn't test every single route response at runtime. Instead
* analysis is done only once at application startup time, it generates a unique route pipeline
* for all the routes that generates a <code>Foo</code> output.
*
* Example:
*
* <pre>{@code
* {
* boolean matches(Type type) {
* return Foo.class == type;
* }
*
* Route.Handler create(Route.Handler next) {
* return ctx -> {
* Foo foo = (Foo) next.apply(ctx);
* return ctx.sendString(foo.toString());
* }
* }
* }
* }</pre>
*
* @since 2.0.0
* @author edgar
*/
public interface ResponseHandler {
/**
* True if response route type is the one expected by the response handler.
*
* @param type Type to test.
* @return True if response route type is the one expected by the response handler.
*/
boolean matches(@Nonnull Type type);
/**
* Creates a handler for a response type. Example:
*
* <pre>{@code
* {
* boolean matches(Type type) {
* return Foo.class == type;
* }
*
* Route.Handler create(Route.Handler next) {
* return ctx -> {
* Foo foo = (Foo) next.apply(ctx);
* return ctx.sendString(foo.toString());
* }
* }
* }
* }</pre>
*
* @param next Next route in pipeline (usually the route handler).
* @return A response handler.
*/
@Nonnull Route.Handler create(Route.Handler next);
}