1 13 package com.tonbeller.wcf.controller; 14 15 import java.io.IOException ; 16 import java.io.PrintWriter ; 17 18 import javax.servlet.Filter ; 19 import javax.servlet.FilterChain ; 20 import javax.servlet.FilterConfig ; 21 import javax.servlet.ServletException ; 22 import javax.servlet.ServletRequest ; 23 import javax.servlet.ServletResponse ; 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.http.HttpServletResponse ; 26 import javax.servlet.http.HttpSession ; 27 import javax.servlet.jsp.jstl.core.Config; 28 29 import org.apache.log4j.Logger; 30 import org.apache.log4j.MDC; 31 32 import com.tonbeller.tbutils.testenv.Environment; 33 import com.tonbeller.wcf.utils.DomUtils; 34 import com.tonbeller.wcf.utils.JDK13Utils; 35 import com.tonbeller.wcf.utils.UrlUtils; 36 37 41 42 public class RequestFilter implements Filter { 43 44 private static Logger logger = Logger.getLogger(RequestFilter.class); 45 46 static final String NEXTVIEW = RequestFilter.class.getName() + ".nextview"; 47 static final String ISNEW = RequestFilter.class.getName() + ".isnew"; 48 49 50 public static final String CONTEXT = "context"; 51 52 53 public static final String FORCE_INDEX_JSP = "com.tonbeller.wcf.controller.FORCE_INDEX_JSP"; 54 55 59 public static final String RESET_RANDOM_SEED = "resetRandomSeed"; 60 61 private String errorJSP = null; 62 private String busyJSP = null; 63 private String indexJSP = null; 64 private String [] passThru = null; 65 66 private String forceExtension = null; 67 68 public RequestFilter() { 69 } 70 71 public void init(FilterConfig config) throws ServletException { 72 errorJSP = config.getInitParameter("errorJSP"); 73 forceExtension = config.getInitParameter("forceExtension"); 74 indexJSP = config.getInitParameter("indexJSP"); 75 busyJSP = config.getInitParameter("busyJSP"); 76 77 String patternList = config.getInitParameter("passThru"); 78 passThru = UrlUtils.parseUrlPatternList(patternList); 79 } 80 81 class MyHandler implements RequestSynchronizer.Handler { 82 protected RequestContext context; 83 protected HttpServletRequest request; 84 protected HttpServletResponse response; 85 protected FilterChain filterChain; 86 87 public MyHandler(RequestContext context, FilterChain filterChain) { 88 this.context = context; 89 this.request = context.getRequest(); 90 this.response = context.getResponse(); 91 this.filterChain = filterChain; 92 } 93 94 public String getResultURI() { 95 String uri = (String ) request.getAttribute(NEXTVIEW); 97 if (uri != null) { 98 uri = UrlUtils.forceExtension(uri, forceExtension); 99 uri = UrlUtils.redirectURI(request, uri); 100 return uri; 101 } 102 103 return request.getRequestURI(); 105 } 106 107 public void normalRequest() throws Exception { 108 HttpSession session = request.getSession(true); 109 try { 110 111 if (request.getParameter(RESET_RANDOM_SEED) != null) 112 DomUtils.setRandomSeed(123); 113 114 if (redirectToIndex()) 115 return; 116 117 Controller controller = WcfController.instance(session); 119 controller.request(context); 120 121 if (response.containsHeader("Location")) 123 return; 124 if (context.isResponseComplete()) 126 return; 127 128 String uri = (String ) request.getAttribute(NEXTVIEW); 131 if (session.getAttribute(FORCE_INDEX_JSP) != null) { 132 session.removeAttribute(FORCE_INDEX_JSP); 133 uri = indexJSP; 134 } 135 136 if (uri != null) 137 forward(uri); 138 else 139 filterChain.doFilter(request, response); 140 } catch (Exception e) { 141 handleException(e); 142 } 143 } 144 145 public void recursiveRequest() throws Exception { 146 try { 147 filterChain.doFilter(request, response); 148 } catch (Exception e) { 149 handleException(e); 150 } 151 } 152 153 private void handleException(Exception e) throws Exception { 154 logger.error("exeption", e); 155 logger.error("cause", JDK13Utils.getCause(e)); 156 157 if (isErrorPage()) 159 throw e; 160 161 if (errorJSP != null) { 162 try { 163 logger.info("redirecting to error page " + errorJSP); 164 request.setAttribute("javax.servlet.jsp.jspException", e); 165 request.getRequestDispatcher(errorJSP).forward(request, response); 166 } catch (Exception e2) { 167 throw e; 171 } 172 } else 173 throw e; 174 } 175 176 public void showBusyPage(boolean redirect) throws Exception { 177 if (redirectToIndex()) 178 return; 179 if (busyJSP != null) { 180 if (redirect) 181 forward(busyJSP); 182 else 183 filterChain.doFilter(request, response); 184 } else 185 throw new IllegalStateException ("concurrent requests and no busy.jsp defined in web.xml"); 186 } 187 188 public boolean isBusyPage() { 189 if (busyJSP == null) 190 return false; 191 return request.getRequestURI().endsWith(busyJSP); 192 } 193 194 public boolean isErrorPage() { 195 if (errorJSP == null) 196 return false; 197 return request.getRequestURI().endsWith(errorJSP); 198 } 199 200 204 private void forward(String uri) throws IOException { 205 uri = UrlUtils.redirectURI(request, uri); 206 uri = UrlUtils.forceExtension(uri, forceExtension); 207 if (logger.isInfoEnabled()) 208 logger.info("redirecting to " + uri); 209 response.sendRedirect(uri); 210 } 211 212 216 protected boolean redirectToIndex() throws Exception { 217 if (indexJSP != null) { 218 if (Environment.isTest()) 220 return false; 221 222 HttpSession session = context.getSession(); 223 boolean isNew = session.isNew(); 224 if (!isNew) 225 isNew = !"false".equals(session.getAttribute(ISNEW)); 226 if (isNew) { 227 session.setAttribute(ISNEW, "false"); 228 forward(indexJSP); 229 return true; 230 } 231 } 232 return false; 233 } 234 235 } 236 237 public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) 238 throws IOException , ServletException { 239 MultiPartEnabledRequest request = new MultiPartEnabledRequest((HttpServletRequest ) req); 240 HttpServletResponse response = (HttpServletResponse ) res; 241 242 HttpSession session = request.getSession(true); 243 MDC.put("SessionID", session.getId()); 244 245 String cpath = request.getContextPath(); 246 request.setAttribute(CONTEXT, cpath); 247 248 RequestContext context = RequestContextFactoryFinder.createContext(request, response, true); 249 try { 250 Config.set(request, Config.FMT_LOCALE, context.getLocale()); 252 if (logger.isInfoEnabled()) 254 logRequest(request); 255 256 if (passThru(req)) { 257 chain.doFilter(req, res); 258 return; 259 } 260 261 MyHandler handler = new MyHandler(context, chain); 262 long t1 = System.currentTimeMillis(); 263 RequestSynchronizer.instance(request).handleRequest(handler); 264 long t2 = System.currentTimeMillis(); 265 if (logger.isInfoEnabled()) 266 logger.info("Request Execution total time: " + (t2 - t1) + " ms"); 267 } catch (Throwable e) { 268 PrintWriter out = null; 269 try { 270 out = response.getWriter(); 271 } catch (Exception e2) { 272 out = new PrintWriter (System.out); 273 logger.error("No output writer could be retrieved, logging to stdout"); 274 } 275 out.println("<html><body>"); 276 while (e != null) { 277 logger.error("Error handling request", e); 278 out.println(); 279 out.println("<h2>" + e.toString() + "</h2><pre>"); 280 e.printStackTrace(out); 281 out.println("</pre>"); 282 283 Throwable prev = e; 284 e = JDK13Utils.getCause(e); 285 if (e == prev) 286 break; 287 } 288 out.println("</body></html>"); 289 } finally { 290 context.invalidate(); 291 } 292 } 293 294 private boolean passThru(ServletRequest req) { 295 if (passThru == null) 296 return false; 297 HttpServletRequest hsr = (HttpServletRequest ) req; 298 return UrlUtils.matchPattern(hsr, passThru); 299 } 300 301 302 void setForceExtension(String forceExtension) { 303 this.forceExtension = forceExtension; 304 } 305 306 private void logRequest(HttpServletRequest request) { 307 logger.info(">>> Request " + request.getScheme() + "://" + request.getServerName() + ":" 308 + request.getServerPort() + request.getContextPath() + request.getServletPath() + "[" 309 + request.getPathInfo() + "]" + "[?" + request.getQueryString() + "]"); 310 } 311 312 public static void setSessionIsNew(HttpSession session, boolean isNew) { 313 session.setAttribute(ISNEW, Boolean.toString(isNew)); 314 } 315 316 public static void setForceIndexJsp(HttpSession session, boolean b) { 317 if (b) 318 session.setAttribute(FORCE_INDEX_JSP, "true"); 319 else 320 session.removeAttribute(FORCE_INDEX_JSP); 321 } 322 323 public void destroy() { 324 } 325 326 } | Popular Tags |