Skip to content

Commit 75ace46

Browse files
committed
revert: default setup of Context As Service
- fix #3754
1 parent 68ec0bc commit 75ace46

File tree

4 files changed

+83
-9
lines changed

4 files changed

+83
-9
lines changed

docs/asciidoc/router-options.adoc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import io.jooby.Jooby;
1616
.setNormalizeSlash(true)
1717
.setResetHeadersOnError(false)
1818
.setTrustProxy(true)
19+
.setContextAsService(true)
1920
);
2021
}
2122
----
@@ -33,6 +34,7 @@ import io.jooby.Jooby
3334
normalizeSlash = true
3435
resetHeadersOnError = false
3536
trustProxy = true
37+
contextAsService = true
3638
}
3739
}
3840
----
@@ -41,6 +43,11 @@ import io.jooby.Jooby
4143
|===
4244
| Option | Value | Default Value| Description
4345

46+
|contextAsService
47+
|boolean
48+
|false
49+
|If enabled, allows to retrieve the javadoc:Context[] object associated with the current request via the service registry while the request is being processed.
50+
4451
|ignoreCase
4552
|boolean
4653
|false
@@ -69,7 +76,7 @@ import io.jooby.Jooby
6976
|trustProxy
7077
|boolean
7178
|false
72-
|When true handles X-Forwarded-* headers by updating the values on the current context to match what was sent in the header(s).
79+
|When true handles `X-Forwarded-*` headers by updating the values on the current context to match what was sent in the header(s).
7380

7481
|===
7582

jooby/src/main/java/io/jooby/RouterOptions.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,35 @@ public class RouterOptions {
6565
*/
6666
private boolean trustProxy;
6767

68+
/**
69+
* If enabled, allows to retrieve the {@link Context} object associated with the current request
70+
* via the service registry while the request is being processed.
71+
*/
72+
private boolean contextAsService;
73+
74+
/**
75+
* If enabled, allows to retrieve the {@link Context} object associated with the current request
76+
* via the service registry while the request is being processed.
77+
*
78+
* @return If enabled, allows to retrieve the {@link Context} object associated with the current
79+
* request via the service registry while the request is being processed.
80+
*/
81+
public boolean isContextAsService() {
82+
return contextAsService;
83+
}
84+
85+
/**
86+
* If enabled, allows to retrieve the {@link Context} object associated with the current request
87+
* via the service registry while the request is being processed.
88+
*
89+
* @param contextAsService True for enabled.
90+
* @return This options.
91+
*/
92+
public RouterOptions setContextAsService(boolean contextAsService) {
93+
this.contextAsService = contextAsService;
94+
return this;
95+
}
96+
6897
/**
6998
* Indicates whenever routing algorithm does case-sensitive matching on an incoming request path.
7099
* Default is <code>false (case sensitive)</code>.

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ private void pureAscii(String pattern, Consumer<String> consumer) {
537537
@NonNull public Router start(@NonNull Jooby app, @NonNull Server server) {
538538
started = true;
539539
configureTrustProxy(routerOptions.isTrustProxy());
540-
setContextAsService();
540+
configureContextAsService(routerOptions.isContextAsService());
541541
var globalErrHandler = defineGlobalErrorHandler(app);
542542
if (err == null) {
543543
err = globalErrHandler;
@@ -610,6 +610,13 @@ private void pureAscii(String pattern, Consumer<String> consumer) {
610610
return this;
611611
}
612612

613+
private void configureContextAsService(boolean contextAsService) {
614+
if (contextAsService) {
615+
addPostDispatchInitializer(ContextAsServiceInitializer.INSTANCE);
616+
getServices().put(Context.class, ContextAsServiceInitializer.INSTANCE);
617+
}
618+
}
619+
613620
/**
614621
* Define the global error handler. If ProblemDetails is enabled the {@link ProblemDetailsHandler}
615622
* instantiated from configuration settings and returned. Otherwise, {@link DefaultErrorHandler}
@@ -802,13 +809,6 @@ public Router setCurrentUser(@NonNull Function<Context, Object> provider) {
802809
return this;
803810
}
804811

805-
private Router setContextAsService() {
806-
addPostDispatchInitializer(ContextAsServiceInitializer.INSTANCE);
807-
getServices().put(Context.class, ContextAsServiceInitializer.INSTANCE);
808-
809-
return this;
810-
}
811-
812812
@Override
813813
public String toString() {
814814
StringBuilder buff = new StringBuilder();

tests/src/test/java/io/jooby/i1937/Issue1937.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,28 @@
99
import static org.junit.jupiter.api.Assertions.assertThrows;
1010

1111
import io.jooby.Context;
12+
import io.jooby.RouterOptions;
1213
import io.jooby.exception.RegistryException;
14+
import io.jooby.guice.GuiceModule;
1315
import io.jooby.junit.ServerTest;
1416
import io.jooby.junit.ServerTestRunner;
1517

1618
public class Issue1937 {
1719

20+
@ServerTest
21+
public void shouldFailIfContextAsServiceWasNotCalled(ServerTestRunner runner) {
22+
runner
23+
.define(
24+
app ->
25+
app.get(
26+
"/i1937",
27+
ctx -> {
28+
app.require(Context.class);
29+
return "OK";
30+
}))
31+
.ready(http -> http.get("/i1937", rsp -> assertEquals(500, rsp.code())));
32+
}
33+
1834
@ServerTest
1935
public void shouldWorkIfContextAsServiceWasCalled(ServerTestRunner runner) {
2036
runner
@@ -26,6 +42,8 @@ public void shouldWorkIfContextAsServiceWasCalled(ServerTestRunner runner) {
2642
app.require(Context.class);
2743
return "OK";
2844
});
45+
46+
app.setRouterOptions(new RouterOptions().setContextAsService(true));
2947
})
3048
.ready(http -> http.get("/i1937", rsp -> assertEquals(200, rsp.code())));
3149
}
@@ -35,6 +53,26 @@ public void shouldThrowIfOutOfScope(ServerTestRunner runner) {
3553
runner
3654
.define(
3755
app -> {
56+
app.setRouterOptions(new RouterOptions().setContextAsService(true));
57+
app.onStarted(
58+
() -> {
59+
Throwable t =
60+
assertThrows(RegistryException.class, () -> app.require(Context.class));
61+
assertEquals(
62+
t.getMessage(),
63+
"Context is not available. Are you getting it from request scope?");
64+
});
65+
})
66+
.ready(http -> {});
67+
}
68+
69+
@ServerTest
70+
public void shouldThrowIfOutOfScopeWithDI(ServerTestRunner runner) {
71+
runner
72+
.define(
73+
app -> {
74+
app.install(new GuiceModule());
75+
app.setRouterOptions(new RouterOptions().setContextAsService(true));
3876
app.onStarted(
3977
() -> {
4078
Throwable t =

0 commit comments

Comments
 (0)