--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+package org.sonar.server.platform;
+
+import com.google.common.collect.ImmutableSet;
+import org.apache.commons.lang.StringUtils;
+import org.sonar.core.profiling.Profiling;
+import org.sonar.core.profiling.Profiling.Level;
+import org.sonar.core.profiling.StopWatch;
+
+import javax.servlet.*;
+import javax.servlet.http.HttpServletRequest;
+
+import java.io.IOException;
+import java.util.Set;
+
+/**
+ * <p>Profile HTTP requests using platform profiling utility.</p>
+ * <p>To avoid profiling of requests for static resources, the <code>staticDirs</code>
+ * filter parameter can be set in the servlet context descriptor. This parameter should
+ * contain a comma-separated list of paths, starting at the context root;
+ * requests on subpaths of these paths will not be profiled.</p>
+ * @since 4.1
+ */
+public class ProfilingFilter implements Filter {
+
+ private static final String CONFIG_SEPARATOR = ",";
+ private static final String URL_SEPARATOR = "/";
+
+ private static final String MESSAGE_WITH_QUERY = "%s %s?%s";
+ private static final String MESSAGE_WITHOUT_QUERY = "%s %s";
+
+ private String contextRoot;
+ private Set<String> staticResourceDirs;
+
+ @Override
+ public void init(FilterConfig filterConfig) throws ServletException {
+ contextRoot = filterConfig.getServletContext().getContextPath();
+
+ String staticResourcesConfig = filterConfig.getInitParameter("staticDirs");
+ if (StringUtils.isNotBlank(staticResourcesConfig)) {
+ staticResourceDirs = ImmutableSet.copyOf(staticResourcesConfig.split(CONFIG_SEPARATOR));
+ } else {
+ staticResourceDirs = ImmutableSet.of();
+ }
+ }
+
+ @Override
+ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
+ if (request instanceof HttpServletRequest) {
+ HttpServletRequest httpRequest = (HttpServletRequest) request;
+ String requestUri = httpRequest.getRequestURI();
+ String rootDir = getRootDir(requestUri);
+
+ if (staticResourceDirs.contains(rootDir)) {
+ // Static resource, not profiled
+ chain.doFilter(request, response);
+ } else {
+ StopWatch watch = getProfiling().start("http", Level.BASIC);
+ try {
+ chain.doFilter(request, response);
+ } finally {
+ String queryString = httpRequest.getQueryString();
+ watch.stop(queryString == null ? MESSAGE_WITHOUT_QUERY : MESSAGE_WITH_QUERY, httpRequest.getMethod(), requestUri, queryString);
+ }
+ }
+ } else {
+ // Not an HTTP request, not profiled
+ chain.doFilter(request, response);
+ }
+ }
+
+ private String getRootDir(String requestUri) {
+ String rootPath = "";
+ String localPath = StringUtils.substringAfter(requestUri, contextRoot);
+ if (localPath.startsWith(URL_SEPARATOR)) {
+ int secondSlash = localPath.indexOf(URL_SEPARATOR, 1);
+ if (secondSlash > 0) {
+ rootPath = URL_SEPARATOR + localPath.substring(1, secondSlash);
+ }
+ }
+ return rootPath;
+ }
+
+ @Override
+ public void destroy() {
+ // Nothing
+ }
+
+
+ Profiling getProfiling() {
+ try {
+ return (Profiling) Platform.component(Profiling.class);
+ } catch(Exception initException) {
+ return null;
+ }
+ }
+}
<filter-name>RackFilter</filter-name>
<filter-class>org.jruby.rack.RackFilter</filter-class>
</filter>
+ <filter>
+ <filter-name>ProfilingFilter</filter-name>
+ <filter-class>org.sonar.server.platform.ProfilingFilter</filter-class>
+ <init-param>
+ <param-name>staticDirs</param-name>
+ <param-value>/images,/javascripts,/stylesheets</param-value>
+ </init-param>
+ </filter>
+ <filter-mapping>
+ <filter-name>ProfilingFilter</filter-name>
+ <url-pattern>/*</url-pattern>
+ </filter-mapping>
<filter-mapping>
<filter-name>DatabaseSessionFilter</filter-name>
<url-pattern>/*</url-pattern>
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+package org.sonar.server.platform;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.sonar.api.config.Settings;
+import org.sonar.core.profiling.Profiling;
+
+import javax.servlet.*;
+import javax.servlet.http.HttpServletRequest;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.when;
+
+
+public class ProfilingFilterTest {
+
+ private ProfilingFilter filter;
+ private FilterChain chain;
+
+ @Before
+ public void initialize() throws Exception {
+
+ FilterConfig filterConfig = mock(FilterConfig.class);
+ when(filterConfig.getInitParameter("staticDirs")).thenReturn("/static,/assets");
+ ServletContext context = mock(ServletContext.class);
+ when(context.getContextPath()).thenReturn("/context");
+ when(filterConfig.getServletContext()).thenReturn(context);
+ chain = mock(FilterChain.class);
+
+ Settings settings = new Settings();
+ settings.setProperty("sonar.log.profilingLevel", "BASIC");
+ Profiling profiling = new Profiling(settings);
+
+ filter = spy(new ProfilingFilter());
+ when(filter.getProfiling()).thenReturn(profiling);
+ filter.init(filterConfig);
+ }
+
+ @Test
+ public void should_profile_service_call() throws Exception {
+ filter.doFilter(request("POST", "/context/service/call", "param=value"), null, chain);
+ }
+
+ @Test
+ public void should_profile_service() throws Exception {
+ filter.doFilter(request("POST", "/context/service", null), null, chain);
+ }
+
+ @Test
+ public void should_profile_context_root_as_slash2() throws Exception {
+ filter.doFilter(request("POST", "/context", null), null, chain);
+ }
+
+ @Test(expected = ServletException.class)
+ public void should_profile_even_when_exception() throws Exception {
+ Mockito.doThrow(new ServletException()).when(chain).doFilter(any(ServletRequest.class), any(ServletResponse.class));
+ filter.doFilter(request("POST", "/context/service/call", "param=value"), null, chain);
+ }
+
+ @Test
+ public void should_not_profile_non_http_request() throws Exception {
+ filter.doFilter(mock(ServletRequest.class), null, chain);
+ }
+
+ @Test
+ public void should_not_profile_static_resource() throws Exception {
+ filter.doFilter(request("GET", "/context/static/image.png", null), null, chain);
+ }
+
+ @Test
+ public void should_profile_static_resource_if_no_config() throws Exception {
+ FilterConfig filterConfig = mock(FilterConfig.class);
+ ServletContext context = mock(ServletContext.class);
+ when(context.getContextPath()).thenReturn("/context");
+ when(filterConfig.getServletContext()).thenReturn(context);
+
+ filter.init(filterConfig);
+ filter.doFilter(request("GET", "/context/static/image.png", null), null, chain);
+ }
+
+ private HttpServletRequest request(String method, String path, String query) {
+ HttpServletRequest request = mock(HttpServletRequest.class);
+ when(request.getMethod()).thenReturn(method);
+ when(request.getRequestURI()).thenReturn(path);
+ when(request.getQueryString()).thenReturn(query);
+ return request;
+ }
+}