Add more than one resource directory to jetty

yoav.str picture yoav.str · Jul 10, 2012 · Viewed 6.9k times · Source

Looking to use multiple static directories with Jetty. When the server runs:

  http://localhost:8282/A
  http://localhost:8282/B 
  http://localhost:8282/C
  • A is placed in X/V/A
  • B is placed in Q/Z/B
  • C is placed in P/T/C

The following failed:

    ResourceHandler resource_handler = new ResourceHandler();
    resource_handler.setWelcomeFiles(new String[]{"index.html"});
    resource_handler.setResourceBase(HTML_SITE);

    ResourceHandler resource_handler1 = new ResourceHandler();
    resource_handler1.setWelcomeFiles(new String[]{"index.html"});
    resource_handler1.setResourceBase(HTML_CLIENTZONE_SITE);

    // deploy engine
    WebAppContext webapp = new WebAppContext();

    String dir = System.getProperty("user.dir");
    webapp.setResourceBase(getWebAppPath());
    webapp.setContextPath("/");


     HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[]{resource_handler,resource_handler1 ,webapp,  new DefaultHandler()});
    server.setHandler(handlers);

How can I add more than one static resource directory?

Answer

pna picture pna · Jul 18, 2012

Since 6.1.12, this is supported by using a ResourceCollection to the WebAppContext's base resource:

Server server = new Server(8282);
WebAppContext context = new WebAppContext();
context.setContextPath("/");
ResourceCollection resources = new ResourceCollection(new String[] {
    "project/webapp/folder",
    "/root/static/folder/A",    
    "/root/static/folder/B",    
});
context.setBaseResource(resources);
server.setHandler(context);
server.start();

To subsequently open a file, use the ServletContext (e.g., WebAppContext), which could be part of an interface definition, such as:

  /**
   * Opens a file using the servlet context.
   */
  public default InputStream open( ServletContext context, String filename ) {
    String f = System.getProperty( "file.separator" ) + filename;
    return context.getResourceAsStream( f );
  }

Such as:

  InputStream in = open( context, "filename.txt" );

This will open filename.txt if it exists in one of the given directories. Note that getResourceAsStream will return null, rather than throw an exception, so it's a good idea to check for it:

  public default InputStream validate( InputStream in, String filename )
    throws FileNotFoundException {
    if( in == null ) {
      throw new FileNotFoundException( filename );
    }

    return in;
  }

Then you can update the open method as follows:

return validate( context.getResourceAsStream( filename ), filename );