if (paramDef == null) {
throw new BadRequestException(String.format("Parameter '%s' is undefined for action '%s'", key, action.key()));
}
+ String value = StringUtils.defaultString(super.param(key), paramDef.defaultValue());
- return StringUtils.defaultString(super.param(key), paramDef.defaultValue());
+ List<String> possibleValues = paramDef.possibleValues();
+ if (value != null && possibleValues != null && !possibleValues.contains(value)) {
+ throw new BadRequestException(String.format("Value of parameter '%s' can only be one of %s", key, possibleValues));
+ }
+ return value;
}
};
}
for (Message message : e.errors()) {
messages.add(message(message.text(), message.l10nKey(), message.l10nParams()));
}
- sendErrors(response, e.httpCode(), messages.toArray(new String[0]));
+ sendErrors(response, e.httpCode(), messages.toArray(new String[messages.size()]));
}
@CheckForNull
- private String message(@Nullable String message, @Nullable String l10nKey, @Nullable Object[] l10nParams){
+ private String message(@Nullable String message, @Nullable String l10nKey, Object... l10nParams) {
if (l10nKey != null) {
return i18n.message(Locale.getDefault(), l10nKey, message, l10nParams);
} else {
- return message;
+ return message;
}
}
import org.sonar.server.plugins.MimeTypes;
import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
import java.io.IOException;
import java.util.HashMap;
return this;
}
- public SimpleRequest setParam(String key, @CheckForNull String value) {
+ public SimpleRequest setParam(String key, @Nullable String value) {
if (value != null) {
params.put(key, value);
}
assertThat(response.stream().outputAsString()).isEqualTo("Hello World by Marcel");
}
+ @Test
+ public void param_value_is_in_possible_values() throws Exception {
+ InternalRequest request = new SimpleRequest()
+ .setParam("message", "Hello World")
+ .setParam("format", "json");
+ ServletResponse response = new ServletResponse();
+ engine.execute(request, response, "api/system", "print");
+
+ assertThat(response.stream().outputAsString()).isEqualTo("Hello World by -");
+ }
+
+ @Test
+ public void param_value_is_not_in_possible_values() throws Exception {
+ InternalRequest request = new SimpleRequest()
+ .setParam("message", "Hello World")
+ .setParam("format", "html");
+ ServletResponse response = new ServletResponse();
+ engine.execute(request, response, "api/system", "print");
+
+ assertThat(response.stream().outputAsString()).isEqualTo("{\"errors\":[{\"msg\":\"Value of parameter 'format' can only be one of [json, xml]\"}]}");
+ }
+
@Test
public void internal_error() throws Exception {
InternalRequest request = new SimpleRequest();
NewAction print = newController.createAction("print");
print.createParam("message").setDescription("required message").setRequired(true);
print.createParam("author").setDescription("optional author").setDefaultValue("-");
+ print.createParam("format").setDescription("optional format").setPossibleValues("json", "xml");
print.setHandler(new RequestHandler() {
- @Override
- public void handle(Request request, Response response) {
- try {
- IOUtils.write(
- request.mandatoryParam("message") + " by " + request.param("author", "nobody"), response.stream().output());
- } catch (IOException e) {
- throw new IllegalStateException(e);
- }
+ @Override
+ public void handle(Request request, Response response) {
+ try {
+ request.param("format");
+ IOUtils.write(
+ request.mandatoryParam("message") + " by " + request.param("author", "nobody"), response.stream().output());
+ } catch (IOException e) {
+ throw new IllegalStateException(e);
}
- });
+ }
+ });
newController.done();
}
}