/** * 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 HttpHandler wrap(HttpHandler handler) { ResourceManager rm = new PathResourceManager(Paths.get(location), 1024); ResourceHandler resourceHandler = new ResourceHandler(rm); resourceHandler.setDirectoryListingEnabled(allowDirectoryListing); return resourceHandler; } }
/** * 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())); } } } }
@Override public HttpHandler createHandler(final OperationContext context, ModelNode model) throws OperationFailedException { final String path = PATH.resolveModelAttribute(context, model).asString(); final boolean directoryListing = DIRECTORY_LISTING.resolveModelAttribute(context, model).asBoolean(); final boolean followSymlink = FOLLOW_SYMLINK.resolveModelAttribute(context, model).asBoolean(); final boolean caseSensitive = CASE_SENSITIVE.resolveModelAttribute(context, model).asBoolean(); final int cacheBufferSize = CACHE_BUFFER_SIZE.resolveModelAttribute(context, model).asInt(); final int cacheBuffers = CACHE_BUFFERS.resolveModelAttribute(context, model).asInt(); final List<String> safePaths = SAFE_SYMLINK_PATHS.unwrap(context, model); final String[] paths = safePaths.toArray(new String[safePaths.size()]); UndertowLogger.ROOT_LOGGER.creatingFileHandler(path, directoryListing, followSymlink, caseSensitive, safePaths); Path base; try { base = Paths.get(path).normalize().toRealPath(); //workaround for JBEAP-10231 } catch (IOException e) { throw new OperationFailedException(UndertowLogger.ROOT_LOGGER.unableAddHandlerForPath(path)); } PathResourceManager resourceManager = new PathResourceManager(base, cacheBufferSize * cacheBuffers, caseSensitive, followSymlink, paths); ResourceHandler handler = new ResourceHandler(resourceManager); handler.setDirectoryListingEnabled(directoryListing); return handler; } }
public PathResourceHandler() { PathResourceConfig config = (PathResourceConfig)Config.getInstance().getJsonObjectConfig(PathResourceConfig.CONFIG_NAME, PathResourceConfig.class); if(config.isPrefix()) { pathHandler = new PathHandler() .addPrefixPath(config.getPath(), new ResourceHandler(new PathResourceManager(Paths.get(config.getBase()), config.getTransferMinSize())) .setDirectoryListingEnabled(config.isDirectoryListingEnabled())); } else { pathHandler = new PathHandler() .addExactPath(config.getPath(), new ResourceHandler(new PathResourceManager(Paths.get(config.getBase()), config.getTransferMinSize())) .setDirectoryListingEnabled(config.isDirectoryListingEnabled())); } }
public VirtualHostHandler() { VirtualHostConfig config = (VirtualHostConfig)Config.getInstance().getJsonObjectConfig(VirtualHostConfig.CONFIG_NAME, VirtualHostConfig.class); virtualHostHandler = new NameVirtualHostHandler(); for(VirtualHost host: config.hosts) { virtualHostHandler.addHost(host.domain, new PathHandler().addPrefixPath(host.getPath(), new ResourceHandler((new PathResourceManager(Paths.get(host.getBase()), host.getTransferMinSize()))).setDirectoryListingEnabled(host.isDirectoryListingEnabled()))); } }
/** * 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); }
/** * 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); }
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)); }
@Override public HttpHandler wrap(HttpHandler handler) { ResourceManager rm = new PathResourceManager(Paths.get(location), 1024); ResourceHandler resourceHandler = new ResourceHandler(rm); resourceHandler.setDirectoryListingEnabled(allowDirectoryListing); return resourceHandler; } }
/** * 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); }
@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 Http4K publishFileSystem( String hostName, String urlPath, int port, FileResourceManager man ) { if ( ! man.getBase().isDirectory() ) { throw new RuntimeException("root must be an existing direcory:"+man.getBase().getAbsolutePath()); } Pair<PathHandler, Undertow> server = getServer(port, hostName); server.car().addPrefixPath(urlPath, new ResourceHandler(man )); return this; } public Http4K publishResourcePath( String hostName, String urlPath, int port, DynamicResourceManager man, boolean compress ) {
/** * publishes given file root * @param hostName * @param urlPath - prefixPath (e.g. /myapp/resource) * @param port * @param root - directory to be published */ public Http4K publishFileSystem( String hostName, String urlPath, int port, File root ) { if ( ! root.exists() ) root.mkdirs(); if ( ! root.isDirectory() ) { throw new RuntimeException("root must be an existing direcory:"+root.getAbsolutePath()); } Pair<PathHandler, Undertow> server = getServer(port, hostName); server.car().addPrefixPath(urlPath, new ResourceHandler(new FileResourceManager(root,100))); return this; }
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 ); } }
private void deployWebJarStaticRouting(Config staticConfig, DeploymentContext context) { if ( staticConfig.getBoolean("webjar-enabled" ) ) { final String urlPrefix = staticConfig.getString("webjar-url-prefix").replaceFirst( "/$", "" ); log.info( "Enabling static routing for webjars at '" + urlPrefix + "'" ); final String webJarInternalLocation = staticConfig.getString("webjar-location", DEFAULT_WEBJAR_LOCATION); final ResourceManager resourceManager = SystemResource.loadResourceManagerFor( webJarInternalLocation ); final HttpHandler webjarHandler = new WebJarHttpHandler( new ResourceHandler(resourceManager, new WebJarNotFound( context.fallbackHandler() ) ), URLMatcher.compile( urlPrefix + "/{path}" ), context.fallbackHandler()); context.fallbackHandler( webjarHandler ); } }
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); }
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); }
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); }