/** * Return a new resource handler * * @param resourceManager The resource manager to use * @return A new resource handler */ public static ResourceHandler resource(final ResourceManager resourceManager) { return new ResourceHandler(resourceManager).setDirectoryListingEnabled(false); }
@Override public void handleRequest(HttpServerExchange exchange) throws Exception { if (exchange.getRequestMethod().equals(Methods.GET) && exchange.getRelativePath().matches(ACME_CHALLENGE_REGEX)) { super.handleRequest(exchange); } else { next.handleRequest(exchange); } } }
private Resource getIndexFiles(HttpServerExchange exchange, ResourceSupplier resourceManager, final String base, List<String> possible) throws IOException { String realBase; if (base.endsWith("/")) { realBase = base; } else { realBase = base + "/"; } for (String possibility : possible) { Resource index = resourceManager.getResource(exchange, canonicalize(realBase + possibility)); if (index != null) { return index; } } return null; }
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); }
/** * Helper to add given PathResourceProviders to a PathHandler. * * @param pathResourceProviders List of instances of classes implementing PathResourceProvider. * @param pathHandler The handler that will have these handlers added to it. */ public static void addProvidersToPathHandler(PathResourceProvider[] pathResourceProviders, PathHandler pathHandler) { if (pathResourceProviders != null && pathResourceProviders.length > 0) { for (PathResourceProvider pathResourceProvider : pathResourceProviders) { if (pathResourceProvider.isPrefixPath()) { pathHandler.addPrefixPath(pathResourceProvider.getPath(), new ResourceHandler(pathResourceProvider.getResourceManager())); } else { pathHandler.addExactPath(pathResourceProvider.getPath(), new ResourceHandler(pathResourceProvider.getResourceManager())); } } } }
String[] welcomeFiles = new String[] { "index.html", "index.html", "default.html", "default.htm" }; final List<String> welcomeFileList = new CopyOnWriteArrayList<>(welcomeFiles); final ResourceHandler resourceHandler = new ResourceHandler() .setResourceManager(resourceManager) .setWelcomeFiles(welcomeFiles) .setDirectoryListingEnabled(false);
public RoutingHandler get() { RoutingHandler router = new RoutingHandler(); final String assetsPath = serviceConfig.getString("path"); final String assetsDirectoryName = serviceConfig.getString("dir"); final Integer assetsCacheTime = serviceConfig.getInt("cache.time"); final FileResourceManager fileResourceManager = new FileResourceManager(Paths.get(assetsDirectoryName).toFile()); router.add(Methods.GET, assetsPath + "/*", io.undertow.Handlers.rewrite("regex('" + assetsPath + "/(.*)')", "/$1", getClass().getClassLoader(), new ResourceHandler(fileResourceManager).setCachable(TruePredicate.instance()).setCacheTime(assetsCacheTime))); this.registeredEndpoints.add(EndpointInfo.builder() .withConsumes("*/*") .withProduces("*/*") .withPathTemplate(assetsPath) .withControllerName(this.getClass().getSimpleName()) .withMethod(Methods.GET) .build()); return router; } }
.setHandler( new DisableCacheHandler( Handlers.path(new ResourceHandler(mrs).addWelcomeFiles("test.html")) .addPrefixPath( "/services/", new HttpHandler() {
public static HttpHandler resource(String prefix, int cacheTime) { ResourceManager resourceManager = null; if (Env.LOCAL == Env.get()) { String path = Paths.get(AssetsConfig.assetsRoot(), prefix).toString(); log.debug("using local file resource manager {}", path); resourceManager = new FileResourceManager(new File(path), 1024 * 1024); } else { log.debug("using classpath file resource manager"); ResourceManager classPathManager = new ClassPathResourceManager(CustomHandlers.class.getClassLoader(), prefix); resourceManager = new CachingResourceManager(100, 65536, new DirectBufferCache(1024, 10, 10480), classPathManager, cacheTime); } ResourceHandler handler = new ResourceHandler(resourceManager); handler.setCacheTime(cacheTime); return handler; }
private void deployDefaultStaticRouting(final Config staticConfig, DeploymentContext context){ final String location = staticConfig.getString("location"); log.info( "Enabling static routing at folder: " + location ); final ResourceManager resourceManager = SystemResource.loadResourceManagerFor( location ); final HttpHandler fallbackHandler = context.fallbackHandler(); final ResourceHandler handler = new ResourceHandler(resourceManager, fallbackHandler); final String welcomeFile = staticConfig.getString("welcome-file", "index.html"); handler.setWelcomeFiles( welcomeFile ); context.fallbackHandler( handler ); } }
.addWelcomeFiles(welcomeFile) .setDirectoryListingEnabled(false);
public static MappedEndpoint staticFiles(String url, String docPath) { url = resolveUrl(url); url = url.isEmpty() ? BASE_PATH : url; docPath = docPath.startsWith(BASE_PATH) ? docPath.replaceFirst(BASE_PATH, "") : docPath; HttpHandler handler = Handlers.path() .addPrefixPath(url, Handlers.resource(new ClassPathResourceManager(Thread.currentThread().getContextClassLoader(), docPath)) .addWelcomeFiles(INDEX_HTML)); return new MappedEndpoint(Methods.GET_STRING, url, MappedEndpoint.Type.STATIC, handler); }
@Override public void handleRequest(final HttpServerExchange exchange) throws Exception { if (exchange.getRequestMethod().equals(Methods.GET) || exchange.getRequestMethod().equals(Methods.POST)) { serveResource(exchange, true); } else if (exchange.getRequestMethod().equals(Methods.HEAD)) { serveResource(exchange, false); } else { if (KNOWN_METHODS.contains(exchange.getRequestMethod())) { exchange.setStatusCode(StatusCodes.METHOD_NOT_ALLOWED); exchange.getResponseHeaders().add(Headers.ALLOW, String.join(", ", Methods.GET_STRING, Methods.HEAD_STRING, Methods.POST_STRING)); } else { exchange.setStatusCode(StatusCodes.NOT_IMPLEMENTED); } exchange.endExchange(); } }
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); }
private ResourceHandler getResourceHandler(String postfix) { if (StringUtils.isBlank(postfix)) { if (this.resourceHandler == null) { this.resourceHandler = new ResourceHandler(new ClassPathResourceManager(Thread.currentThread().getContextClassLoader(), "files/")); } return this.resourceHandler; } return new ResourceHandler(new ClassPathResourceManager(Thread.currentThread().getContextClassLoader(), "files" + postfix)); }
public static MappedEndpoint staticFiles(String url, String docPath, List<Interceptor> interceptors) { Objects.requireNonNull(url, Messages.INVALID_URL); url = resolveUrl(url); docPath = docPath.startsWith(BASE_PATH) ? docPath.replaceFirst(BASE_PATH, "") : docPath; HttpHandler handler = Handlers.path() .addPrefixPath(url, Handlers.resource(new ClassPathResourceManager(Thread.currentThread().getContextClassLoader(), docPath)) .addWelcomeFiles("index.html")); InterceptorHandler interceptorHandler = new InterceptorHandler(interceptors); interceptorHandler.setNext(handler); return new MappedEndpoint(Methods.GET_STRING, url, MappedEndpoint.Type.STATIC, interceptorHandler); }
@Override public void handleRequest(final HttpServerExchange exchange) throws Exception { if (exchange.getRequestMethod().equals(Methods.GET) || exchange.getRequestMethod().equals(Methods.POST)) { serveResource(exchange, true); } else if (exchange.getRequestMethod().equals(Methods.HEAD)) { serveResource(exchange, false); } else { if (KNOWN_METHODS.contains(exchange.getRequestMethod())) { exchange.setStatusCode(StatusCodes.METHOD_NOT_ALLOWED); exchange.getResponseHeaders().add(Headers.ALLOW, String.join(", ", Methods.GET_STRING, Methods.HEAD_STRING, Methods.POST_STRING)); } else { exchange.setStatusCode(StatusCodes.NOT_IMPLEMENTED); } exchange.endExchange(); } }
@Override public HttpHandler wrap(HttpHandler handler) { ResourceManager rm = new PathResourceManager(Paths.get(location), 1024); ResourceHandler resourceHandler = new ResourceHandler(rm); resourceHandler.setDirectoryListingEnabled(allowDirectoryListing); return resourceHandler; } }
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); }
/** * Creates the resource handle for a zip file, specified by the URL. * * @param zipFile * url to a zip file * * @return the resource handler to handle requests to files in the zip * * @throws IOException */ private ResourceHandler createZipResourceHandler(final URL zipFile) { final FileSystem fileSystem; try { fileSystem = newFileSystem(URI.create("jar:" + zipFile), Collections.<String, Object>emptyMap()); } catch (IOException e) { throw new RuntimeException("Could not access zip file", e); } final ResourceManager resMgr = new FileSystemResourceManager(fileSystem); return new ResourceHandler(resMgr); }