@Override public void body(String body) { delegate.body(body); }
@Override public String body() { return delegate.body(); }
private void invalidJsonPayload(JsonParseException ex, Request req, Response res) { res.body(new Gson().toJson(Collections.singletonMap("error", "Payload data is not valid JSON: " + ex.getMessage()))); res.status(HttpStatus.SC_BAD_REQUEST); }
private void unprocessableEntity(UnprocessableEntityException exception, Request request, Response response) { response.body(new Gson().toJson(Collections.singletonMap("message", "Your request could not be processed. " + exception.getMessage()))); response.status(HttpStatus.SC_UNPROCESSABLE_ENTITY); }
default void notFound(Exception ex, Request req, Response res) { res.status(HttpStatus.NOT_FOUND.value()); res.body(MessageJson.create(notFoundMessage())); }
/** * Modifies the HTTP response and body based on the provided exception. */ static void modify(HttpServletRequest httpRequest, HttpServletResponse httpResponse, Body body, RequestWrapper requestWrapper, ResponseWrapper responseWrapper, ExceptionMapper exceptionMapper, Exception e) { ExceptionHandlerImpl handler = exceptionMapper.getHandler(e); if (handler != null) { handler.handle(e, requestWrapper, responseWrapper); String bodyAfterFilter = responseWrapper.getDelegate().body(); if (bodyAfterFilter != null) { body.set(bodyAfterFilter); } } else { LOG.error("", e); httpResponse.setStatus(500); if (CustomErrorPages.existsFor(500)) { requestWrapper.setDelegate(RequestResponseFactory.create(httpRequest)); responseWrapper.setDelegate(RequestResponseFactory.create(httpResponse)); body.set(CustomErrorPages.getFor(500, requestWrapper, responseWrapper)); } else { body.set(CustomErrorPages.INTERNAL_ERROR); } } }
@Override public void setupRoutes() { path(controllerBasePath(), () -> { before("", mimeType, this::setContentType); before("/*", mimeType, this::setContentType); before("", this::verifyContentType); before("/*", this::verifyContentType); before("", mimeType, apiAuthenticationHelper::checkAnyAdminUserAnd403); before("/*", mimeType, apiAuthenticationHelper::checkAnyAdminUserAnd403); before("", mimeType, this::checkRateLimitAvailable); post("", mimeType, this::encrypt); exception(CryptoException.class, (CryptoException exception, Request request, Response response) -> { response.status(HttpStatus.INTERNAL_SERVER_ERROR.value()); response.body(MessageJson.create(HaltApiMessages.errorWhileEncryptingMessage())); }); }); }
@Override public void setupRoutes() { path(controllerPath(), () -> { before("", mimeType, this::setContentType); before("/*", mimeType, this::setContentType); before("", this::verifyContentType); before("/*", this::verifyContentType); before("", mimeType, apiAuthenticationHelper::checkAdminUserAnd403); before("/*", mimeType, apiAuthenticationHelper::checkAdminUserAnd403); get("", mimeType, this::index); post("", mimeType, this::create); get(Routes.Roles.NAME_PATH, mimeType, this::show); put(Routes.Roles.NAME_PATH, mimeType, this::update); delete(Routes.Roles.NAME_PATH, mimeType, this::destroy); exception(InvalidPluginTypeException.class, (ex, req, res) -> { res.body(this.messageJson(ex)); res.status(HttpStatus.BAD_REQUEST.value()); }); exception(RecordNotFoundException.class, this::notFound); }); }
@Override public void setupRoutes() { path(controllerPath(), () -> { before("", mimeType, this::setContentType); before("/*", mimeType, this::setContentType); before("", this::verifyContentType); before("/*", this::verifyContentType); before("", mimeType, apiAuthenticationHelper::checkAdminUserAnd403); before("/*", mimeType, apiAuthenticationHelper::checkAdminUserAnd403); get("", mimeType, this::index); post("", mimeType, this::create); patch("", mimeType, this::bulkUpdate); get(Routes.Roles.NAME_PATH, mimeType, this::show); put(Routes.Roles.NAME_PATH, mimeType, this::update); delete(Routes.Roles.NAME_PATH, mimeType, this::destroy); exception(InvalidPluginTypeException.class, (ex, req, res) -> { res.body(this.messageJson(ex)); res.status(HttpStatus.BAD_REQUEST.value()); }); exception(RecordNotFoundException.class, this::notFound); }); }
@Override public void setupRoutes() { path(controllerPath(), () -> { before("", mimeType, this::setContentType); before("/*", mimeType, this::setContentType); before("", this::verifyContentType); before("/*", this::verifyContentType); before(Routes.Pipeline.PAUSE_PATH, mimeType, apiAuthenticationHelper::checkPipelineGroupOperateUserAnd403); before(Routes.Pipeline.UNPAUSE_PATH, mimeType, apiAuthenticationHelper::checkPipelineGroupOperateUserAnd403); before(Routes.Pipeline.UNLOCK_PATH, mimeType, apiAuthenticationHelper::checkPipelineGroupOperateUserAnd403); before(Routes.Pipeline.TRIGGER_OPTIONS_PATH, mimeType, apiAuthenticationHelper::checkPipelineGroupOperateUserAnd403); before(Routes.Pipeline.SCHEDULE_PATH, mimeType, apiAuthenticationHelper::checkPipelineGroupOperateUserAnd403); post(Routes.Pipeline.PAUSE_PATH, mimeType, this::pause); post(Routes.Pipeline.UNPAUSE_PATH, mimeType, this::unpause); post(Routes.Pipeline.UNLOCK_PATH, mimeType, this::unlock); get(Routes.Pipeline.TRIGGER_OPTIONS_PATH, mimeType, this::triggerOptions); post(Routes.Pipeline.SCHEDULE_PATH, mimeType, this::schedule); exception(RecordNotFoundException.class, this::notFound); exception(InvalidGoCipherTextRuntimeException.class, (InvalidGoCipherTextRuntimeException exception, Request request, Response response) -> { response.status(HttpStatus.INTERNAL_SERVER_ERROR.value()); response.body(MessageJson.create(HaltApiMessages.errorWhileEncryptingMessage())); }); }); }
static void execute(RouteContext context) throws Exception { Object content = context.body().get(); List<RouteMatch> matchSet = context.routeMatcher().findMultiple(HttpMethod.before, context.uri(), context.acceptType()); for (RouteMatch filterMatch : matchSet) { Object filterTarget = filterMatch.getTarget(); if (filterTarget instanceof FilterImpl) { Request request = RequestResponseFactory.create(filterMatch, context.httpRequest()); FilterImpl filter = (FilterImpl) filterTarget; context.requestWrapper().setDelegate(request); context.responseWrapper().setDelegate(context.response()); filter.handle(context.requestWrapper(), context.responseWrapper()); String bodyAfterFilter = context.response().body(); if (bodyAfterFilter != null) { content = bodyAfterFilter; } } } context.body().set(content); }
static void execute(RouteContext context) throws Exception { Object content = context.body().get(); List<RouteMatch> matchSet = context.routeMatcher().findMultiple(HttpMethod.after, context.uri(), context.acceptType()); for (RouteMatch filterMatch : matchSet) { Object filterTarget = filterMatch.getTarget(); if (filterTarget instanceof FilterImpl) { if (context.requestWrapper().getDelegate() == null) { Request request = RequestResponseFactory.create(filterMatch, context.httpRequest()); context.requestWrapper().setDelegate(request); } else { context.requestWrapper().changeMatch(filterMatch); } context.responseWrapper().setDelegate(context.response()); FilterImpl filter = (FilterImpl) filterTarget; filter.handle(context.requestWrapper(), context.responseWrapper()); String bodyAfterFilter = context.response().body(); if (bodyAfterFilter != null) { content = bodyAfterFilter; } } } context.body().set(content); }
static void execute(RouteContext context) throws Exception { Object content = context.body().get(); List<RouteMatch> matchSet = context.routeMatcher().findMultiple(HttpMethod.afterafter, context.uri(), context.acceptType()); for (RouteMatch filterMatch : matchSet) { Object filterTarget = filterMatch.getTarget(); if (filterTarget instanceof FilterImpl) { if (context.requestWrapper().getDelegate() == null) { Request request = RequestResponseFactory.create(filterMatch, context.httpRequest()); context.requestWrapper().setDelegate(request); } else { context.requestWrapper().changeMatch(filterMatch); } context.responseWrapper().setDelegate(context.response()); FilterImpl filter = (FilterImpl) filterTarget; filter.handle(context.requestWrapper(), context.responseWrapper()); String bodyAfterFilter = context.response().body(); if (bodyAfterFilter != null) { content = bodyAfterFilter; } } } context.body().set(content); }
@Override public void body(String body) { delegate.body(body); }
@Override public void handle(Exception exception, Request request, Response response) { Gson gson = new Gson(); response.body(gson.toJson(Resp.create(500, exception.getMessage()))); } });
@Override public void handle(Exception exception, Request request, Response response) { Gson gson = new Gson(); response.body(gson.toJson(Resp.create(400, exception.getMessage()))); } });
protected void handleExceptions(final Gson jsonConverter) { exception(Exception.class, (ex, req, res) -> { res.status(HttpStatus.INTERNAL_SERVER_ERROR_500); res.body(jsonConverter.toJson(ex.getMessage())); }); }
protected String configureResponseData(Response response, ResponseData r) { response.status(r.status); response.type(r.contentType); response.body(r.text); return r.text; }
/** * Creates a controller that maps requests to actions. */ public UserController(final UserService userService) { Spark.staticFileLocation("/public"); get("/api/users", (req, res) -> userService.getAllUsers(), json()); get("/api/users/:id", (req, res) -> userService.getUser(req.params(":id")), json()); post("/api/users", (req, res) -> userService.createUser(req.queryParams("name"), req.queryParams("email")), json()); put("/api/users/:id", (req, res) -> userService.updateUser( req.params(":id"), req.queryParams("name"), req.queryParams("email") ), json()); delete("/api/users/:id", (req, res) -> userService.deleteUser(req.params(":id")), json()); after((req, res) -> { res.type("application/json"); }); exception(IllegalArgumentException.class, (error, req, res) -> { res.status(400); res.body(toJson(new ResponseError(error))); }); }
@Override public void handle(Request request, Response response) throws Exception { MDCStructuredLogger.forLogger(LOGGER) .addField(STATUS, response.status()) .addField(RESPONSE_BODY, response.body()) .log(logger -> logger.info("WebAdmin response received")); } }