001/* 002 * (C) Copyright 2006-2007 Nuxeo SA (http://nuxeo.com/) and others. 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 * Contributors: 017 * Nuxeo - initial API and implementation 018 * 019 * $Id$ 020 */ 021 022package org.nuxeo.ecm.webengine.servlet; 023 024import java.io.IOException; 025import java.io.InputStream; 026import java.io.OutputStream; 027 028import javax.servlet.ServletException; 029import javax.servlet.http.HttpServlet; 030import javax.servlet.http.HttpServletRequest; 031import javax.servlet.http.HttpServletResponse; 032 033import org.apache.commons.logging.Log; 034import org.apache.commons.logging.LogFactory; 035import org.nuxeo.common.utils.FileUtils; 036import org.nuxeo.ecm.webengine.WebEngine; 037import org.nuxeo.ecm.webengine.model.Module; 038import org.nuxeo.ecm.webengine.scripting.ScriptFile; 039import org.nuxeo.runtime.api.Framework; 040 041/** 042 * Servlet for accessing common file resources 043 * 044 * @author <a href="mailto:bs@nuxeo.com">Stefanescu Bogdan</a> 045 */ 046public class ResourceServlet extends HttpServlet { 047 048 protected static final Log log = LogFactory.getLog(ResourceServlet.class); 049 050 private static final long serialVersionUID = 6548084847887645044L; 051 052 @Override 053 protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 054 WebEngine engine = Framework.getService(WebEngine.class); 055 String path = req.getPathInfo(); 056 if (path == null) { 057 resp.sendError(404); 058 return; 059 } 060 int p = path.indexOf('/', 1); 061 String moduleName = null; 062 if (p > -1) { 063 moduleName = path.substring(1, p); 064 path = path.substring(p); 065 } else { 066 resp.sendError(404); 067 return; 068 } 069 070 Module module = engine.getModule(moduleName); 071 if (module == null) { 072 resp.sendError(404); 073 return; 074 } 075 076 try { 077 service(req, resp, module, "/resources" + path); 078 } catch (IOException e) { 079 log.error("Unable to serve resource for " + path, e); 080 resp.sendError(404); 081 } 082 } 083 084 protected void service(HttpServletRequest req, HttpServletResponse resp, Module module, String path) 085 throws IOException { 086 087 ScriptFile file = module.getSkinResource(path); 088 if (file != null) { 089 long lastModified = file.lastModified(); 090 resp.setDateHeader("Last-Modified:", lastModified); 091 resp.addHeader("Cache-Control", "public"); 092 resp.addHeader("Server", "Nuxeo/WebEngine-1.0"); 093 094 WebEngine engine = Framework.getService(WebEngine.class); 095 String mimeType = engine.getMimeType(file.getExtension()); 096 if (mimeType == null) { 097 mimeType = "text/plain"; 098 } 099 resp.setContentType(mimeType); 100 if (mimeType.startsWith("text/")) { 101 sendTextContent(file, resp); 102 } else { 103 sendBinaryContent(file, resp); 104 } 105 return; 106 } 107 resp.sendError(404); 108 } 109 110 protected static void sendBinaryContent(ScriptFile file, HttpServletResponse resp) throws IOException { 111 OutputStream out = resp.getOutputStream(); 112 InputStream in = file.getInputStream(); 113 try { 114 FileUtils.copy(in, out); 115 } finally { 116 in.close(); 117 } 118 out.flush(); 119 } 120 121 protected static void sendTextContent(ScriptFile file, HttpServletResponse resp) throws IOException { 122 // Writer out = resp.getWriter(); 123 OutputStream out = resp.getOutputStream(); 124 InputStream in = file.getInputStream(); 125 try { 126 FileUtils.copy(in, out); 127 } finally { 128 in.close(); 129 } 130 out.flush(); 131 } 132 133}