@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, "/");
+ String pluginKey = getPluginKey(request);
+ String resource = getResourcePath(request);
PluginClassLoaders pluginClassLoaders = Platform.getInstance().getContainer().getComponent(PluginClassLoaders.class);
-
ClassLoader classLoader = pluginClassLoaders.getClassLoader(pluginKey);
if (classLoader == null) {
+ LOG.error("Plugin not found: " + pluginKey);
return;
}
-
InputStream in = null;
OutputStream out = null;
try {
if (in != null) {
out = response.getOutputStream();
IOUtils.copy(in, out);
+ } else {
+ LOG.error("Unable to find resource '" + resource + "' in plugin '" + pluginKey + "'");
}
} catch (Exception e) {
LOG.error("Unable to load static resource '" + resource + "' from plugin '" + pluginKey + "'", e);
IOUtils.closeQuietly(out);
}
}
+
+ /**
+ * @return part of request URL after servlet path
+ */
+ protected String getPluginKeyAndResourcePath(HttpServletRequest request) {
+ return StringUtils.substringAfter(request.getRequestURI(), request.getContextPath() + request.getServletPath() + "/");
+ }
+
+ protected String getPluginKey(HttpServletRequest request) {
+ return StringUtils.substringBefore(getPluginKeyAndResourcePath(request), "/");
+ }
+
+ protected String getResourcePath(HttpServletRequest request) {
+ return "/static/" + StringUtils.substringAfter(getPluginKeyAndResourcePath(request), "/");
+ }
}
--- /dev/null
+package org.sonar.server.plugins;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import javax.servlet.http.HttpServletRequest;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class StaticResourcesServletTest {
+
+ private StaticResourcesServlet servlet;
+ private HttpServletRequest request;
+
+ @Before
+ public void setUp() throws Exception {
+ servlet = new StaticResourcesServlet();
+ request = mock(HttpServletRequest.class);
+ }
+
+ @Test
+ public void shouldDeterminePluginKey() {
+ when(request.getContextPath()).thenReturn("/");
+ when(request.getServletPath()).thenReturn("static");
+ when(request.getRequestURI()).thenReturn("/static/myplugin/image.png");
+ assertThat(servlet.getPluginKey(request), is("myplugin"));
+
+ when(request.getRequestURI()).thenReturn("/static/myplugin/images/image.png");
+ assertThat(servlet.getPluginKey(request), is("myplugin"));
+
+ when(request.getRequestURI()).thenReturn("/static/myplugin/");
+ assertThat(servlet.getPluginKey(request), is("myplugin"));
+ }
+
+ @Test
+ public void shouldDetermineResourcePath() {
+ when(request.getContextPath()).thenReturn("/");
+ when(request.getServletPath()).thenReturn("static");
+ when(request.getRequestURI()).thenReturn("/static/myplugin/image.png");
+ assertThat(servlet.getResourcePath(request), is("/static/image.png"));
+
+ when(request.getRequestURI()).thenReturn("/static/myplugin/images/image.png");
+ assertThat(servlet.getResourcePath(request), is("/static/images/image.png"));
+
+ when(request.getRequestURI()).thenReturn("/static/myplugin/");
+ assertThat(servlet.getResourcePath(request), is("/static/"));
+ }
+}