001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.server.dispatch;
030:
031: import com.caucho.config.ConfigException;
032: import com.caucho.jsp.Page;
033: import com.caucho.jsp.QServlet;
034: import com.caucho.make.AlwaysModified;
035: import com.caucho.util.L10N;
036: import com.caucho.util.Log;
037:
038: import javax.servlet.FilterChain;
039: import javax.servlet.ServletContext;
040: import javax.servlet.ServletException;
041: import javax.servlet.ServletRequest;
042: import javax.servlet.ServletResponse;
043: import javax.servlet.http.HttpServletRequest;
044: import javax.servlet.http.HttpServletResponse;
045: import java.io.IOException;
046: import java.io.PrintWriter;
047: import java.util.logging.Level;
048: import java.util.logging.Logger;
049:
050: /**
051: * Represents the final servlet in a filter chain.
052: */
053: public class PrecompilePageFilterChain implements FilterChain {
054: private static final Logger log = Log.open(PageFilterChain.class);
055: private static final L10N L = new L10N(PageFilterChain.class);
056:
057: private QServlet _servlet;
058: private ServletContext _servletContext;
059:
060: /**
061: * Create the filter chain servlet.
062: *
063: * @param servlet the JSP servlet
064: */
065: PrecompilePageFilterChain(QServlet servlet) {
066: _servlet = servlet;
067: }
068:
069: static FilterChain create(ServletInvocation invocation,
070: PageFilterChain pageChain) {
071: String query = invocation.getQueryString();
072:
073: if (query == null)
074: return pageChain;
075:
076: int p = query.indexOf("jsp_precompile");
077:
078: if (p < 0)
079: return pageChain;
080:
081: String tail = query.substring(p + "jsp_precompile".length());
082:
083: if (tail.startsWith("=\"true\"") || tail.startsWith("=true")
084: || !tail.startsWith("=")) {
085: if (invocation instanceof Invocation) {
086: Invocation inv = (Invocation) invocation;
087:
088: inv.setDependency(AlwaysModified.create());
089: }
090:
091: return new PrecompilePageFilterChain(pageChain.getServlet());
092: } else if (tail.startsWith("=\"false\"")
093: || tail.startsWith("=false")) {
094: // jsp/1910
095: return pageChain;
096: } else
097: return new ExceptionFilterChain(new ConfigException(
098: "jsp_precompile requires a true or false value at '"
099: + tail + "'."));
100: }
101:
102: /**
103: * Sets the servlet context.
104: */
105: public void setServletContext(ServletContext servletContext) {
106: _servletContext = servletContext;
107: }
108:
109: /**
110: * Gets the servlet context.
111: */
112: public ServletContext getServletContext() {
113: return _servletContext;
114: }
115:
116: /**
117: * Invokes the final servlet at the end of the chain.
118: *
119: * @param req the servlet request
120: * @param res the servlet response
121: */
122: public void doFilter(ServletRequest request,
123: ServletResponse response) throws ServletException,
124: IOException {
125: HttpServletRequest req = (HttpServletRequest) request;
126: HttpServletResponse res = (HttpServletResponse) response;
127:
128: try {
129: Page page = _servlet.getPage(req, res);
130:
131: if (page == null) {
132: res.sendError(res.SC_NOT_FOUND);
133: return;
134: }
135: } catch (Exception e) {
136: log.log(Level.FINER, e.toString(), e);
137:
138: throw new ServletException(e);
139: }
140:
141: PrintWriter out = res.getWriter();
142: out.println("Precompiled page.");
143: }
144: }
|