import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
+
import java.util.Collection;
import java.util.List;
import java.util.Map;
}
class NewParam {
- private String key, description, exampleValue;
+ private String key, description, exampleValue, defaultValue;
private boolean required = false;
private String[] possibleValues = null;
return this;
}
+ /**
+ * @since 4.4
+ */
+ public NewParam setDefaultValue(@Nullable String s) {
+ this.defaultValue = s;
+ return this;
+ }
+
@Override
public String toString() {
return key;
@Immutable
class Param {
- private final String key, description, exampleValue;
+ private final String key, description, exampleValue, defaultValue;
private final boolean required;
private final String[] possibleValues;
this.key = newParam.key;
this.description = newParam.description;
this.exampleValue = newParam.exampleValue;
+ this.defaultValue = newParam.defaultValue;
this.required = newParam.required;
this.possibleValues = (newParam.possibleValues == null ? new String[0] : newParam.possibleValues);
}
return possibleValues;
}
+ /**
+ * @since 4.4
+ */
+ @CheckForNull
+ public String defaultValue() {
+ return defaultValue;
+ }
+
@Override
public String toString() {
return key;
NewController newController = context.createController("api/rule");
NewAction create = newController.createAction("create").setHandler(mock(RequestHandler.class));
create.createParam("key").setDescription("Key of the new rule");
- create.createParam("severity");
+ create.createParam("severity").setDefaultValue("MAJOR");
newController.done();
}
}.define(context);
assertThat(action.param("severity").key()).isEqualTo("severity");
assertThat(action.param("severity").description()).isNull();
+ assertThat(action.param("severity").defaultValue()).isEqualTo("MAJOR");
}
@Test
--- /dev/null
+/*
+ * 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);
+ }
+}
*/
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;
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;
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()
}
}
+ 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());
assertThat(response.stream().outputAsString()).isEqualTo("pong");
}
+ @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();
}
});
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) {
}
});
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) {
}
});
+ 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);
}