本文整理汇总了Java中com.google.inject.servlet.GuiceFilter类的典型用法代码示例。如果您正苦于以下问题:Java GuiceFilter类的具体用法?Java GuiceFilter怎么用?Java GuiceFilter使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
GuiceFilter类属于com.google.inject.servlet包,在下文中一共展示了GuiceFilter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: run
import com.google.inject.servlet.GuiceFilter; //导入依赖的package包/类
@Override
public void run(final T config, final Environment environment) throws Exception {
Module bootstrapModule = new AbstractModule() {
@Override
protected void configure() {
install(new DropwizardModule());
if (mainModule != null)
install(mainModule);
bind(Configuration.class).toInstance(config);
bind(configClass).toInstance(config);
bind(Environment.class).toInstance(environment);
}
};
environment.servlets() //
.addFilter("guice-request-scope", new GuiceFilter()) //
.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");
Guice.createInjector(bootstrapModule).getInstance(applicationClass).run();
}
示例2: startJetty
import com.google.inject.servlet.GuiceFilter; //导入依赖的package包/类
@Before
public void startJetty() throws Exception {
servletTester = new ServletTester();
servletTester.getContext().addEventListener(new GuiceServletContextListener()
{
final Injector injector = Guice.createInjector(new TestModule());
@Override
protected Injector getInjector() {
return injector;
}
});
url = servletTester.createConnector(true) + TestModule.MOUNT_POINT;
servletTester.addFilter(GuiceFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
servletTester.addServlet(DummyServlet.class, "/*");
servletTester.start();
client = ClientBuilder.newClient();
}
示例3: configure
import com.google.inject.servlet.GuiceFilter; //导入依赖的package包/类
@Override
protected void configure() {
// we will look these up later...
requireBinding(GuiceFilter.class);
requireBinding(BeanManager.class);
bind(ManagedLifecycleManager.class).to(NexusLifecycleManager.class);
bind(ServletContext.class).toInstance(servletContext);
bind(ParameterKeys.PROPERTIES).toInstance(nexusProperties);
install(new StateGuardModule());
install(new TransactionModule());
install(new TimeTypeConverter());
install(new WebSecurityModule(servletContext));
// enable OSGi service lookup of Karaf components
final MutableBeanLocator locator = new DefaultBeanLocator();
locator.add(new ServiceBindings(bundleContext, ALLOW_SERVICES, IGNORE_SERVICES, Integer.MIN_VALUE));
bind(MutableBeanLocator.class).toInstance(locator);
}
示例4: run
import com.google.inject.servlet.GuiceFilter; //导入依赖的package包/类
public void run() {
try {
Server server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(port);
server.addConnector(connector);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.NO_SESSIONS);
context.setContextPath(contextPath);
context.addFilter(GuiceFilter.class, "/api/*", null);
server.setHandler(context);
ServerContainer wsContainer = WebSocketServerContainerInitializer.configureContext(context);
KafkaWebsocketEndpoint.Configurator.setKafkaProps(consumerProps, producerProps);
wsContainer.addEndpoint(KafkaWebsocketEndpoint.class);
server.start();
server.join();
} catch (Exception e) {
logger.error("Failed to start the server: ", e);
}
}
示例5: startServer
import com.google.inject.servlet.GuiceFilter; //导入依赖的package包/类
public void startServer() throws Exception {
int serverPort = propertiesConfiguration.getInt("server.port.http");
Server server = new Server(serverPort);
ResourceHandler resourceHandler = new ResourceHandler();
resourceHandler.setBaseResource(Resource.newClassPathResource("/web"));
ServletContextHandler servletContextHandler = new ServletContextHandler(server, "/api");
servletContextHandler.addFilter(GuiceFilter.class, "/*", EnumSet.allOf(DispatcherType.class));
initGuiceInjector();
HandlerList handlerList = new HandlerList();
handlerList.addHandler(resourceHandler);
handlerList.addHandler(servletContextHandler);
server.setHandler(handlerList);
server.start();
server.join();
}
示例6: main
import com.google.inject.servlet.GuiceFilter; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
final Server server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(PORT);
server.setConnectors(new Connector[] { connector });
// Configure the Guice application.
ServletContextHandler servletHandler = new ServletContextHandler();
servletHandler.addFilter(GuiceFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
servletHandler.addEventListener(new SampleServletContextListener());
server.setHandler(servletHandler);
// Start the server and wait for it.
server.start();
server.join();
}
示例7: execute1
import com.google.inject.servlet.GuiceFilter; //导入依赖的package包/类
protected void execute1(String component) throws Exception {
System.out.println("Starting codeine "+component+" at version " + CodeineVersion.get());
injector = Guice.createInjector(getModules(component));
FilterHolder guiceFilter = new FilterHolder(injector.getInstance(GuiceFilter.class));
ServletContextHandler handler = createServletContextHandler();
handler.setContextPath("/");
FilterHolder crossHolder = new FilterHolder(new CrossOriginFilter());
crossHolder.setInitParameter(CrossOriginFilter.ALLOWED_METHODS_PARAM, "GET, POST, PUT, DELETE");
crossHolder.setInitParameter(CrossOriginFilter.ALLOWED_HEADERS_PARAM, "X-Requested-With,Origin,Content-Type,Accept,api_token");
handler.addFilter(crossHolder, "/api/*", EnumSet.allOf(DispatcherType.class));
handler.addFilter(crossHolder, "/api-with-token/*", EnumSet.allOf(DispatcherType.class));
handler.addFilter(guiceFilter, "/*", EnumSet.allOf(DispatcherType.class));
createAdditionalServlets(handler);
ContextHandlerCollection contexts = createFileServerContexts();
contexts.addHandler(handler);
int port = startServer(contexts);
log.info("jetty started on port " + port);
injector.getInstance(CodeineRuntimeInfo.class).setPort(port);
execute();
}
示例8: build
import com.google.inject.servlet.GuiceFilter; //导入依赖的package包/类
public Server build() {
ServiceLocator locator = BootstrapUtils.newServiceLocator();
Injector injector = BootstrapUtils.newInjector(locator, Arrays.asList(new ServletModule(), new MyModule()));
BootstrapUtils.install(locator);
Server server = new Server(port);
ResourceConfig config = ResourceConfig.forApplication(new MyApplication());
ServletContainer servletContainer = new ServletContainer(config);
ServletHolder sh = new ServletHolder(servletContainer);
ServletContextHandler context = new ServletContextHandler(
ServletContextHandler.SESSIONS);
context.setContextPath("/");
FilterHolder filterHolder = new FilterHolder(GuiceFilter.class);
context.addFilter(filterHolder, "/*",
EnumSet.allOf(DispatcherType.class));
context.addServlet(sh, "/*");
server.setHandler(context);
return server;
}
示例9: testInvokeResourceWithCategory
import com.google.inject.servlet.GuiceFilter; //导入依赖的package包/类
@Test
public void testInvokeResourceWithCategory() throws Exception {
final Map<String, String> initParams = new HashMap<>();
initParams.put(NewRelicResourceFilterFactory.TRANSACTION_CATEGORY_PROP, "someCategory");
initParams.put(ResourceConfig.PROPERTY_RESOURCE_FILTER_FACTORIES,
NewRelicResourceFilterFactory.class.getCanonicalName());
server = getServer(getInjector(initParams).getInstance(GuiceFilter.class));
server.start();
Response response = httpClient.prepareGet("http://localhost:" + PORT + "/foo").execute().get();
assertEquals(200, response.getStatusCode());
assertEquals(newArrayList("someCategory:/foo GET"), wrapper.getNames());
assertTrue(wrapper.getThrowables().isEmpty());
}
示例10: testInvokeResourceWithoutCategory
import com.google.inject.servlet.GuiceFilter; //导入依赖的package包/类
@Test
public void testInvokeResourceWithoutCategory() throws Exception {
final Map<String, String> initParams = new HashMap<>();
initParams.put(ResourceConfig.PROPERTY_RESOURCE_FILTER_FACTORIES,
NewRelicResourceFilterFactory.class.getCanonicalName());
server = getServer(getInjector(initParams).getInstance(GuiceFilter.class));
server.start();
Response response = httpClient.prepareGet("http://localhost:" + PORT + "/foo").execute().get();
assertEquals(200, response.getStatusCode());
assertEquals(newArrayList("/foo GET"), wrapper.getNames());
assertTrue(wrapper.getThrowables().isEmpty());
}
示例11: testInvokeThrowsResource
import com.google.inject.servlet.GuiceFilter; //导入依赖的package包/类
@Test
public void testInvokeThrowsResource() throws Exception {
final Map<String, String> initParams = new HashMap<>();
initParams.put(ResourceConfig.PROPERTY_RESOURCE_FILTER_FACTORIES,
NewRelicResourceFilterFactory.class.getCanonicalName());
server = getServer(getInjector(initParams).getInstance(GuiceFilter.class));
server.start();
Response response = httpClient.prepareGet("http://localhost:" + PORT + "/foo/throw").execute().get();
assertEquals(500, response.getStatusCode());
assertEquals(newArrayList("/foo/throw GET"), wrapper.getNames());
assertEquals(1, wrapper.getThrowables().size());
assertEquals("zomg", wrapper.getThrowables().get(0).getMessage());
}
示例12: testInvokeThrowsMappedResource
import com.google.inject.servlet.GuiceFilter; //导入依赖的package包/类
@Test
public void testInvokeThrowsMappedResource() throws Exception {
final Map<String, String> initParams = new HashMap<>();
initParams.put(ResourceConfig.PROPERTY_RESOURCE_FILTER_FACTORIES,
NewRelicResourceFilterFactory.class.getCanonicalName());
server = getServer(getInjector(initParams).getInstance(GuiceFilter.class));
server.start();
Response response = httpClient.prepareGet("http://localhost:" + PORT + "/foo/throwMapped").execute().get();
assertEquals(javax.ws.rs.core.Response.Status.NOT_ACCEPTABLE.getStatusCode(), response.getStatusCode());
assertEquals(newArrayList("/foo/throwMapped GET"), wrapper.getNames());
assertEquals(1, wrapper.getThrowables().size());
assertEquals("asdf", wrapper.getThrowables().get(0).getMessage());
}
示例13: getServer
import com.google.inject.servlet.GuiceFilter; //导入依赖的package包/类
private Server getServer(GuiceFilter filter) {
Server server = new Server(PORT);
ServletContextHandler servletHandler = new ServletContextHandler();
servletHandler.addServlet(new ServletHolder(new HttpServlet() {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
IOException {
resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
resp.setContentType("text/plain");
resp.setContentType("UTF-8");
resp.getWriter().append("404");
}
}), "/*");
// add guice servlet filter
servletHandler.addFilter(new FilterHolder(filter), "/*", EnumSet.allOf(DispatcherType.class));
server.setHandler(servletHandler);
return server;
}
示例14: testWorksWithoutBindingAnyWrapperFactories
import com.google.inject.servlet.GuiceFilter; //导入依赖的package包/类
@Test
public void testWorksWithoutBindingAnyWrapperFactories() throws Exception {
Injector injector = getInjector(new AbstractModule() {
@Override
protected void configure() {
// no op
}
});
server = getServer(injector.getInstance(GuiceFilter.class));
server.start();
FooResource resource = injector.getInstance(FooResource.class);
com.ning.http.client.Response response =
httpClient.preparePost("http://localhost:" + PORT + "/foo").execute().get();
assertEquals(Response.Status.NO_CONTENT.getStatusCode(), response.getStatusCode());
assertEquals(1, resource.counter.get());
}
开发者ID:palominolabs,项目名称:jersey-guice-dispatch-wrapper,代码行数:22,代码来源:ResourceMethodWrappedDispatchTest.java
示例15: getInjector
import com.google.inject.servlet.GuiceFilter; //导入依赖的package包/类
private static Injector getInjector(final Module module) {
return Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
binder().requireExplicitBindings();
install(new ResourceMethodWrappedDispatchModule());
install(new ServletModule() {
@Override
protected void configureServlets() {
serve("/*").with(GuiceContainer.class);
}
});
install(new JerseyServletModule());
bind(GuiceFilter.class);
bind(GuiceContainer.class);
bind(FooResource.class);
install(module);
}
});
}
开发者ID:palominolabs,项目名称:jersey-guice-dispatch-wrapper,代码行数:21,代码来源:ResourceMethodWrappedDispatchTest.java