Java Code Examples for org.eclipse.jetty.server.handler.ResourceHandler#setMimeTypes()

The following examples show how to use org.eclipse.jetty.server.handler.ResourceHandler#setMimeTypes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WebServerTestCase.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Starts the web server on the default {@link #PORT}.
 * The given resourceBase is used to be the ROOT directory that serves the default context.
 * <p><b>Don't forget to stop the returned HttpServer after the test</b>
 *
 * @param resourceBase the base of resources for the default context
 * @throws Exception if the test fails
 */
protected void startWebServer(final String resourceBase) throws Exception {
    if (server_ != null) {
        throw new IllegalStateException("startWebServer() can not be called twice");
    }
    final Server server = buildServer(PORT);

    final WebAppContext context = new WebAppContext();
    context.setContextPath("/");
    context.setResourceBase(resourceBase);

    final ResourceHandler resourceHandler = new ResourceHandler();
    resourceHandler.setResourceBase(resourceBase);
    final MimeTypes mimeTypes = new MimeTypes();
    mimeTypes.addMimeMapping("js", MimeType.APPLICATION_JAVASCRIPT);
    resourceHandler.setMimeTypes(mimeTypes);

    final HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[]{resourceHandler, context});
    server.setHandler(handlers);
    server.setHandler(resourceHandler);

    tryStart(PORT, server);
    server_ = server;
}
 
Example 2
Source File: JettyAppServer.java    From selenium with Apache License 2.0 6 votes vote down vote up
protected ServletContextHandler addResourceHandler(String contextPath, Path resourceBase) {
  ServletContextHandler context = new ServletContextHandler();

  ResourceHandler staticResource = new ResourceHandler();
  staticResource.setDirectoriesListed(true);
  staticResource.setWelcomeFiles(new String[] { "index.html" });
  staticResource.setResourceBase(resourceBase.toAbsolutePath().toString());
  MimeTypes mimeTypes = new MimeTypes();
  mimeTypes.addMimeMapping("appcache", "text/cache-manifest");
  staticResource.setMimeTypes(mimeTypes);

  context.setContextPath(contextPath);
  context.setAliasChecks(Arrays.asList(new ApproveAliases(), new AllowSymLinkAliasChecker()));

  HandlerList allHandlers = new HandlerList();
  allHandlers.addHandler(staticResource);
  allHandlers.addHandler(context.getHandler());
  context.setHandler(allHandlers);

  handlers.addHandler(context);

  return context;
}
 
Example 3
Source File: GerritRestClientTest.java    From gerrit-rest-java-client with Apache License 2.0 5 votes vote down vote up
public String startJetty(Class<? extends HttpServlet> loginServletClass) throws Exception {
    Server server = new Server(0);

    ResourceHandler resourceHandler = new ResourceHandler();
    MimeTypes mimeTypes = new MimeTypes();
    mimeTypes.addMimeMapping("json", "application/json");
    resourceHandler.setMimeTypes(mimeTypes);
    URL url = this.getClass().getResource(".");
    resourceHandler.setBaseResource(new FileResource(url));
    resourceHandler.setWelcomeFiles(new String[] {"changes.json", "projects.json", "account.json"});

    ServletContextHandler servletContextHandler = new ServletContextHandler();
    servletContextHandler.addServlet(loginServletClass, "/login/");

    ServletContextHandler basicAuthContextHandler = new ServletContextHandler(ServletContextHandler.SECURITY);
    basicAuthContextHandler.setSecurityHandler(basicAuth("foo", "bar", "Gerrit Auth"));
    basicAuthContextHandler.setContextPath("/a");

    HandlerCollection handlers = new HandlerCollection();
    handlers.setHandlers(new Handler[] {
        servletContextHandler,
        resourceHandler,
        basicAuthContextHandler
    });
    server.setHandler(handlers);

    server.start();

    Connector connector = server.getConnectors()[0];
    String host = "localhost";
    int port = connector.getLocalPort();
    return String.format("http://%s:%s", host, port);
}