diff options
author | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2017-02-22 16:15:28 +0100 |
---|---|---|
committer | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2017-02-22 17:53:38 +0100 |
commit | b3873be6278543d72f9a5eb6d40083256b52e5e5 (patch) | |
tree | 6690b93d99823f85eba6c4b3cd7f564716232dd5 /server | |
parent | 5b368d3853dbdabe21fc93b3831516b108e7c41a (diff) | |
download | sonarqube-b3873be6278543d72f9a5eb6d40083256b52e5e5.tar.gz sonarqube-b3873be6278543d72f9a5eb6d40083256b52e5e5.zip |
SONAR-8773 Move WS Reroute WS update_key and bulk_update_key to api/projects
Diffstat (limited to 'server')
5 files changed, 211 insertions, 27 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java index ca54bd9a4e5..52238ccbcfa 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java @@ -214,6 +214,7 @@ import org.sonar.server.webhook.ws.WebhooksWsModule; import org.sonar.server.ws.DeprecatedPropertiesWsFilter; import org.sonar.server.ws.WebServiceEngine; import org.sonar.server.ws.WebServiceFilter; +import org.sonar.server.ws.WebServiceReroutingFilter; import org.sonar.server.ws.ws.WebServicesWsModule; public class PlatformLevel4 extends PlatformLevel { @@ -326,6 +327,7 @@ public class PlatformLevel4 extends PlatformLevel { WebServicesWsModule.class, WebServiceFilter.class, DeprecatedPropertiesWsFilter.class, + WebServiceReroutingFilter.class, // localization L10nWs.class, diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/web/RedirectFilter.java b/server/sonar-server/src/main/java/org/sonar/server/platform/web/RedirectFilter.java index e580f08f288..12220c383cd 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/web/RedirectFilter.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/web/RedirectFilter.java @@ -41,7 +41,7 @@ public class RedirectFilter implements Filter { private static final String EMPTY = ""; private static final List<Redirect> REDIRECTS = ImmutableList.of( - new WebServiceListRedirect(), + newSimpleRedirect("/api", "/api/webservices/list"), new BatchRedirect(), new BatchBootstrapRedirect(), new ProfilesExportRedirect()); @@ -68,6 +68,20 @@ public class RedirectFilter implements Filter { } } + public static Redirect newSimpleRedirect(String from, String to) { + return new Redirect() { + @Override + public boolean test(String path) { + return from.equals(path); + } + + @Override + public String apply(HttpServletRequest request) { + return format("%s%s", request.getContextPath(), to); + } + }; + } + @Override public void init(FilterConfig filterConfig) throws ServletException { // Nothing @@ -86,19 +100,6 @@ public class RedirectFilter implements Filter { String apply(HttpServletRequest request); } - private static class WebServiceListRedirect implements Redirect { - - @Override - public boolean test(String path) { - return "/api".equals(path); - } - - @Override - public String apply(HttpServletRequest request) { - return format("%s/api/webservices/list", request.getContextPath()); - } - } - /** * Old scanners were using /batch/file.jar url (see SCANNERAPI-167) */ diff --git a/server/sonar-server/src/main/java/org/sonar/server/ws/WebServiceFilter.java b/server/sonar-server/src/main/java/org/sonar/server/ws/WebServiceFilter.java index 59860fe0530..6a1bd19f46b 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/ws/WebServiceFilter.java +++ b/server/sonar-server/src/main/java/org/sonar/server/ws/WebServiceFilter.java @@ -32,7 +32,9 @@ import org.sonar.api.server.ws.WebService; import org.sonar.api.web.ServletFilter; import org.sonar.core.util.stream.Collectors; +import static java.util.stream.Stream.concat; import static org.sonar.server.property.ws.PropertiesWs.CONTROLLER_PROPERTIES; +import static org.sonar.server.ws.WebServiceReroutingFilter.MOVED_WEB_SERVICES; /** * This filter is used to execute Web Services. @@ -51,21 +53,20 @@ public class WebServiceFilter extends ServletFilter { public WebServiceFilter(WebServiceEngine webServiceEngine) { this.webServiceEngine = webServiceEngine; - this.includeUrls = Stream.concat( + this.includeUrls = concat( Stream.of("/api/*"), - webServiceEngine.controllers() - .stream() - .flatMap(controller -> controller.actions().stream() - .map(toPath()))) - .collect(Collectors.toSet()); - this.excludeUrls = Stream.concat( + webServiceEngine.controllers().stream() + .flatMap(controller -> controller.actions().stream()) + .map(toPath())) + .collect(Collectors.toSet()); + this.excludeUrls = concat(concat( Stream.of("/" + CONTROLLER_PROPERTIES + "*"), - webServiceEngine.controllers() - .stream() - .flatMap(controller -> controller.actions().stream() - .filter(action -> action.handler() instanceof ServletFilterHandler) - .map(toPath()))) - .collect(Collectors.toSet()); + MOVED_WEB_SERVICES.stream()), + webServiceEngine.controllers().stream() + .flatMap(controller -> controller.actions().stream()) + .filter(action -> action.handler() instanceof ServletFilterHandler) + .map(toPath())) + .collect(Collectors.toSet()); } @Override diff --git a/server/sonar-server/src/main/java/org/sonar/server/ws/WebServiceReroutingFilter.java b/server/sonar-server/src/main/java/org/sonar/server/ws/WebServiceReroutingFilter.java new file mode 100644 index 00000000000..1231a891c87 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/ws/WebServiceReroutingFilter.java @@ -0,0 +1,90 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program 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. + * + * This program 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.ws; + +import com.google.common.collect.ImmutableMap; +import java.io.IOException; +import java.util.Map; +import java.util.Set; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import org.sonar.api.web.ServletFilter; + +/** + * This filter is used to execute renamed/moved web services + */ +public class WebServiceReroutingFilter extends ServletFilter { + + private static final Map<String, String> REDIRECTS = ImmutableMap.<String, String>builder() + .put("/api/components/bulk_update_key", "/api/projects/bulk_update_key") + .put("/api/components/update_key", "/api/projects/update_key") + .build(); + static final Set<String> MOVED_WEB_SERVICES = REDIRECTS.keySet(); + + private final WebServiceEngine webServiceEngine; + + public WebServiceReroutingFilter(WebServiceEngine webServiceEngine) { + this.webServiceEngine = webServiceEngine; + } + + @Override + public UrlPattern doGetPattern() { + return UrlPattern.builder() + .includes(MOVED_WEB_SERVICES) + .build(); + } + + @Override + public void doFilter(javax.servlet.ServletRequest servletRequest, javax.servlet.ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException { + HttpServletRequest httpRequest = (HttpServletRequest) servletRequest; + RedirectionRequest wsRequest = new RedirectionRequest(httpRequest); + ServletResponse wsResponse = new ServletResponse((HttpServletResponse) servletResponse); + + webServiceEngine.execute(wsRequest, wsResponse); + } + + @Override + public void init(FilterConfig filterConfig) throws ServletException { + // Nothing to do + } + + @Override + public void destroy() { + // Nothing to do + } + + private static class RedirectionRequest extends ServletRequest { + private final String redirectedPath; + + public RedirectionRequest(HttpServletRequest source) { + super(source); + this.redirectedPath = REDIRECTS.getOrDefault(source.getServletPath(), source.getServletPath()); + } + + @Override + public String getPath() { + return redirectedPath; + } + } +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/ws/WebServiceReroutingFilterTest.java b/server/sonar-server/src/test/java/org/sonar/server/ws/WebServiceReroutingFilterTest.java new file mode 100644 index 00000000000..2288ce2b398 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/ws/WebServiceReroutingFilterTest.java @@ -0,0 +1,90 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program 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. + * + * This program 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.ws; + +import java.io.IOException; +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.mockito.ArgumentCaptor; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +public class WebServiceReroutingFilterTest { + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + private WebServiceEngine webServiceEngine = mock(WebServiceEngine.class); + + private HttpServletRequest request = mock(HttpServletRequest.class); + private HttpServletResponse response = mock(HttpServletResponse.class); + private FilterChain chain = mock(FilterChain.class); + private ArgumentCaptor<ServletRequest> servletRequestCaptor = ArgumentCaptor.forClass(ServletRequest.class); + + private WebServiceReroutingFilter underTest = new WebServiceReroutingFilter(webServiceEngine); + + @Before + public void setUp() throws Exception { + when(request.getContextPath()).thenReturn("/sonarqube"); + } + + @Test + public void do_get_pattern() throws Exception { + assertThat(underTest.doGetPattern().matches("/api/components/update_key")).isTrue(); + assertThat(underTest.doGetPattern().matches("/api/components/bulk_update_key")).isTrue(); + assertThat(underTest.doGetPattern().matches("/api/projects/update_key")).isFalse(); + } + + @Test + public void redirect_components_update_key() throws Exception { + when(request.getServletPath()).thenReturn("/api/components/update_key"); + when(request.getMethod()).thenReturn("POST"); + + underTest.doFilter(request, response, chain); + + assertRedirection("/api/projects/update_key", "POST"); + } + + @Test + public void redirect_components_bulk_update_key() throws IOException, ServletException { + when(request.getServletPath()).thenReturn("/api/components/bulk_update_key"); + when(request.getMethod()).thenReturn("POST"); + + underTest.doFilter(request, response, chain); + + assertRedirection("/api/projects/bulk_update_key", "POST"); + } + + private void assertRedirection(String path, String method) { + verify(webServiceEngine).execute(servletRequestCaptor.capture(), any(ServletResponse.class)); + assertThat(servletRequestCaptor.getValue().getPath()).isEqualTo(path); + assertThat(servletRequestCaptor.getValue().method()).isEqualTo(method); + } +} |