static class SystemWebService implements WebService {
@Override
public void define(Context context) {
NewController newController = context.createController("api/system");
newController.createAction("health")
.setHandler(new RequestHandler() {
@Override
public void handle(Request request, Response response) {
try {
response.stream().output().write("good".getBytes());
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
});
newController.createAction("ping")
.setPost(true)
.setHandler(new RequestHandler() {
@Override
public void handle(Request request, Response response) {
try {
response.stream().output().write("pong".getBytes());
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
});
newController.createAction("fail")
.setHandler(new RequestHandler() {
@Override
public void handle(Request request, Response response) {
throw new IllegalStateException("Unexpected");
}
});
newController.createAction("fail_with_i18n_message")
.setHandler(new RequestHandler() {
@Override
public void handle(Request request, Response response) {
throw new BadRequestException("bad.request.reason", 0);
}
});
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) {
Errors errors = new Errors();
for (int count = 0; count < Integer.valueOf(request.param("count")); count++) {
errors.add(Message.of("Bad request reason #" + count));
}
throw new BadRequestException(errors);
}
});
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) {
Errors errors = new Errors();
for (int count = 0; count < Integer.valueOf(request.param("count")); count++) {
errors.add(Message.of("bad.request.reason", count));
}
throw new BadRequestException(errors);
}
});
newController.createAction("alive")
.setHandler(new RequestHandler() {
@Override
public void handle(Request request, Response response) {
response.noContent();
}
});
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"
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 {
request.param("format");
IOUtils.write(