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: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.servlets.ssi;
031:
032: import com.caucho.vfs.Path;
033: import com.caucho.vfs.Vfs;
034: import com.caucho.vfs.WriteStream;
035:
036: import javax.servlet.ServletException;
037: import javax.servlet.http.HttpServlet;
038: import javax.servlet.http.HttpServletRequest;
039: import javax.servlet.http.HttpServletResponse;
040: import java.io.IOException;
041: import java.util.logging.Level;
042: import java.util.logging.Logger;
043:
044: /**
045: * Serves server-side include files.
046: */
047: public class SSIServlet extends HttpServlet {
048: private static final Logger log = Logger.getLogger(SSIServlet.class
049: .getName());
050:
051: private SSIFactory _factory;
052:
053: /**
054: * Set's the SSIFactory, default is a factory that handles
055: * the standard Apache SSI commands.
056: */
057: public void setFactory(SSIFactory factory) {
058: _factory = factory;
059: }
060:
061: public void init() throws ServletException {
062: super .init();
063:
064: if (_factory == null)
065: _factory = new SSIFactory();
066: }
067:
068: public void doGet(HttpServletRequest request,
069: HttpServletResponse response) throws ServletException,
070: IOException {
071: String servletPath;
072: String pathInfo;
073:
074: servletPath = (String) request
075: .getAttribute("javax.servlet.include.servlet_path");
076: pathInfo = (String) request
077: .getAttribute("javax.servlet.include.path_info");
078:
079: if (servletPath == null && pathInfo == null) {
080: servletPath = request.getServletPath();
081: pathInfo = request.getPathInfo();
082: }
083:
084: String fullPath;
085:
086: if (pathInfo != null)
087: fullPath = servletPath + pathInfo;
088: else
089: fullPath = servletPath;
090:
091: // XXX: check cache
092:
093: String realPath = request.getRealPath(servletPath);
094:
095: Path path = Vfs.lookup().lookup(realPath);
096:
097: if (!path.canRead() || path.isDirectory()) {
098: response.sendError(HttpServletResponse.SC_NOT_FOUND);
099: return;
100: }
101:
102: response.setContentType("text/html");
103:
104: Statement stmt = new SSIParser(_factory).parse(path);
105:
106: WriteStream out = Vfs.openWrite(response.getOutputStream());
107:
108: try {
109: stmt.apply(out, request, response);
110: out.close();
111: } catch (Exception e) {
112: String errmsg = (String) request
113: .getAttribute("caucho.ssi.errmsg");
114:
115: if (errmsg != null && !response.isCommitted()) {
116: log.log(Level.FINE, e.toString(), e);
117:
118: response.setStatus(500, errmsg);
119: response.setContentType("text/html");
120:
121: out.clearWrite();
122: out.println("<html><head>");
123: out.println("<title>" + errmsg + "</title>");
124: out.println("</head>");
125:
126: out.println("<h1>" + errmsg + "</h1>");
127: out.println("</html>");
128: out.close();
129: } else if (e instanceof RuntimeException)
130: throw (RuntimeException) e;
131: else if (e instanceof IOException)
132: throw (IOException) e;
133: else if (e instanceof ServletException)
134: throw (ServletException) e;
135: else
136: throw new ServletException(e);
137: }
138: }
139: }
|