static RespBody toRespBody(Req req, RespImpl resp) { try { return createRespBodyFromResult(req, resp); } catch (Throwable e) { HttpIO.INSTANCE.error(req, e, LogLevel.ERROR); try { return createRespBodyFromResult(req, resp); } catch (Exception e1) { Log.error("Internal rendering error!", e1); return new RespBodyBytes(HttpUtils.getErrorMessageAndSetCode(resp, e1).getBytes()); } } }
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); }
@Override public Object wrap(final Req req, final HandlerInvocation invocation) { final AtomicReference<Object> resultHolder = new AtomicReference<>(); U.must(txMode != null && txMode != TransactionMode.NONE); boolean readOnly = (txMode == TransactionMode.AUTO) ? HttpUtils.isGetReq(req) : txMode == TransactionMode.READ_ONLY; try { JPA.transaction(() -> { Object res = invocation.invokeAndTransformResultCatchingErrors(result -> { if (result instanceof Throwable) { return result; } // serialize the result into a HTTP response body, while still inside tx (see #153) RespBody body = BodyRenderer.resultToRespBody(req.response(), result); return body; }); if (res instanceof Throwable) { // throw to rollback Throwable err = (Throwable) res; throw U.rte("Error occurred inside the transactional web handler!", err); } else { resultHolder.set(res); } }, readOnly); } catch (Throwable e) { resultHolder.set(e); } return resultHolder.get(); }
private void renderResponse() { HttpUtils.postProcessResponse(response); if (response.raw() != null) { int posBeforeResponse = channel.output().size(); byte[] bytes = Msc.toBytes(response.raw()); channel.write(bytes); if (willSaveToCache()) posBeforeBody = posBeforeResponse + HttpUtils.findBodyStart(bytes); completed = true; HttpIO.INSTANCE.done(this); } else { // render the response body RespBody body = BodyRenderer.toRespBody(this, response); // render the response doRendering(response.code(), body); } }
private void renderResponse() { HttpUtils.postProcessResponse(response); if (response.raw() != null) { int posBeforeResponse = channel.output().size(); byte[] bytes = Msc.toBytes(response.raw()); channel.write(bytes); if (willSaveToCache()) posBeforeBody = posBeforeResponse + HttpUtils.findBodyStart(bytes); completed = true; HttpIO.INSTANCE.done(this); } else { // render the response body RespBody body = BodyRenderer.toRespBody(this, response); // render the response doRendering(response.code(), body); } }
private static RespBody createRespBodyFromResult(Req req, RespImpl resp) { Object result = resp.result(); Object body = resp.body(); if (result instanceof RespBody) { return (RespBody) result; } else if (body instanceof RespBody) { return (RespBody) body; } else if (resp.mvc()) { byte[] bytes = ResponseRenderer.renderMvc((ReqImpl) req, resp); HttpUtils.postProcessResponse(resp); // the response might have been changed, so post-process again return new RespBodyBytes(bytes); } else if (body != null) { return new RespBodyBytes(Msc.toBytes(body)); } else if (result != null) { return resultToRespBody(resp, result); } else { throw U.rte("There's nothing to render!"); } }
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); }
static RespBody toRespBody(Req req, RespImpl resp) { try { return createRespBodyFromResult(req, resp); } catch (Throwable e) { HttpIO.INSTANCE.error(req, e, LogLevel.ERROR); try { return createRespBodyFromResult(req, resp); } catch (Exception e1) { Log.error("Internal rendering error!", e1); return new RespBodyBytes(HttpUtils.getErrorMessageAndSetCode(resp, e1).getBytes()); } } }
private static RespBody createRespBodyFromResult(Req req, RespImpl resp) { Object result = resp.result(); Object body = resp.body(); if (result instanceof RespBody) { return (RespBody) result; } else if (body instanceof RespBody) { return (RespBody) body; } else if (resp.mvc()) { byte[] bytes = ResponseRenderer.renderMvc((ReqImpl) req, resp); HttpUtils.postProcessResponse(resp); // the response might have been changed, so post-process again return new RespBodyBytes(bytes); } else if (body != null) { return new RespBodyBytes(Msc.toBytes(body)); } else if (result != null) { return resultToRespBody(resp, result); } else { throw U.rte("There's nothing to render!"); } }