Permalink
Browse files

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.
  • Loading branch information...
a-peyrard committed Feb 10, 2016
1 parent e0e01e5 commit fce9d8d9b3f9779686851241c449e5fdad784446
Showing with 33 additions and 0 deletions.
  1. +33 −0 restx-core/src/main/java/restx/security/SecurityModule.java
@@ -1,12 +1,26 @@
package restx.security;
import com.google.common.base.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import restx.RestxContext;
import restx.RestxHandler;
import restx.RestxHandlerMatch;
import restx.RestxRequest;
import restx.RestxRequestMatch;
import restx.RestxResponse;
import restx.RestxRoute;
import restx.StdRestxRequestMatch;
import restx.WebException;
import restx.common.RestxConfig;
import restx.config.SettingsKey;
import restx.factory.AutoStartable;
import restx.factory.Module;
import restx.factory.Provides;
import restx.factory.When;
import restx.http.HttpStatus;
import restx.security.RestxSession.Definition.EntryCacheManager;
import javax.inject.Named;
@@ -65,4 +79,23 @@ public int sessionsLimit() {
public EntryCacheManager guavaCacheManager() {
return new GuavaEntryCacheManager();
}
@Provides(priority = 100000)
@When(name = "restx.mode", value = "prod")
public RestxRoute productionNotFoundHandler() {
return new RestxRoute() {
@Override
public Optional<RestxHandlerMatch> match(RestxRequest req) {
return Optional.of(new RestxHandlerMatch(
new StdRestxRequestMatch("*", req.getRestxPath()),
new RestxHandler() {
@Override
public void handle(RestxRequestMatch match, RestxRequest req, RestxResponse resp, RestxContext ctx) throws IOException {
throw new WebException(HttpStatus.NOT_FOUND);
}
}
));
}
};
}
}

0 comments on commit fce9d8d

Please sign in to comment.