KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > dispatch > PrecompilePageFilterChain


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

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 JavaDoc;
39 import javax.servlet.ServletContext JavaDoc;
40 import javax.servlet.ServletException JavaDoc;
41 import javax.servlet.ServletRequest JavaDoc;
42 import javax.servlet.ServletResponse JavaDoc;
43 import javax.servlet.http.HttpServletRequest JavaDoc;
44 import javax.servlet.http.HttpServletResponse JavaDoc;
45 import java.io.IOException JavaDoc;
46 import java.io.PrintWriter JavaDoc;
47 import java.util.logging.Level JavaDoc;
48 import java.util.logging.Logger JavaDoc;
49
50 /**
51  * Represents the final servlet in a filter chain.
52  */

53 public class PrecompilePageFilterChain implements FilterChain JavaDoc {
54   private static final Logger JavaDoc log = Log.open(PageFilterChain.class);
55   private static final L10N L = new L10N(PageFilterChain.class);
56   
57   private QServlet _servlet;
58   private ServletContext JavaDoc _servletContext;
59
60   /**
61    * Create the filter chain servlet.
62    *
63    * @param servlet the JSP servlet
64    */

65   PrecompilePageFilterChain(QServlet servlet)
66   {
67     _servlet = servlet;
68   }
69
70   static FilterChain JavaDoc create(ServletInvocation invocation,
71                             PageFilterChain pageChain)
72   {
73     String JavaDoc 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 JavaDoc 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       // jsp/1910
99
return pageChain;
100     }
101     else
102       return new ExceptionFilterChain(new ConfigException("jsp_precompile requires a true or false value at '" + tail + "'."));
103   }
104
105   /**
106    * Sets the servlet context.
107    */

108   public void setServletContext(ServletContext JavaDoc servletContext)
109   {
110     _servletContext = servletContext;
111   }
112
113   /**
114    * Gets the servlet context.
115    */

116   public ServletContext JavaDoc getServletContext()
117   {
118     return _servletContext;
119   }
120   
121   /**
122    * Invokes the final servlet at the end of the chain.
123    *
124    * @param req the servlet request
125    * @param res the servlet response
126    */

127   public void doFilter(ServletRequest JavaDoc request, ServletResponse JavaDoc response)
128     throws ServletException JavaDoc, IOException JavaDoc
129   {
130     HttpServletRequest JavaDoc req = (HttpServletRequest JavaDoc) request;
131     HttpServletResponse JavaDoc res = (HttpServletResponse JavaDoc) 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 JavaDoc e) {
141       log.log(Level.FINER, e.toString(), e);
142         
143       throw new ServletException JavaDoc(e);
144     }
145     
146     PrintWriter JavaDoc out = res.getWriter();
147     out.println("Precompiled page.");
148   }
149 }
150
Popular Tags