diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2017-01-27 11:31:32 +0100 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2017-01-30 13:26:35 +0100 |
commit | 026111175ba6fd17945f0c86f7c6e907db2f32d4 (patch) | |
tree | 4c144ec5d61d90e84d4a210963c48947c17f28e5 /server | |
parent | 355b4aaa6a1d177f8c3209e95af203c0b46032fd (diff) | |
download | sonarqube-026111175ba6fd17945f0c86f7c6e907db2f32d4.tar.gz sonarqube-026111175ba6fd17945f0c86f7c6e907db2f32d4.zip |
SONAR-7300 Display a proper message when id is missing
Diffstat (limited to 'server')
-rw-r--r-- | server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java | 4 | ||||
-rw-r--r-- | server/sonar-server/src/main/java/org/sonar/server/ws/DeprecatedPropertiesWsFilter.java (renamed from server/sonar-server/src/main/java/org/sonar/server/ws/DeprecatedRestWebServiceFilter.java) | 22 | ||||
-rw-r--r-- | server/sonar-server/src/test/java/org/sonar/server/ws/DeprecatedPropertiesWsFilterTest.java (renamed from server/sonar-server/src/test/java/org/sonar/server/ws/DeprecatedRestWebServiceFilterTest.java) | 34 |
3 files changed, 48 insertions, 12 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 6c22c8f0436..895df2df58c 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 @@ -211,7 +211,7 @@ import org.sonar.server.view.index.ViewIndex; import org.sonar.server.view.index.ViewIndexDefinition; import org.sonar.server.view.index.ViewIndexer; import org.sonar.server.webhook.ws.WebhooksWsModule; -import org.sonar.server.ws.DeprecatedRestWebServiceFilter; +import org.sonar.server.ws.DeprecatedPropertiesWsFilter; import org.sonar.server.ws.WebServiceEngine; import org.sonar.server.ws.WebServiceFilter; import org.sonar.server.ws.WebServicesWs; @@ -325,7 +325,7 @@ public class PlatformLevel4 extends PlatformLevel { WebServiceEngine.class, WebServicesWs.class, WebServiceFilter.class, - DeprecatedRestWebServiceFilter.class, + DeprecatedPropertiesWsFilter.class, // localization L10nWs.class, diff --git a/server/sonar-server/src/main/java/org/sonar/server/ws/DeprecatedRestWebServiceFilter.java b/server/sonar-server/src/main/java/org/sonar/server/ws/DeprecatedPropertiesWsFilter.java index 2e54e06f42a..b940440d018 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/ws/DeprecatedRestWebServiceFilter.java +++ b/server/sonar-server/src/main/java/org/sonar/server/ws/DeprecatedPropertiesWsFilter.java @@ -40,6 +40,7 @@ import org.apache.commons.io.IOUtils; import org.sonar.api.web.ServletFilter; import org.sonar.server.property.ws.IndexAction; +import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Strings.isNullOrEmpty; import static java.nio.charset.StandardCharsets.UTF_8; import static org.sonar.server.property.ws.PropertiesWs.CONTROLLER_PROPERTIES; @@ -53,15 +54,15 @@ import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_VALUE; import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_VALUES; /** - * This filter is used to execute some deprecated Java WS, that were using REST + * This filter is used to execute deprecated api/properties WS, that were using REST */ -public class DeprecatedRestWebServiceFilter extends ServletFilter { +public class DeprecatedPropertiesWsFilter extends ServletFilter { private static final Splitter VALUE_SPLITTER = Splitter.on(",").omitEmptyStrings(); private final WebServiceEngine webServiceEngine; - public DeprecatedRestWebServiceFilter(WebServiceEngine webServiceEngine) { + public DeprecatedPropertiesWsFilter(WebServiceEngine webServiceEngine) { this.webServiceEngine = webServiceEngine; } @@ -95,7 +96,7 @@ public class DeprecatedRestWebServiceFilter extends ServletFilter { private final Response restResponse; - public RestServletRequest(HttpServletRequest request) { + RestServletRequest(HttpServletRequest request) { super(request); this.restResponse = new Response(request); } @@ -142,17 +143,19 @@ public class DeprecatedRestWebServiceFilter extends ServletFilter { void init() { String method = request.getMethod(); - Optional<String> key = getKeyOrId(); + Optional<String> id = getKeyOrId(); switch (method) { case "POST": case "PUT": - handlePutAndPost(key, getValues(), getComponent()); + failIfIdIsNull(id); + handlePutAndPost(id, getValues(), getComponent()); break; case "DELETE": - handleDelete(key, getComponent()); + failIfIdIsNull(id); + handleDelete(id, getComponent()); break; default: - handleGet(key, getComponent()); + handleGet(id, getComponent()); } } @@ -245,6 +248,9 @@ public class DeprecatedRestWebServiceFilter extends ServletFilter { value.ifPresent(s -> additionalParams.put(parameterKey, s)); } + private static void failIfIdIsNull(Optional<String> id) { + checkArgument(id.isPresent(), "The 'id' parameter is missing"); + } } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/ws/DeprecatedRestWebServiceFilterTest.java b/server/sonar-server/src/test/java/org/sonar/server/ws/DeprecatedPropertiesWsFilterTest.java index 2d88112cb84..370ac221726 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/ws/DeprecatedRestWebServiceFilterTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/ws/DeprecatedPropertiesWsFilterTest.java @@ -41,7 +41,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -public class DeprecatedRestWebServiceFilterTest { +public class DeprecatedPropertiesWsFilterTest { @Rule public ExpectedException expectedException = ExpectedException.none(); @@ -53,7 +53,7 @@ public class DeprecatedRestWebServiceFilterTest { private FilterChain chain = mock(FilterChain.class); private ArgumentCaptor<ServletRequest> servletRequestCaptor = ArgumentCaptor.forClass(ServletRequest.class); - private DeprecatedRestWebServiceFilter underTest = new DeprecatedRestWebServiceFilter(webServiceEngine); + private DeprecatedPropertiesWsFilter underTest = new DeprecatedPropertiesWsFilter(webServiceEngine); @Before public void setUp() throws Exception { @@ -264,6 +264,36 @@ public class DeprecatedRestWebServiceFilterTest { assertNoParam("value", "values"); } + @Test + public void fail_to_redirect_put_api_properties_when_no_id() throws Exception { + when(request.getRequestURI()).thenReturn("/api/properties/"); + when(request.getMethod()).thenReturn("PUT"); + + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("The 'id' parameter is missing"); + underTest.doFilter(request, response, chain); + } + + @Test + public void fail_to_redirect_post_api_properties_when_no_id() throws Exception { + when(request.getRequestURI()).thenReturn("/api/properties/"); + when(request.getMethod()).thenReturn("POST"); + + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("The 'id' parameter is missing"); + underTest.doFilter(request, response, chain); + } + + @Test + public void fail_to_redirect_delete_api_properties_when_no_id() throws Exception { + when(request.getRequestURI()).thenReturn("/api/properties"); + when(request.getMethod()).thenReturn("DELETE"); + + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("The 'id' parameter is missing"); + underTest.doFilter(request, response, chain); + } + private void assertRedirection(String path, String method) { verify(webServiceEngine).execute(servletRequestCaptor.capture(), any(ServletResponse.class)); assertThat(servletRequestCaptor.getValue().getPath()).isEqualTo(path); |