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;
WebServiceEngine.class,
WebServicesWs.class,
WebServiceFilter.class,
- DeprecatedRestWebServiceFilter.class,
+ DeprecatedPropertiesWsFilter.class,
// localization
L10nWs.class,
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.base.Splitter;
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.Multimap;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletInputStream;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+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;
+import static org.sonarqube.ws.client.setting.SettingsWsParameters.ACTION_RESET;
+import static org.sonarqube.ws.client.setting.SettingsWsParameters.ACTION_SET;
+import static org.sonarqube.ws.client.setting.SettingsWsParameters.CONTROLLER_SETTINGS;
+import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_COMPONENT;
+import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_KEY;
+import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_KEYS;
+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 deprecated api/properties WS, that were using REST
+ */
+public class DeprecatedPropertiesWsFilter extends ServletFilter {
+
+ private static final Splitter VALUE_SPLITTER = Splitter.on(",").omitEmptyStrings();
+
+ private final WebServiceEngine webServiceEngine;
+
+ public DeprecatedPropertiesWsFilter(WebServiceEngine webServiceEngine) {
+ this.webServiceEngine = webServiceEngine;
+ }
+
+ @Override
+ public UrlPattern doGetPattern() {
+ return UrlPattern.builder()
+ .includes("/" + CONTROLLER_PROPERTIES + "/*")
+ .build();
+ }
+
+ @Override
+ public void doFilter(javax.servlet.ServletRequest servletRequest, javax.servlet.ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
+ HttpServletRequest request = (HttpServletRequest) servletRequest;
+ HttpServletResponse response = (HttpServletResponse) servletResponse;
+ RestServletRequest wsRequest = new RestServletRequest(request);
+ ServletResponse wsResponse = new ServletResponse(response);
+ webServiceEngine.execute(wsRequest, wsResponse);
+ }
+
+ @Override
+ public void init(FilterConfig filterConfig) throws ServletException {
+ // Nothing to do
+ }
+
+ @Override
+ public void destroy() {
+ // Nothing to do
+ }
+
+ static class RestServletRequest extends org.sonar.server.ws.ServletRequest {
+
+ private final Response restResponse;
+
+ RestServletRequest(HttpServletRequest request) {
+ super(request);
+ this.restResponse = new Response(request);
+ }
+
+ @Override
+ public String getPath() {
+ return restResponse.redirectedPath;
+ }
+
+ @Override
+ public boolean hasParam(String key) {
+ return restResponse.additionalParams.containsKey(key) || restResponse.additionalMultiParams.containsKey(key);
+ }
+
+ @Override
+ protected String readParam(String key) {
+ return restResponse.additionalParams.get(key);
+ }
+
+ @Override
+ protected List<String> readMultiParam(String key) {
+ return new ArrayList<>(restResponse.additionalMultiParams.get(key));
+ }
+
+ @Override
+ public String method() {
+ return restResponse.redirectedMethod;
+ }
+ }
+
+ private static class Response {
+ private final HttpServletRequest request;
+ private final Map<String, String> additionalParams = new HashMap<>();
+ private final Multimap<String, String> additionalMultiParams = ArrayListMultimap.create();
+ private final String originalPath;
+ private String redirectedPath;
+ private String redirectedMethod;
+
+ Response(HttpServletRequest request) {
+ this.request = request;
+ this.originalPath = request.getRequestURI().replaceFirst(request.getContextPath(), "");
+ init();
+ }
+
+ void init() {
+ String method = request.getMethod();
+ Optional<String> id = getKeyOrId();
+ switch (method) {
+ case "POST":
+ case "PUT":
+ failIfIdIsNull(id);
+ handlePutAndPost(id, getValues(), getComponent());
+ break;
+ case "DELETE":
+ failIfIdIsNull(id);
+ handleDelete(id, getComponent());
+ break;
+ default:
+ handleGet(id, getComponent());
+ }
+ }
+
+ private void handlePutAndPost(Optional<String> key, List<String> values, Optional<String> component) {
+ if (values.isEmpty()) {
+ redirectToReset(key, component);
+ } else {
+ redirectToSet(key, values, component);
+ }
+ }
+
+ private void handleDelete(Optional<String> key, Optional<String> component) {
+ redirectToReset(key, component);
+ }
+
+ private void handleGet(Optional<String> key, Optional<String> component) {
+ addParameterIfPresent(IndexAction.PARAM_ID, key);
+ addParameterIfPresent(IndexAction.PARAM_COMPONENT, component);
+ addParameterIfPresent(IndexAction.PARAM_FORMAT, readParam(IndexAction.PARAM_FORMAT));
+ redirectedPath = CONTROLLER_PROPERTIES + "/index";
+ redirectedMethod = "GET";
+ }
+
+ private Optional<String> getKeyOrId() {
+ Optional<String> key = getKey();
+ return key.isPresent() ? key : readParam("id");
+ }
+
+ private Optional<String> getKey() {
+ if (originalPath.equals("/" + CONTROLLER_PROPERTIES)) {
+ return Optional.empty();
+ }
+ String key = originalPath.replace("/" + CONTROLLER_PROPERTIES + "/", "");
+ return key.isEmpty() ? Optional.empty() : Optional.of(key);
+ }
+
+ private Optional<String> getComponent() {
+ return readParam(IndexAction.PARAM_COMPONENT);
+ }
+
+ private List<String> getValues() {
+ Optional<String> value = readParam("value");
+ if (!value.isPresent()) {
+ List<String> values = new ArrayList<>();
+ readBody().ifPresent(values::add);
+ return values;
+ }
+ return VALUE_SPLITTER.splitToList(value.get());
+ }
+
+ private Optional<String> readParam(String paramKey) {
+ String paramValue = request.getParameter(paramKey);
+ return isNullOrEmpty(paramValue) ? Optional.empty() : Optional.of(paramValue);
+ }
+
+ private Optional<String> readBody() {
+ StringWriter writer = new StringWriter();
+ try {
+ ServletInputStream inputStream = request.getInputStream();
+ if (inputStream == null) {
+ return Optional.empty();
+ }
+ IOUtils.copy(inputStream, writer, UTF_8.name());
+ return Optional.of(writer.toString());
+ } catch (IOException e) {
+ throw new IllegalStateException(e);
+ }
+ }
+
+ private void redirectToSet(Optional<String> key, List<String> values, Optional<String> component) {
+ addParameterIfPresent(PARAM_KEY, key);
+ if (values.size() == 1) {
+ additionalParams.put(PARAM_VALUE, values.get(0));
+ } else {
+ additionalMultiParams.putAll(PARAM_VALUES, values);
+ }
+ addParameterIfPresent(PARAM_COMPONENT, component);
+ redirectedPath = CONTROLLER_SETTINGS + "/" + ACTION_SET;
+ redirectedMethod = "POST";
+ }
+
+ private void redirectToReset(Optional<String> key, Optional<String> component) {
+ addParameterIfPresent(PARAM_KEYS, key);
+ addParameterIfPresent(PARAM_COMPONENT, component);
+ redirectedPath = CONTROLLER_SETTINGS + "/" + ACTION_RESET;
+ redirectedMethod = "POST";
+ }
+
+ private void addParameterIfPresent(String parameterKey, Optional<String> value) {
+ value.ifPresent(s -> additionalParams.put(parameterKey, s));
+ }
+
+ private static void failIfIdIsNull(Optional<String> id) {
+ checkArgument(id.isPresent(), "The 'id' parameter is missing");
+ }
+ }
+
+}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact 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.base.Splitter;
-import com.google.common.collect.ArrayListMultimap;
-import com.google.common.collect.Multimap;
-import java.io.IOException;
-import java.io.StringWriter;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Optional;
-import javax.servlet.FilterChain;
-import javax.servlet.FilterConfig;
-import javax.servlet.ServletException;
-import javax.servlet.ServletInputStream;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-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.Strings.isNullOrEmpty;
-import static java.nio.charset.StandardCharsets.UTF_8;
-import static org.sonar.server.property.ws.PropertiesWs.CONTROLLER_PROPERTIES;
-import static org.sonarqube.ws.client.setting.SettingsWsParameters.ACTION_RESET;
-import static org.sonarqube.ws.client.setting.SettingsWsParameters.ACTION_SET;
-import static org.sonarqube.ws.client.setting.SettingsWsParameters.CONTROLLER_SETTINGS;
-import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_COMPONENT;
-import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_KEY;
-import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_KEYS;
-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
- */
-public class DeprecatedRestWebServiceFilter extends ServletFilter {
-
- private static final Splitter VALUE_SPLITTER = Splitter.on(",").omitEmptyStrings();
-
- private final WebServiceEngine webServiceEngine;
-
- public DeprecatedRestWebServiceFilter(WebServiceEngine webServiceEngine) {
- this.webServiceEngine = webServiceEngine;
- }
-
- @Override
- public UrlPattern doGetPattern() {
- return UrlPattern.builder()
- .includes("/" + CONTROLLER_PROPERTIES + "/*")
- .build();
- }
-
- @Override
- public void doFilter(javax.servlet.ServletRequest servletRequest, javax.servlet.ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
- HttpServletRequest request = (HttpServletRequest) servletRequest;
- HttpServletResponse response = (HttpServletResponse) servletResponse;
- RestServletRequest wsRequest = new RestServletRequest(request);
- ServletResponse wsResponse = new ServletResponse(response);
- webServiceEngine.execute(wsRequest, wsResponse);
- }
-
- @Override
- public void init(FilterConfig filterConfig) throws ServletException {
- // Nothing to do
- }
-
- @Override
- public void destroy() {
- // Nothing to do
- }
-
- static class RestServletRequest extends org.sonar.server.ws.ServletRequest {
-
- private final Response restResponse;
-
- public RestServletRequest(HttpServletRequest request) {
- super(request);
- this.restResponse = new Response(request);
- }
-
- @Override
- public String getPath() {
- return restResponse.redirectedPath;
- }
-
- @Override
- public boolean hasParam(String key) {
- return restResponse.additionalParams.containsKey(key) || restResponse.additionalMultiParams.containsKey(key);
- }
-
- @Override
- protected String readParam(String key) {
- return restResponse.additionalParams.get(key);
- }
-
- @Override
- protected List<String> readMultiParam(String key) {
- return new ArrayList<>(restResponse.additionalMultiParams.get(key));
- }
-
- @Override
- public String method() {
- return restResponse.redirectedMethod;
- }
- }
-
- private static class Response {
- private final HttpServletRequest request;
- private final Map<String, String> additionalParams = new HashMap<>();
- private final Multimap<String, String> additionalMultiParams = ArrayListMultimap.create();
- private final String originalPath;
- private String redirectedPath;
- private String redirectedMethod;
-
- Response(HttpServletRequest request) {
- this.request = request;
- this.originalPath = request.getRequestURI().replaceFirst(request.getContextPath(), "");
- init();
- }
-
- void init() {
- String method = request.getMethod();
- Optional<String> key = getKeyOrId();
- switch (method) {
- case "POST":
- case "PUT":
- handlePutAndPost(key, getValues(), getComponent());
- break;
- case "DELETE":
- handleDelete(key, getComponent());
- break;
- default:
- handleGet(key, getComponent());
- }
- }
-
- private void handlePutAndPost(Optional<String> key, List<String> values, Optional<String> component) {
- if (values.isEmpty()) {
- redirectToReset(key, component);
- } else {
- redirectToSet(key, values, component);
- }
- }
-
- private void handleDelete(Optional<String> key, Optional<String> component) {
- redirectToReset(key, component);
- }
-
- private void handleGet(Optional<String> key, Optional<String> component) {
- addParameterIfPresent(IndexAction.PARAM_ID, key);
- addParameterIfPresent(IndexAction.PARAM_COMPONENT, component);
- addParameterIfPresent(IndexAction.PARAM_FORMAT, readParam(IndexAction.PARAM_FORMAT));
- redirectedPath = CONTROLLER_PROPERTIES + "/index";
- redirectedMethod = "GET";
- }
-
- private Optional<String> getKeyOrId() {
- Optional<String> key = getKey();
- return key.isPresent() ? key : readParam("id");
- }
-
- private Optional<String> getKey() {
- if (originalPath.equals("/" + CONTROLLER_PROPERTIES)) {
- return Optional.empty();
- }
- String key = originalPath.replace("/" + CONTROLLER_PROPERTIES + "/", "");
- return key.isEmpty() ? Optional.empty() : Optional.of(key);
- }
-
- private Optional<String> getComponent() {
- return readParam(IndexAction.PARAM_COMPONENT);
- }
-
- private List<String> getValues() {
- Optional<String> value = readParam("value");
- if (!value.isPresent()) {
- List<String> values = new ArrayList<>();
- readBody().ifPresent(values::add);
- return values;
- }
- return VALUE_SPLITTER.splitToList(value.get());
- }
-
- private Optional<String> readParam(String paramKey) {
- String paramValue = request.getParameter(paramKey);
- return isNullOrEmpty(paramValue) ? Optional.empty() : Optional.of(paramValue);
- }
-
- private Optional<String> readBody() {
- StringWriter writer = new StringWriter();
- try {
- ServletInputStream inputStream = request.getInputStream();
- if (inputStream == null) {
- return Optional.empty();
- }
- IOUtils.copy(inputStream, writer, UTF_8.name());
- return Optional.of(writer.toString());
- } catch (IOException e) {
- throw new IllegalStateException(e);
- }
- }
-
- private void redirectToSet(Optional<String> key, List<String> values, Optional<String> component) {
- addParameterIfPresent(PARAM_KEY, key);
- if (values.size() == 1) {
- additionalParams.put(PARAM_VALUE, values.get(0));
- } else {
- additionalMultiParams.putAll(PARAM_VALUES, values);
- }
- addParameterIfPresent(PARAM_COMPONENT, component);
- redirectedPath = CONTROLLER_SETTINGS + "/" + ACTION_SET;
- redirectedMethod = "POST";
- }
-
- private void redirectToReset(Optional<String> key, Optional<String> component) {
- addParameterIfPresent(PARAM_KEYS, key);
- addParameterIfPresent(PARAM_COMPONENT, component);
- redirectedPath = CONTROLLER_SETTINGS + "/" + ACTION_RESET;
- redirectedMethod = "POST";
- }
-
- private void addParameterIfPresent(String parameterKey, Optional<String> value) {
- value.ifPresent(s -> additionalParams.put(parameterKey, s));
- }
-
- }
-
-}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.ByteArrayInputStream;
+import java.io.IOException;
+import java.util.Arrays;
+import javax.servlet.FilterChain;
+import javax.servlet.ReadListener;
+import javax.servlet.ServletInputStream;
+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 java.nio.charset.StandardCharsets.UTF_8;
+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 DeprecatedPropertiesWsFilterTest {
+
+ @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 DeprecatedPropertiesWsFilter underTest = new DeprecatedPropertiesWsFilter(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/properties")).isTrue();
+ assertThat(underTest.doGetPattern().matches("/api/properties/")).isTrue();
+ assertThat(underTest.doGetPattern().matches("/api/properties/my.property")).isTrue();
+ assertThat(underTest.doGetPattern().matches("/api/properties/my_property")).isTrue();
+
+ assertThat(underTest.doGetPattern().matches("/api/issues/search")).isFalse();
+ assertThat(underTest.doGetPattern().matches("/batch/index")).isFalse();
+ assertThat(underTest.doGetPattern().matches("/foo")).isFalse();
+ }
+
+ @Test
+ public void redirect_api_properties_to_api_properties_index() throws Exception {
+ when(request.getRequestURI()).thenReturn("/api/properties");
+ when(request.getMethod()).thenReturn("GET");
+
+ underTest.doFilter(request, response, chain);
+
+ assertRedirection("api/properties/index", "GET");
+ assertNoParam("key", "component", "value", "values");
+ }
+
+ @Test
+ public void redirect_api_properties_to_api_properties_index_when_no_property() throws Exception {
+ when(request.getRequestURI()).thenReturn("/api/properties/");
+ when(request.getMethod()).thenReturn("GET");
+
+ underTest.doFilter(request, response, chain);
+
+ assertRedirection("api/properties/index", "GET");
+ assertNoParam("key", "component", "value", "values");
+ }
+
+ @Test
+ public void redirect_api_properties_with_property_key() throws Exception {
+ when(request.getRequestURI()).thenReturn("/api/properties/my.property");
+ when(request.getMethod()).thenReturn("GET");
+
+ underTest.doFilter(request, response, chain);
+
+ assertRedirection("api/properties/index", "GET");
+ assertParam("id", "my.property");
+ assertNoParam("component", "value", "values");
+ }
+
+ @Test
+ public void redirect_api_properties_with_property_id() throws Exception {
+ when(request.getRequestURI()).thenReturn("/api/properties");
+ when(request.getParameter("id")).thenReturn("my.property");
+ when(request.getMethod()).thenReturn("GET");
+
+ underTest.doFilter(request, response, chain);
+
+ assertRedirection("api/properties/index", "GET");
+ assertParam("id", "my.property");
+ assertNoParam("component", "value", "values");
+ }
+
+ @Test
+ public void redirect_api_properties_when_url_ands_with_slash() throws Exception {
+ when(request.getRequestURI()).thenReturn("/api/properties/");
+ when(request.getParameter("id")).thenReturn("my.property");
+ when(request.getMethod()).thenReturn("GET");
+
+ underTest.doFilter(request, response, chain);
+
+ assertRedirection("api/properties/index", "GET");
+ assertParam("id", "my.property");
+ assertNoParam("component", "value", "values");
+ }
+
+ @Test
+ public void redirect_api_properties_when_resource() throws Exception {
+ when(request.getRequestURI()).thenReturn("/api/properties/my.property");
+ when(request.getParameter("resource")).thenReturn("my_project");
+ when(request.getMethod()).thenReturn("GET");
+
+ underTest.doFilter(request, response, chain);
+
+ assertRedirection("api/properties/index", "GET");
+ assertParam("id", "my.property");
+ assertParam("resource", "my_project");
+ assertNoParam("component", "value", "values");
+ }
+
+ @Test
+ public void redirect_api_properties_when_format() throws Exception {
+ when(request.getRequestURI()).thenReturn("/api/properties/my.property");
+ when(request.getParameter("format")).thenReturn("json");
+ when(request.getMethod()).thenReturn("GET");
+
+ underTest.doFilter(request, response, chain);
+
+ assertRedirection("api/properties/index", "GET");
+ assertParam("id", "my.property");
+ assertParam("format", "json");
+ assertNoParam("component", "value", "values");
+ }
+
+ @Test
+ public void redirect_put_api_properties_to_api_settings_set() throws Exception {
+ when(request.getRequestURI()).thenReturn("/api/properties/my.property");
+ when(request.getParameter("value")).thenReturn("my_value");
+ when(request.getParameter("resource")).thenReturn("my_project");
+ when(request.getMethod()).thenReturn("PUT");
+
+ underTest.doFilter(request, response, chain);
+
+ assertRedirection("api/settings/set", "POST");
+ assertParam("key", "my.property");
+ assertParam("value", "my_value");
+ assertParam("component", "my_project");
+ assertNoParam("values");
+ }
+
+ @Test
+ public void redirect_post_api_properties_to_api_settings_set() throws Exception {
+ when(request.getRequestURI()).thenReturn("/api/properties/my.property");
+ when(request.getParameter("value")).thenReturn("my_value");
+ when(request.getParameter("resource")).thenReturn("my_project");
+ when(request.getMethod()).thenReturn("POST");
+
+ underTest.doFilter(request, response, chain);
+
+ assertRedirection("api/settings/set", "POST");
+ assertParam("key", "my.property");
+ assertParam("value", "my_value");
+ assertParam("component", "my_project");
+ assertNoParam("values");
+ }
+
+ @Test
+ public void redirect_post_api_properties_to_api_settings_set_when_value_is_in_body() throws Exception {
+ when(request.getRequestURI()).thenReturn("/api/properties/my.property");
+ when(request.getMethod()).thenReturn("POST");
+ when(request.getInputStream()).thenReturn(new TestInputStream("my_value"));
+
+ underTest.doFilter(request, response, chain);
+
+ assertRedirection("api/settings/set", "POST");
+ assertParam("key", "my.property");
+ assertParam("value", "my_value");
+ assertNoParam("values", "component");
+ }
+
+ @Test
+ public void redirect_post_api_properties_to_api_settings_set_when_multi_values() throws Exception {
+ when(request.getRequestURI()).thenReturn("/api/properties/my.property");
+ when(request.getParameter("value")).thenReturn("value1,value2,value3");
+ when(request.getMethod()).thenReturn("POST");
+
+ underTest.doFilter(request, response, chain);
+
+ assertRedirection("api/settings/set", "POST");
+ assertParam("key", "my.property");
+ assertNoParam("value");
+ assertThat(servletRequestCaptor.getValue().hasParam("values")).as("Parameter '%s' hasn't been found", "values").isTrue();
+ assertThat(servletRequestCaptor.getValue().readMultiParam("values")).containsOnly("value1", "value2", "value3");
+ }
+
+ @Test
+ public void redirect_post_api_properties_to_api_settings_reset_when_no_value() throws Exception {
+ when(request.getRequestURI()).thenReturn("/api/properties/my.property");
+ when(request.getParameter("resource")).thenReturn("my_project");
+ when(request.getMethod()).thenReturn("POST");
+
+ underTest.doFilter(request, response, chain);
+
+ assertRedirection("api/settings/reset", "POST");
+ assertParam("keys", "my.property");
+ assertParam("component", "my_project");
+ assertNoParam("value", "values");
+ }
+
+ @Test
+ public void redirect_post_api_properties_to_api_settings_reset_when_empty_value() throws Exception {
+ when(request.getRequestURI()).thenReturn("/api/properties/my.property");
+ when(request.getParameter("value")).thenReturn("");
+ when(request.getParameter("resource")).thenReturn("my_project");
+ when(request.getMethod()).thenReturn("POST");
+
+ underTest.doFilter(request, response, chain);
+
+ assertRedirection("api/settings/reset", "POST");
+ assertParam("keys", "my.property");
+ assertParam("component", "my_project");
+ assertNoParam("value", "values");
+ }
+
+ @Test
+ public void redirect_delete_api_properties_to_api_settings_reset() throws Exception {
+ when(request.getRequestURI()).thenReturn("/api/properties/my.property");
+ when(request.getParameter("resource")).thenReturn("my_project");
+ when(request.getMethod()).thenReturn("DELETE");
+
+ underTest.doFilter(request, response, chain);
+
+ assertRedirection("api/settings/reset", "POST");
+ assertParam("keys", "my.property");
+ assertParam("component", "my_project");
+ 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);
+ assertThat(servletRequestCaptor.getValue().method()).isEqualTo(method);
+ }
+
+ private void assertParam(String key, String value) {
+ assertThat(servletRequestCaptor.getValue().hasParam(key)).as("Parameter '%s' hasn't been found", key).isTrue();
+ assertThat(servletRequestCaptor.getValue().readParam(key)).isEqualTo(value);
+ }
+
+ private void assertNoParam(String... keys) {
+ Arrays.stream(keys).forEach(key -> {
+ assertThat(servletRequestCaptor.getValue().hasParam(key)).as(key).isFalse();
+ assertThat(servletRequestCaptor.getValue().readParam(key)).as(key).isNull();
+ });
+ }
+
+ private static class TestInputStream extends ServletInputStream {
+
+ private final ByteArrayInputStream byteArrayInputStream;
+
+ TestInputStream(String value) {
+ this.byteArrayInputStream = new ByteArrayInputStream(value.getBytes(UTF_8));
+ }
+
+ @Override
+ public boolean isFinished() {
+ return false;
+ }
+
+ @Override
+ public boolean isReady() {
+ return false;
+ }
+
+ @Override
+ public void setReadListener(ReadListener listener) {
+ throw new UnsupportedOperationException("Not available");
+ }
+
+ @Override
+ public int read() throws IOException {
+ return byteArrayInputStream.read();
+ }
+ }
+}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact 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.ByteArrayInputStream;
-import java.io.IOException;
-import java.util.Arrays;
-import javax.servlet.FilterChain;
-import javax.servlet.ReadListener;
-import javax.servlet.ServletInputStream;
-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 java.nio.charset.StandardCharsets.UTF_8;
-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 DeprecatedRestWebServiceFilterTest {
-
- @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 DeprecatedRestWebServiceFilter underTest = new DeprecatedRestWebServiceFilter(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/properties")).isTrue();
- assertThat(underTest.doGetPattern().matches("/api/properties/")).isTrue();
- assertThat(underTest.doGetPattern().matches("/api/properties/my.property")).isTrue();
- assertThat(underTest.doGetPattern().matches("/api/properties/my_property")).isTrue();
-
- assertThat(underTest.doGetPattern().matches("/api/issues/search")).isFalse();
- assertThat(underTest.doGetPattern().matches("/batch/index")).isFalse();
- assertThat(underTest.doGetPattern().matches("/foo")).isFalse();
- }
-
- @Test
- public void redirect_api_properties_to_api_properties_index() throws Exception {
- when(request.getRequestURI()).thenReturn("/api/properties");
- when(request.getMethod()).thenReturn("GET");
-
- underTest.doFilter(request, response, chain);
-
- assertRedirection("api/properties/index", "GET");
- assertNoParam("key", "component", "value", "values");
- }
-
- @Test
- public void redirect_api_properties_to_api_properties_index_when_no_property() throws Exception {
- when(request.getRequestURI()).thenReturn("/api/properties/");
- when(request.getMethod()).thenReturn("GET");
-
- underTest.doFilter(request, response, chain);
-
- assertRedirection("api/properties/index", "GET");
- assertNoParam("key", "component", "value", "values");
- }
-
- @Test
- public void redirect_api_properties_with_property_key() throws Exception {
- when(request.getRequestURI()).thenReturn("/api/properties/my.property");
- when(request.getMethod()).thenReturn("GET");
-
- underTest.doFilter(request, response, chain);
-
- assertRedirection("api/properties/index", "GET");
- assertParam("id", "my.property");
- assertNoParam("component", "value", "values");
- }
-
- @Test
- public void redirect_api_properties_with_property_id() throws Exception {
- when(request.getRequestURI()).thenReturn("/api/properties");
- when(request.getParameter("id")).thenReturn("my.property");
- when(request.getMethod()).thenReturn("GET");
-
- underTest.doFilter(request, response, chain);
-
- assertRedirection("api/properties/index", "GET");
- assertParam("id", "my.property");
- assertNoParam("component", "value", "values");
- }
-
- @Test
- public void redirect_api_properties_when_url_ands_with_slash() throws Exception {
- when(request.getRequestURI()).thenReturn("/api/properties/");
- when(request.getParameter("id")).thenReturn("my.property");
- when(request.getMethod()).thenReturn("GET");
-
- underTest.doFilter(request, response, chain);
-
- assertRedirection("api/properties/index", "GET");
- assertParam("id", "my.property");
- assertNoParam("component", "value", "values");
- }
-
- @Test
- public void redirect_api_properties_when_resource() throws Exception {
- when(request.getRequestURI()).thenReturn("/api/properties/my.property");
- when(request.getParameter("resource")).thenReturn("my_project");
- when(request.getMethod()).thenReturn("GET");
-
- underTest.doFilter(request, response, chain);
-
- assertRedirection("api/properties/index", "GET");
- assertParam("id", "my.property");
- assertParam("resource", "my_project");
- assertNoParam("component", "value", "values");
- }
-
- @Test
- public void redirect_api_properties_when_format() throws Exception {
- when(request.getRequestURI()).thenReturn("/api/properties/my.property");
- when(request.getParameter("format")).thenReturn("json");
- when(request.getMethod()).thenReturn("GET");
-
- underTest.doFilter(request, response, chain);
-
- assertRedirection("api/properties/index", "GET");
- assertParam("id", "my.property");
- assertParam("format", "json");
- assertNoParam("component", "value", "values");
- }
-
- @Test
- public void redirect_put_api_properties_to_api_settings_set() throws Exception {
- when(request.getRequestURI()).thenReturn("/api/properties/my.property");
- when(request.getParameter("value")).thenReturn("my_value");
- when(request.getParameter("resource")).thenReturn("my_project");
- when(request.getMethod()).thenReturn("PUT");
-
- underTest.doFilter(request, response, chain);
-
- assertRedirection("api/settings/set", "POST");
- assertParam("key", "my.property");
- assertParam("value", "my_value");
- assertParam("component", "my_project");
- assertNoParam("values");
- }
-
- @Test
- public void redirect_post_api_properties_to_api_settings_set() throws Exception {
- when(request.getRequestURI()).thenReturn("/api/properties/my.property");
- when(request.getParameter("value")).thenReturn("my_value");
- when(request.getParameter("resource")).thenReturn("my_project");
- when(request.getMethod()).thenReturn("POST");
-
- underTest.doFilter(request, response, chain);
-
- assertRedirection("api/settings/set", "POST");
- assertParam("key", "my.property");
- assertParam("value", "my_value");
- assertParam("component", "my_project");
- assertNoParam("values");
- }
-
- @Test
- public void redirect_post_api_properties_to_api_settings_set_when_value_is_in_body() throws Exception {
- when(request.getRequestURI()).thenReturn("/api/properties/my.property");
- when(request.getMethod()).thenReturn("POST");
- when(request.getInputStream()).thenReturn(new TestInputStream("my_value"));
-
- underTest.doFilter(request, response, chain);
-
- assertRedirection("api/settings/set", "POST");
- assertParam("key", "my.property");
- assertParam("value", "my_value");
- assertNoParam("values", "component");
- }
-
- @Test
- public void redirect_post_api_properties_to_api_settings_set_when_multi_values() throws Exception {
- when(request.getRequestURI()).thenReturn("/api/properties/my.property");
- when(request.getParameter("value")).thenReturn("value1,value2,value3");
- when(request.getMethod()).thenReturn("POST");
-
- underTest.doFilter(request, response, chain);
-
- assertRedirection("api/settings/set", "POST");
- assertParam("key", "my.property");
- assertNoParam("value");
- assertThat(servletRequestCaptor.getValue().hasParam("values")).as("Parameter '%s' hasn't been found", "values").isTrue();
- assertThat(servletRequestCaptor.getValue().readMultiParam("values")).containsOnly("value1", "value2", "value3");
- }
-
- @Test
- public void redirect_post_api_properties_to_api_settings_reset_when_no_value() throws Exception {
- when(request.getRequestURI()).thenReturn("/api/properties/my.property");
- when(request.getParameter("resource")).thenReturn("my_project");
- when(request.getMethod()).thenReturn("POST");
-
- underTest.doFilter(request, response, chain);
-
- assertRedirection("api/settings/reset", "POST");
- assertParam("keys", "my.property");
- assertParam("component", "my_project");
- assertNoParam("value", "values");
- }
-
- @Test
- public void redirect_post_api_properties_to_api_settings_reset_when_empty_value() throws Exception {
- when(request.getRequestURI()).thenReturn("/api/properties/my.property");
- when(request.getParameter("value")).thenReturn("");
- when(request.getParameter("resource")).thenReturn("my_project");
- when(request.getMethod()).thenReturn("POST");
-
- underTest.doFilter(request, response, chain);
-
- assertRedirection("api/settings/reset", "POST");
- assertParam("keys", "my.property");
- assertParam("component", "my_project");
- assertNoParam("value", "values");
- }
-
- @Test
- public void redirect_delete_api_properties_to_api_settings_reset() throws Exception {
- when(request.getRequestURI()).thenReturn("/api/properties/my.property");
- when(request.getParameter("resource")).thenReturn("my_project");
- when(request.getMethod()).thenReturn("DELETE");
-
- underTest.doFilter(request, response, chain);
-
- assertRedirection("api/settings/reset", "POST");
- assertParam("keys", "my.property");
- assertParam("component", "my_project");
- assertNoParam("value", "values");
- }
-
- 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);
- }
-
- private void assertParam(String key, String value) {
- assertThat(servletRequestCaptor.getValue().hasParam(key)).as("Parameter '%s' hasn't been found", key).isTrue();
- assertThat(servletRequestCaptor.getValue().readParam(key)).isEqualTo(value);
- }
-
- private void assertNoParam(String... keys) {
- Arrays.stream(keys).forEach(key -> {
- assertThat(servletRequestCaptor.getValue().hasParam(key)).as(key).isFalse();
- assertThat(servletRequestCaptor.getValue().readParam(key)).as(key).isNull();
- });
- }
-
- private static class TestInputStream extends ServletInputStream {
-
- private final ByteArrayInputStream byteArrayInputStream;
-
- TestInputStream(String value) {
- this.byteArrayInputStream = new ByteArrayInputStream(value.getBytes(UTF_8));
- }
-
- @Override
- public boolean isFinished() {
- return false;
- }
-
- @Override
- public boolean isReady() {
- return false;
- }
-
- @Override
- public void setReadListener(ReadListener listener) {
- throw new UnsupportedOperationException("Not available");
- }
-
- @Override
- public int read() throws IOException {
- return byteArrayInputStream.read();
- }
- }
-}
*/
public String mandatoryParam(String key) {
String value = param(key);
- if (value == null) {
- throw new IllegalArgumentException(String.format(MSG_PARAMETER_MISSING, key));
- }
+ checkArgument(value != null, String.format(MSG_PARAMETER_MISSING, key));
return value;
}