aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-server/src
diff options
context:
space:
mode:
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>2014-04-28 17:56:43 +0200
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>2014-04-28 17:57:08 +0200
commitdd38eb3d057eb1eb37a1be1567960906c85f10be (patch)
tree21c248466ba66fbcc075ad756353c349d2940b27 /sonar-server/src
parentacf2f9c447cd5931a67f6aa2643dad4d919cc27d (diff)
downloadsonarqube-dd38eb3d057eb1eb37a1be1567960906c85f10be.tar.gz
sonarqube-dd38eb3d057eb1eb37a1be1567960906c85f10be.zip
SONAR-5111 Add support for default parameters in Java WS, protection against undeclared params
Diffstat (limited to 'sonar-server/src')
-rw-r--r--sonar-server/src/main/java/org/sonar/server/ws/InternalRequestWrapper.java40
-rw-r--r--sonar-server/src/main/java/org/sonar/server/ws/WebServiceEngine.java19
-rw-r--r--sonar-server/src/test/java/org/sonar/server/ws/WebServiceEngineTest.java29
3 files changed, 82 insertions, 6 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/ws/InternalRequestWrapper.java b/sonar-server/src/main/java/org/sonar/server/ws/InternalRequestWrapper.java
new file mode 100644
index 00000000000..16a83331244
--- /dev/null
+++ b/sonar-server/src/main/java/org/sonar/server/ws/InternalRequestWrapper.java
@@ -0,0 +1,40 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 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.ws;
+
+
+public abstract class InternalRequestWrapper extends InternalRequest {
+
+ private InternalRequest wrapped;
+
+ protected InternalRequestWrapper(InternalRequest request) {
+ this.wrapped = request;
+ }
+
+ @Override
+ public String method() {
+ return wrapped.method();
+ }
+
+ @Override
+ public String param(String key) {
+ return wrapped.param(key);
+ }
+}
diff --git a/sonar-server/src/main/java/org/sonar/server/ws/WebServiceEngine.java b/sonar-server/src/main/java/org/sonar/server/ws/WebServiceEngine.java
index 2faf1cf4127..27349e9e578 100644
--- a/sonar-server/src/main/java/org/sonar/server/ws/WebServiceEngine.java
+++ b/sonar-server/src/main/java/org/sonar/server/ws/WebServiceEngine.java
@@ -19,6 +19,7 @@
*/
package org.sonar.server.ws;
+import org.apache.commons.lang.StringUtils;
import org.elasticsearch.common.collect.Lists;
import org.picocontainer.Startable;
import org.slf4j.LoggerFactory;
@@ -26,6 +27,7 @@ import org.sonar.api.ServerComponent;
import org.sonar.api.i18n.I18n;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.WebService;
+import org.sonar.api.server.ws.WebService.Param;
import org.sonar.api.utils.text.JsonWriter;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.BadRequestException.Message;
@@ -82,7 +84,8 @@ public class WebServiceEngine implements ServerComponent, Startable {
WebService.Action action = getAction(controllerPath, actionKey);
request.setAction(action);
verifyRequest(action, request);
- action.handler().handle(request, response);
+ InternalRequest wrapped = wrapWithDefaults(action, request);
+ action.handler().handle(wrapped, response);
} catch (IllegalArgumentException e) {
// TODO replace by BadRequestException in Request#mandatoryParam()
@@ -117,6 +120,20 @@ public class WebServiceEngine implements ServerComponent, Startable {
}
}
+ private InternalRequest wrapWithDefaults(final WebService.Action action, InternalRequest request) {
+ return new InternalRequestWrapper(request) {
+ @Override
+ public String param(String key) {
+ Param paramDef = action.param(key);
+ if (paramDef == null) {
+ throw new BadRequestException(String.format("Parameter '%s' is undefined for action '%s'", key, action.key()));
+ }
+
+ return StringUtils.defaultString(super.param(key), paramDef.defaultValue());
+ }
+ };
+ }
+
private void sendError(BadRequestException e, ServletResponse response) {
Collection<String> messages = Lists.newArrayList();
String exceptionMessage = message(e.getMessage(), e.l10nKey(), e.l10nParams());
diff --git a/sonar-server/src/test/java/org/sonar/server/ws/WebServiceEngineTest.java b/sonar-server/src/test/java/org/sonar/server/ws/WebServiceEngineTest.java
index 3a7aa81ede7..03baa5579a0 100644
--- a/sonar-server/src/test/java/org/sonar/server/ws/WebServiceEngineTest.java
+++ b/sonar-server/src/test/java/org/sonar/server/ws/WebServiceEngineTest.java
@@ -158,6 +158,15 @@ public class WebServiceEngineTest {
}
@Test
+ public void unknown_parameter_is_set() throws Exception {
+ InternalRequest request = new SimpleRequest().setParam("unknown", "Unknown");
+ ServletResponse response = new ServletResponse();
+ engine.execute(request, response, "api/system", "fail_with_undeclared_parameter");
+
+ assertThat(response.stream().outputAsString()).isEqualTo("{\"errors\":[{\"msg\":\"Parameter 'unknown' is undefined for action 'fail_with_undeclared_parameter'\"}]}");
+ }
+
+ @Test
public void required_parameter_is_not_set() throws Exception {
InternalRequest request = new SimpleRequest();
ServletResponse response = new ServletResponse();
@@ -302,6 +311,7 @@ public class WebServiceEngineTest {
}
});
newController.createAction("fail_with_multiple_messages")
+ .createParam("count", "Number of error messages to generate")
.setHandler(new RequestHandler() {
@Override
public void handle(Request request, Response response) {
@@ -313,6 +323,7 @@ public class WebServiceEngineTest {
}
});
newController.createAction("fail_with_multiple_i18n_messages")
+ .createParam("count", "Number of error messages to generate")
.setHandler(new RequestHandler() {
@Override
public void handle(Request request, Response response) {
@@ -338,16 +349,24 @@ public class WebServiceEngineTest {
}
});
+ newController.createAction("fail_with_undeclared_parameter")
+ .setHandler(new RequestHandler() {
+ @Override
+ public void handle(Request request, Response response) {
+ response.newJsonWriter().prop("unknown", request.param("unknown"));
+ }
+ });
+
// parameter "message" is required but not "author"
- newController.createAction("print")
- .createParam("message", "required message")
- .createParam("author", "optional author")
- .setHandler(new RequestHandler() {
+ NewAction print = newController.createAction("print");
+ print.createParam("message").setDescription("required message").setRequired(true);
+ print.createParam("author").setDescription("optional author").setDefaultValue("-");
+ print.setHandler(new RequestHandler() {
@Override
public void handle(Request request, Response response) {
try {
IOUtils.write(
- request.mandatoryParam("message") + " by " + request.param("author", "-"), response.stream().output());
+ request.mandatoryParam("message") + " by " + request.param("author", "nobody"), response.stream().output());
} catch (IOException e) {
throw new IllegalStateException(e);
}