Hi Stuart,
Tomaž warned me by IRC chat about that.
At the end I developed like this:
URL rootUrl = getClassLoader().getResource(".");
File root = new File(rootUrl.getFile());
ResourceManager resourceManager = new FileResourceManager(root, 1024 *
1024);
deploymentInfo.setResourceManager(resourceManager);
It works, and now I will try to experiment better perfomance
using CachingResourceManager as suggested by Tomaž.
Thanks everybody!
--
Davide
On Wed, Nov 12, 2014 at 9:12 PM, Stuart Douglas <sdouglas(a)redhat.com> wrote:
ServletResourceManager is only really relevant to Wildfly. You will
want
to use either FileResourceManager or ClassPathResourceManager.
Stuart
----- Original Message -----
> From: "Tomaž Cerar" <tomaz.cerar(a)gmail.com>
> To: "Davide Ungari" <ungarida(a)gmail.com>
> Cc: undertow-dev(a)lists.jboss.org
> Sent: Wednesday, 12 November, 2014 11:47:01 PM
> Subject: Re: [undertow-dev] ServletContext.getResource() and FileNotFound
>
> I think you are missing configuration for resourceManager, as if not set
it
> defaults to empty resource manager.
>
>
> add something like this to getDeploymentInfo
> ResourceManager resourceManager = new ServletResourceManager(
deploymentRoot
> , overlays , explodedDeployment );
> //this is not needed, but it good for performance
> resourceManager = new CachingResourceManager( 100 , 10 * 1024 * 1024 ,
> servletContainer.getBufferCache(), resourceManager, explodedDeployment ?
> 2000 : - 1 );
> deploymentInfo.setResourceManager(resourceManager);
> --
> tomaz
>
> On Wed, Nov 12, 2014 at 10:56 AM, Davide Ungari < ungarida(a)gmail.com >
wrote:
>
>
>
> Hi,
> I'm using version 1.1.0.CR8.
>
> --
> Davide
>
> On Wed, Nov 12, 2014 at 8:47 AM, Davide Ungari < ungarida(a)gmail.com >
wrote:
>
>
>
> Hi everybody,
> I'm trying to migrate my webapp from Jetty to Undertow.
>
> The stack is composed by Undertow Guice and Struts2.
>
> I developed the following class to configure undertow:
>
>
>
>
> package com.billdrawer.website.server;
>
> import io.undertow.Undertow;
> import io.undertow.predicate.Predicates;
> import io.undertow.server.HandlerWrapper;
> import io.undertow.server.HttpHandler;
> import io.undertow.server.HttpServerExchange;
> import io.undertow.server.handlers.PathHandler;
> import io.undertow.server.handlers.PredicateHandler;
> import io.undertow.server.handlers.resource.ResourceHandler;
> import io.undertow.servlet.api.ConfidentialPortManager;
> import io.undertow.servlet.api.Deployment;
> import io.undertow.servlet.api.DeploymentInfo;
> import io.undertow.servlet.api.DeploymentManager;
> import io.undertow.servlet.api.FilterInfo;
> import io.undertow.servlet.api.ListenerInfo;
> import io.undertow.servlet.api.LoginConfig;
> import io.undertow.servlet.api.SecurityConstraint;
> import io.undertow.servlet.api.ServletContainer;
> import io.undertow.servlet.api.WebResourceCollection;
>
> import java.net.URL;
> import java.nio.file.Files;
> import java.nio.file.Paths;
> import java.util.EnumSet;
>
> import javax.inject.Inject;
> import javax.inject.Named;
> import javax.servlet.DispatcherType;
> import javax.servlet.ServletContext;
> import javax.servlet.ServletException;
>
> import org.apache.log4j.Logger;
> import
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter;
> import org.keycloak.adapters.AdapterConstants;
> import org.keycloak.adapters.undertow.KeycloakServletExtension;
>
> import com.billdrawer.website.common.Config;
> import com.billdrawer.website.listener.WebsiteServletContextListener;
> import com.google.inject.servlet.GuiceFilter;
>
> public class UndertowServer implements Server {
> private Logger _logger = Logger.getLogger(UndertowServer.class);
> private Undertow _server;
> private DeploymentManager _manager;
> private int _port;
> private String _host;
>
> @Inject
> public UndertowServer((a)Named(Config.STAGE) String stage,
> @Named(Config.SERVER_HOST) String host, @Named(Config.SERVER_PORT)
int
> port,
> @Named(Config.SERVER_TIMEOUT) int timeout) {
> _port = port;
> _host = host;
>
> DeploymentInfo deploymentInfo = getDeploymentInfo(stage);
>
> ServletContainer _container =
ServletContainer.Factory.newInstance();
> _manager = _container.addDeployment(deploymentInfo);
> _manager.deploy();
>
> Deployment deployment = _manager.getDeployment();
>
> KeycloakServletExtension keycloak = new
KeycloakServletExtension();
> deploymentInfo.addServletExtension(keycloak);
>
deploymentInfo.addInitParameter(AdapterConstants.AUTH_DATA_PARAM_NAME,
> getKeyloakJson());
> keycloak.handleDeployment(deploymentInfo,
> _manager.getDeployment().getServletContext());
>
> final ServletContext servletContext =
deployment.getServletContext();
> _logger.debug("Context
> initialized:"+servletContext.getContextPath());
> }
>
> protected String getKeyloakJson() {
> String keycloak = "";
> URL url = getClassLoader().getResource("keycloak.json");
> if (url != null) {
> try {
> byte[] encoded =
Files.readAllBytes(Paths.get(url.toURI()));
> keycloak = new String(encoded, "utf-8");
> } catch (Exception e) {
> _logger.error("Can't read keycloak.json", e);
> }
> }
> return keycloak;
> }
>
> protected DeploymentInfo getDeploymentInfo(String stage) {
> final DeploymentInfo deploymentInfo = new DeploymentInfo();
> deploymentInfo.setClassLoader(getClassLoader());
> deploymentInfo.setContextPath("/");
> deploymentInfo.setDefaultEncoding("UTF-8");
> deploymentInfo.setDeploymentName("website.war");
> deploymentInfo.setDisplayName("WebsiteServer");
> deploymentInfo.setUrlEncoding("UTF-8");
>
> deploymentInfo.addListener(new
> ListenerInfo(WebsiteServletContextListener.class));
>
> FilterInfo guiceFilter = new FilterInfo("GuiceFilter",
> GuiceFilter.class);
> deploymentInfo.addFilter(guiceFilter);
> for (DispatcherType dispatcher :
EnumSet.allOf(DispatcherType.class))
> {
> deploymentInfo.addFilterUrlMapping(guiceFilter.getName(),
"/*",
> dispatcher);
> }
>
> FilterInfo strutsPrepareAndExecuteFilter = new
> FilterInfo("StrutsFilter", StrutsPrepareAndExecuteFilter.class);
> deploymentInfo.addFilter(strutsPrepareAndExecuteFilter);
> for (DispatcherType dispatcher :
EnumSet.allOf(DispatcherType.class))
> {
>
deploymentInfo.addFilterUrlMapping(strutsPrepareAndExecuteFilter.getName(),
> "/*", dispatcher);
> }
>
> deploymentInfo.addInitialHandlerChainWrapper(new
HandlerWrapper() {
> @Override
> public HttpHandler wrap(final HttpHandler handler) {
> final ResourceHandler resourceHandler = new
> ResourceHandler(deploymentInfo.getResourceManager());
> PredicateHandler predicateHandler = new
>
PredicateHandler(Predicates.suffixes(".css",".html",".js"),resourceHandler,
> handler);
> return predicateHandler;
> }
> });
>
> configureSecurity(deploymentInfo);
>
> return deploymentInfo;
> }
>
> protected void configureSecurity(DeploymentInfo deploymentInfo) {
> deploymentInfo.setLoginConfig(new LoginConfig("KEYCLOAK",
> "billdrawer"));
> deploymentInfo.setConfidentialPortManager(new
> ConfidentialPortManager() {
> @Override
> public int getConfidentialPort(HttpServerExchange exchange) {
> return _port;
> }
> });
> SecurityConstraint securityConstraint = new SecurityConstraint();
> WebResourceCollection webResourceCollection = new
> WebResourceCollection();
> webResourceCollection.addUrlPattern("/dashboard/*");
>
securityConstraint.addWebResourceCollection(webResourceCollection);
> securityConstraint.addRoleAllowed("user");
> deploymentInfo.addSecurityConstraint(securityConstraint);
> }
>
> protected ClassLoader getClassLoader() {
> return getClass().getClassLoader();
> }
>
> public void start() {
> _logger.debug("starting...");
> try {
> Undertow.Builder builder = Undertow.builder();
> builder.addHttpListener(_port, _host);
>
> final PathHandler rootHandler = new PathHandler();
>
rootHandler.addPrefixPath(_manager.getDeployment().getDeploymentInfo().getContextPath(),
> _manager.start());
>
> builder.setHandler(rootHandler);
> _server = builder.build();
> _server.start();
> } catch (ServletException e) {
> _logger.error("Unexpected exception", e);
> }
> _ logger.info ("###START###");
> }
>
> public void stop() {
> _logger.debug("stopping...");
> try {
> _manager.stop();
> _server.stop();
> } catch (ServletException e) {
> _logger.error("Unexpected exception", e);
> }
> _ logger.info ("###STOP###");
> }
> }
> Everything is working until I do not try to load resources from context.
> I need some help before rollback to Jetty :(
>
> --
> Davide
>
>
> _______________________________________________
> undertow-dev mailing list
> undertow-dev(a)lists.jboss.org
>
https://lists.jboss.org/mailman/listinfo/undertow-dev
>
>
> _______________________________________________
> undertow-dev mailing list
> undertow-dev(a)lists.jboss.org
>
https://lists.jboss.org/mailman/listinfo/undertow-dev