Skip to content

Commit c114bce

Browse files
committed
Cannot use chain.next() with current MVC route implementation fix #924
1 parent a088ffb commit c114bce

File tree

10 files changed

+536
-458
lines changed

10 files changed

+536
-458
lines changed

jooby/src/main/java/org/jooby/Jooby.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2949,6 +2949,8 @@ private Injector bootstrap(final Config args,
29492949

29502950
binder.bind(Request.class).toProvider(Providers.outOfScope(Request.class))
29512951
.in(RequestScoped.class);
2952+
binder.bind(Route.Chain.class).toProvider(Providers.outOfScope(Route.Chain.class))
2953+
.in(RequestScoped.class);
29522954
binder.bind(Response.class).toProvider(Providers.outOfScope(Response.class))
29532955
.in(RequestScoped.class);
29542956
/** server sent event */

jooby/src/main/java/org/jooby/internal/HttpHandlerImpl.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,8 @@ public boolean equals(final Object obj) {
312312

313313
private static final Key<Request> REQ = Key.get(Request.class);
314314

315+
private static final Key<Route.Chain> CHAIN = Key.get(Route.Chain.class);
316+
315317
private static final Key<Response> RSP = Key.get(Response.class);
316318

317319
private static final Key<Sse> SSE = Key.get(Sse.class);
@@ -489,7 +491,9 @@ public void handle(final NativeRequest request, final NativeResponse response) t
489491
Route[] routes = routeCache
490492
.getUnchecked(new RouteKey(method, path, type, req.accept()));
491493

492-
new RouteChain(req, rsp, routes).next(req, rsp);
494+
RouteChain chain = new RouteChain(req, rsp, routes);
495+
scope.put(CHAIN, chain);
496+
chain.next(req, rsp);
493497

494498
} catch (DeferredExecution ex) {
495499
deferred = true;

jooby/src/main/java/org/jooby/internal/MappedHandler.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,18 +215,23 @@
215215
@SuppressWarnings({"unchecked", "rawtypes"})
216216
public class MappedHandler implements Filter {
217217

218-
private Throwing.Function2<Request, Response, Object> supplier;
218+
private Throwing.Function3<Request, Response, Route.Chain, Object> supplier;
219219
private Mapper mapper;
220220

221-
public MappedHandler(final Throwing.Function2<Request, Response, Object> supplier,
221+
public MappedHandler(final Throwing.Function3<Request, Response, Route.Chain, Object> supplier,
222222
final Route.Mapper mapper) {
223223
this.supplier = supplier;
224224
this.mapper = mapper;
225225
}
226226

227+
public MappedHandler(final Throwing.Function2<Request, Response, Object> supplier,
228+
final Route.Mapper mapper) {
229+
this((req, rsp, chain) -> supplier.apply(req, rsp), mapper);
230+
}
231+
227232
@Override
228233
public void handle(final Request req, final Response rsp, final Chain chain) throws Throwable {
229-
Object input = supplier.apply(req, rsp);
234+
Object input = supplier.apply(req, rsp, chain);
230235
Object output = Try
231236
.apply(() -> mapper.map(input))
232237
.recover(ClassCastException.class, input)

jooby/src/main/java/org/jooby/internal/RouteImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,8 @@ public RouteImpl(final Filter filter, final Definition route, final String metho
258258
if (((MvcHandler) filter).method().getReturnType() == void.class) {
259259
this.filter = filter;
260260
} else {
261-
this.filter = new MappedHandler((req, rsp) -> ((MvcHandler) filter).invoke(req, rsp),
261+
this.filter = new MappedHandler((req, rsp, chain) -> ((MvcHandler) filter).invoke(req, rsp,
262+
chain),
262263
mapper);
263264
}
264265
} else {

jooby/src/main/java/org/jooby/internal/mvc/MvcHandler.java

Lines changed: 217 additions & 215 deletions
Large diffs are not rendered by default.

jooby/src/main/java/org/jooby/internal/mvc/RequestParam.java

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ public class RequestParam {
236236

237237
private interface GetValue {
238238

239-
Object apply(Request req, Response rsp, RequestParam param) throws Exception;
239+
Object apply(Request req, Response rsp, Route.Chain chain, RequestParam param) throws Exception;
240240

241241
}
242242

@@ -255,56 +255,60 @@ private interface GetValue {
255255
/**
256256
* Body
257257
*/
258-
builder.put(bodyType, (req, rsp, param) -> req.body().to(param.type));
258+
builder.put(bodyType, (req, rsp, chain, param) -> req.body().to(param.type));
259259
/**
260260
* Request
261261
*/
262-
builder.put(TypeLiteral.get(Request.class), (req, rsp, param) -> req);
262+
builder.put(TypeLiteral.get(Request.class), (req, rsp, chain, param) -> req);
263263
/**
264264
* Route
265265
*/
266-
builder.put(TypeLiteral.get(Route.class), (req, rsp, param) -> req.route());
266+
builder.put(TypeLiteral.get(Route.class), (req, rsp, chain, param) -> req.route());
267267
/**
268268
* Response
269269
*/
270-
builder.put(TypeLiteral.get(Response.class), (req, rsp, param) -> rsp);
270+
builder.put(TypeLiteral.get(Response.class), (req, rsp, chain, param) -> rsp);
271+
/**
272+
* Route.Chain
273+
*/
274+
builder.put(TypeLiteral.get(Route.Chain.class), (req, rsp, chain, param) -> chain);
271275
/**
272276
* Session
273277
*/
274-
builder.put(TypeLiteral.get(Session.class), (req, rsp, param) -> req.session());
278+
builder.put(TypeLiteral.get(Session.class), (req, rsp, chain, param) -> req.session());
275279
builder.put(TypeLiteral.get(Types.newParameterizedType(Optional.class, Session.class)),
276-
(req, rsp, param) -> req.ifSession());
280+
(req, rsp, chain, param) -> req.ifSession());
277281

278282
/**
279283
* Files
280284
*/
281-
builder.put(TypeLiteral.get(Upload.class), (req, rsp, param) -> req.file(param.name));
285+
builder.put(TypeLiteral.get(Upload.class), (req, rsp, chain, param) -> req.file(param.name));
282286
builder.put(TypeLiteral.get(Types.newParameterizedType(Optional.class, Upload.class)),
283-
(req, rsp, param) -> {
287+
(req, rsp, chain, param) -> {
284288
List<Upload> files = req.files(param.name);
285289
return files.size() == 0 ? Optional.empty() : Optional.of(files.get(0));
286290
});
287291
builder.put(TypeLiteral.get(Types.newParameterizedType(List.class, Upload.class)),
288-
(req, rsp, param) -> req.files(param.name));
292+
(req, rsp, chain, param) -> req.files(param.name));
289293

290294
/**
291295
* Cookie
292296
*/
293-
builder.put(TypeLiteral.get(Cookie.class), (req, rsp, param) -> req.cookies().stream()
297+
builder.put(TypeLiteral.get(Cookie.class), (req, rsp, chain, param) -> req.cookies().stream()
294298
.filter(c -> c.name().equalsIgnoreCase(param.name)).findFirst().get());
295-
builder.put(TypeLiteral.get(Types.listOf(Cookie.class)), (req, rsp, param) -> req.cookies());
299+
builder.put(TypeLiteral.get(Types.listOf(Cookie.class)), (req, rsp, chain, param) -> req.cookies());
296300
builder.put(TypeLiteral.get(Types.newParameterizedType(Optional.class, Cookie.class)),
297-
(req, rsp, param) -> req.cookies().stream()
301+
(req, rsp, chain, param) -> req.cookies().stream()
298302
.filter(c -> c.name().equalsIgnoreCase(param.name)).findFirst());
299303
/**
300304
* Header
301305
*/
302-
builder.put(headerType, (req, rsp, param) -> req.header(param.name).to(param.type));
306+
builder.put(headerType, (req, rsp, chain, param) -> req.header(param.name).to(param.type));
303307

304308
/**
305309
* Local
306310
*/
307-
builder.put(localType, (req, rsp, param) -> {
311+
builder.put(localType, (req, rsp, chain, param) -> {
308312
if (param.type.getRawType() == Map.class) {
309313
return req.attributes();
310314
}
@@ -318,7 +322,7 @@ private interface GetValue {
318322
/**
319323
* Flash
320324
*/
321-
builder.put(flashType, (req, rsp, param) -> {
325+
builder.put(flashType, (req, rsp, chain, param) -> {
322326
Class rawType = param.type.getRawType();
323327
if (Map.class.isAssignableFrom(rawType)) {
324328
return req.flash();
@@ -360,8 +364,8 @@ public RequestParam(final AnnotatedElement elem, final String name, final Type t
360364
this.strategy = injector.getOrDefault(strategyType, param());
361365
}
362366

363-
public Object value(final Request req, final Response rsp) throws Throwable {
364-
return strategy.apply(req, rsp, this);
367+
public Object value(final Request req, final Response rsp, final Route.Chain chain) throws Throwable {
368+
return strategy.apply(req, rsp, chain, this);
365369
}
366370

367371
public static String nameFor(final Parameter param) {
@@ -387,7 +391,7 @@ private static String findName(final AnnotatedElement elem) {
387391
}
388392

389393
private static final GetValue param() {
390-
return (req, rsp, param) -> {
394+
return (req, rsp, chain, param) -> {
391395
Mutant mutant = req.param(param.name);
392396
if (mutant.isSet() || param.optional) {
393397
return mutant.to(param.type);
@@ -400,5 +404,4 @@ private static final GetValue param() {
400404
}
401405
};
402406
}
403-
404407
}

0 commit comments

Comments
 (0)