Skip to content

Commit 9af5e36

Browse files
committed
route: remove handle field
- more code cleanup
1 parent eee7937 commit 9af5e36

7 files changed

Lines changed: 32 additions & 61 deletions

File tree

jooby/src/main/java/io/jooby/Route.java

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -442,8 +442,6 @@ public MethodHandle toMethodHandle() {
442442

443443
private MessageEncoder encoder;
444444

445-
private Object handle;
446-
447445
private List<MediaType> produces = EMPTY_LIST;
448446

449447
private List<MediaType> consumes = EMPTY_LIST;
@@ -477,7 +475,6 @@ public Route(@NonNull String method, @NonNull String pattern, @NonNull Handler h
477475
this.method = method.toUpperCase();
478476
this.pattern = pattern;
479477
this.handler = handler;
480-
this.handle = handler;
481478
}
482479

483480
/**
@@ -561,17 +558,6 @@ public Route(@NonNull String method, @NonNull String pattern, @NonNull Handler h
561558
return Router.reverse(getPattern(), values);
562559
}
563560

564-
/**
565-
* Handler instance which might or might not be the same as {@link #getHandler()}.
566-
*
567-
* <p>The handle is required to extract correct metadata.
568-
*
569-
* @return Handle.
570-
*/
571-
public @NonNull Object getHandle() {
572-
return handle;
573-
}
574-
575561
/**
576562
* After filter or <code>null</code>.
577563
*
@@ -612,17 +598,6 @@ public Route(@NonNull String method, @NonNull String pattern, @NonNull Handler h
612598
return this;
613599
}
614600

615-
/**
616-
* Set route handle instance, required when handle is different from {@link #getHandler()}.
617-
*
618-
* @param handle Handle instance.
619-
* @return This route.
620-
*/
621-
public @NonNull Route setHandle(@NonNull Object handle) {
622-
this.handle = handle;
623-
return this;
624-
}
625-
626601
/**
627602
* Set route pipeline. This method is part of public API but isn't intended to be used by public.
628603
*
@@ -1112,17 +1087,11 @@ private void addHttpMethod(boolean enabled, String httpMethod) {
11121087
}
11131088

11141089
private Route.Handler computePipeline() {
1115-
Route.Handler pipeline = computeHeadPipeline();
1090+
Route.Handler pipeline = filter == null ? handler : filter.then(handler);
11161091

11171092
if (after != null) {
11181093
pipeline = pipeline.then(after);
11191094
}
11201095
return pipeline;
11211096
}
1122-
1123-
private Route.Handler computeHeadPipeline() {
1124-
Route.Handler pipeline = filter == null ? handler : filter.then(handler);
1125-
1126-
return pipeline;
1127-
}
11281097
}

jooby/src/main/java/io/jooby/WebSocket.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ interface Initializer {
5555
void init(@NonNull Context ctx, @NonNull WebSocketConfigurer configurer);
5656
}
5757

58+
interface Handler extends Route.Handler {
59+
Initializer getInitializer();
60+
}
61+
5862
/** On connect callback. */
5963
interface OnConnect {
6064
/**

jooby/src/main/java/io/jooby/internal/RouterImpl.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -446,14 +446,12 @@ public Router setOutputFactory(@NonNull BufferedOutputFactory outputFactory) {
446446

447447
@NonNull @Override
448448
public Route ws(@NonNull String pattern, @NonNull WebSocket.Initializer handler) {
449-
return route(WS, pattern, new WebSocketHandler(handler)).setHandle(handler);
449+
return route(WS, pattern, new WebSocketHandler(handler));
450450
}
451451

452452
@NonNull @Override
453453
public Route sse(@NonNull String pattern, @NonNull ServerSentEmitter.Handler handler) {
454-
return route(SSE, pattern, new ServerSentEventHandler(handler))
455-
.setHandle(handler)
456-
.setExecutorKey("worker");
454+
return route(SSE, pattern, new ServerSentEventHandler(handler)).setExecutorKey("worker");
457455
}
458456

459457
@Override
@@ -889,7 +887,6 @@ private void copy(Route src, Route it) {
889887
it.setFilter(filter);
890888
it.setAfter(after);
891889
it.setEncoder(src.getEncoder());
892-
it.setHandle(src.getHandle());
893890
it.setProduces(src.getProduces());
894891
it.setConsumes(src.getConsumes());
895892
it.setAttributes(src.getAttributes());

jooby/src/main/java/io/jooby/internal/handler/WebSocketHandler.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,20 @@
77

88
import edu.umd.cs.findbugs.annotations.NonNull;
99
import io.jooby.Context;
10-
import io.jooby.Route;
1110
import io.jooby.StatusCode;
1211
import io.jooby.WebSocket;
1312

14-
public class WebSocketHandler implements Route.Handler {
13+
public class WebSocketHandler implements WebSocket.Handler {
1514
private WebSocket.Initializer handler;
1615

1716
public WebSocketHandler(WebSocket.Initializer handler) {
1817
this.handler = handler;
1918
}
2019

20+
public WebSocket.Initializer getInitializer() {
21+
return handler;
22+
}
23+
2124
@NonNull @Override
2225
public Object apply(@NonNull Context ctx) {
2326
boolean webSocket = ctx.header("Upgrade").value("").equalsIgnoreCase("WebSocket");

modules/jooby-kotlin/src/main/kotlin/io/jooby/kt/CoroutineRouter.kt

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -141,28 +141,26 @@ class CoroutineRouter(val coroutineStart: CoroutineStart, val router: Router) {
141141
route(OPTIONS, pattern, handler)
142142

143143
fun route(method: String, pattern: String, handler: suspend HandlerContext.() -> Any): Route =
144-
router
145-
.route(method, pattern) { ctx ->
146-
val handlerContext = HandlerContext(ctx)
147-
launch(handlerContext) {
144+
router.route(method, pattern) { ctx ->
145+
val handlerContext = HandlerContext(ctx)
146+
launch(handlerContext) {
147+
try {
148+
val result = handler(handlerContext)
149+
ctx.route.after?.apply(ctx, result, null)
150+
if (result != ctx && !ctx.isResponseStarted) {
151+
ctx.render(result)
152+
}
153+
} catch (cause: Throwable) {
148154
try {
149-
val result = handler(handlerContext)
150-
ctx.route.after?.apply(ctx, result, null)
151-
if (result != ctx && !ctx.isResponseStarted) {
152-
ctx.render(result)
153-
}
154-
} catch (cause: Throwable) {
155-
try {
156-
ctx.route.after?.apply(ctx, null, cause)
157-
} finally {
158-
errorHandler.invoke(ErrorHandlerContext(ctx, cause, router.errorCode(cause)))
159-
}
155+
ctx.route.after?.apply(ctx, null, cause)
156+
} finally {
157+
errorHandler.invoke(ErrorHandlerContext(ctx, cause, router.errorCode(cause)))
160158
}
161159
}
162-
// Return context to mark as handled
163-
ctx
164160
}
165-
.setHandle(handler)
161+
// Return context to mark as handled
162+
ctx
163+
}
166164

167165
internal fun launch(handlerContext: HandlerContext, block: suspend CoroutineScope.() -> Unit) {
168166
// Global catch-all exception handler

modules/jooby-kotlin/src/main/kotlin/io/jooby/kt/Kooby.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ open class Kooby() : Jooby() {
275275

276276
@RouterDsl
277277
fun route(method: String, pattern: String, handler: HandlerContext.() -> Any): Route {
278-
return super.route(method, pattern) { ctx -> handler(HandlerContext(ctx)) }.setHandle(handler)
278+
return super.route(method, pattern) { ctx -> handler(HandlerContext(ctx)) }
279279
}
280280

281281
@RouterDsl

modules/jooby-test/src/main/java/io/jooby/test/MockRouter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -495,12 +495,12 @@ private MockValue call(
495495
findContext.setRoute(route);
496496
Object value;
497497
try {
498-
Route.Handler handler = fullExecution ? route.getPipeline() : route.getHandler();
499498
if (route.getMethod().equals(Router.WS)) {
500-
WebSocket.Initializer initializer = (WebSocket.Initializer) route.getHandle();
501-
MockWebSocketConfigurer configurer = new MockWebSocketConfigurer(ctx, initializer);
499+
var initializer = ((WebSocket.Handler) route.getHandler()).getInitializer();
500+
var configurer = new MockWebSocketConfigurer(ctx, initializer);
502501
return new SingleMockValue(configurer);
503502
} else {
503+
var handler = fullExecution ? route.getPipeline() : route.getHandler();
504504
value = handler.apply(ctx);
505505
if (ctx instanceof MockContext) {
506506
MockResponse response = ((MockContext) ctx).getResponse();

0 commit comments

Comments
 (0)