Java源码示例:io.undertow.server.handlers.PredicateHandler
示例1
@Override
protected void setRootHandler(HttpHandler current) {
final PredicateHandler handler = new PredicateHandler(Predicates.path("/login"), new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
exchange.writeAsync("Login Page");
}
}, current);
super.setRootHandler(new SessionAttachmentHandler(handler, new InMemorySessionManager("test"), new SessionCookieConfig()));
}
示例2
static ResourceHandlerDefinition createStaticContentHandler(ResourceManager resource, String context) {
final io.undertow.server.handlers.resource.ResourceHandler handler = new io.undertow.server.handlers.resource.ResourceHandler(resource)
.setCacheTime(60 * 60 * 24 * 31)
.setAllowed(not(path("META-INF")))
.setResourceManager(resource)
.setDirectoryListingEnabled(false)
.setCachable(not(suffixes(NOCACHE_JS, APP_HTML, INDEX_HTML)));
// avoid clickjacking attacks: console must not be included in (i)frames
SetHeaderHandler frameHandler = new SetHeaderHandler(handler, "X-Frame-Options", "SAMEORIGIN");
// we also need to setup the default resource redirect
PredicateHandler predicateHandler = new PredicateHandler(path("/"), new RedirectHandler(ExchangeAttributes.constant(context + DEFAULT_RESOURCE)), frameHandler);
return new ResourceHandlerDefinition(context, DEFAULT_RESOURCE, predicateHandler);
}
示例3
static ResourceHandlerDefinition createConsoleHandler(String slot, String resource) throws ModuleLoadException {
final ClassPathResourceManager cpresource = new ClassPathResourceManager(getClassLoader(Module.getCallerModuleLoader(), ERROR_MODULE, slot), "");
final io.undertow.server.handlers.resource.ResourceHandler handler = new io.undertow.server.handlers.resource.ResourceHandler(cpresource)
.setAllowed(not(path("META-INF")))
.setResourceManager(cpresource)
.setDirectoryListingEnabled(false)
.setCachable(Predicates.<HttpServerExchange>falsePredicate());
//we also need to setup the default resource redirect
PredicateHandler predicateHandler = new PredicateHandler(path("/"), new RedirectHandler(ExchangeAttributes.constant(CONTEXT + resource)), handler);
return new ResourceHandlerDefinition(CONTEXT, resource, predicateHandler);
}
示例4
public static HttpHandler createErrorContext(final String slot) throws ModuleLoadException {
final ClassPathResourceManager cpresource = new ClassPathResourceManager(getClassLoader(Module.getCallerModuleLoader(), ERROR_MODULE, slot), "");
final io.undertow.server.handlers.resource.ResourceHandler handler = new io.undertow.server.handlers.resource.ResourceHandler(cpresource)
.setAllowed(not(path("META-INF")))
.setDirectoryListingEnabled(false)
.setCachable(Predicates.<HttpServerExchange>falsePredicate());
//we also need to setup the default resource redirect
return new PredicateHandler(path("/"), new RedirectHandler(ExchangeAttributes.constant(ERROR_CONTEXT + DEFAULT_RESOURCE)), handler);
}