Skip to content

Commit

Permalink
Fix for #75 and #76 - automatic registration of servlets and filters …
Browse files Browse the repository at this point in the history
…based on @webfilter and @WebServlet annotations.
  • Loading branch information
johnament committed Mar 15, 2017
1 parent d20af07 commit 4c02dc9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 21 deletions.
46 changes: 44 additions & 2 deletions web-spi/src/main/java/ws/ament/hammock/web/spi/StartWebServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ws.ament.hammock.bootstrap.Bootstrapper;
import ws.ament.hammock.utils.ClassUtils;
import ws.ament.hammock.web.api.FilterDescriptor;
import ws.ament.hammock.web.api.ServletDescriptor;
import ws.ament.hammock.web.api.WebServer;
Expand All @@ -31,8 +32,15 @@
import javax.enterprise.context.Initialized;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.Any;
import javax.enterprise.inject.Instance;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.inject.Inject;
import javax.servlet.Filter;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.StringJoiner;
Expand All @@ -44,6 +52,14 @@
*/
@ApplicationScoped
public class StartWebServer {
@Inject
@Any
private Instance<HttpServlet> servlets;

@Inject
@Any
private Instance<Filter> filters;

private WebServer webServer;
private final Logger logger = LoggerFactory.getLogger(StartWebServer.class);

Expand All @@ -54,11 +70,12 @@ public void init(@Observes @Initialized(ApplicationScoped.class) Object obj, Bea
extension.processListeners(webServer::addListener);
processInstances(beanManager, ServletDescriptor.class, webServer::addServlet);
processInstances(beanManager, FilterDescriptor.class, webServer::addFilter);
this.webServer = webServer;
this.processFilters();
this.procesServlets();
processInstances(beanManager, ServletContextAttributeProvider.class,
s -> s.getAttributes().forEach(webServer::addServletContextAttribute));
webServer.start();

this.webServer = webServer;
}

@PreDestroy
Expand Down Expand Up @@ -86,4 +103,29 @@ private <T> void processInstances(BeanManager beanManager, Class<T> clazz, Consu
Set<Bean<?>> beans = beanManager.getBeans(clazz);
beans.stream().map(bean -> (T) beanManager.getReference(bean, clazz, beanManager.createCreationalContext(bean))).forEach(consumer);
}

private void processFilters() {
filters.forEach(filter -> {
WebFilter webFilter = ClassUtils.getAnnotation(filter.getClass(), WebFilter.class);
if(webFilter != null) {
FilterDescriptor filterDescriptor = new FilterDescriptor(webFilter.filterName(),
webFilter.value(), webFilter.urlPatterns(), webFilter.dispatcherTypes(),
webFilter.initParams(), webFilter.asyncSupported(), webFilter.servletNames(),
filter.getClass());
webServer.addFilter(filterDescriptor);
}
});
}

private void procesServlets() {
servlets.forEach(servlet -> {
WebServlet webServlet = ClassUtils.getAnnotation(servlet.getClass(), WebServlet.class);
if(webServlet != null) {
ServletDescriptor servletDescriptor = new ServletDescriptor(webServlet.name(),
webServlet.value(), webServlet.urlPatterns(), webServlet.loadOnStartup(),
webServlet.initParams(),webServlet.asyncSupported(),servlet.getClass());
webServer.addServlet(servletDescriptor);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@

package ws.ament.hammock.web.tck;

import ws.ament.hammock.web.api.FilterDescriptor;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.Dependent;
import javax.enterprise.inject.Produces;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

@ApplicationScoped
@Dependent
@WebFilter(filterName = "Default",urlPatterns = "/*", dispatcherTypes = DispatcherType.REQUEST)
public class DefaultFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
Expand All @@ -42,8 +40,4 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo
public void destroy() {

}

@Produces
@Dependent
private FilterDescriptor filterDescriptor = new FilterDescriptor("Default", null, new String[]{"/*"}, new DispatcherType[]{DispatcherType.REQUEST}, null, true, null, DefaultFilter.class);
}
13 changes: 3 additions & 10 deletions web-tck/src/main/java/ws/ament/hammock/web/tck/DefaultServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,18 @@

package ws.ament.hammock.web.tck;

import ws.ament.hammock.web.api.ServletDescriptor;
import ws.ament.hammock.web.spi.WebParam;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.Dependent;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@ApplicationScoped
@Dependent
@WebServlet(name = "Default",urlPatterns = {"/"},loadOnStartup = 1,initParams = {@WebInitParam(name="name",value="value")})
public class DefaultServlet extends HttpServlet {
@Inject
private MessageProvider messageProvider;
Expand All @@ -42,8 +39,4 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
resp.getWriter().println(messageProvider.getMessage());
resp.getWriter().println(", "+getInitParameter("name"));
}

@Produces
@Dependent
private ServletDescriptor descriptor = new ServletDescriptor("Default",null,new String[]{"/"},1,new WebInitParam[]{new WebParam("name","value")},true,DefaultServlet.class);
}

0 comments on commit 4c02dc9

Please sign in to comment.