/
Handlers.java
313 lines (284 loc) · 10.5 KB
/
Handlers.java
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ratpack.handling;
import ratpack.api.Nullable;
import ratpack.file.internal.AssetHandler;
import ratpack.file.internal.FileSystemBindingHandler;
import ratpack.func.Action;
import ratpack.handling.internal.*;
import ratpack.http.internal.AcceptsHandler;
import ratpack.http.internal.ContentTypeHandler;
import ratpack.http.internal.HeaderHandler;
import ratpack.http.internal.MethodHandler;
import ratpack.launch.LaunchConfig;
import ratpack.path.PathBinder;
import ratpack.path.internal.PathHandler;
import ratpack.path.internal.TokenPathBinder;
import ratpack.registry.Registry;
import java.util.List;
import static com.google.common.collect.ImmutableList.copyOf;
/**
* Factory methods for handler decorations.
*/
public abstract class Handlers {
private Handlers() {
}
/**
* A handler that delegates to the next handler if the request claims that it can accept one of the given types, otherwise raises a 406 client error.
*
* @param contentTypes The content types to verify that the request can support for the response
* @return A handler
*/
public static Handler accepts(String... contentTypes) {
return new AcceptsHandler(contentTypes);
}
/**
* A handler that serves static assets at the given file system path, relative to the contextual file system binding.
* <p>
* The file to serve is calculated based on the contextual {@link ratpack.file.FileSystemBinding} and the
* contextual {@link ratpack.path.PathBinding}.
* The {@link ratpack.path.PathBinding#getPastBinding()} of the contextual path binding is used to find a file/directory
* relative to the contextual file system binding.
* <p>
* If the request matches a directory, an index file may be served.
* The {@code indexFiles} array specifies the names of files to look for in order to serve.
* <p>
* If no file can be found to serve, then control will be delegated to the next handler.
*
* @param launchConfig The application launch config
* @param path The relative path to the location of the assets to serve
* @param indexFiles The index files to try if the request is for a directory
* @return A handler
*/
public static Handler assets(LaunchConfig launchConfig, String path, List<String> indexFiles) {
Handler handler = new AssetHandler(copyOf(indexFiles));
return fileSystem(launchConfig, path, handler);
}
/**
* Builds a handler chain, with no backing registry.
*
* @param launchConfig The application launch config
* @param action The chain definition
* @return A handler
* @throws Exception any thrown by {@code action}
*/
public static Handler chain(LaunchConfig launchConfig, Action<? super Chain> action) throws Exception {
return chain(launchConfig, null, action);
}
/**
* Builds a chain, backed by the given registry.
*
* @param launchConfig The application launch config
* @param registry The registry.
* @param action The chain building action.
* @return A handler
* @throws Exception any thrown by {@code action}
*/
public static Handler chain(@Nullable LaunchConfig launchConfig, @Nullable Registry registry, Action<? super Chain> action) throws Exception {
return ChainBuilders.build(launchConfig != null && launchConfig.isReloadable(), new ChainActionTransformer(launchConfig, registry), action);
}
/**
* Creates a handler chain from the given handlers.
*
* @param handlers The handlers to connect into a chain
* @return A new handler that is the given handlers connected into a chain
*/
public static Handler chain(List<? extends Handler> handlers) {
if (handlers.size() == 0) {
return next();
} else if (handlers.size() == 1) {
return handlers.get(0);
} else {
return new ChainHandler(handlers);
}
}
/**
* Creates a handler chain from the given handlers.
*
* @param handlers The handlers to connect into a chain
* @return A new handler that is the given handlers connected into a chain
*/
public static Handler chain(Handler... handlers) {
if (handlers.length == 0) {
return next();
} else if (handlers.length == 1) {
return handlers[0];
} else {
return new ChainHandler(handlers);
}
}
/**
* A handler that simply calls {@link Context#clientError(int)} with the given status code.
*
* @param statusCode The 4xx client error status code
* @return A handler
*/
public static Handler clientError(int statusCode) {
return new ClientErrorForwardingHandler(statusCode);
}
/**
* A handler that delegates to the next handler if the content type of the request is one of the given types, otherwise raises a 415 client error.
*
* @param contentTypes The request content types to require
* @return A handler
*/
public static Handler contentTypes(String... contentTypes) {
return new ContentTypeHandler(contentTypes);
}
/**
* A handler that delegates to the next handler if the request is DELETE, otherwise raises a 405 client error.
*
* @return A handler
*/
public static Handler delete() {
return MethodHandler.DELETE;
}
/**
* A handlers that changes the {@link ratpack.file.FileSystemBinding} for the given handlers.
* <p>
* The new file system binding will be created by the {@link ratpack.file.FileSystemBinding#binding(String)} method of the contextual binding.
*
* @param launchConfig The application launch config
* @param path The relative path to the new file system binding point
* @param handler The handler to execute with the new file system binding
* @return A handler
*/
public static Handler fileSystem(LaunchConfig launchConfig, String path, Handler handler) {
return new FileSystemBindingHandler(launchConfig, path, handler);
}
/**
* A handler that delegates to the next handler if the request is GET, otherwise raises a 405 client error.
*
* @return A handler
*/
public static Handler get() {
return MethodHandler.GET;
}
/**
* Creates a handler that delegates to the given handler if the {@code request} has a {@code HTTPHeader} with the
* given name and a it's value matches the given value exactly.
*
* @param headerName the name of the HTTP Header to match on
* @param headerValue the value of the HTTP Header to match on
* @param handler the handler to delegate to
* @return A handler
*/
public static Handler header(String headerName, String headerValue, Handler handler) {
return new HeaderHandler(headerName, headerValue, handler);
}
/**
* A handler that simply delegates to the next handler.
* <p>
* Effectively a noop.
*
* @return A handler
*/
public static Handler next() {
return NextHandler.INSTANCE;
}
/**
* Convenience for {@link #clientError(int) clientError(404)}.
*
* @return A handler
*/
public static Handler notFound() {
return ClientErrorForwardingHandler.NOT_FOUND;
}
/**
* A handler that delegates to the next handler if the request is PATCH, otherwise raises a 405 client error.
*
* @return A handler
*/
public static Handler patch() {
return MethodHandler.PATCH;
}
/**
* Creates a handler that delegates to the given handlers if the request matches the given path exactly.
* <p>
* The {@code path} is relative to the contextual {@link ratpack.path.PathBinding} of the exchange.
* <p>
* A new contextual {@link ratpack.path.PathBinding} will be established for the given handlers,
* using the given path as the bind point.
*
* @param path The exact path to match to
* @param handler The handlers to delegate to if the path matches
* @return A handler
*/
public static Handler path(String path, Handler handler) {
return path(new TokenPathBinder(path, true), handler);
}
/**
* Creates a handler that delegates to the given handlers if the request can be bound by the given path binder.
*
* @param pathBinder The path binder that may bind to the request path
* @param handler The handlers to delegate to if path binder does bind to the path
* @return A handler
*/
public static Handler path(PathBinder pathBinder, Handler handler) {
return new PathHandler(pathBinder, handler);
}
/**
* A handler that delegates to the next handler if the request is POST, otherwise raises a 405 client error.
*
* @return A handler
*/
public static Handler post() {
return MethodHandler.POST;
}
/**
* Creates a handler that delegates to the given handlers if the request path starts with the given prefix.
* <p>
* The {@code prefix} is relative to the contextual {@link ratpack.path.PathBinding} of the exchange.
* <p>
* A new contextual {@link ratpack.path.PathBinding} will be established for the given handlers,
* using the given prefix as the bind point.
*
* @param prefix The path prefix to match
* @param handler The handler to delegate to
* @return A handler
*/
public static Handler prefix(String prefix, Handler handler) {
return path(new TokenPathBinder(prefix, false), handler);
}
/**
* A handler that delegates to the next handler if the request is PUT, otherwise raises a 405 client error.
*
* @return A handler
*/
public static Handler put() {
return MethodHandler.PUT;
}
/**
* A handler that simply calls {@link Context#insert(Registry, Handler...)} with the given registry and handler.
*
* @param registry the registry to insert
* @param handler The handler to insert
* @return A handler
*/
public static Handler register(Registry registry, Handler handler) {
return new RegistryInsertHandler(registry, handler);
}
/**
* A handler that simply calls {@link Context#next(Registry)} with the given registry.
*
* @param registry The registry to make available to the next handlers
* @return A handler
* @see Context#next(Registry)
*/
public static Handler register(Registry registry) {
return new RegistryNextHandler(registry);
}
}