[undertow-dev] serving static files

Stuart Douglas sdouglas at redhat.com
Mon Feb 3 05:38:06 EST 2014


You need to make a choice in the handler when to delegate to the next handler (i.e. servlet), and when to intercept the request and serve your static files. 

Generally you will do this by simply looking at the path. If for example you only want to serve some paths from JAX-RS you can use a path prefix predicate to specify the paths that get delegated to the servlet handler, and hand everything else to your resource handler. 

Note that this is all a bit of a hack, the correct way to do this would be to modify the resteasy server so it does not register itself as the root handler.

Stuart

----- Original Message -----
> From: "Matthias Wessendorf" <matzew at redhat.com>
> To: "Stuart Douglas" <sdouglas at redhat.com>
> Cc: undertow-dev at lists.jboss.org
> Sent: Monday, 3 February, 2014 12:29:56 PM
> Subject: Re: [undertow-dev] serving static files
> 
> But now the JAX-RS endpoint no longer works :)
> 
> 
> 
> ----- Original Message -----
> > From: "Matthias Wessendorf" <matzew at redhat.com>
> > To: "Stuart Douglas" <sdouglas at redhat.com>
> > Cc: undertow-dev at lists.jboss.org
> > Sent: Monday, February 3, 2014 11:24:23 AM
> > Subject: Re: [undertow-dev] serving static files
> > 
> > Thanks for the reply,
> > 
> > using the Extension works very well - updated my example project:
> > 
> > https://github.com/matzew/undertow-jaxrs-test/blob/master/src/main/resources/META-INF/services/io.undertow.servlet.ServletExtension
> > 
> > -M
> > 
> > ----- Original Message -----
> > > From: "Stuart Douglas" <sdouglas at redhat.com>
> > > To: "Matthias Wessendorf" <matzew at redhat.com>
> > > Cc: undertow-dev at lists.jboss.org
> > > Sent: Monday, February 3, 2014 10:16:16 AM
> > > Subject: Re: [undertow-dev] serving static files
> > > 
> > > Yes and no.
> > > 
> > > It looks like Bill has not added that capability into the Resteasy
> > > server,
> > > however you could use a ServletExtension to serve static files from
> > > certain
> > > paths:
> > > 
> > > http://undertow.io/documentation/servlet/using-non-blocking-handlers-with-servlet.html
> > > 
> > > The other option would be to file a Resteasy JIRA (or better yet provide
> > > a
> > > pull request) to add more flexibility to the server.
> > > 
> > > Stuart
> > > 
> > > ----- Original Message -----
> > > > From: "Matthias Wessendorf" <matzew at redhat.com>
> > > > To: "Stuart Douglas" <sdouglas at redhat.com>
> > > > Cc: undertow-dev at lists.jboss.org
> > > > Sent: Monday, 3 February, 2014 11:12:30 AM
> > > > Subject: Re: [undertow-dev] serving static files
> > > > 
> > > > 
> > > > 
> > > > ----- Original Message -----
> > > > > From: "Stuart Douglas" <sdouglas at redhat.com>
> > > > > To: "Matthias Wessendorf" <matzew at redhat.com>
> > > > > Cc: undertow-dev at lists.jboss.org
> > > > > Sent: Monday, February 3, 2014 9:57:42 AM
> > > > > Subject: Re: [undertow-dev] serving static files
> > > > > 
> > > > > This is the code from the Resteasy Undertow server:
> > > > > 
> > > > >    public UndertowJaxrsServer start(Undertow.Builder builder)
> > > > >    {
> > > > >       server = builder.setHandler(root).build();
> > > > >       server.start();
> > > > >       return this;
> > > > >    }
> > > > > 
> > > > > So basically when you call start the servlet container is always
> > > > > registered
> > > > > as the root resource.
> > > > 
> > > > So, that means I am not able to provide a 'FileResourceManager' in that
> > > > Undertow/RestEasy combination ?
> > > > 
> > > > > 
> > > > > 
> > > > > Stuart
> > > > > 
> > > > > ----- Original Message -----
> > > > > > From: "Matthias Wessendorf" <matzew at redhat.com>
> > > > > > To: "Stuart Douglas" <sdouglas at redhat.com>
> > > > > > Cc: undertow-dev at lists.jboss.org
> > > > > > Sent: Monday, 3 February, 2014 10:38:04 AM
> > > > > > Subject: Re: [undertow-dev] serving static files
> > > > > > 
> > > > > > Hello Stuart,
> > > > > > 
> > > > > > I pushed my test to github. the 'main' is located here:
> > > > > > https://github.com/matzew/undertow-jaxrs-test/blob/master/src/main/java/net/wessendorf/undertow/App.java
> > > > > > 
> > > > > > When I run that and I am accessing "http://localhost:8080/status",
> > > > > > all
> > > > > > is
> > > > > > fine, my 'StatusEndpoint' JAX-RS resource is executed and I get my
> > > > > > response;
> > > > > > However, when I am hitting "http://localhost:8080" am getting 404
> > > > > > in
> > > > > > the
> > > > > > browser, and the following on the console in my IDE:
> > > > > > 
> > > > > > 
> > > > > > Feb 03, 2014 9:26:04 AM org.jboss.resteasy.core.ExceptionHandler
> > > > > > SEVERE: failed to execute
> > > > > > javax.ws.rs.NotFoundException: Could not find resource for full
> > > > > > path:
> > > > > > http://localhost:8080/
> > > > > > 	at
> > > > > > 	org.jboss.resteasy.core.registry.ClassNode.match(ClassNode.java:73)
> > > > > > 	at
> > > > > > 	org.jboss.resteasy.core.registry.RootClassNode.match(RootClassNode.java:48)
> > > > > > 	at
> > > > > > 	org.jboss.resteasy.core.ResourceMethodRegistry.getResourceInvoker(ResourceMethodRegistry.java:444)
> > > > > > 	at
> > > > > > 	org.jboss.resteasy.core.SynchronousDispatcher.getInvoker(SynchronousDispatcher.java:234)
> > > > > > 	at
> > > > > > 	org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:171)
> > > > > > 	at
> > > > > > 	org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:220)
> > > > > > 	at
> > > > > > 	org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)
> > > > > > 	at
> > > > > > 	org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51)
> > > > > > 	at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
> > > > > > 	at
> > > > > > 	io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
> > > > > > 	at
> > > > > > 	io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:61)
> > > > > > 	at
> > > > > > 	io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
> > > > > > 	at
> > > > > > 	io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:113)
> > > > > > 	at
> > > > > > 	io.undertow.security.handlers.AuthenticationCallHandler.handleRequest(AuthenticationCallHandler.java:52)
> > > > > > 	at
> > > > > > 	io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:45)
> > > > > > 	at
> > > > > > 	io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:61)
> > > > > > 	at
> > > > > > 	io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:70)
> > > > > > 	at
> > > > > > 	io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76)
> > > > > > 	at
> > > > > > 	io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
> > > > > > 	at
> > > > > > 	io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
> > > > > > 	at
> > > > > > 	io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:240)
> > > > > > 	at
> > > > > > 	io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:227)
> > > > > > 	at
> > > > > > 	io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:73)
> > > > > > 	at
> > > > > > 	io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:146)
> > > > > > 	at
> > > > > > 	io.undertow.server.Connectors.executeRootHandler(Connectors.java:168)
> > > > > > 	at
> > > > > > 	io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:687)
> > > > > > 	at
> > > > > > 	java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
> > > > > > 	at
> > > > > > 	java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
> > > > > > 	at java.lang.Thread.run(Thread.java:722)
> > > > > > 
> > > > > > 
> > > > > > 
> > > > > > 
> > > > > > 
> > > > > > 
> > > > > > 
> > > > > > So, looks like the JAX-RS is somewhat having an effect on the
> > > > > > actual
> > > > > > "FileResourceManager" that should serve my static files
> > > > > > 
> > > > > > 
> > > > > > 
> > > > > > -Matthias
> > > > > > 
> > > > > > 
> > > > > > 
> > > > > > 
> > > > > > ----- Original Message -----
> > > > > > > From: "Stuart Douglas" <sdouglas at redhat.com>
> > > > > > > To: "Matthias Wessendorf" <matzew at redhat.com>
> > > > > > > Cc: undertow-dev at lists.jboss.org
> > > > > > > Sent: Monday, February 3, 2014 8:39:08 AM
> > > > > > > Subject: Re: [undertow-dev] serving static files
> > > > > > > 
> > > > > > > What does the rest of your code look like? And when you say it
> > > > > > > does
> > > > > > > not
> > > > > > > work,
> > > > > > > what sort of failure are you getting? A 404?
> > > > > > > 
> > > > > > > Stuart
> > > > > > > 
> > > > > > > ----- Original Message -----
> > > > > > > > From: "Matthias Wessendorf" <matzew at redhat.com>
> > > > > > > > To: "Stuart Douglas" <sdouglas at redhat.com>
> > > > > > > > Cc: undertow-dev at lists.jboss.org
> > > > > > > > Sent: Monday, 3 February, 2014 8:39:11 AM
> > > > > > > > Subject: Re: [undertow-dev] serving static files
> > > > > > > > 
> > > > > > > > HEllo,
> > > > > > > > 
> > > > > > > > that works fine - however trying the same in my 'test': does
> > > > > > > > not
> > > > > > > > work
> > > > > > > > :-/
> > > > > > > > 
> > > > > > > > 
> > > > > > > >         UndertowJaxrsServer ut = new UndertowJaxrsServer();
> > > > > > > > 
> > > > > > > >         TestApplication ta = new TestApplication();
> > > > > > > > 
> > > > > > > >         ut.deploy(ta);
> > > > > > > > 
> > > > > > > >         ut.start(
> > > > > > > >                 Undertow.builder()
> > > > > > > >                         .addHttpListener(8080, "localhost")
> > > > > > > >                         .setHandler(resource(new
> > > > > > > >                         FileResourceManager(new
> > > > > > > >                         File(System.getProperty("user.home")),
> > > > > > > >                         100))
> > > > > > > >                                 .setDirectoryListingEnabled(true))
> > > > > > > > 
> > > > > > > > 
> > > > > > > >         );
> > > > > > > > 
> > > > > > > >         try {
> > > > > > > >             Thread.currentThread().join();
> > > > > > > >         }
> > > > > > > >         catch (InterruptedException e) {
> > > > > > > >             logger.info("shutting down");
> > > > > > > >             ut.stop();
> > > > > > > >         }
> > > > > > > > 
> > > > > > > > 
> > > > > > > > 
> > > > > > > > 
> > > > > > > > -Matthias
> > > > > > > > 
> > > > > > > > 
> > > > > > > > 
> > > > > > > > 
> > > > > > > > ----- Original Message -----
> > > > > > > > > From: "Stuart Douglas" <sdouglas at redhat.com>
> > > > > > > > > To: "Matthias Wessendorf" <matzew at redhat.com>
> > > > > > > > > Cc: undertow-dev at lists.jboss.org
> > > > > > > > > Sent: Sunday, February 2, 2014 6:37:59 PM
> > > > > > > > > Subject: Re: [undertow-dev] serving static files
> > > > > > > > > 
> > > > > > > > > I just added a file serving example to the Undertow examples,
> > > > > > > > > can
> > > > > > > > > you
> > > > > > > > > try
> > > > > > > > > that out and let me know if it works for you?
> > > > > > > > > 
> > > > > > > > > Just get the latest Undertow, build the examples, then run:
> > > > > > > > > 
> > > > > > > > > mvn exec:exec
> > > > > > > > > 
> > > > > > > > > And select the file serving one, it should serve up you home
> > > > > > > > > directory.
> > > > > > > > > 
> > > > > > > > > Stuart
> > > > > > > > > 
> > > > > > > > > ----- Original Message -----
> > > > > > > > > > From: "Matthias Wessendorf" <matzew at redhat.com>
> > > > > > > > > > To: undertow-dev at lists.jboss.org
> > > > > > > > > > Sent: Sunday, 2 February, 2014 1:58:14 PM
> > > > > > > > > > Subject: [undertow-dev] serving static files
> > > > > > > > > > 
> > > > > > > > > > Hello,
> > > > > > > > > > 
> > > > > > > > > > I might be just doing it wrong, but the following attempts
> > > > > > > > > > are
> > > > > > > > > > not
> > > > > > > > > > successful
> > > > > > > > > > for serving static files from a folder:
> > > > > > > > > > 
> > > > > > > > > > 
> > > > > > > > > >         UndertowJaxrsServer ut = new UndertowJaxrsServer();
> > > > > > > > > >  
> > > > > > > > > >         MyRestApplication app = new MyRestApplication();
> > > > > > > > > >  
> > > > > > > > > >         ut.deploy(app);
> > > > > > > > > >  
> > > > > > > > > >         ut.start(
> > > > > > > > > >                 Undertow.builder()
> > > > > > > > > >                         .addHttpListener(8080, "0.0.0.0")
> > > > > > > > > > 
> > > > > > > > > >  
> > > > > > > > > >                 // first tried this:
> > > > > > > > > >                         .setHandler(path()
> > > > > > > > > >                                     .addPrefixPath("/",resource(new
> > > > > > > > > >                                     FileResourceManager(new
> > > > > > > > > >                                     File("/some/folder/containing/html/files"),
> > > > > > > > > >                                     1024))))
> > > > > > > > > >  
> > > > > > > > > > 
> > > > > > > > > >                 // afterwards also tried this:
> > > > > > > > > >                         .setHandler(resource(new
> > > > > > > > > >                         FileResourceManager(new
> > > > > > > > > >                         File("/some/folder/containing/html/files"),
> > > > > > > > > >                         1024)))
> > > > > > > > > >         );
> > > > > > > > > >  
> > > > > > > > > >         try {
> > > > > > > > > >             Thread.currentThread().join();
> > > > > > > > > >         }
> > > > > > > > > >         catch (InterruptedException e) {
> > > > > > > > > >             logger.info("shutting down");
> > > > > > > > > >             ut.stop();
> > > > > > > > > >         }
> > > > > > > > > > 
> > > > > > > > > > 
> > > > > > > > > > 
> > > > > > > > > > 
> > > > > > > > > > Not sure, but none of the above handlers seems to server
> > > > > > > > > > files
> > > > > > > > > > from
> > > > > > > > > > a
> > > > > > > > > > folder
> > > > > > > > > > on my box
> > > > > > > > > > 
> > > > > > > > > > 
> > > > > > > > > > Greetings,
> > > > > > > > > > Matthias
> > > > > > > > > > 
> > > > > > > > > > 
> > > > > > > > > > 
> > > > > > > > > > 
> > > > > > > > > > 
> > > > > > > > > > --
> > > > > > > > > > Matthias Wessendorf
> > > > > > > > > > matzew at redhat.com
> > > > > > > > > > 
> > > > > > > > > > JBoss, by Red Hat
> > > > > > > > > > _______________________________________________
> > > > > > > > > > undertow-dev mailing list
> > > > > > > > > > undertow-dev at lists.jboss.org
> > > > > > > > > > https://lists.jboss.org/mailman/listinfo/undertow-dev
> > > > > > > > > > 
> > > > > > > > > 
> > > > > > > > 
> > > > > > > > --
> > > > > > > > Matthias Wessendorf
> > > > > > > > matzew at redhat.com
> > > > > > > > 
> > > > > > > > JBoss, by Red Hat
> > > > > > > > 
> > > > > > > 
> > > > > > 
> > > > > > --
> > > > > > Matthias Wessendorf
> > > > > > matzew at redhat.com
> > > > > > 
> > > > > > JBoss, by Red Hat
> > > > > > 
> > > > > 
> > > > 
> > > > --
> > > > Matthias Wessendorf
> > > > matzew at redhat.com
> > > > 
> > > > JBoss, by Red Hat
> > > > 
> > > 
> > 
> > --
> > Matthias Wessendorf
> > matzew at redhat.com
> > 
> > JBoss, by Red Hat
> > _______________________________________________
> > undertow-dev mailing list
> > undertow-dev at lists.jboss.org
> > https://lists.jboss.org/mailman/listinfo/undertow-dev
> > 
> 
> --
> Matthias Wessendorf
> matzew at redhat.com
> 
> JBoss, by Red Hat
> 


More information about the undertow-dev mailing list