001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.webengine.servlet;
021
022import java.io.IOException;
023import java.io.InputStream;
024import java.io.OutputStream;
025
026import javax.servlet.ServletException;
027import javax.servlet.http.HttpServlet;
028import javax.servlet.http.HttpServletRequest;
029import javax.servlet.http.HttpServletResponse;
030
031import org.apache.commons.logging.Log;
032import org.apache.commons.logging.LogFactory;
033import org.nuxeo.common.utils.FileUtils;
034import org.nuxeo.ecm.webengine.WebEngine;
035import org.nuxeo.ecm.webengine.model.Module;
036import org.nuxeo.ecm.webengine.scripting.ScriptFile;
037import org.nuxeo.runtime.api.Framework;
038
039/**
040 * Servlet for accessing common file resources
041 *
042 * @author <a href="mailto:bs@nuxeo.com">Stefanescu Bogdan</a>
043 */
044public class ResourceServlet extends HttpServlet {
045
046    protected static final Log log = LogFactory.getLog(ResourceServlet.class);
047
048    private static final long serialVersionUID = 6548084847887645044L;
049
050    @Override
051    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
052        WebEngine engine = Framework.getService(WebEngine.class);
053        String path = req.getPathInfo();
054        if (path == null) {
055            resp.sendError(404);
056            return;
057        }
058        int p = path.indexOf('/', 1);
059        String moduleName = null;
060        if (p > -1) {
061            moduleName = path.substring(1, p);
062            path = path.substring(p);
063        } else {
064            resp.sendError(404);
065            return;
066        }
067
068        Module module = engine.getModule(moduleName);
069        if (module == null) {
070            resp.sendError(404);
071            return;
072        }
073
074        try {
075            service(req, resp, module, "/resources" + path);
076        } catch (IOException e) {
077            log.error("Unable to serve resource for " + path, e);
078            resp.sendError(404);
079        }
080    }
081
082    protected void service(HttpServletRequest req, HttpServletResponse resp, Module module, String path)
083            throws IOException {
084
085        ScriptFile file = module.getSkinResource(path);
086        if (file != null) {
087            long lastModified = file.lastModified();
088            resp.setDateHeader("Last-Modified:", lastModified);
089            resp.addHeader("Cache-Control", "public");
090            resp.addHeader("Server", "Nuxeo/WebEngine-1.0");
091
092            WebEngine engine = Framework.getService(WebEngine.class);
093            String mimeType = engine.getMimeType(file.getExtension());
094            if (mimeType == null) {
095                mimeType = "text/plain";
096            }
097            resp.setContentType(mimeType);
098            if (mimeType.startsWith("text/")) {
099                sendTextContent(file, resp);
100            } else {
101                sendBinaryContent(file, resp);
102            }
103            return;
104        }
105        resp.sendError(404);
106    }
107
108    protected static void sendBinaryContent(ScriptFile file, HttpServletResponse resp) throws IOException {
109        OutputStream out = resp.getOutputStream();
110        InputStream in = file.getInputStream();
111        try {
112            FileUtils.copy(in, out);
113        } finally {
114            in.close();
115        }
116        out.flush();
117    }
118
119    protected static void sendTextContent(ScriptFile file, HttpServletResponse resp) throws IOException {
120        // Writer out = resp.getWriter();
121        OutputStream out = resp.getOutputStream();
122        InputStream in = file.getInputStream();
123        try {
124            FileUtils.copy(in, out);
125        } finally {
126            in.close();
127        }
128        out.flush();
129    }
130
131}