1 16 package org.directwebremoting.servlet; 17 18 import java.io.IOException ; 19 20 import javax.servlet.Filter ; 21 import javax.servlet.FilterChain ; 22 import javax.servlet.FilterConfig ; 23 import javax.servlet.ServletConfig ; 24 import javax.servlet.ServletContext ; 25 import javax.servlet.ServletException ; 26 import javax.servlet.ServletRequest ; 27 import javax.servlet.ServletResponse ; 28 import javax.servlet.http.HttpServlet ; 29 import javax.servlet.http.HttpServletRequest ; 30 import javax.servlet.http.HttpServletResponse ; 31 32 import org.directwebremoting.Container; 33 import org.directwebremoting.WebContextFactory.WebContextBuilder; 34 import org.directwebremoting.util.Logger; 35 import org.directwebremoting.util.ServletLoggingOutput; 36 37 43 public class DwrWebContextFilter implements Filter 44 { 45 48 public void init(FilterConfig aFilterConfig) throws ServletException 49 { 50 this.filterConfig = aFilterConfig; 51 } 52 53 56 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException , ServletException 57 { 58 ServletContext servletContext = filterConfig.getServletContext(); 59 60 Container container = (Container) servletContext.getAttribute(Container.class.getName()); 61 if (container == null) 62 { 63 log.error("DwrWebContextFilter can not find ServletContext attribute for the DWR Container. Is DwrServlet configured in this web-application?"); 64 } 65 66 ServletConfig servletConfig = (ServletConfig ) servletContext.getAttribute(ServletConfig .class.getName()); 67 if (servletConfig == null) 68 { 69 log.error("DwrWebContextFilter can not find ServletContext attribute for the ServletConfig."); 70 } 71 72 WebContextBuilder webContextBuilder = (WebContextBuilder) servletContext.getAttribute(WebContextBuilder.class.getName()); 73 if (webContextBuilder == null) 74 { 75 log.error("DwrWebContextFilter can not find ServletContext attribute for the WebContextBuilder. WebContext will not be available."); 76 } 77 else 78 { 79 try 80 { 81 webContextBuilder.set((HttpServletRequest ) request, (HttpServletResponse ) response, servletConfig, servletContext, container); 82 83 HttpServlet servlet = (HttpServlet ) servletContext.getAttribute(HttpServlet .class.getName()); 86 if (servlet != null) 87 { 88 ServletLoggingOutput.setExecutionContext(servlet); 89 } 90 91 chain.doFilter(request, response); 92 } 93 finally 94 { 95 webContextBuilder.unset(); 96 ServletLoggingOutput.unsetExecutionContext(); 97 } 98 } 99 } 100 101 104 public void destroy() 105 { 106 } 107 108 111 private static final Logger log = Logger.getLogger(DwrWebContextFilter.class); 112 113 116 private FilterConfig filterConfig; 117 } 118 | Popular Tags |