Skip to content

Commit fce9d8d

Browse files
committed
provides a default route to handle 404 response in prod mode
The factory was listing all the existing endpoints if no route was found for a request. By defining this new route, which match all kind of request, we will bypass the `RestxMainRouterFactory`. This route is only activated in "prod" mode, as the listing might be useful during development.
1 parent e0e01e5 commit fce9d8d

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

restx-core/src/main/java/restx/security/SecurityModule.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
11
package restx.security;
22

3+
import com.google.common.base.Optional;
34
import org.slf4j.Logger;
45
import org.slf4j.LoggerFactory;
6+
7+
import java.io.IOException;
8+
import restx.RestxContext;
9+
import restx.RestxHandler;
10+
import restx.RestxHandlerMatch;
11+
import restx.RestxRequest;
12+
import restx.RestxRequestMatch;
13+
import restx.RestxResponse;
14+
import restx.RestxRoute;
15+
import restx.StdRestxRequestMatch;
16+
import restx.WebException;
517
import restx.common.RestxConfig;
618
import restx.config.SettingsKey;
719
import restx.factory.AutoStartable;
820
import restx.factory.Module;
921
import restx.factory.Provides;
22+
import restx.factory.When;
23+
import restx.http.HttpStatus;
1024
import restx.security.RestxSession.Definition.EntryCacheManager;
1125

1226
import javax.inject.Named;
@@ -65,4 +79,23 @@ public int sessionsLimit() {
6579
public EntryCacheManager guavaCacheManager() {
6680
return new GuavaEntryCacheManager();
6781
}
82+
83+
@Provides(priority = 100000)
84+
@When(name = "restx.mode", value = "prod")
85+
public RestxRoute productionNotFoundHandler() {
86+
return new RestxRoute() {
87+
@Override
88+
public Optional<RestxHandlerMatch> match(RestxRequest req) {
89+
return Optional.of(new RestxHandlerMatch(
90+
new StdRestxRequestMatch("*", req.getRestxPath()),
91+
new RestxHandler() {
92+
@Override
93+
public void handle(RestxRequestMatch match, RestxRequest req, RestxResponse resp, RestxContext ctx) throws IOException {
94+
throw new WebException(HttpStatus.NOT_FOUND);
95+
}
96+
}
97+
));
98+
}
99+
};
100+
}
68101
}

0 commit comments

Comments
 (0)