From 2dc85f396a6e04650ffa909f55aa6d34173b5eb1 Mon Sep 17 00:00:00 2001 From: Godin Date: Sat, 25 Sep 2010 01:14:53 +0000 Subject: [PATCH] SONAR-1709: Add StaticResourcesServlet for loading static resources from plugins --- sonar-server/src/dev/web.xml | 9 ++++ .../plugins/StaticResourcesServlet.java | 51 +++++++++++++++++++ .../WEB-INF/app/helpers/application_helper.rb | 6 +-- sonar-server/src/main/webapp/WEB-INF/web.xml | 11 +++- 4 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 sonar-server/src/main/java/org/sonar/server/plugins/StaticResourcesServlet.java diff --git a/sonar-server/src/dev/web.xml b/sonar-server/src/dev/web.xml index afd470ea8e0..5943fb50f09 100644 --- a/sonar-server/src/dev/web.xml +++ b/sonar-server/src/dev/web.xml @@ -53,6 +53,11 @@ gchart org.jfree.eastwood.ChartServlet + + static + org.sonar.server.plugins.StaticResourcesServlet + + chart /chart @@ -61,6 +66,10 @@ gchart /gchart + + static + /static/* + org.sonar.server.platform.PlatformLifecycleListener diff --git a/sonar-server/src/main/java/org/sonar/server/plugins/StaticResourcesServlet.java b/sonar-server/src/main/java/org/sonar/server/plugins/StaticResourcesServlet.java new file mode 100644 index 00000000000..4864f895e49 --- /dev/null +++ b/sonar-server/src/main/java/org/sonar/server/plugins/StaticResourcesServlet.java @@ -0,0 +1,51 @@ +package org.sonar.server.plugins; + +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.sonar.server.platform.Platform; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public class StaticResourcesServlet extends HttpServlet { + + private static final Logger LOG = LoggerFactory.getLogger(StaticResourcesServlet.class); + + @Override + public void doGet(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + String path = StringUtils.substringAfter(request.getRequestURI(), request.getContextPath() + request.getServletPath() + "/"); + String pluginKey = StringUtils.substringBefore(path, "/"); + String resource = "/static/" + StringUtils.substringAfter(path, "/"); + + PluginClassLoaders pluginClassLoaders = Platform.getInstance().getContainer().getComponent(PluginClassLoaders.class); + + ClassLoader classLoader = pluginClassLoaders.getClassLoader(pluginKey); + if (classLoader == null) { + return; + } + + InputStream in = null; + OutputStream out = null; + try { + in = classLoader.getResourceAsStream(resource); + if (in != null) { + out = response.getOutputStream(); + IOUtils.copy(in, out); + } + } catch (Exception e) { + LOG.error("Unable to load static resource '" + resource + "' from plugin '" + pluginKey + "'", e); + } finally { + IOUtils.closeQuietly(in); + IOUtils.closeQuietly(out); + } + } +} diff --git a/sonar-server/src/main/webapp/WEB-INF/app/helpers/application_helper.rb b/sonar-server/src/main/webapp/WEB-INF/app/helpers/application_helper.rb index 92ac5eabba9..f8997db4be1 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/helpers/application_helper.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/helpers/application_helper.rb @@ -162,9 +162,9 @@ module ApplicationHelper if options[:resource] "#{ApplicationController.root_context}/plugins/resource/#{options[:resource]}?page=#{page}" elsif @project - "#{ApplicationController.root_context}/plugins/resource/#{@project.id}?page=#{page}" - else - '' + "#{ApplicationController.root_context}/plugins/resource/#{@project.id}?page=#{page}" + else + '' end end diff --git a/sonar-server/src/main/webapp/WEB-INF/web.xml b/sonar-server/src/main/webapp/WEB-INF/web.xml index 58c1f1a825b..99d68dcc5f6 100644 --- a/sonar-server/src/main/webapp/WEB-INF/web.xml +++ b/sonar-server/src/main/webapp/WEB-INF/web.xml @@ -74,13 +74,22 @@ gchart org.jfree.eastwood.ChartServlet + + static + org.sonar.server.plugins.StaticResourcesServlet + + chart /chart gchart - /gchart + /gchart + + + static + /static/* -- 2.39.5