private static void postProcessRedirect(Resp resp) { String redirect = resp.redirect(); if (redirect != null) { if (resp.code() < 300 || resp.code() >= 400) { resp.code(303); } resp.headers().put(HttpHeaders.LOCATION.name(), redirect); if (resp.result() == null && resp.body() == null) { resp.body(EMPTY_RESPONSE); } } }
public static String getErrorMessageAndSetCode(Resp resp, Throwable err) { ErrCodeAndMsg codeAndMsg = Msc.getErrorCodeAndMsg(err); resp.code(codeAndMsg.code()); return codeAndMsg.msg(); }
protected Object defaultErrorHandling(Req req, Throwable error) { if (error instanceof NotFound) { Resp resp = req.response().code(404); if (resp.contentType() == MediaType.JSON) { return error; } else { return resp.view("404").result(U.map("req", req)); } } return error; }
Resp resp = req.response(); ctype = resp.contentType(); code = resp.code();
void error(final Req req, final Throwable error, LogLevel logLevel) { try { logError(req, error, logLevel); Resp resp = req.response().code(500).result(null); Object result = Customization.of(req).errorHandler().handleError(req, resp, error); HttpUtils.resultOf(req, result); } catch (Exception e) { Log.error("An error occurred inside the error handler!", e); HttpUtils.resultToResponse(req, HttpUtils.getErrorInfo(req.response(), e)); } }
private void process(final Req req, final Resp resp, final ProxyMapping mapping, final int attempts, final long since) { final String targetUrl = mapping.getTargetUrl(req); Map<String, String> headers = U.map(req.headers()); headers.remove("transfer-encoding"); headers.remove("content-length"); addExtraRequestHeaders(req, headers); HttpClient client = getOrCreateClient(); client.req() .verb(req.verb()) .url(targetUrl) .headers(headers) .cookies(req.cookies()) .body(req.body()) .raw(true) .execute((result, error) -> { if (error == null) { resp.code(result.code()); resp.body(result.bodyBytes()); // process the response headers SimpleHttpResp proxyResp = new SimpleHttpResp(); HttpUtils.proxyResponseHeaders(result.headers(), proxyResp); if (proxyResp.contentType != null) resp.contentType(proxyResp.contentType); if (proxyResp.headers != null) resp.headers().putAll(proxyResp.headers); if (proxyResp.cookies != null) resp.cookies().putAll(proxyResp.cookies); resp.done(); } else { handleError(error, req, resp, mapping, attempts, since); } }); }
protected Object page(Req req, Resp resp, Throwable error) { if (error instanceof SecurityException) { resp.model("embedded", req.attr("_embedded", false)); resp.model("req", req); resp.model("loginUri", Msc.specialUri("login")); return resp.code(403).view("login").mvc(true); } else { BasicConfig zone = HttpUtils.zone(req); String home = zone.entry("home").or("/"); Map<String, ?> errorInfo = HttpUtils.getErrorInfo(resp, error); resp.model("req", req); resp.model("error", errorInfo); resp.model("home", home); return resp.mvc(true).view("error"); } }
private static void postProcessRedirect(Resp resp) { String redirect = resp.redirect(); if (redirect != null) { if (resp.code() < 300 || resp.code() >= 400) { resp.code(303); } resp.headers().put(HttpHeaders.LOCATION.name(), redirect); if (resp.result() == null && resp.body() == null) { resp.body(EMPTY_RESPONSE); } } }
public static String getErrorMessageAndSetCode(Resp resp, Throwable err) { ErrCodeAndMsg codeAndMsg = Msc.getErrorCodeAndMsg(err); resp.code(codeAndMsg.code()); return codeAndMsg.msg(); }
private void registerLoginProvider() { My.loginProvider((req, username, password) -> { User requestedUser = userDao.getUser(username); if (requestedUser == null) { req.response().code(HttpStatus.UNAUTHORIZED); return false; } if (!requestedUser.isEnabled()) { req.response().code(HttpStatus.FORBIDDEN); return false; } if (PasswordManager.checkPassword(password, requestedUser.getHashedPassword())) { return true; } else { req.response().code(HttpStatus.UNAUTHORIZED); return false; } }); }
@Administrator @POST("/deployment-permission") public void addDeploymentPermission(String name, String serviceId, String environmentId, DeploymentPermission.PermissionType permissionType, Req req) { DeploymentPermission newDeploymentPermission = new DeploymentPermission(); newDeploymentPermission.setName(name); // This is how rapidoid represent nulls. also has to be string to not have a NumberFormatException if (serviceId.equals("null") && environmentId.equals("null")) { req.response().code(HttpStatus.BAD_REQUEST); req.response().json("One of serviceId or environmentId must be present!"); } else { if (!serviceId.equals("null")) { newDeploymentPermission.setServiceId(Integer.parseInt(serviceId)); } if (!environmentId.equals("null")) { newDeploymentPermission.setEnvironmentId(Integer.parseInt(environmentId)); } } newDeploymentPermission.setPermissionType(permissionType); deploymentPermissionDao.addDeploymentPermission(newDeploymentPermission); assignJsonResponseToReq(req, HttpStatus.CREATED, newDeploymentPermission); }
protected Object defaultErrorHandling(Req req, Throwable error) { if (error instanceof NotFound) { Resp resp = req.response().code(404); if (resp.contentType() == MediaType.JSON) { return error; } else { return resp.view("404").result(U.map("req", req)); } } return error; }
public static void assignJsonResponseToReq(Req req, int code, Object json) { req.response().code(code); req.response().contentType(MediaType.APPLICATION_JSON); req.response().json(json); }
public static void assignJsonBytesToReq(Req req, int code, byte[] bytes) { req.response().code(code); req.response().contentType(MediaType.APPLICATION_JSON); req.response().body(bytes); } }
@Override public void onDone(HttpResp result, Throwable error) { if (error == null) { resp.code(result.code()); resp.body(result.bodyBytes()); // process the response headers SimpleHttpResp proxyResp = new SimpleHttpResp(); HttpUtils.proxyResponseHeaders(result.headers(), proxyResp); if (proxyResp.contentType != null) resp.contentType(proxyResp.contentType); if (proxyResp.headers != null) resp.headers().putAll(proxyResp.headers); if (proxyResp.cookies != null) resp.cookies().putAll(proxyResp.cookies); resp.done(); } else { handleError(error, req, resp, mapping, attempts, since); } }
private void registerRolesProvider() { My.rolesProvider((req, username) -> { try { User user = userDao.getUser(username); if (!user.isEnabled()) { req.response().code(HttpStatus.FORBIDDEN); return null; } if (user.isAdmin()) { return U.set(Role.ADMINISTRATOR); } return U.set(Role.ANYBODY); } catch (Exception e) { logger.error("Got exception while getting user roles! setting to ANYBODY", e); return U.set(Role.ANYBODY); } }); } }
void error(final Req req, final Throwable error, LogLevel logLevel) { try { logError(req, error, logLevel); Resp resp = req.response().code(500).result(null); Object result = Customization.of(req).errorHandler().handleError(req, resp, error); HttpUtils.resultOf(req, result); } catch (Exception e) { Log.error("An error occurred inside the error handler!", e); HttpUtils.resultToResponse(req, HttpUtils.getErrorInfo(req.response(), e)); } }
protected Object page(Req req, Resp resp, Throwable error) { if (error instanceof SecurityException) { resp.model("embedded", req.attr("_embedded", false)); resp.model("req", req); resp.model("loginUri", Msc.specialUri("login")); return resp.code(403).view("login").mvc(true); } else { BasicConfig zone = HttpUtils.zone(req); String home = zone.entry("home").or("/"); Map<String, ?> errorInfo = HttpUtils.getErrorInfo(resp, error); resp.model("req", req); resp.model("error", errorInfo); resp.model("home", home); return resp.mvc(true).view("error"); } }