Skip to content

Commit 3dbbddd

Browse files
committed
router: move more options to corresponding class
1 parent 8c37006 commit 3dbbddd

File tree

8 files changed

+91
-147
lines changed

8 files changed

+91
-147
lines changed

jooby/src/main/java/io/jooby/DefaultContext.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,9 @@ default long getRequestLength() {
361361
@Override
362362
default String getHostAndPort() {
363363
Optional<String> header =
364-
getRouter().isTrustProxy() ? header("X-Forwarded-Host").toOptional() : Optional.empty();
364+
getRouter().getRouterOptions().isTrustProxy()
365+
? header("X-Forwarded-Host").toOptional()
366+
: Optional.empty();
365367
var value =
366368
header.orElseGet(
367369
() ->

jooby/src/main/java/io/jooby/Jooby.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -450,11 +450,6 @@ public ServerOptions getServerOptions() {
450450
return router.getServerOptions();
451451
}
452452

453-
@Override
454-
public boolean isTrustProxy() {
455-
return router.isTrustProxy();
456-
}
457-
458453
@Override
459454
public boolean isStarted() {
460455
return started.get();
@@ -465,12 +460,6 @@ public boolean isStopped() {
465460
return stopped.get();
466461
}
467462

468-
@NonNull @Override
469-
public Jooby setTrustProxy(boolean trustProxy) {
470-
this.router.setTrustProxy(trustProxy);
471-
return this;
472-
}
473-
474463
@NonNull @Override
475464
public Route.Set domain(@NonNull String domain, @NonNull Router subrouter) {
476465
return this.router.domain(domain, subrouter);
@@ -863,12 +852,6 @@ public Jooby setCurrentUser(@NonNull Function<Context, Object> provider) {
863852
return this;
864853
}
865854

866-
@NonNull @Override
867-
public Jooby setContextAsService(boolean contextAsService) {
868-
router.setContextAsService(contextAsService);
869-
return this;
870-
}
871-
872855
@NonNull @Override
873856
public Jooby setHiddenMethod(@NonNull String parameterName) {
874857
router.setHiddenMethod(parameterName);

jooby/src/main/java/io/jooby/Router.java

Lines changed: 8 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -204,53 +204,10 @@ default Object execute(@NonNull Context context) {
204204
*/
205205
@NonNull String getContextPath();
206206

207-
/**
208-
* When true handles X-Forwarded-* headers by updating the values on the current context to match
209-
* what was sent in the header(s).
210-
*
211-
* <p>This should only be installed behind a reverse proxy that has been configured to send the
212-
* <code>X-Forwarded-*</code> header, otherwise a remote user can spoof their address by sending a
213-
* header with bogus values.
214-
*
215-
* <p>The headers that are read/set are:
216-
*
217-
* <ul>
218-
* <li>X-Forwarded-For: Set/update the remote address {@link Context#setRemoteAddress(String)}.
219-
* <li>X-Forwarded-Proto: Set/update request scheme {@link Context#setScheme(String)}.
220-
* <li>X-Forwarded-Host: Set/update the request host {@link Context#setHost(String)}.
221-
* <li>X-Forwarded-Port: Set/update the request port {@link Context#setPort(int)}.
222-
* </ul>
223-
*
224-
* @return True when enabled. Default is false.
225-
*/
226-
boolean isTrustProxy();
227-
228207
boolean isStarted();
229208

230209
boolean isStopped();
231210

232-
/**
233-
* When true handles X-Forwarded-* headers by updating the values on the current context to match
234-
* what was sent in the header(s).
235-
*
236-
* <p>This should only be installed behind a reverse proxy that has been configured to send the
237-
* <code>X-Forwarded-*</code> header, otherwise a remote user can spoof their address by sending a
238-
* header with bogus values.
239-
*
240-
* <p>The headers that are read/set are:
241-
*
242-
* <ul>
243-
* <li>X-Forwarded-For: Set/update the remote address {@link Context#setRemoteAddress(String)}.
244-
* <li>X-Forwarded-Proto: Set/update request scheme {@link Context#setScheme(String)}.
245-
* <li>X-Forwarded-Host: Set/update the request host {@link Context#setHost(String)}.
246-
* <li>X-Forwarded-Port: Set/update the request port {@link Context#setPort(int)}.
247-
* </ul>
248-
*
249-
* @param trustProxy True to enable.
250-
* @return This router.
251-
*/
252-
@NonNull Router setTrustProxy(boolean trustProxy);
253-
254211
/**
255212
* Provides a way to override the current HTTP method. Request must be:
256213
*
@@ -280,15 +237,6 @@ default Object execute(@NonNull Context context) {
280237
*/
281238
@NonNull Router setCurrentUser(@NonNull Function<Context, Object> provider);
282239

283-
/**
284-
* If enabled, allows to retrieve the {@link Context} object associated with the current request
285-
* via the service registry while the request is being processed.
286-
*
287-
* @param contextAsService whether to enable or disable this feature
288-
* @return This router.
289-
*/
290-
@NonNull Router setContextAsService(boolean contextAsService);
291-
292240
/* ***********************************************************************************************
293241
* use(Router)
294242
* ***********************************************************************************************
@@ -304,7 +252,8 @@ default Object execute(@NonNull Context context) {
304252
* }
305253
* }</pre>
306254
*
307-
* NOTE: if you run behind a reverse proxy you might to enabled {@link #setTrustProxy(boolean)}.
255+
* NOTE: if you run behind a reverse proxy you might to enabled {@link
256+
* RouterOptions#setTrustProxy(boolean)}.
308257
*
309258
* <p>NOTE: ONLY routes are imported. Services, callback, etc.. are ignored.
310259
*
@@ -328,7 +277,8 @@ default Object execute(@NonNull Context context) {
328277
* }
329278
* }</pre>
330279
*
331-
* NOTE: if you run behind a reverse proxy you might to enabled {@link #setTrustProxy(boolean)}.
280+
* NOTE: if you run behind a reverse proxy you might to enabled {@link
281+
* RouterOptions#setTrustProxy(boolean)}.
332282
*
333283
* @param domain Predicate
334284
* @param body Route action.
@@ -352,7 +302,7 @@ default Object execute(@NonNull Context context) {
352302
* Imported routes are matched only when predicate pass.
353303
*
354304
* <p>NOTE: if you run behind a reverse proxy you might to enabled {@link
355-
* #setTrustProxy(boolean)}.
305+
* RouterOptions#setTrustProxy(boolean)}.
356306
*
357307
* <p>NOTE: ONLY routes are imported. Services, callback, etc.. are ignored.
358308
*
@@ -379,7 +329,8 @@ default Object execute(@NonNull Context context) {
379329
* }
380330
* }</pre>
381331
*
382-
* NOTE: if you run behind a reverse proxy you might to enabled {@link #setTrustProxy(boolean)}.
332+
* NOTE: if you run behind a reverse proxy you might to enabled {@link
333+
* RouterOptions#setTrustProxy(boolean)}.
383334
*
384335
* <p>NOTE: ONLY routes are imported. Services, callback, etc.. are ignored.
385336
*
@@ -852,9 +803,7 @@ default Object execute(@NonNull Context context) {
852803
@NonNull Router setRouterOptions(@NonNull RouterOptions options);
853804

854805
/**
855-
* Session store. Default use a cookie ID with a memory storage.
856-
*
857-
* <p>See {@link SessionStore#memory()}.
806+
* Session store. Default is {@link SessionStore#UNSUPPORTED}.
858807
*
859808
* @return Session store.
860809
*/

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

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
package io.jooby;
77

8+
import edu.umd.cs.findbugs.annotations.NonNull;
9+
810
/**
911
* Router options:
1012
*
@@ -38,6 +40,25 @@ public class RouterOptions {
3840
/** Indicates whenever response headers are clear/reset in case of exception. */
3941
private boolean resetHeadersOnError;
4042

43+
/**
44+
* When true handles X-Forwarded-* headers by updating the values on the current context to match
45+
* what was sent in the header(s).
46+
*
47+
* <p>This should only be installed behind a reverse proxy that has been configured to send the
48+
* <code>X-Forwarded-*</code> header, otherwise a remote user can spoof their address by sending a
49+
* header with bogus values.
50+
*
51+
* <p>The headers that are read/set are:
52+
*
53+
* <ul>
54+
* <li>X-Forwarded-For: Set/update the remote address {@link Context#setRemoteAddress(String)}.
55+
* <li>X-Forwarded-Proto: Set/update request scheme {@link Context#setScheme(String)}.
56+
* <li>X-Forwarded-Host: Set/update the request host {@link Context#setHost(String)}.
57+
* <li>X-Forwarded-Port: Set/update the request port {@link Context#setPort(int)}.
58+
* </ul>
59+
*/
60+
private boolean trustProxy;
61+
4162
/**
4263
* Indicates whenever routing algorithm does case-sensitive matching on an incoming request path.
4364
* Default is <code>false (case sensitive)</code>.
@@ -178,4 +199,52 @@ public static RouterOptions defaults() {
178199
public static RouterOptions caseInsensitive() {
179200
return new RouterOptions().ignoreCase(true).resetHeaderOnError(true);
180201
}
202+
203+
/**
204+
* When true handles X-Forwarded-* headers by updating the values on the current context to match
205+
* what was sent in the header(s).
206+
*
207+
* <p>This should only be installed behind a reverse proxy that has been configured to send the
208+
* <code>X-Forwarded-*</code> header, otherwise a remote user can spoof their address by sending a
209+
* header with bogus values.
210+
*
211+
* <p>The headers that are read/set are:
212+
*
213+
* <ul>
214+
* <li>X-Forwarded-For: Set/update the remote address {@link Context#setRemoteAddress(String)}.
215+
* <li>X-Forwarded-Proto: Set/update request scheme {@link Context#setScheme(String)}.
216+
* <li>X-Forwarded-Host: Set/update the request host {@link Context#setHost(String)}.
217+
* <li>X-Forwarded-Port: Set/update the request port {@link Context#setPort(int)}.
218+
* </ul>
219+
*
220+
* @return True when enabled. Default is false.
221+
*/
222+
public boolean isTrustProxy() {
223+
return trustProxy;
224+
}
225+
226+
/**
227+
* When true handles X-Forwarded-* headers by updating the values on the current context to match
228+
* what was sent in the header(s).
229+
*
230+
* <p>This should only be installed behind a reverse proxy that has been configured to send the
231+
* <code>X-Forwarded-*</code> header, otherwise a remote user can spoof their address by sending a
232+
* header with bogus values.
233+
*
234+
* <p>The headers that are read/set are:
235+
*
236+
* <ul>
237+
* <li>X-Forwarded-For: Set/update the remote address {@link Context#setRemoteAddress(String)}.
238+
* <li>X-Forwarded-Proto: Set/update request scheme {@link Context#setScheme(String)}.
239+
* <li>X-Forwarded-Host: Set/update the request host {@link Context#setHost(String)}.
240+
* <li>X-Forwarded-Port: Set/update the request port {@link Context#setPort(int)}.
241+
* </ul>
242+
*
243+
* @param trustProxy True to enable.
244+
* @return This options.
245+
*/
246+
@NonNull public RouterOptions setTrustProxy(boolean trustProxy) {
247+
this.trustProxy = trustProxy;
248+
return this;
249+
}
181250
}

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

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
import io.jooby.output.OutputFactory;
4646
import io.jooby.problem.ProblemDetailsHandler;
4747
import io.jooby.value.ValueFactory;
48-
import jakarta.inject.Provider;
4948

5049
public class RouterImpl implements Router {
5150

@@ -159,10 +158,6 @@ public Stack executor(Executor executor) {
159158

160159
private RouterOptions routerOptions = RouterOptions.defaults();
161160

162-
private boolean trustProxy;
163-
164-
private boolean contextAsService;
165-
166161
private boolean started;
167162

168163
private boolean stopped;
@@ -232,11 +227,6 @@ public List<Route> getRoutes() {
232227
return routes;
233228
}
234229

235-
@Override
236-
public boolean isTrustProxy() {
237-
return trustProxy;
238-
}
239-
240230
@Override
241231
public boolean isStarted() {
242232
return started;
@@ -247,15 +237,12 @@ public boolean isStopped() {
247237
return stopped;
248238
}
249239

250-
@NonNull @Override
251-
public Router setTrustProxy(boolean trustProxy) {
252-
this.trustProxy = trustProxy;
240+
private void configureTrustProxy(boolean trustProxy) {
253241
if (trustProxy) {
254242
addPreDispatchInitializer(ContextInitializer.PROXY_PEER_ADDRESS);
255243
} else {
256244
removePreDispatchInitializer(ContextInitializer.PROXY_PEER_ADDRESS);
257245
}
258-
return this;
259246
}
260247

261248
@NonNull @Override
@@ -548,6 +535,8 @@ private void pureAscii(String pattern, Consumer<String> consumer) {
548535

549536
@NonNull public Router start(@NonNull Jooby app, @NonNull Server server) {
550537
started = true;
538+
configureTrustProxy(routerOptions.isTrustProxy());
539+
setContextAsService();
551540
var globalErrHandler = defineGlobalErrorHandler(app);
552541
if (err == null) {
553542
err = globalErrHandler;
@@ -812,21 +801,9 @@ public Router setCurrentUser(@NonNull Function<Context, Object> provider) {
812801
return this;
813802
}
814803

815-
@NonNull @Override
816-
public Router setContextAsService(boolean contextAsService) {
817-
if (this.contextAsService == contextAsService) {
818-
return this;
819-
}
820-
821-
this.contextAsService = contextAsService;
822-
823-
if (contextAsService) {
824-
addPostDispatchInitializer(ContextAsServiceInitializer.INSTANCE);
825-
getServices().put(Context.class, ContextAsServiceInitializer.INSTANCE);
826-
} else {
827-
removePostDispatchInitializer(ContextAsServiceInitializer.INSTANCE);
828-
getServices().put(Context.class, (Provider<Context>) null);
829-
}
804+
private Router setContextAsService() {
805+
addPostDispatchInitializer(ContextAsServiceInitializer.INSTANCE);
806+
getServices().put(Context.class, ContextAsServiceInitializer.INSTANCE);
830807

831808
return this;
832809
}

0 commit comments

Comments
 (0)