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); } }); }
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); } } }
@Override public Object getParamValue(Req req) { return req.response().screen(); }
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; }
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"); } }
public static void assignJsonBytesToReq(Req req, int code, byte[] bytes) { req.response().code(code); req.response().contentType(MediaType.APPLICATION_JSON); req.response().body(bytes); } }
public static RespBody resultToRespBody(Resp resp, Object result) { if (result instanceof RespBody) return (RespBody) result; byte[] bytes = HttpUtils.responseToBytes(resp.request(), result, resp.contentType(), mediaResponseRenderer(resp)); return new RespBodyBytes(bytes); }
private void execHandlerJob(final Channel channel, final boolean isKeepAlive, final MediaType contentType, final Req req) { With.tag(CTX_TAG_HANDLER).exchange(req).run(() -> { try { req.response() .view(options.view()) .contentType(options.contentType()) .mvc(options.mvc()); handleWithWrappers(channel, isKeepAlive, contentType, req, wrappers.get()); } catch (Throwable e) { handleError(req, e); } }); }
ctype = resp.contentType(); code = resp.code();
public static void assignJsonResponseToReq(Req req, int code, Object json) { req.response().code(code); req.response().contentType(MediaType.APPLICATION_JSON); req.response().json(json); }
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)); } }
void startResponse(Resp resp, Channel channel, int code, boolean isKeepAlive, MediaType contentType) { channel.write(code == 200 ? HTTP_200_OK : HttpResponseCodes.get(code)); addDefaultHeaders(channel, isKeepAlive, contentType); if (resp != null) { if (U.notEmpty(resp.headers())) { for (Map.Entry<String, String> e : resp.headers().entrySet()) { addCustomHeader(channel, e.getKey().getBytes(), e.getValue().getBytes()); } } if (U.notEmpty(resp.cookies())) { for (Map.Entry<String, String> e : resp.cookies().entrySet()) { String cookie = e.getKey() + "=" + e.getValue(); addCustomHeader(channel, HttpHeaders.SET_COOKIE.getBytes(), cookie.getBytes()); } } } }
void writeHttpResp(final MaybeReq req, final Channel ctx, final boolean isKeepAlive, int code, final MediaType contentType, final Object value) { Object result = value; Resp resp = respOrNull(req); if (resp != null) { if (resp.body() != null) { byte[] bytes = Msc.toBytes(resp.body()); writeResponse(req, ctx, isKeepAlive, code, contentType, bytes); return; } else if (resp.result() != null) { result = resp.result(); } } if (contentType == MediaType.JSON) { writeJsonResponse(req, resp, ctx, isKeepAlive, code, contentType, result); } else { byte[] bytes = Msc.toBytes(result); writeResponse(req, ctx, isKeepAlive, code, contentType, bytes); } }
public static void setContentType(Resp resp, MediaType mediaType) { resp.contentType(mediaType); }
@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); }
@Override public Object execute(Req x) { String domain = this.domain.getOrNull(); String loginURL = OAuth.getLoginURL(x, provider, domain); return x.response().redirect(loginURL); }