Class TryPathsHandler

java.lang.Object
org.eclipse.jetty.util.component.AbstractLifeCycle
org.eclipse.jetty.util.component.ContainerLifeCycle
All Implemented Interfaces:
Handler, Handler.Container, Handler.Singleton, Request.Handler, org.eclipse.jetty.util.component.Container, org.eclipse.jetty.util.component.Destroyable, org.eclipse.jetty.util.component.Dumpable, org.eclipse.jetty.util.component.Dumpable.DumpableContainer, org.eclipse.jetty.util.component.LifeCycle, org.eclipse.jetty.util.thread.Invocable

public class TryPathsHandler extends Handler.Wrapper

Inspired by nginx's try_files functionality.

This handler can be configured with a list of rewrite URI paths. The special token $path represents the current request pathInContext (the portion after the context path).

Typical example of how this handler can be configured is the following:


 TryPathsHandler tryPathsHandler = new TryPathsHandler();
 tryPathsHandler.setPaths("/maintenance.html", "$path", "/index.php?p=$path");

 PathMappingsHandler pathMappingsHandler = new PathMappingsHandler();
 tryPathsHandler.setHandler(pathMappingsHandler);

 pathMappingsHandler.addMapping(new ServletPathSpec("*.php"), new PHPHandler());
 pathMappingsHandler.addMapping(new ServletPathSpec("/"), new ResourceHandler());
 

For a request such as /context/path/to/resource.ext:

  • This handler rewrites the request pathInContext to /maintenance.html and forwards the request to the next handler, where it matches the / mapping, hitting the ResourceHandler that serves the file if it exists.
  • Otherwise, this handler rewrites the request pathInContext to /path/to/resource.ext and forwards the request to the next handler, where it matches the / mapping, hitting the ResourceHandler that serves the file if it exists.
  • Otherwise, this handler rewrites the request pathInContext to /index.php?p=/path/to/resource.ext and forwards the request to the next handler, where it matches the *.php mapping, hitting the PHPHandler.

The original path and query may be stored as request attributes, under the names specified by setOriginalPathAttribute(String) and setOriginalQueryAttribute(String).