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 writeContentLengthAndBody(final MaybeReq req, final Channel ctx, final byte[] body) { writeContentLengthHeader(ctx, body.length); closeHeaders(req, ctx.output()); ctx.write(body); }
public void done(Req req) { impl.done(req); }
void writeResponse(final MaybeReq req, final Channel ctx, final boolean isKeepAlive, final int code, final MediaType contentType, final byte[] content) { startResponse(respOrNull(req), ctx, code, isKeepAlive, contentType); writeContentLengthAndBody(req, ctx, content); }
@SuppressWarnings("unused") private void writeOnBufferAsJson(MaybeReq req, Channel ctx, int code, boolean isKeepAlive, Object value) { startResponse(respOrNull(req), ctx, code, isKeepAlive, MediaType.JSON); Buf output = ctx.output(); synchronized (output) { writeJsonBody(req, output.unwrap(), value); } }
startResponse(null, channel, code, isKeepAlive, contentType); addCustomHeader(channel, e.getKey().getBytes(), e.getValue().getBytes()); for (Map.Entry<String, String> e : cookies.entrySet()) { String cookie = e.getKey() + "=" + e.getValue(); addCustomHeader(channel, HttpHeaders.SET_COOKIE.getBytes(), cookie.getBytes()); closeHeaders(maybeReq, output); writeContentLengthHeader(channel, body.length()); closeHeaders(maybeReq, output); body.writeTo(channel); done(req);
private void writeJsonResponse(MaybeReq req, Resp resp, Channel ctx, boolean isKeepAlive, int code, MediaType contentType, Object result) { startResponse(resp, ctx, code, isKeepAlive, contentType); RapidoidThreadLocals locals = Msc.locals(); ReusableWritable out = locals.jsonRenderingStream(); // FIXME headers JSON.stringify(result, out); writeContentLengthHeader(ctx, out.size()); closeHeaders(req, ctx.output()); ctx.write(out.array(), 0, out.size()); }
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 void closeHeaders(MaybeReq req, Buf out) { impl.closeHeaders(req, out); }
public void error(Req req, Throwable error, LogLevel logLevel) { impl.error(req, error, logLevel); }
public void respond(MaybeReq maybeReq, Channel channel, long connId, long handle, int code, boolean isKeepAlive, MediaType contentType, RespBody body, Map<String, String> headers, Map<String, String> cookies) { impl.respond(maybeReq, channel, connId, handle, code, isKeepAlive, contentType, body, headers, cookies); }
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)); } }
public HttpStatus errorAndDone(Req req, Throwable error, LogLevel logLevel) { return impl.errorAndDone(req, error, logLevel); }
public void removeTrailingSlash(Buf buf, BufRange range) { impl.removeTrailingSlash(buf, range); }
startResponse(null, channel, code, isKeepAlive, contentType); addCustomHeader(channel, e.getKey().getBytes(), e.getValue().getBytes()); for (Map.Entry<String, String> e : cookies.entrySet()) { String cookie = e.getKey() + "=" + e.getValue(); addCustomHeader(channel, HttpHeaders.SET_COOKIE.getBytes(), cookie.getBytes()); closeHeaders(maybeReq, output); writeContentLengthHeader(channel, body.length()); closeHeaders(maybeReq, output); body.writeTo(channel); done(req);
private void writeJsonResponse(MaybeReq req, Resp resp, Channel ctx, boolean isKeepAlive, int code, MediaType contentType, Object result) { startResponse(resp, ctx, code, isKeepAlive, contentType); RapidoidThreadLocals locals = Msc.locals(); ReusableWritable out = locals.jsonRenderingStream(); // FIXME headers JSON.stringify(result, out); writeContentLengthHeader(ctx, out.size()); closeHeaders(req, ctx.output()); ctx.write(out.array(), 0, out.size()); }
void writeResponse(final MaybeReq req, final Channel ctx, final boolean isKeepAlive, final int code, final MediaType contentType, final byte[] content) { startResponse(respOrNull(req), ctx, code, isKeepAlive, contentType); writeContentLengthAndBody(req, ctx, content); }
@SuppressWarnings("unused") private void writeOnBufferAsJson(MaybeReq req, Channel ctx, int code, boolean isKeepAlive, Object value) { startResponse(respOrNull(req), ctx, code, isKeepAlive, MediaType.JSON); Buf output = ctx.output(); synchronized (output) { writeJsonBody(req, output.unwrap(), value); } }
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); } }
private void writeJsonBody(MaybeReq req, Buf out, Object value) { // Content-Length header out.append(CONTENT_LENGTH_UNKNOWN); int posConLen = out.size() - 1; out.append(CR_LF); closeHeaders(req, out); int posBefore = out.size(); JSON.stringify(value, out.asOutputStream()); int posAfter = out.size(); int contentLength = posAfter - posBefore; out.putNumAsText(posConLen, contentLength, false); }