use of com.google.inject.servlet.GuiceServletContextListener 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.GuiceServletContextListener in project commons by twitter.
the class HttpStatsFilterIntegrationTest method setUp.
@Before
public void setUp() {
Stats.flush();
server = new JettyHttpServerDispatch();
server.listen(0);
server.registerFilter(GuiceFilter.class, "/*");
clock = new FakeClock();
final Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(TestServlet.class).in(Singleton.class);
bind(Clock.class).toInstance(clock);
bind(HttpStatsFilter.class).in(Singleton.class);
}
}, new JerseyServletModule() {
@Override
protected void configureServlets() {
filter("/*").through(HttpStatsFilter.class);
serve("/*").with(GuiceContainer.class, ImmutableMap.of(PROPERTY_CONTAINER_RESPONSE_FILTERS, HttpStatsFilter.class.getName()));
}
});
server.getRootContext().addEventListener(new GuiceServletContextListener() {
@Override
protected Injector getInjector() {
return injector;
}
});
ClientConfig config = new DefaultClientConfig();
client = Client.create(config);
}
use of com.google.inject.servlet.GuiceServletContextListener in project meteo by pierre.
the class JettyServer method start.
public void start(final Injector injector) throws Exception {
final long startTime = System.currentTimeMillis();
server = new Server();
// Setup JMX
final MBeanContainer mbContainer = new MBeanContainer(mbeanServer);
server.getContainer().addEventListener(mbContainer);
server.addBean(mbContainer);
mbContainer.addBean(Log.getLog());
final Connector connector = new SelectChannelConnector();
connector.setStatsOn(config.isJettyStatsOn());
connector.setHost(config.getLocalIp());
connector.setPort(config.getLocalPort());
server.addConnector(connector);
server.setStopAtShutdown(true);
final ServletContextHandler context = new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS);
context.setContextPath("/");
context.addEventListener(new GuiceServletContextListener() {
@Override
protected Injector getInjector() {
return injector;
}
});
// Jersey insists on using java.util.logging (JUL)
final EventListener listener = new SetupJULBridge();
context.addEventListener(listener);
// Make sure Guice filter all requests
final FilterHolder filterHolder = new FilterHolder(GuiceFilter.class);
context.addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC));
// Backend servlet for Guice - never used
final ServletHolder sh = new ServletHolder(DefaultServlet.class);
context.addServlet(sh, "/*");
server.start();
final long secondsToStart = (System.currentTimeMillis() - startTime) / 1000;
log.info(String.format("Jetty server started in %d:%02d", secondsToStart / 60, secondsToStart % 60));
}
use of com.google.inject.servlet.GuiceServletContextListener in project joynr by bmwcarit.
the class AbstractServiceInterfaceTest method setUp.
@Before
public void setUp() throws Exception {
// starts the server with a random port
jettyServer = new Server(0);
WebAppContext bpCtrlWebapp = new WebAppContext();
bpCtrlWebapp.setResourceBase("./src/main/java");
bpCtrlWebapp.setParentLoaderPriority(true);
bpCtrlWebapp.addFilter(GuiceFilter.class, "/*", null);
bpCtrlWebapp.addEventListener(new GuiceServletContextListener() {
private Injector injector;
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
injector = Guice.createInjector(getServletTestModule());
super.contextInitialized(servletContextEvent);
}
@Override
protected Injector getInjector() {
return injector;
}
});
jettyServer.setHandler(bpCtrlWebapp);
jettyServer.start();
int port = ((ServerConnector) jettyServer.getConnectors()[0]).getLocalPort();
serverUrl = String.format("http://localhost:%d", port);
}
use of com.google.inject.servlet.GuiceServletContextListener in project gerrit by GerritCodeReview.
the class JettyServer method makeContext.
private ContextHandler makeContext(final String contextPath, JettyEnv env, Config cfg, SessionHandler sessionHandler) {
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(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);
Map<String, String> initParams = new HashMap<>();
Set<String> initParamKeys = cfg.getNames("filterClass", filterClassName, true);
initParamKeys.forEach(paramKey -> {
String paramValue = cfg.getString("filterClass", filterClassName, paramKey);
initParams.put(paramKey, paramValue);
});
FilterHolder filterHolder = new FilterHolder(filter);
if (initParams.size() > 0) {
filterHolder.setInitParameters(initParams);
}
app.addFilter(filterHolder, "/*", EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC));
} catch (Exception e) {
throw new IllegalArgumentException("Unable to instantiate front-end HTTP Filter " + filterClassName, 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;
}
Aggregations