1 28 29 package com.caucho.server.dispatch; 30 31 import com.caucho.config.ConfigException; 32 import com.caucho.jsp.Page; 33 import com.caucho.jsp.QServlet; 34 import com.caucho.make.AlwaysModified; 35 import com.caucho.util.L10N; 36 import com.caucho.util.Log; 37 38 import javax.servlet.FilterChain ; 39 import javax.servlet.ServletContext ; 40 import javax.servlet.ServletException ; 41 import javax.servlet.ServletRequest ; 42 import javax.servlet.ServletResponse ; 43 import javax.servlet.http.HttpServletRequest ; 44 import javax.servlet.http.HttpServletResponse ; 45 import java.io.IOException ; 46 import java.io.PrintWriter ; 47 import java.util.logging.Level ; 48 import java.util.logging.Logger ; 49 50 53 public class PrecompilePageFilterChain implements FilterChain { 54 private static final Logger log = Log.open(PageFilterChain.class); 55 private static final L10N L = new L10N(PageFilterChain.class); 56 57 private QServlet _servlet; 58 private ServletContext _servletContext; 59 60 65 PrecompilePageFilterChain(QServlet servlet) 66 { 67 _servlet = servlet; 68 } 69 70 static FilterChain create(ServletInvocation invocation, 71 PageFilterChain pageChain) 72 { 73 String query = invocation.getQueryString(); 74 75 if (query == null) 76 return pageChain; 77 78 int p = query.indexOf("jsp_precompile"); 79 80 if (p < 0) 81 return pageChain; 82 83 String tail = query.substring(p + "jsp_precompile".length()); 84 85 if (tail.startsWith("=\"true\"") || 86 tail.startsWith("=true") || 87 ! tail.startsWith("=")) { 88 if (invocation instanceof Invocation) { 89 Invocation inv = (Invocation) invocation; 90 91 inv.setDependency(AlwaysModified.create()); 92 } 93 94 return new PrecompilePageFilterChain(pageChain.getServlet()); 95 } 96 else if (tail.startsWith("=\"false\"") || 97 tail.startsWith("=false")) { 98 return pageChain; 100 } 101 else 102 return new ExceptionFilterChain(new ConfigException("jsp_precompile requires a true or false value at '" + tail + "'.")); 103 } 104 105 108 public void setServletContext(ServletContext servletContext) 109 { 110 _servletContext = servletContext; 111 } 112 113 116 public ServletContext getServletContext() 117 { 118 return _servletContext; 119 } 120 121 127 public void doFilter(ServletRequest request, ServletResponse response) 128 throws ServletException , IOException 129 { 130 HttpServletRequest req = (HttpServletRequest ) request; 131 HttpServletResponse res = (HttpServletResponse ) response; 132 133 try { 134 Page page = _servlet.getPage(req, res); 135 136 if (page == null) { 137 res.sendError(res.SC_NOT_FOUND); 138 return; 139 } 140 } catch (Exception e) { 141 log.log(Level.FINER, e.toString(), e); 142 143 throw new ServletException (e); 144 } 145 146 PrintWriter out = res.getWriter(); 147 out.println("Precompiled page."); 148 } 149 } 150 | Popular Tags |