Java Code Examples for org.eclipse.jetty.http.MimeTypes#addMimeMapping()

The following examples show how to use org.eclipse.jetty.http.MimeTypes#addMimeMapping() . 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: TestSupport.java    From p3-batchrefine with Apache License 2.0 5 votes vote down vote up
private int startTransformServer() throws Exception {
    URL transforms = EngineTest.class.getClassLoader().getResource(
            "transforms");
    if (transforms == null) {
        Assert.fail();
    }

    int port = findFreePort();

    Server fileServer = new Server(port);

    ResourceHandler handler = new ResourceHandler();
    MimeTypes mimeTypes = handler.getMimeTypes();
    mimeTypes.addMimeMapping("json", "application/json; charset=UTF-8");

    handler.setDirectoriesListed(true);
    handler.setBaseResource(JarResource.newResource(transforms));

    HandlerList handlers = new HandlerList();
    handlers.addHandler(handler);
    handlers.addHandler(new DefaultHandler());

    fileServer.setHandler(handlers);
    fileServer.start();

    return port;
}
 
Example 4
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);
}
 
Example 5
Source File: DremioServer.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
public void startDremioServer(
  SingletonRegistry registry,
  DACConfig config,
  Provider<ServerHealthMonitor> serverHealthMonitor,
  Provider<NodeEndpoint> endpointProvider,
  Provider<SabotContext> contextProvider,
  Provider<RestServerV2> restServerProvider,
  Provider<APIServer> apiServerProvider,
  DremioBinder dremioBinder,
  Tracer tracer,
  String uiType,
  boolean isInternalUS
) throws Exception {
  try {
    if (!embeddedJetty.isRunning()) {
      createConnector(config);
      addHandlers();
    }

    // security header filters
    servletContextHandler.addFilter(SecurityHeadersFilter.class.getName(), "/*", EnumSet.of(DispatcherType.REQUEST));

    // server tracing filter.
    servletContextHandler.addFilter(SpanFinishingFilter.class.getName(), "/*", EnumSet.of(DispatcherType.REQUEST));

    // add the font mime type.
    final MimeTypes mimeTypes = servletContextHandler.getMimeTypes();
    mimeTypes.addMimeMapping("woff2", "application/font-woff2; charset=utf-8");
    servletContextHandler.setMimeTypes(mimeTypes);

    // WebSocket API
    final SocketServlet servlet = new SocketServlet(registry.lookup(JobsService.class), registry.lookup(TokenManager.class));
    final ServletHolder wsHolder = new ServletHolder(servlet);
    wsHolder.setInitOrder(3);
    servletContextHandler.addServlet(wsHolder, "/apiv2/socket");

    // Rest API
    ResourceConfig restServer = restServerProvider.get();

    restServer.property(RestServerV2.ERROR_STACKTRACE_ENABLE, config.sendStackTraceToClient);
    restServer.property(RestServerV2.TEST_API_ENABLE, config.allowTestApis);
    restServer.property(RestServerV2.FIRST_TIME_API_ENABLE, isInternalUS);

    restServer.register(dremioBinder);
    restServer.register(new ServerTracingDynamicFeature(tracer));

    final ServletHolder restHolder = new ServletHolder(new ServletContainer(restServer));
    restHolder.setInitOrder(2);
    servletContextHandler.addServlet(restHolder, "/apiv2/*");

    // Public API
    ResourceConfig apiServer = apiServerProvider.get();
    apiServer.register(dremioBinder);
    apiServer.register(new ServerTracingDynamicFeature(tracer));

    final ServletHolder apiHolder = new ServletHolder(new ServletContainer(apiServer));
    apiHolder.setInitOrder(3);
    servletContextHandler.addServlet(apiHolder, "/api/v3/*");

    if (config.verboseAccessLog) {
      accessLogFilter = new AccessLogFilter();
      servletContextHandler.addFilter(
        new FilterHolder(accessLogFilter),
        "/*",
        EnumSet.of(DispatcherType.REQUEST));
    }

    if (config.serveUI) {
      final String basePath = "rest/dremio_static/";
      final String markerPath = String.format("META-INF/%s.properties", uiType);

      final ServletHolder fallbackServletHolder = new ServletHolder("fallback-servlet", registry.lookup(DremioServlet.class));
      addStaticPath(fallbackServletHolder, basePath, markerPath);
      servletContextHandler.addServlet(fallbackServletHolder, "/*");

      // TODO DX-1556 - temporary static asset serving for showing Profiles
      final String baseStaticPath = "rest/static/";
      final String arrowDownResourceRelativePath = "rest/static/img/arrow-down-small.svg";
      ServletHolder restStaticHolder = new ServletHolder("static", DefaultServlet.class);
      // Get resource URL for legacy static assets, based on where some image is located
      addStaticPath(restStaticHolder, baseStaticPath, arrowDownResourceRelativePath);
      servletContextHandler.addServlet(restStaticHolder, "/static/*");
    }

    if (!embeddedJetty.isRunning()) {
      embeddedJetty.start();
    }

    setPortFromConnector();
    logger.info("Started on {}://localhost:" + port, config.webSSLEnabled() ? "https" : "http");

    serviceStarted = true;
  } catch (Exception ex) {
    throw new ServerErrorException(ex);
  }
}