Search in sources :

Example 1 with GuiceFilter

use of com.google.inject.servlet.GuiceFilter in project graphhopper by graphhopper.

the class GHServer method start.

public void start(Injector injector) throws Exception {
    ResourceHandler resHandler = new ResourceHandler();
    resHandler.setDirectoriesListed(false);
    resHandler.setWelcomeFiles(new String[] { "index.html" });
    resHandler.setResourceBase(args.get("jetty.resourcebase", "./web/src/main/webapp"));
    server = new Server();
    // getSessionHandler and getSecurityHandler should always return null
    ServletContextHandler servHandler = new ServletContextHandler(ServletContextHandler.NO_SECURITY | ServletContextHandler.NO_SESSIONS);
    servHandler.setErrorHandler(new GHErrorHandler());
    servHandler.setContextPath("/");
    servHandler.addServlet(new ServletHolder(new InvalidRequestServlet()), "/*");
    FilterHolder guiceFilter = new FilterHolder(injector.getInstance(GuiceFilter.class));
    servHandler.addFilter(guiceFilter, "/*", EnumSet.allOf(DispatcherType.class));
    ServerConnector connector0 = new ServerConnector(server);
    int httpPort = args.getInt("jetty.port", 8989);
    String host = args.get("jetty.host", "");
    connector0.setPort(httpPort);
    int requestHeaderSize = args.getInt("jetty.request_header_size", -1);
    if (requestHeaderSize > 0)
        connector0.getConnectionFactory(HttpConnectionFactory.class).getHttpConfiguration().setRequestHeaderSize(requestHeaderSize);
    if (!host.isEmpty())
        connector0.setHost(host);
    server.addConnector(connector0);
    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] { resHandler, servHandler });
    GzipHandler gzipHandler = new GzipHandler();
    gzipHandler.setIncludedMethods("GET", "POST");
    // Note: gzip only affects the response body like our previous 'GHGZIPHook' behaviour: http://stackoverflow.com/a/31565805/194609
    // If no mimeTypes are defined the content-type is "not 'application/gzip'", See also https://github.com/graphhopper/directions-api/issues/28 for pitfalls
    // gzipHandler.setIncludedMimeTypes();
    gzipHandler.setHandler(handlers);
    server.setHandler(gzipHandler);
    server.start();
    logger.info("Started server at HTTP " + host + ":" + httpPort);
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) ServerConnector(org.eclipse.jetty.server.ServerConnector) GuiceFilter(com.google.inject.servlet.GuiceFilter) GzipHandler(org.eclipse.jetty.server.handler.gzip.GzipHandler) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) DispatcherType(javax.servlet.DispatcherType)

Example 2 with GuiceFilter

use of com.google.inject.servlet.GuiceFilter in project gerrit by GerritCodeReview.

the class HttpPluginServlet method install.

private void install(Plugin plugin) {
    GuiceFilter filter = load(plugin);
    final String name = plugin.getName();
    final PluginHolder holder = new PluginHolder(plugin, filter);
    plugin.add(new RegistrationHandle() {

        @Override
        public void remove() {
            plugins.remove(name, holder);
        }
    });
    plugins.put(name, holder);
}
Also used : RegistrationHandle(com.google.gerrit.extensions.registration.RegistrationHandle) GuiceFilter(com.google.inject.servlet.GuiceFilter)

Example 3 with GuiceFilter

use of com.google.inject.servlet.GuiceFilter in project gerrit by GerritCodeReview.

the class JettyServer method makeContext.

private ContextHandler makeContext(final String contextPath, final JettyEnv env, final Config cfg) {
    final ServletContextHandler app = new ServletContextHandler();
    // This enables the use of sessions in Jetty, feature available
    // for Gerrit plug-ins to enable user-level sessions.
    //
    app.setSessionHandler(new SessionHandler());
    app.setErrorHandler(new HiddenErrorHandler());
    // This is the path we are accessed by clients within our domain.
    //
    app.setContextPath(contextPath);
    // HTTP front-end filters to be used as surrogate of Apache HTTP
    // reverse-proxy filtering.
    // It is meant to be used as simpler tiny deployment of custom-made
    // security enforcement (Security tokens, IP-based security filtering, others)
    String[] filterClassNames = cfg.getStringList("httpd", null, "filterClass");
    for (String filterClassName : filterClassNames) {
        try {
            @SuppressWarnings("unchecked") Class<? extends Filter> filterClass = (Class<? extends Filter>) Class.forName(filterClassName);
            Filter filter = env.webInjector.getInstance(filterClass);
            app.addFilter(new FilterHolder(filter), "/*", EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC));
        } catch (Throwable e) {
            String errorMessage = "Unable to instantiate front-end HTTP Filter " + filterClassName;
            log.error(errorMessage, e);
            throw new IllegalArgumentException(errorMessage, e);
        }
    }
    // Perform the same binding as our web.xml would do, but instead
    // of using the listener to create the injector pass the one we
    // already have built.
    //
    GuiceFilter filter = env.webInjector.getInstance(GuiceFilter.class);
    app.addFilter(new FilterHolder(filter), "/*", EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC));
    app.addEventListener(new GuiceServletContextListener() {

        @Override
        protected Injector getInjector() {
            return env.webInjector;
        }
    });
    // Jetty requires at least one servlet be bound before it will
    // bother running the filter above. Since the filter has all
    // of our URLs except the static resources, the only servlet
    // we need to bind is the default static resource servlet from
    // the Jetty container.
    //
    final ServletHolder ds = app.addServlet(DefaultServlet.class, "/");
    ds.setInitParameter("dirAllowed", "false");
    ds.setInitParameter("redirectWelcome", "false");
    ds.setInitParameter("useFileMappedBuffer", "false");
    ds.setInitParameter("gzip", "true");
    app.setWelcomeFiles(new String[0]);
    return app;
}
Also used : SessionHandler(org.eclipse.jetty.server.session.SessionHandler) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) GuiceFilter(com.google.inject.servlet.GuiceFilter) Filter(javax.servlet.Filter) GuiceFilter(com.google.inject.servlet.GuiceFilter) Injector(com.google.inject.Injector) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) GuiceServletContextListener(com.google.inject.servlet.GuiceServletContextListener)

Example 4 with GuiceFilter

use of com.google.inject.servlet.GuiceFilter in project gerrit by GerritCodeReview.

the class HttpPluginServlet method load.

private GuiceFilter load(Plugin plugin) {
    if (plugin.getHttpInjector() != null) {
        final String name = plugin.getName();
        final GuiceFilter filter;
        try {
            filter = plugin.getHttpInjector().getInstance(GuiceFilter.class);
        } catch (RuntimeException e) {
            log.warn(String.format("Plugin %s cannot load GuiceFilter", name), e);
            return null;
        }
        try {
            ServletContext ctx = PluginServletContext.create(plugin, wrapper.getFullPath(name));
            filter.init(new WrappedFilterConfig(ctx));
        } catch (ServletException e) {
            log.warn(String.format("Plugin %s failed to initialize HTTP", name), e);
            return null;
        }
        plugin.add(new RegistrationHandle() {

            @Override
            public void remove() {
                filter.destroy();
            }
        });
        return filter;
    }
    return null;
}
Also used : ServletException(javax.servlet.ServletException) RegistrationHandle(com.google.gerrit.extensions.registration.RegistrationHandle) GuiceFilter(com.google.inject.servlet.GuiceFilter) ServletContext(javax.servlet.ServletContext)

Example 5 with GuiceFilter

use of com.google.inject.servlet.GuiceFilter in project gerrit by GerritCodeReview.

the class LfsPluginServlet method install.

private void install(Plugin plugin) {
    if (!plugin.getName().equals(pluginName)) {
        return;
    }
    final GuiceFilter guiceFilter = load(plugin);
    plugin.add(new RegistrationHandle() {

        @Override
        public void remove() {
            filter.compareAndSet(guiceFilter, null);
        }
    });
    filter.set(guiceFilter);
}
Also used : RegistrationHandle(com.google.gerrit.extensions.registration.RegistrationHandle) GuiceFilter(com.google.inject.servlet.GuiceFilter)

Aggregations

GuiceFilter (com.google.inject.servlet.GuiceFilter)6 RegistrationHandle (com.google.gerrit.extensions.registration.RegistrationHandle)4 ServletContext (javax.servlet.ServletContext)2 ServletException (javax.servlet.ServletException)2 FilterHolder (org.eclipse.jetty.servlet.FilterHolder)2 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)2 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)2 Injector (com.google.inject.Injector)1 GuiceServletContextListener (com.google.inject.servlet.GuiceServletContextListener)1 DispatcherType (javax.servlet.DispatcherType)1 Filter (javax.servlet.Filter)1 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)1 Server (org.eclipse.jetty.server.Server)1 ServerConnector (org.eclipse.jetty.server.ServerConnector)1 HandlerList (org.eclipse.jetty.server.handler.HandlerList)1 ResourceHandler (org.eclipse.jetty.server.handler.ResourceHandler)1 GzipHandler (org.eclipse.jetty.server.handler.gzip.GzipHandler)1 SessionHandler (org.eclipse.jetty.server.session.SessionHandler)1