Advertisement
m4c0

wicket filter per host

Jul 7th, 2011
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.85 KB | None | 0 0
  1.     @WebFilter(urlPatterns = "/*")
  2.     public static class Filter implements IWebApplicationFactory, javax.servlet.Filter {
  3.  
  4.         private final Map<String, WicketFilter> filters = new HashMap<String, WicketFilter>();
  5.         private final ThreadLocal<String> hosts = new ThreadLocal<String>();
  6.         private FilterConfig config;
  7.        
  8.         public Filter() {
  9.             System.setProperty("wicket.configuration", DEPLOYMENT);
  10.         }
  11.  
  12.         @Override
  13.         public void init(FilterConfig filterConfig) throws ServletException {
  14.             this.config = filterConfig;
  15.         }
  16.  
  17.         @Override
  18.         public void destroy() {
  19.             for (WicketFilter w : filters.values()) {
  20.                 w.destroy();
  21.             }
  22.             filters.clear();
  23.         }
  24.  
  25.         @Override
  26.         public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
  27.             hosts.set(request.getServerName());
  28.             try {
  29.                 getFilterForHost().doFilter(request, response, chain);
  30.             } finally {
  31.                 hosts.remove();
  32.             }
  33.         }
  34.        
  35.         private WicketFilter getFilterForHost() throws ServletException {
  36.             String host = hosts.get();
  37.             if (!filters.containsKey(host)) {
  38.                 WicketFilter w = new WicketFilter() {
  39.  
  40.                     @Override
  41.                     protected IWebApplicationFactory getApplicationFactory() {
  42.                         return Filter.this;
  43.                     }
  44.                 };
  45.                 w.init(config);
  46.                 filters.put(host, w);
  47.             }
  48.             return filters.get(host);
  49.         }
  50.  
  51.         @Override
  52.         public WebApplication createApplication(WicketFilter wf) {
  53.             return new WicketApp(hosts.get());
  54.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement