1 4 package com.opensymphony.module.sitemesh.filter; 5 6 import com.opensymphony.module.sitemesh.*; 7 import com.opensymphony.module.sitemesh.util.Container; 8 9 import javax.servlet.*; 10 import javax.servlet.http.HttpServletRequest ; 11 import javax.servlet.http.HttpServletResponse ; 12 import java.io.IOException ; 13 import java.io.PrintWriter ; 14 15 22 public class PageFilter implements Filter, RequestConstants { 23 private FilterConfig filterConfig = null; 24 private Factory factory = null; 25 26 32 public void doFilter(ServletRequest rq, ServletResponse rs, FilterChain chain) 33 throws IOException , ServletException { 34 35 HttpServletRequest request = (HttpServletRequest ) rq; 36 37 if (rq.getAttribute(FILTER_APPLIED) != null || factory.isPathExcluded(extractRequestPath(request))) { 38 chain.doFilter(rq, rs); 40 } 41 else { 42 request.setAttribute(FILTER_APPLIED, Boolean.TRUE); 43 44 if (Container.get() == Container.TOMCAT) { 47 request.getSession(true); 48 } 49 HttpServletResponse response = (HttpServletResponse ) rs; 50 51 Page page = parsePage(request, response, chain); 53 54 if (page != null) { 55 page.setRequest(request); 56 57 Decorator decorator = factory.getDecoratorMapper().getDecorator(request, page); 58 if (decorator != null && decorator.getPage() != null) { 59 applyDecorator(page, decorator, request, response); 60 page = null; 61 return; 62 } 63 64 writeOriginal(request, response, page); 68 page = null; 69 } 70 } 71 } 72 73 private String extractRequestPath(HttpServletRequest request) { 74 String servletPath = request.getServletPath(); 75 String pathInfo = request.getPathInfo(); 76 String query = request.getQueryString(); 77 return (servletPath == null ? "" : servletPath) 78 + (pathInfo == null ? "" : pathInfo) 79 + (query == null ? "" : ("?" + query)); 80 } 81 82 83 public void init(FilterConfig filterConfig) { 84 if (filterConfig != null) { 85 this.filterConfig = filterConfig; 86 factory = Factory.getInstance(new Config(filterConfig)); 87 } 88 else { 89 destroy(); 90 } 91 } 92 93 94 public FilterConfig getFilterConfig() { 96 return filterConfig; 97 } 98 99 100 public void setFilterConfig(FilterConfig filterConfig) { 102 init(filterConfig); 103 } 104 105 106 public void destroy() { 107 factory = null; 108 } 109 110 115 protected Page parsePage(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException , ServletException { 116 try { 117 PageResponseWrapper pageResponse = new PageResponseWrapper(response, factory); 118 chain.doFilter(request, pageResponse); 119 Page result = (Page)request.getAttribute(PAGE); 121 if (result == null) { 122 result = pageResponse.getPage(); 124 } 125 request.setAttribute(USING_STREAM, new Boolean (pageResponse.isUsingStream())); 126 return result; 127 } 128 catch (IllegalStateException e) { 129 if (Container.get() != Container.WEBLOGIC) throw e; 133 return null; 134 } 135 } 136 137 141 protected void applyDecorator(Page page, Decorator decorator, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 142 try { 143 request.setAttribute(PAGE, page); 144 ServletContext context = filterConfig.getServletContext(); 145 if (decorator.getURIPath() != null) { 147 if (context.getContext(decorator.getURIPath()) != null) { 150 context = context.getContext(decorator.getURIPath()); 151 } 152 } 153 RequestDispatcher dispatcher = context.getRequestDispatcher(decorator.getPage()); 155 dispatcher.include(request, response); 157 158 while (decorator.getInitParameterNames().hasNext()) { 160 String initParam = (String ) decorator.getInitParameterNames().next(); 161 if (initParam.startsWith("header.")) { 162 response.setHeader(initParam.substring(initParam.indexOf('.')), decorator.getInitParameter(initParam)); 163 } 164 } 165 166 request.removeAttribute(PAGE); 167 } 168 catch (RuntimeException e) { 169 if (Container.get() == Container.TOMCAT) 172 e.printStackTrace(); 173 174 throw e; 175 } 176 } 177 178 179 private void writeOriginal(HttpServletRequest request, HttpServletResponse response, Page page) throws IOException { 180 response.setContentLength(page.getContentLength()); 181 if (request.getAttribute(USING_STREAM).equals(Boolean.TRUE)) 182 { 183 PrintWriter writer = new PrintWriter (response.getOutputStream()); 184 page.writePage(writer); 185 writer.flush(); 187 response.getOutputStream().flush(); 188 } 189 else 190 { 191 page.writePage(response.getWriter()); 192 response.getWriter().flush(); 193 } 194 } 195 } 196 | Popular Tags |