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);
}
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);
}
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;
}
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;
}
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);
}
Aggregations