In details, I am using one single jetty server by java embedding that will handle multiple request. I am injecting the event listener and the security filter with guice. But i need to server servlet upon the request domian. I am trying with virtual host, but as i am not using xml based configuration there is not much documentation to get help of.
So i am trying is that -
If domain a.b.c -> handler AHandler and filter applied AFilter,
If domain b.b.c -> handler BHandler and filter applied BFilter.
My Contex initialization is like this:
getHandler(server) {
ServletContextHandler handler = new ServletContextHandler(ServletContextHandler.SESSIONS);
handler.setVirtualHosts(new String[]{HOST});
handler.setClassLoader(Thread.currentThread().getContextClassLoader());
handler.setServer(server);
handler.addFilter(GuiceFilter.class, "/*", EnumSet.allOf(DispatcherType.class));
handler.setContextPath("/");
handler.addEventListener(new ServerContextListener());
}
Listener class
class ServerContextListener extends GuiceServletContextListener {
private ServletContext servletContext;
public ServletContext getServletContext() {
return servletContext;
}
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
this.servletContext = checkNotNull(servletContextEvent.getServletContext(), "servletContext");
super.contextInitialized(servletContextEvent);
}
@Override
protected Injector getInjector() {
return Guice.createInjector(getModules());
}
private Iterable<Module> getModules() {
return ImmutableList.of(
new SecurityModule(getServletContext()),
new ResourceModule());
}
}
Security Module:
class SecurityModule extends ShiroWebModule {
public SecurityModule(ServletContext servletContext) {
super(servletContext);
}
@SuppressWarnings("unchecked")
@Override
protected void configureShiroWeb() {
addFilterChain("/**", AUTHC_BASIC);
}
}
class ResourceModule extends ServletModule {
@Override
protected void configureServlets() {
serve("/*").with(AServlet.class);
}
}
And the in the server like this :
Server server = new Server(new InetSocketAddress("0.0.0.0", 8585));
HandlerCollection handler = new HandlerCollection();
handler.addHandler(AContext.getHandler(server));
handler.addHandler(BContext.getHandler(server));
server.setHandler(handler);
server.start();
and the AServlet and BServlet are regular servlet with get post put methods.
But my implementaion is not working as expected. It sends all request to the last handler registered.
Need some help to accomplish what I need. Where I am doing things wrong?
Whatever the
Handler
implementation is returned for these 2 calls...They should have a
ContextHandler
wrapping them, as that's how Virtual Hosts are configured and controlled for a specific Handler implementation.Added VirtualHostsExample.java to jetty-project/embedded-jetty-cookbook.
With output like this ...