001: /*
002: * SSIServlet.java
003: * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/ssi/SSIServlet.java,v 1.1 2002/05/26 00:00:55 remm Exp $
004: * $Revision: 1.1 $
005: * $Date: 2002/05/26 00:00:55 $
006: *
007: * ====================================================================
008: *
009: * The Apache Software License, Version 1.1
010: *
011: * Copyright (c) 1999 The Apache Software Foundation. All rights
012: * reserved.
013: *
014: * Redistribution and use in source and binary forms, with or without
015: * modification, are permitted provided that the following conditions
016: * are met:
017: *
018: * 1. Redistributions of source code must retain the above copyright
019: * notice, this list of conditions and the following disclaimer.
020: *
021: * 2. Redistributions in binary form must reproduce the above copyright
022: * notice, this list of conditions and the following disclaimer in
023: * the documentation and/or other materials provided with the
024: * distribution.
025: *
026: * 3. The end-user documentation included with the redistribution, if
027: * any, must include the following acknowlegement:
028: * "This product includes software developed by the
029: * Apache Software Foundation (http://www.apache.org/)."
030: * Alternately, this acknowlegement may appear in the software itself,
031: * if and wherever such third-party acknowlegements normally appear.
032: *
033: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
034: * Foundation" must not be used to endorse or promote products derived
035: * from this software without prior written permission. For written
036: * permission, please contact apache@apache.org.
037: *
038: * 5. Products derived from this software may not be called "Apache"
039: * nor may "Apache" appear in their names without prior written
040: * permission of the Apache Group.
041: *
042: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
043: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
044: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
045: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
046: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
047: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
048: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
049: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
050: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
051: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
052: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
053: * SUCH DAMAGE.
054: * ====================================================================
055: *
056: * This software consists of voluntary contributions made by many
057: * individuals on behalf of the Apache Software Foundation. For more
058: * information on the Apache Software Foundation, please see
059: * <http://www.apache.org/>.
060: *
061: * [Additional notices, if required by prior licensing conditions]
062: *
063: */
064:
065: package org.apache.catalina.ssi;
066:
067: import java.io.IOException;
068: import java.io.InputStream;
069: import java.io.InputStreamReader;
070: import java.io.OutputStream;
071: import java.io.OutputStreamWriter;
072: import java.io.BufferedInputStream;
073: import java.io.BufferedReader;
074: import java.io.ByteArrayOutputStream;
075: import java.io.PrintWriter;
076: import java.io.Reader;
077: import java.io.StringWriter;
078: import java.io.Writer;
079: import java.net.URL;
080: import java.net.URLConnection;
081: import java.net.URLDecoder;
082: import java.util.ArrayList;
083: import java.util.Collection;
084: import java.util.Date;
085: import java.util.Enumeration;
086: import java.util.HashMap;
087: import java.util.Locale;
088: import java.text.SimpleDateFormat;
089: import java.util.StringTokenizer;
090: import java.util.TimeZone;
091: import javax.servlet.RequestDispatcher;
092: import javax.servlet.ServletException;
093: import javax.servlet.ServletContext;
094: import javax.servlet.ServletOutputStream;
095: import javax.servlet.http.HttpServlet;
096: import javax.servlet.http.HttpServletRequest;
097: import javax.servlet.http.HttpServletResponse;
098:
099: /**
100: * Servlet to process SSI requests within a webpage.
101: * Mapped to a path from within web.xml.
102: *
103: * @author Bip Thelin
104: * @author Amy Roh
105: * @author Dan Sandberg
106: * @version $Revision: 1.1 $, $Date: 2002/05/26 00:00:55 $
107: */
108: public class SSIServlet extends HttpServlet {
109: /** Debug level for this servlet. */
110: protected int debug = 0;
111:
112: /** Should the output be buffered. */
113: protected boolean buffered = false;
114:
115: /** Expiration time in seconds for the doc. */
116: protected Long expires = null;
117:
118: /** virtual path can be webapp-relative */
119: protected boolean isVirtualWebappRelative = false;
120:
121: //----------------- Public methods.
122:
123: /**
124: * Initialize this servlet.
125: * @exception ServletException if an error occurs
126: */
127: public void init() throws ServletException {
128: String value = null;
129: try {
130: value = getServletConfig().getInitParameter("debug");
131: debug = Integer.parseInt(value);
132: } catch (Throwable t) {
133: ;
134: }
135:
136: try {
137: value = getServletConfig().getInitParameter(
138: "isVirtualWebappRelative");
139: isVirtualWebappRelative = Integer.parseInt(value) > 0 ? true
140: : false;
141: } catch (Throwable t) {
142: ;
143: }
144:
145: try {
146: value = getServletConfig().getInitParameter("expires");
147: expires = Long.valueOf(value);
148: } catch (NumberFormatException e) {
149: expires = null;
150: log("Invalid format for expires initParam; expected integer (seconds)");
151: } catch (Throwable t) {
152: ;
153: }
154: try {
155: value = getServletConfig().getInitParameter("buffered");
156: buffered = Integer.parseInt(value) > 0 ? true : false;
157: } catch (Throwable t) {
158: ;
159: }
160: if (debug > 0)
161: log("SSIServlet.init() SSI invoker started with 'debug'="
162: + debug);
163: }
164:
165: /**
166: * Process and forward the GET request
167: * to our <code>requestHandler()</code> *
168: * @param req a value of type 'HttpServletRequest'
169: * @param res a value of type 'HttpServletResponse'
170: * @exception IOException if an error occurs
171: * @exception ServletException if an error occurs
172: */
173: public void doGet(HttpServletRequest req, HttpServletResponse res)
174: throws IOException, ServletException {
175:
176: if (debug > 0)
177: log("SSIServlet.doGet()");
178: requestHandler(req, res);
179: }
180:
181: /**
182: * Process and forward the POST request
183: * to our <code>requestHandler()</code>.
184: *
185: * @param req a value of type 'HttpServletRequest'
186: * @param res a value of type 'HttpServletResponse'
187: * @exception IOException if an error occurs
188: * @exception ServletException if an error occurs
189: */
190: public void doPost(HttpServletRequest req, HttpServletResponse res)
191: throws IOException, ServletException {
192:
193: if (debug > 0)
194: log("SSIServlet.doPost()");
195: requestHandler(req, res);
196: }
197:
198: /**
199: * Process our request and locate right SSI command.
200: * @param req a value of type 'HttpServletRequest'
201: * @param res a value of type 'HttpServletResponse'
202: */
203: protected void requestHandler(HttpServletRequest req,
204: HttpServletResponse res) throws IOException,
205: ServletException {
206:
207: ServletContext servletContext = getServletContext();
208: String path = SSIServletRequestUtil.getRelativePath(req);
209:
210: if (debug > 0)
211: log("SSIServlet.requestHandler()\n" + "Serving "
212: + (buffered ? "buffered " : "unbuffered ")
213: + "resource '" + path + "'");
214:
215: // Exclude any resource in the /WEB-INF and /META-INF subdirectories
216: // (the "toUpperCase()" avoids problems on Windows systems)
217: if (path == null || path.toUpperCase().startsWith("/WEB-INF")
218: || path.toUpperCase().startsWith("/META-INF")) {
219:
220: res.sendError(res.SC_NOT_FOUND, path);
221: log("Can't serve file: " + path);
222: return;
223: }
224:
225: URL resource = servletContext.getResource(path);
226: if (resource == null) {
227: res.sendError(res.SC_NOT_FOUND, path);
228: log("Can't find file: " + path);
229: return;
230: }
231:
232: res.setContentType("text/html;charset=UTF-8");
233:
234: if (expires != null) {
235: res.setDateHeader("Expires", (new java.util.Date())
236: .getTime()
237: + expires.longValue() * 1000);
238: }
239:
240: processSSI(req, res, resource);
241: }
242:
243: protected void processSSI(HttpServletRequest req,
244: HttpServletResponse res, URL resource) throws IOException {
245: SSIExternalResolver ssiExternalResolver = new SSIServletExternalResolver(
246: this , req, res, isVirtualWebappRelative, debug);
247: SSIProcessor ssiProcessor = new SSIProcessor(
248: ssiExternalResolver, debug);
249:
250: PrintWriter printWriter = null;
251: StringWriter stringWriter = null;
252: if (buffered) {
253: stringWriter = new StringWriter();
254: printWriter = new PrintWriter(stringWriter);
255: } else {
256: printWriter = res.getWriter();
257: }
258:
259: URLConnection resourceInfo = resource.openConnection();
260: InputStream resourceInputStream = resourceInfo.getInputStream();
261: BufferedReader bufferedReader = new BufferedReader(
262: new InputStreamReader(resourceInputStream));
263: Date lastModifiedDate = new Date(resourceInfo.getLastModified());
264: ssiProcessor.process(bufferedReader, lastModifiedDate,
265: printWriter);
266:
267: if (buffered) {
268: printWriter.flush();
269: String text = stringWriter.toString();
270: res.getWriter().write(text);
271: }
272: }
273: }
|