io.undertow.server.handlers.resource.ResourceManager Java Examples

The following examples show how to use io.undertow.server.handlers.resource.ResourceManager. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DirectoryPredicate.java    From quarkus-http with Apache License 2.0 6 votes vote down vote up
@Override
public boolean resolve(final HttpServerExchange value) {
    String location = this.location.readAttribute(value);
    ServletRequestContext src = value.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
    if(src == null) {
        return false;
    }
    ResourceManager manager = src.getDeployment().getDeploymentInfo().getResourceManager();
    if(manager == null) {
        return false;
    }
    try {
        Resource resource = manager.getResource(location);
        if(resource == null) {
            return false;
        }
        return resource.isDirectory();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #2
Source File: PathResourceManagerTestCase.java    From quarkus-http with Apache License 2.0 6 votes vote down vote up
@Test
public void testETagFunction() throws Exception {
    final String fileName = "page.html";
    final Path rootPath = Paths.get(getClass().getResource(fileName).toURI()).getParent();
    final ResourceManager resourceManager = PathResourceManager.builder()
            .setBase(rootPath)
            .setETagFunction(new PathResourceManager.ETagFunction() {
                @Override
                public ETag generate(Path path) {
                    return new ETag(true, path.getFileName().toString());
                }
            })
            .build();
    ETag expected = new ETag(true, fileName);
    ETag actual = resourceManager.getResource("page.html").getETag();
    Assert.assertEquals(expected, actual);
}
 
Example #3
Source File: SikulixServer.java    From SikuliX1 with MIT License 6 votes vote down vote up
private static Undertow createServer(int port, String ipAddr) {
  ControllerCommand controller = new ControllerCommand();
  TasksCommand tasks = new TasksCommand();
  ScriptsCommand scripts = new ScriptsCommand(tasks);
  GroupsCommand groups = new GroupsCommand(scripts);

  ResourceManager resourceManager = new ClassPathResourceManager(RunTime.class.getClassLoader(), "htdocs");
  ResourceHandler resource = new ResourceHandler(resourceManager, AbstractCommand.getFallbackHandler());
  resource.addWelcomeFiles("ControlBox.html");

  RoutingHandler commands = Handlers.routing()
          .addAll(controller.getRouting())
          .addAll(tasks.getRouting())
          .addAll(scripts.getRouting())
          .addAll(groups.getRouting())
          .setFallbackHandler(resource);
  CommandRootHttpHandler cmdRoot = new CommandRootHttpHandler(commands);
  cmdRoot.addExceptionHandler(Throwable.class, AbstractCommand.getExceptionHttpHandler());

  Undertow server = Undertow.builder()
          .addHttpListener(port, ipAddr)
          .setServerOption(UndertowOptions.RECORD_REQUEST_START_TIME, true)
          .setHandler(cmdRoot)
          .build();
  return server;
}
 
Example #4
Source File: DirectoryPredicate.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean resolve(final HttpServerExchange value) {
    String location = this.location.readAttribute(value);
    ServletRequestContext src = value.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
    if(src == null) {
        return false;
    }
    ResourceManager manager = src.getDeployment().getDeploymentInfo().getResourceManager();
    if(manager == null) {
        return false;
    }
    try {
        Resource resource = manager.getResource(location);
        if(resource == null) {
            return false;
        }
        return resource.isDirectory();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #5
Source File: CompositeResourceManagerTest.java    From joinfaces with Apache License 2.0 6 votes vote down vote up
@Test
public void testClose() throws IOException {
	ResourceManager resourceManager1 = mock(ResourceManager.class);
	ResourceManager resourceManager2 = mock(ResourceManager.class);
	ResourceManager resourceManager3 = mock(ResourceManager.class);

	CompositeResourceManager compositeResourceManager = new CompositeResourceManager(
			resourceManager1, resourceManager2, resourceManager3
	);

	compositeResourceManager.close();

	verify(resourceManager1).close();
	verify(resourceManager2).close();
	verify(resourceManager3).close();
}
 
Example #6
Source File: CustomResourceHandler.java    From PYX-Reloaded with Apache License 2.0 5 votes vote down vote up
private static ResourceManager buildResourceManager(Preferences preferences) {
    Path root = Paths.get(preferences.getString("webContent", "./WebContent")).toAbsolutePath();
    boolean cacheEnabled = preferences.getBoolean("cacheEnabled", true);

    return PathResourceManager.builder()
            .setBase(root)
            .setAllowResourceChangeListeners(false)
            .setETagFunction(new ETagSupplier(cacheEnabled, Utils.generateAlphanumericString(5)))
            .build();
}
 
Example #7
Source File: PrefixingResourceManager.java    From HotswapAgent with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void removeResourceChangeListener(ResourceChangeListener listener) {
    for(ResourceManager del : delegates) {
        if(del.isResourceChangeListenerSupported()) {
            del.removeResourceChangeListener(listener);
        }
    }
}
 
Example #8
Source File: PrefixingResourceManager.java    From HotswapAgent with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void registerResourceChangeListener(ResourceChangeListener listener) {
    for(ResourceManager del : delegates) {
        if(del.isResourceChangeListenerSupported()) {
            del.registerResourceChangeListener(listener);
        }
    }
}
 
Example #9
Source File: PrefixingResourceManager.java    From HotswapAgent with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Resource getResource(String path) throws IOException {
    for(ResourceManager d : delegates) {
        Resource res = d.getResource(path);
        if(res != null) {
            return res;
        }
    }
    return null;
}
 
Example #10
Source File: PrefixingResourceManager.java    From HotswapAgent with GNU General Public License v2.0 5 votes vote down vote up
public void setExtraResources(List extraResources) {
    List<ResourceManager> delegates = new ArrayList<>();
    for (Object o: extraResources) {
        File resource = File.class.cast(o);
        try {
            delegates.add(new FileResourceManager(resource.getCanonicalFile(), 1024, true, false, "/"));
        } catch (IOException e) {
            LOGGER.warning("Unable to create cannonical file from {}. File skipped.", resource.getName(), e);
        }
    }
    delegates.addAll(this.delegates);
    this.delegates = delegates;
}
 
Example #11
Source File: Server.java    From divolte-collector with Apache License 2.0 5 votes vote down vote up
private static HttpHandler createStaticResourceHandler() {
    final ResourceManager staticResources =
            new ClassPathResourceManager(Server.class.getClassLoader(), "static");
    // Cache tuning is copied from Undertow unit tests.
    final ResourceManager cachedResources =
            new CachingResourceManager(100, 65536,
                                       new DirectBufferCache(1024, 10, 10480),
                                       staticResources,
                                       (int)Duration.ofDays(1).getSeconds());
    final ResourceHandler resourceHandler = new ResourceHandler(cachedResources);
    resourceHandler.setWelcomeFiles("index.html");
    return resourceHandler;
}
 
Example #12
Source File: ManagementHttpServer.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public synchronized void addStaticContext(String contextName, ResourceManager resourceManager) {
    Assert.checkNotNullParam("contextName", contextName);
    Assert.checkNotNullParam("resourceManager", resourceManager);
    String context = fixPath(contextName);
    // Reject reserved contexts or duplicate extensions
    if (extensionHandlers.reservedContexts.contains(context) || !extensionHandlers.extensionContexts.add(context)) {
        throw new IllegalStateException();
    }
    ResourceHandlerDefinition def = DomainUtil.createStaticContentHandler(resourceManager, context);
    HttpHandler readinessHandler = new RedirectReadinessHandler(extensionHandlers.readyFunction, def.getHandler(),
            ErrorContextHandler.ERROR_CONTEXT);
    extensionHandlers.extensionPathHandler.addPrefixPath(context, readinessHandler);
}
 
Example #13
Source File: DomainUtil.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
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);
}
 
Example #14
Source File: CompositeResourceManager.java    From joinfaces with Apache License 2.0 5 votes vote down vote up
@Override
public Resource getResource(String path) throws IOException {
	for (ResourceManager resourceManager : this.resourceManagers) {
		Resource resource = resourceManager.getResource(path);
		if (resource != null) {
			return resource;
		}
	}
	return null;
}
 
Example #15
Source File: FilePredicate.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@Override
public boolean resolve(final HttpServerExchange value) {
    String location = this.location.readAttribute(value);
    ServletRequestContext src = value.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
    if(src == null) {
        return false;
    }
    ResourceManager manager = src.getDeployment().getDeploymentInfo().getResourceManager();
    if(manager == null) {
        return false;
    }
    try {
        Resource resource = manager.getResource(location);
        if(resource == null) {
            return false;
        }
        if(resource.isDirectory()) {
            return false;
        }
        if(requireContent){
          return resource.getContentLength() != null && resource.getContentLength() > 0;
        } else {
            return true;
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #16
Source File: ManagedServlet.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public synchronized void start() throws ServletException {
    try {
        handle = factory.createInstance();
    } catch (Exception e) {
        throw UndertowServletMessages.MESSAGES.couldNotInstantiateComponent(servletInfo.getName(), e);
    }
    instance = handle.getInstance();
    new LifecyleInterceptorInvocation(servletContext.getDeployment().getDeploymentInfo().getLifecycleInterceptors(), servletInfo, instance, new ServletConfigImpl(servletInfo, servletContext)).proceed();

    //if a servlet implements FileChangeCallback it will be notified of file change events
    final ResourceManager resourceManager = servletContext.getDeployment().getDeploymentInfo().getResourceManager();
    if(instance instanceof ResourceChangeListener && resourceManager.isResourceChangeListenerSupported()) {
        resourceManager.registerResourceChangeListener(changeListener = (ResourceChangeListener) instance);
    }
}
 
Example #17
Source File: DelegatingResourceManager.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Override
public Resource getResource(String path) throws IOException {
    for (ResourceManager i : delegates) {
        Resource res = i.getResource(path);
        if (res != null) {
            return res;
        }
    }
    return null;
}
 
Example #18
Source File: FilePredicate.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean resolve(final HttpServerExchange value) {
    String location = this.location.readAttribute(value);
    ServletRequestContext src = value.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
    if(src == null) {
        return false;
    }
    ResourceManager manager = src.getDeployment().getDeploymentInfo().getResourceManager();
    if(manager == null) {
        return false;
    }
    try {
        Resource resource = manager.getResource(location);
        if(resource == null) {
            return false;
        }
        if(resource.isDirectory()) {
            return false;
        }
        if(requireContent){
          return resource.getContentLength() != null && resource.getContentLength() > 0;
        } else {
            return true;
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #19
Source File: ManagedServlet.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public synchronized void stop() {
    if (handle != null) {
        final ResourceManager resourceManager = servletContext.getDeployment().getDeploymentInfo().getResourceManager();
        if(changeListener != null) {
            resourceManager.removeResourceChangeListener(changeListener);
        }
        invokeDestroy();
        handle.release();
    }
}
 
Example #20
Source File: DelegatingResourceManager.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@Override
public void close() throws IOException {
    for (ResourceManager i : delegates) {
        i.close();
    }
}
 
Example #21
Source File: PrefixingResourceManager.java    From HotswapAgent with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void close() throws IOException {
    for(ResourceManager del : delegates) {
        IoUtils.safeClose(del);
    }
}
 
Example #22
Source File: DeploymentInfo.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
public ResourceManager getResourceManager() {
    return resourceManager;
}
 
Example #23
Source File: DeploymentInfo.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
public DeploymentInfo setResourceManager(final ResourceManager resourceManager) {
    this.resourceManager = resourceManager;
    return this;
}
 
Example #24
Source File: DelegatingResourceManager.java    From quarkus with Apache License 2.0 4 votes vote down vote up
public DelegatingResourceManager(ResourceManager... delegates) {
    this.delegates = Arrays.asList(delegates);
}
 
Example #25
Source File: PrefixingResourceManager.java    From HotswapAgent with GNU General Public License v2.0 4 votes vote down vote up
public PrefixingResourceManager(ResourceManager delegate) {
    this.delegates = new ArrayList<>();
    this.delegates.add(delegate);
}
 
Example #26
Source File: UndertowHttpManagementService.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void addStaticContext(String contextName, ResourceManager resourceManager) {
    Assert.assertNotNull(serverManagement);
    serverManagement.addStaticContext(contextName, resourceManager);
}
 
Example #27
Source File: DeploymentInfo.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public ResourceManager getResourceManager() {
    return resourceManager;
}
 
Example #28
Source File: DeploymentInfo.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public DeploymentInfo setResourceManager(final ResourceManager resourceManager) {
    this.resourceManager = resourceManager;
    return this;
}
 
Example #29
Source File: CompositeResourceManager.java    From joinfaces with Apache License 2.0 4 votes vote down vote up
@Override
public void close() throws IOException {
	for (ResourceManager resourceManager : this.resourceManagers) {
		resourceManager.close();
	}
}
 
Example #30
Source File: CompositeResourceManager.java    From joinfaces with Apache License 2.0 4 votes vote down vote up
CompositeResourceManager(ResourceManager... resourceManagers) {
	this.resourceManagers = Arrays.asList(resourceManagers);
}