001: /*
002: * Copyright 1999,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.catalina.ssi;
018:
019: import java.io.BufferedReader;
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.io.InputStreamReader;
023: import java.io.PrintWriter;
024: import java.io.StringWriter;
025: import java.net.URL;
026: import java.net.URLConnection;
027: import java.util.Date;
028:
029: import javax.servlet.ServletContext;
030: import javax.servlet.ServletException;
031: import javax.servlet.http.HttpServlet;
032: import javax.servlet.http.HttpServletRequest;
033: import javax.servlet.http.HttpServletResponse;
034:
035: /**
036: * Servlet to process SSI requests within a webpage.
037: * Mapped to a path from within web.xml.
038: *
039: * @author Bip Thelin
040: * @author Amy Roh
041: * @author Dan Sandberg
042: * @version $Revision: 1.6.2.1 $, $Date: 2004/08/21 15:49:53 $
043: */
044: public class SSIServlet extends HttpServlet {
045: /** Debug level for this servlet. */
046: protected int debug = 0;
047:
048: /** Should the output be buffered. */
049: protected boolean buffered = false;
050:
051: /** Expiration time in seconds for the doc. */
052: protected Long expires = null;
053:
054: /** virtual path can be webapp-relative */
055: protected boolean isVirtualWebappRelative = false;
056:
057: //----------------- Public methods.
058:
059: /**
060: * Initialize this servlet.
061: * @exception ServletException if an error occurs
062: */
063: public void init() throws ServletException {
064: String value = null;
065: try {
066: value = getServletConfig().getInitParameter("debug");
067: debug = Integer.parseInt(value);
068: } catch (Throwable t) {
069: ;
070: }
071:
072: try {
073: value = getServletConfig().getInitParameter(
074: "isVirtualWebappRelative");
075: isVirtualWebappRelative = Integer.parseInt(value) > 0 ? true
076: : false;
077: } catch (Throwable t) {
078: ;
079: }
080:
081: try {
082: value = getServletConfig().getInitParameter("expires");
083: expires = Long.valueOf(value);
084: } catch (NumberFormatException e) {
085: expires = null;
086: log("Invalid format for expires initParam; expected integer (seconds)");
087: } catch (Throwable t) {
088: ;
089: }
090: try {
091: value = getServletConfig().getInitParameter("buffered");
092: buffered = Integer.parseInt(value) > 0 ? true : false;
093: } catch (Throwable t) {
094: ;
095: }
096: if (debug > 0)
097: log("SSIServlet.init() SSI invoker started with 'debug'="
098: + debug);
099: }
100:
101: /**
102: * Process and forward the GET request
103: * to our <code>requestHandler()</code> *
104: * @param req a value of type 'HttpServletRequest'
105: * @param res a value of type 'HttpServletResponse'
106: * @exception IOException if an error occurs
107: * @exception ServletException if an error occurs
108: */
109: public void doGet(HttpServletRequest req, HttpServletResponse res)
110: throws IOException, ServletException {
111:
112: if (debug > 0)
113: log("SSIServlet.doGet()");
114: requestHandler(req, res);
115: }
116:
117: /**
118: * Process and forward the POST request
119: * to our <code>requestHandler()</code>.
120: *
121: * @param req a value of type 'HttpServletRequest'
122: * @param res a value of type 'HttpServletResponse'
123: * @exception IOException if an error occurs
124: * @exception ServletException if an error occurs
125: */
126: public void doPost(HttpServletRequest req, HttpServletResponse res)
127: throws IOException, ServletException {
128:
129: if (debug > 0)
130: log("SSIServlet.doPost()");
131: requestHandler(req, res);
132: }
133:
134: /**
135: * Process our request and locate right SSI command.
136: * @param req a value of type 'HttpServletRequest'
137: * @param res a value of type 'HttpServletResponse'
138: */
139: protected void requestHandler(HttpServletRequest req,
140: HttpServletResponse res) throws IOException,
141: ServletException {
142:
143: ServletContext servletContext = getServletContext();
144: String path = SSIServletRequestUtil.getRelativePath(req);
145:
146: if (debug > 0)
147: log("SSIServlet.requestHandler()\n" + "Serving "
148: + (buffered ? "buffered " : "unbuffered ")
149: + "resource '" + path + "'");
150:
151: // Exclude any resource in the /WEB-INF and /META-INF subdirectories
152: // (the "toUpperCase()" avoids problems on Windows systems)
153: if (path == null || path.toUpperCase().startsWith("/WEB-INF")
154: || path.toUpperCase().startsWith("/META-INF")) {
155:
156: res.sendError(HttpServletResponse.SC_NOT_FOUND, path);
157: log("Can't serve file: " + path);
158: return;
159: }
160:
161: URL resource = servletContext.getResource(path);
162: if (resource == null) {
163: res.sendError(HttpServletResponse.SC_NOT_FOUND, path);
164: log("Can't find file: " + path);
165: return;
166: }
167:
168: String resourceMimeType = servletContext.getMimeType(path);
169: if (resourceMimeType == null) {
170: resourceMimeType = "text/html;charset=UTF-8";
171: }
172: res.setContentType(resourceMimeType);
173:
174: if (expires != null) {
175: res.setDateHeader("Expires", (new java.util.Date())
176: .getTime()
177: + expires.longValue() * 1000);
178: }
179:
180: processSSI(req, res, resource);
181: }
182:
183: protected void processSSI(HttpServletRequest req,
184: HttpServletResponse res, URL resource) throws IOException {
185:
186: SSIExternalResolver ssiExternalResolver = new SSIServletExternalResolver(
187: this , req, res, isVirtualWebappRelative, debug);
188: SSIProcessor ssiProcessor = new SSIProcessor(
189: ssiExternalResolver, debug);
190:
191: PrintWriter printWriter = null;
192: StringWriter stringWriter = null;
193: if (buffered) {
194: stringWriter = new StringWriter();
195: printWriter = new PrintWriter(stringWriter);
196: } else {
197: printWriter = res.getWriter();
198: }
199:
200: URLConnection resourceInfo = resource.openConnection();
201: InputStream resourceInputStream = resourceInfo.getInputStream();
202: BufferedReader bufferedReader = new BufferedReader(
203: new InputStreamReader(resourceInputStream));
204: Date lastModifiedDate = new Date(resourceInfo.getLastModified());
205: ssiProcessor.process(bufferedReader, lastModifiedDate,
206: printWriter);
207:
208: if (buffered) {
209: printWriter.flush();
210: String text = stringWriter.toString();
211: res.getWriter().write(text);
212: }
213: }
214: }
|