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 Emil Ong
028: */
029:
030: package com.caucho.xtpdoc;
031:
032: import com.caucho.config.*;
033: import com.caucho.vfs.Path;
034: import com.caucho.vfs.ReadStream;
035: import com.caucho.vfs.Vfs;
036:
037: import javax.servlet.ServletConfig;
038: import javax.servlet.ServletException;
039: import javax.servlet.http.HttpServlet;
040: import javax.servlet.http.HttpServletRequest;
041: import javax.servlet.http.HttpServletResponse;
042: import javax.xml.stream.FactoryConfigurationError;
043: import javax.xml.stream.XMLOutputFactory;
044: import javax.xml.stream.XMLStreamWriter;
045: import java.io.IOException;
046: import java.io.OutputStream;
047: import java.io.PrintWriter;
048: import java.util.logging.*;
049:
050: public class ResinDocServlet extends HttpServlet {
051: private static Logger log = Logger.getLogger(ResinDocServlet.class
052: .getName());
053:
054: private String _contextPath;
055: private Config _config;
056: private Path _pwd;
057: private XMLOutputFactory _outputFactory;
058: private String _encoding = "utf-8";
059:
060: public void setDocumentEncoding(String encoding) {
061: _encoding = encoding;
062: }
063:
064: public void setDocContextPath(String contextPath) {
065: _contextPath = contextPath;
066: }
067:
068: public void init(ServletConfig config) throws ServletException {
069: super .init(config);
070:
071: _config = new Config();
072: _config.setEL(false);
073: _pwd = Vfs.lookup().createRoot();
074:
075: if (_contextPath == null)
076: _contextPath = config.getServletContext()
077: .getServletContextName();
078:
079: try {
080: _outputFactory = XMLOutputFactory.newInstance();
081: } catch (FactoryConfigurationError e) {
082: throw new ServletException("Error configuring factory", e);
083: }
084:
085: if (_outputFactory == null)
086: throw new ServletException("Error configuring factory");
087: }
088:
089: public void service(HttpServletRequest request,
090: HttpServletResponse response) throws ServletException,
091: IOException {
092: OutputStream os = response.getOutputStream();
093: String servletPath = request.getServletPath();
094:
095: Path path = Vfs.lookup(request.getRealPath(servletPath));
096:
097: Document document = new Document(getServletContext(), path,
098: _contextPath, request.getContextPath() + servletPath,
099: _encoding);
100:
101: try {
102: response.setContentType("text/html");
103: //response.addHeader("Cache-Control", "max-age=3600");
104: //response.addHeader("Cache-Control", "max-age=60");
105:
106: XMLStreamWriter xmlOut = _outputFactory
107: .createXMLStreamWriter(os, _encoding);
108:
109: _config.configure(document, path);
110:
111: if (document.getRedirect() != null) {
112: response.sendRedirect(document.getRedirect());
113: return;
114: }
115:
116: document.writeHtml(xmlOut);
117:
118: xmlOut.flush();
119: } catch (ConfigException e) {
120: throw e;
121: } catch (Exception e) {
122: throw new ServletException("Error configuring document", e);
123: }
124: }
125: }
|