001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2008-2012 SonarSource 004 * mailto:contact AT sonarsource DOT com 005 * 006 * Sonar is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 3 of the License, or (at your option) any later version. 010 * 011 * Sonar is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public 017 * License along with Sonar; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 019 */ 020 package org.sonar.server.plugins; 021 022 import org.apache.commons.io.IOUtils; 023 import org.apache.commons.lang.StringUtils; 024 import org.slf4j.Logger; 025 import org.slf4j.LoggerFactory; 026 import org.sonar.server.platform.Platform; 027 028 import javax.servlet.ServletException; 029 import javax.servlet.http.HttpServlet; 030 import javax.servlet.http.HttpServletRequest; 031 import javax.servlet.http.HttpServletResponse; 032 import java.io.IOException; 033 import java.io.InputStream; 034 import java.io.OutputStream; 035 036 public class StaticResourcesServlet extends HttpServlet { 037 038 private static final Logger LOG = LoggerFactory.getLogger(StaticResourcesServlet.class); 039 private static final long serialVersionUID = -2577454614650178426L; 040 041 @Override 042 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 043 String pluginKey = getPluginKey(request); 044 String resource = getResourcePath(request); 045 046 DefaultServerPluginRepository pluginRepository = Platform.getInstance().getContainer().getComponentByType(DefaultServerPluginRepository.class); 047 ClassLoader classLoader = pluginRepository.getClassLoader(pluginKey); 048 if (classLoader == null) { 049 LOG.error("Plugin not found: " + pluginKey); 050 response.sendError(HttpServletResponse.SC_NOT_FOUND); 051 return; 052 } 053 InputStream in = null; 054 OutputStream out = null; 055 try { 056 in = classLoader.getResourceAsStream(resource); 057 if (in != null) { 058 out = response.getOutputStream(); 059 IOUtils.copy(in, out); 060 } else { 061 LOG.error("Unable to find resource '" + resource + "' in plugin '" + pluginKey + "'"); 062 response.sendError(HttpServletResponse.SC_NOT_FOUND); 063 } 064 } catch (Exception e) { 065 LOG.error("Unable to load static resource '" + resource + "' from plugin '" + pluginKey + "'", e); 066 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); 067 } finally { 068 IOUtils.closeQuietly(in); 069 IOUtils.closeQuietly(out); 070 } 071 } 072 073 /** 074 * @return part of request URL after servlet path 075 */ 076 protected String getPluginKeyAndResourcePath(HttpServletRequest request) { 077 return StringUtils.substringAfter(request.getRequestURI(), request.getContextPath() + request.getServletPath() + "/"); 078 } 079 080 protected String getPluginKey(HttpServletRequest request) { 081 return StringUtils.substringBefore(getPluginKeyAndResourcePath(request), "/"); 082 } 083 084 /** 085 * Note that returned value should not have a leading "/" - see {@link Class#resolveName(String)}. 086 */ 087 protected String getResourcePath(HttpServletRequest request) { 088 return "static/" + StringUtils.substringAfter(getPluginKeyAndResourcePath(request), "/"); 089 } 090 }