Map jetty ResourceHandler to a URL - jetty

Is it possible using embedded Jetty to serve static files from directory X but mapped to URL Y? I have static files stored under directory "web", but I want the URL be something like http://host/myapp.
I have already successfully ran a server configured with ResourceHandler in the following way:
ResourceHandler ctx = new ResourceHandler();
ctx.setResourceBase("path-to-web");
HandlerList list = new HandlerList();
list.addHandler(ctx);
...
server.setHandler(list);
But the result is serving the files under /web and not under the desired URL mapping.

The ResourceHandler has no context configurable, but you can simply wrap it in a ContextHandler to achieve that.
Try this instead:
ContextHandler ctx = new ContextHandler("/my-files"); /* the server uri path */
ResourceHandler resHandler = new ResourceHandler();
resHandler.setResourceBase("path-to-web");
ctx.setHandler(resHandler);
server.setHandler(ctx);
That will serve /my-files as the ResourceHandler content of the filesystem path-to-web

The above doesn't work for Jetty 9, but this does:
ContextHandler contextHandler = new ContextHandler("/my-files");
contextHandler.setResourceBase("/tmp/static");
ResourceHandler resourceHandler = new ResourceHandler();
contextHandler.setHandler(resourceHandler);
server.setHandler(contextHandler);

Related

HowTo configure the ErrorPageErrorHandler in embedded Jetty?

Is it possible to configure the ErrorPageErrorHandler in way that it redirects to a static Page if no content/service is found?
Here is my Code:
server = new Server(port);
Resource webRoot = Resource.newResource(webContent);
if (!webRoot.exists()) {
logger.warn("Unable to find root resource:" + webRoot.getName());
} else {
logger.info("Root resource is " + webRoot.getName());
}
ResourceHandler res = new ResourceHandler();
res.setBaseResource(webRoot);
res.setDirAllowed(false);
//servlet handler
ServletContextHandler servletCtx = new ServletContextHandler(ServletContextHandler.SESSIONS);
servletCtx.setContextPath("/service");
servletCtx.addServlet(new ServletHolder("sample", new MyServletSample()), "/sample");
ErrorPageErrorHandler errorHandler = new ErrorPageErrorHandler();
errorHandler.addErrorPage(404, "index.html");
servletCtx.setErrorHandler(errorHandler);
// static file handler
ContextHandler staticCtx = new ContextHandler("/");
staticCtx.setBaseResource(webRoot);
staticCtx.setHandler(res);
// add handlers
HandlerList handlerList = new HandlerList();
handlerList.addHandler(servletCtx);
handlerList.addHandler(staticCtx);
// add handerList to server
server.setHandler(handlerList);
This code show me index.html on localhost:8080 and I can access the sample service http://localhost:8080/service/sample. However, I want to show a static error page (i.e. documentation) to show up if an error like "404 Not Found" occured.
With this code, the Error handler logs:
"WARN o.e.j.server.handler.ErrorHandler - No error page found
index.html"
. What is correct way/syntax to define the URI?
Thanks in advance!
This was answered before at https://stackoverflow.com/a/32383973/775715
Don't mix ResourceHandler and ServletContextHandler unless you REALLY know what you are doing, and fully understand the nature of javax.servlet.ServletContext and all of the rules it brings to the table.
See also:
What is difference between ServletContextHandler.setResourceBase and ResourceHandler.setResourceBase when using Jetty embedded container?
Serving static files from alternate path in embedded Jetty
Here's an example of your setup working with NO ResourceHandler, 1 ServletContextHandler, and a DefaultServlet providing the static file serving.
// servlet handler
ServletContextHandler servletCtx = new ServletContextHandler(ServletContextHandler.SESSIONS);
servletCtx.setContextPath("/");
servletCtx.setBaseResource(webRoot); // what static content to serve
servletCtx.setWelcomeFiles(new String[] { "index.html" });
servletCtx.addServlet(new ServletHolder("sample", new MyServletSample()), "/service/sample");
ErrorPageErrorHandler errorHandler = new ErrorPageErrorHandler();
errorHandler.addErrorPage(404, "/index.html");
servletCtx.setErrorHandler(errorHandler);
// static file serving, and context based error handling
ServletHolder defaultServ = new ServletHolder("default", DefaultServlet.class);
defaultServ.setInitParameter("dirAllowed","false");
servletCtx.addServlet(defaultServ,"/");
// add handlers
HandlerList handlerList = new HandlerList();
handlerList.addHandler(servletCtx);
handlerList.addHandler(new DefaultHandler()); // non-context error handling

jetty - create main and resource servlet

I'm trying to create two servlets. first, the main servlet with the "/" path and a resource servlet from another path. but both paths starts from "/" (my work dir)
I wrote:
Server server = new Server(8001);
ServletHandler handler = new ServletHandler();
handler.addServletWithMapping(Servlet.class, "/");
ResourceHandler resourceHandler = new ResourceHandler();
resourceHandler.setResourceBase("./classes/static/");
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[] { resourceHandler,handler});
server.setHandler(handlers);
server.start();
server.join();
but one handler overlap the other.
basically i want my code to act like :
handler.addServletWithMapping(Servlet.class, "/q");
(localhost:8001/q)
instead of:
handler.addServletWithMapping(Servlet.class, "/");
hope I was clear enough.
Thanks,
find it here
https://examples.javacodegeeks.com/enterprise-java/jetty/jetty-resource-handler-example/
needed to declare path to the contant :
Server server = new Server(8001);
ServletHandler handler = new ServletHandler();
handler.addServletWithMapping(Servlet.class, "/");
ResourceHandler resourceHandler = new ResourceHandler();
resourceHandler.setResourceBase("./classes/static/");
resourceHandler.setDirectoriesListed(true);
ContextHandler contextHandler= new ContextHandler("/static");
contextHandler.setHandler(resourceHandler);
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[] {contextHandler,handler});
server.setHandler(handlers);
server.start();
server.join();

jetty java web start standalone

I have (jetty) java web start standalone code something like this. I need to add SSL Certificate for the same.
org.mortbay.util.Log.instance().add(new TomeLogSink(NAME));
server = new HttpServer();
SocketListener listener = new SocketListener();
listener.setPort(port);
server.addListener(listener);
// context to handle all other request including servlets
HttpContext context = new HttpContext();
context.setContextPath(HttpConstants.HTTP_CONTEXT_PATH);
context.setResourceBase(HttpConstants.HTTP_PROVIDENT_ROOT);
//handler for servlets
servletHandler = new ServletHandler();
addServlet("*.jnlp", JnlpServlet.class);
addServlet("/index.htm", IndexServlet.class);
addServlet("/index.html", IndexServlet.class);
CcAdminServiceImpl cc = (CcAdminServiceImpl) RegistryManager.get(
CcAdminService.REGISTERED_NAME);
cc.createWebContent(this);
//handler for static content
ResourceHandler handler = new ResourceHandler();
handler.setDirAllowed(false);
handler.setAcceptRanges(false);
context.addHandler(servletHandler);
context.addHandler(handler);
context.addHandler(new NotFoundHandler());
//setup and start the server
server.addContext(createStaticContext(
HttpConstants.HTTP_HELP_PATH,
HttpConstants.HTTP_HELP_ROOT));
server.addContext(context);
server.start();

Getting a 403 on root requests when using a ResourceHandler and custom handler in Jetty

In (embedded) Jetty, I'm trying to use a ResourceHandler to serve static files and a custom handler to respond to dynamic requests. Based on this page I have a setup that looks like this:
public static void main(String[] args) throws Exception
{
Server server = new Server();
SelectChannelConnector connector = new SelectChannelConnector();
connector.setPort(8080);
server.addConnector(connector);
ResourceHandler resource_handler = new ResourceHandler();
resource_handler.setDirectoriesListed(false);
resource_handler.setResourceBase(".");
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[] { resource_handler, new MyHandler() });
server.setHandler(handlers);
server.start();
server.join();
}
This works in the sense that it correctly:
Serves up static content from files in my public directory, like /public/style.css
Runs MyHandler on paths that aren't present in the public directory, like /foo/bar
The problem is that I get a 403 in response to the root path (/). MyHandler is capable of responding to those requests, but they get intercepted by the ResourceHandler first. Is there any way to force Jetty to send / requests to MyHandler?
Thanks in advance!
Jetty tries each Handler sequentially until one of the handlers calls setHandled(true) on the request. Not sure why ResourceHandler doesn't do this for "/".
My solution was to reverse the order in which you list the handlers so that yours is called first. Then check for the special case "/" in the URL. If you'd like to pass the request on to the ResourceHandler, simply return without declaring the request as handled.
Declare the order of handlers like this:
Server server = new Server(8080);
CustomHandler default = new CustomHandler();
default.setServer(server);
ResourceHandler files = new ResourceHandler();
files.setServer(server);
files.setResourceBase("./path/to/resources");
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[] {default, files});
server.setHandler(handlers);
server.start();
server.join();
And define CustomHandler's handle method like this:
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
if(!request.getRequestURI().equals("/")){
return;
}
// Do Stuff...
baseRequest.setHandled(true);
return;
}
I agree it would be most elegant to have ResourceHandler simply yield on "/" instead of handling the response with a 403.
My solution:
put MyHandler on a differnt context path than "/" e.g. "/index"
use a rewrite rule to intercept calls to "/" and redirect them to "/index"
The code I use looks like this:
RewriteHandler rewriteHandler = new RewriteHandler();
rewriteHandler.setRewriteRequestURI(true);
rewriteHandler.setRewritePathInfo(false);
rewriteHandler.setOriginalPathAttribute("requestedPath");
RewriteRegexRule rewriteIndex = new RewriteRegexRule();
rewriteIndex.setRegex("^/$");
rewriteIndex.setReplacement("/index.html");
rewriteHandler.addRule(rewriteIndex);
rewriteHandler.setHandler(rootHandlerCollection);
server.setHandler(rewriteHandler);
The regex ensures to only match the exact path, so that "/whatever" is still first handled by the ResourceHandler.

Can I respond to POST requests using Jetty's ResourceHandler?

Can I respond to POST requests using Jetty's ResourceHandler? If so, how?
For context, here's snippet configuring a file server using ResourceHandler from the Jetty tutorials:
public class FileServer
{
public static void main(String[] args) throws Exception
{
Server server = new Server();
SelectChannelConnector connector = new SelectChannelConnector();
connector.setPort(8080);
server.addConnector(connector);
ResourceHandler resource_handler = new ResourceHandler();
resource_handler.setDirectoriesListed(true);
resource_handler.setWelcomeFiles(new String[]{ "index.html" });
resource_handler.setResourceBase(".");
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[] { resource_handler, new DefaultHandler() });
server.setHandler(handlers);
server.start();
server.join();
}
}
The ResourceHandler seems to only support GET request. This makes sense, as the ResourceHandler only serves static resources (files, directories). A POST input would be discarded anyway.
I find it hard to make up a scenario, where one would need the ResourceHandler to reply to POST requests, but if you really want to achieve this, you could write your own Handler that wraps around the ResourceHandler and calls the GET methods for POST Requests. Some hints on how to do this can be found here: http://www.eclipse.org/jetty/documentation/current/writing-custom-handlers.html#passing-request-and-response