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