Skip to content
This repository was archived by the owner on Mar 3, 2026. It is now read-only.

Commit b30ff3b

Browse files
committed
sse license header+docs+qualify special route handlers
1 parent ba5934c commit b30ff3b

File tree

10 files changed

+139
-100
lines changed

10 files changed

+139
-100
lines changed

jooby-jetty/src/main/java/org/jooby/internal/jetty/JettySse.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
119
package org.jooby.internal.jetty;
220

321
import java.util.Optional;

jooby-netty/src/main/java/org/jooby/internal/netty/NettySse.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
119
package org.jooby.internal.netty;
220

321
import java.util.Optional;

jooby/src/main/java/org/jooby/Route.java

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,22 +1473,6 @@ default void handle(final Request req, final Response rsp, final Route.Chain cha
14731473
Object handle() throws Throwable;
14741474
}
14751475

1476-
/**
1477-
* Marker interface for interceptors.
1478-
*
1479-
* @author edgar
1480-
* @since 1.0.0.CR
1481-
*/
1482-
interface Interceptor extends Filter {
1483-
1484-
/**
1485-
* Interceptor's name.
1486-
*
1487-
* @return Interceptor's name.
1488-
*/
1489-
String name();
1490-
}
1491-
14921476
/**
14931477
* <h2>before</h2>
14941478
*
@@ -1543,11 +1527,7 @@ interface Interceptor extends Filter {
15431527
* @author edgar
15441528
* @since 1.0.0.CR
15451529
*/
1546-
interface Before extends Interceptor {
1547-
@Override
1548-
default String name() {
1549-
return "before";
1550-
}
1530+
interface Before extends Route.Filter {
15511531

15521532
@Override
15531533
default void handle(final Request req, final Response rsp, final Chain chain) throws Throwable {
@@ -1626,12 +1606,7 @@ default void handle(final Request req, final Response rsp, final Chain chain) th
16261606
* @author edgar
16271607
* @since 1.0.0.CR
16281608
*/
1629-
interface BeforeSend extends Interceptor {
1630-
@Override
1631-
default String name() {
1632-
return "before-send";
1633-
}
1634-
1609+
interface BeforeSend extends Filter {
16351610
@Override
16361611
default void handle(final Request req, final Response rsp, final Chain chain) throws Throwable {
16371612
chain.next(req, new Response.Forwarding(rsp) {
@@ -1757,12 +1732,7 @@ public void send(final Result result) throws Exception {
17571732
* @author edgar
17581733
* @since 1.0.0.CR
17591734
*/
1760-
interface After extends Interceptor {
1761-
1762-
@Override
1763-
default String name() {
1764-
return "after";
1765-
}
1735+
interface After extends Filter {
17661736

17671737
@Override
17681738
default void handle(final Request req, final Response rsp, final Chain chain) throws Throwable {

jooby/src/main/java/org/jooby/Routes.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2384,7 +2384,39 @@ default Route.Definition after(final String pattern, final Route.After handler)
23842384
*/
23852385
WebSocket.Definition ws(String path, WebSocket.Handler handler);
23862386

2387+
/**
2388+
* Add a server-sent event handler.
2389+
*
2390+
* <pre>{@code
2391+
* {
2392+
* sse("/path",(req, sse) -> {
2393+
* // 1. connected
2394+
* sse.send("data"); // 2. send/push data
2395+
* });
2396+
* }
2397+
* }</pre>
2398+
*
2399+
* @param path Event path.
2400+
* @param handler Callback. It might executed in a different thread (web server choice).
2401+
* @return A route definition.
2402+
*/
23872403
Route.Definition sse(String path, Sse.Handler handler);
23882404

2405+
/**
2406+
* Add a server-sent event handler.
2407+
*
2408+
* <pre>{@code
2409+
* {
2410+
* sse("/path", sse -> {
2411+
* // 1. connected
2412+
* sse.send("data"); // 2. send/push data
2413+
* });
2414+
* }
2415+
* }</pre>
2416+
*
2417+
* @param path Event path.
2418+
* @param handler Callback. It might executed in a different thread (web server choice).
2419+
* @return A route definition.
2420+
*/
23892421
Route.Definition sse(String path, Sse.Handler1 handler);
23902422
}

jooby/src/main/java/org/jooby/Sse.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.concurrent.TimeUnit;
3333
import java.util.concurrent.atomic.AtomicReference;
3434

35+
import org.jooby.Route.Chain;
3536
import org.jooby.internal.SseRenderer;
3637
import org.slf4j.Logger;
3738
import org.slf4j.LoggerFactory;
@@ -416,14 +417,15 @@ public Future<Optional<Object>> send() {
416417
}
417418

418419
/**
419-
* Server sent event handler.
420+
* Server-sent event handler.
420421
*
421422
* @author edgar
422423
* @since 1.0.0.CR
423424
*/
424-
public interface Handler extends Route.Handler {
425+
public interface Handler extends Route.Filter {
426+
425427
@Override
426-
default void handle(final Request req, final Response rsp) throws Exception {
428+
default void handle(final Request req, final Response rsp, final Chain chain) throws Throwable {
427429
Sse sse = req.require(Sse.class);
428430
String path = req.path();
429431
rsp.send(new Deferred(deferred -> {

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
*/
1919
package org.jooby.internal;
2020

21+
import static javaslang.API.$;
22+
import static javaslang.API.Case;
23+
import static javaslang.API.Match;
24+
import static javaslang.Predicates.instanceOf;
25+
2126
import java.util.Set;
2227
import java.util.function.Function;
2328

@@ -69,31 +74,31 @@ public String toString() {
6974

7075
private void routes(final StringBuilder buffer) {
7176
Function<Route.Definition, String> p = route -> {
72-
String pattern = route.pattern();
73-
Route.Filter filter = route.filter();
74-
if (filter instanceof Route.Interceptor) {
75-
pattern = ":" + ((Route.Interceptor) filter).name() + " " + pattern;
76-
}
77-
return pattern;
77+
return Match(route.filter()).of(
78+
Case(instanceOf(Route.Before.class), "> " + route.pattern()),
79+
Case(instanceOf(Route.BeforeSend.class), route.pattern() + " >>"),
80+
Case(instanceOf(Route.After.class), route.pattern() + " >"),
81+
Case($(), route.pattern()));
7882
};
7983

8084
int verbMax = 0, routeMax = 0, consumesMax = 0, producesMax = 0;
8185
for (Route.Definition route : routes) {
8286
verbMax = Math.max(verbMax, route.method().length());
8387

84-
routeMax = Math.max(routeMax, p.apply(route).length());
88+
routeMax = Math.max(routeMax, route.pattern().length());
8589

8690
consumesMax = Math.max(consumesMax, route.consumes().toString().length());
8791

8892
producesMax = Math.max(producesMax, route.produces().toString().length());
8993
}
9094

91-
String format = " %-" + verbMax + "s %-" + routeMax + "s %" + consumesMax + "s %"
92-
+ producesMax + "s (%s)\n";
95+
String format = " %-" + verbMax + "s %-" + routeMax + "s %" + consumesMax
96+
+ "s %" + producesMax + "s (%s)\n";
9397

9498
for (Route.Definition route : routes) {
95-
buffer.append(String.format(format, route.method(), p.apply(route), route.consumes(),
96-
route.produces(), route.name()));
99+
buffer.append(
100+
String.format(format, route.method(), p.apply(route), route.consumes(),
101+
route.produces(), route.name()));
97102
}
98103

99104
sockets(buffer, Math.max(verbMax, "WS".length()), routeMax, consumesMax, producesMax);

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
119
package org.jooby.internal;
220

321
import java.io.InputStream;

jooby/src/test/java/org/jooby/InterceptorNameTest.java

Lines changed: 0 additions & 41 deletions
This file was deleted.

jooby/src/test/java/org/jooby/SseTest.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ protected void handshake(final Runnable handler) throws Exception {
305305
@Test
306306
public void sseHandlerSuccess() throws Exception {
307307
CountDownLatch latch = new CountDownLatch(1);
308-
new MockUnit(Request.class, Response.class, Sse.class)
308+
new MockUnit(Request.class, Response.class, Route.Chain.class, Sse.class)
309309
.expect(unit -> {
310310
Request req = unit.get(Request.class);
311311
Sse sse = unit.get(Sse.class);
@@ -323,7 +323,8 @@ public void sseHandlerSuccess() throws Exception {
323323
Sse.Handler handler = (req, sse) -> {
324324
latch.countDown();
325325
};
326-
handler.handle(unit.get(Request.class), unit.get(Response.class));
326+
handler.handle(unit.get(Request.class), unit.get(Response.class),
327+
unit.get(Route.Chain.class));
327328
}, unit -> {
328329
Deferred deferred = unit.captured(Deferred.class).iterator().next();
329330
deferred.handler((value, ex) -> {
@@ -337,7 +338,7 @@ public void sseHandlerSuccess() throws Exception {
337338

338339
@Test
339340
public void sseHandlerFailure() throws Exception {
340-
new MockUnit(Request.class, Response.class, Sse.class)
341+
new MockUnit(Request.class, Response.class, Sse.class, Route.Chain.class)
341342
.expect(unit -> {
342343
Request req = unit.get(Request.class);
343344
Sse sse = unit.get(Sse.class);
@@ -355,7 +356,8 @@ public void sseHandlerFailure() throws Exception {
355356
Sse.Handler handler = (req, sse) -> {
356357
throw new IllegalStateException("intentional err");
357358
};
358-
handler.handle(unit.get(Request.class), unit.get(Response.class));
359+
handler.handle(unit.get(Request.class), unit.get(Response.class),
360+
unit.get(Route.Chain.class));
359361
}, unit -> {
360362
Deferred deferred = unit.captured(Deferred.class).iterator().next();
361363
deferred.handler((value, ex) -> {
@@ -367,7 +369,7 @@ public void sseHandlerFailure() throws Exception {
367369

368370
@Test
369371
public void sseHandlerHandshakeFailure() throws Exception {
370-
new MockUnit(Request.class, Response.class, Sse.class)
372+
new MockUnit(Request.class, Response.class, Sse.class, Route.Chain.class)
371373
.expect(unit -> {
372374
Request req = unit.get(Request.class);
373375
Sse sse = unit.get(Sse.class);
@@ -385,7 +387,8 @@ public void sseHandlerHandshakeFailure() throws Exception {
385387
.run(unit -> {
386388
Sse.Handler handler = (req, sse) -> {
387389
};
388-
handler.handle(unit.get(Request.class), unit.get(Response.class));
390+
handler.handle(unit.get(Request.class), unit.get(Response.class),
391+
unit.get(Route.Chain.class));
389392
}, unit -> {
390393
Deferred deferred = unit.captured(Deferred.class).iterator().next();
391394
deferred.handler((value, ex) -> {

jooby/src/test/java/org/jooby/internal/AppPrinterTest.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@ public class AppPrinterTest {
1919
@Test
2020
public void print() {
2121
String setup = new AppPrinter(
22-
Sets.newLinkedHashSet(Arrays.asList(interceptor("/"), route("/"), route("/home"))),
22+
Sets.newLinkedHashSet(
23+
Arrays.asList(before("/"), beforeSend("/"), after("/"), route("/"), route("/home"))),
2324
Sets.newLinkedHashSet(Arrays.asList(socket("/ws"))), config("/"))
2425
.toString();
25-
assertEquals(" GET :before / [*/*] [*/*] (/anonymous)\n" +
26-
" GET / [*/*] [*/*] (/anonymous)\n" +
27-
" GET /home [*/*] [*/*] (/anonymous)\n" +
28-
" WS /ws [*/*] [*/*]\n" +
26+
assertEquals(" GET > / [*/*] [*/*] (/anonymous)\n" +
27+
" GET / >> [*/*] [*/*] (/anonymous)\n" +
28+
" GET / > [*/*] [*/*] (/anonymous)\n" +
29+
" GET / [*/*] [*/*] (/anonymous)\n" +
30+
" GET /home [*/*] [*/*] (/anonymous)\n" +
31+
" WS /ws [*/*] [*/*]\n" +
2932
"\n" +
3033
"listening on:\n" +
3134
" http://localhost:8080/", setup);
@@ -86,11 +89,22 @@ private Route.Definition route(final String pattern) {
8689
});
8790
}
8891

89-
private Route.Definition interceptor(final String pattern) {
92+
private Route.Definition before(final String pattern) {
9093
return new Route.Definition("GET", pattern, (Before) (req, rsp) -> {
9194
});
9295
}
9396

97+
private Route.Definition beforeSend(final String pattern) {
98+
return new Route.Definition("GET", pattern, (Route.BeforeSend) (req, rsp, r) -> {
99+
return r;
100+
});
101+
}
102+
103+
private Route.Definition after(final String pattern) {
104+
return new Route.Definition("GET", pattern, (Route.After) (req, rsp, r) -> {
105+
});
106+
}
107+
94108
private WebSocket.Definition socket(final String pattern) {
95109
return new WebSocket.Definition(pattern, (ws) -> {
96110
});

0 commit comments

Comments
 (0)