@Override public HttpServerResponse setStatusCode(int statusCode) { if (statusCode < 0) { throw new IllegalArgumentException("code: " + statusCode + " (expected: 0+)"); } synchronized (conn) { checkHeadWritten(); this.status = HttpResponseStatus.valueOf(statusCode); return this; } }
@Override public HttpServerResponse setStatusCode(int statusCode) { status = statusMessage != null ? new HttpResponseStatus(statusCode, statusMessage) : HttpResponseStatus.valueOf(statusCode); return this; }
@Override public void reject(int status) { reject(HttpResponseStatus.valueOf(status)); }
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { if (!ExceptionHandler.isResetByPeer(cause)) { log.error(cause.getMessage(), cause); FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.valueOf(500)); ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); } }
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { if (!ExceptionHandler.isResetByPeer(cause)) { log.error(cause.getMessage(), cause); FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.valueOf(500)); ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); } }
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { if (!ExceptionHandler.isResetByPeer(cause)) { log.error(cause.getMessage(), cause); FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.valueOf(500)); ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); } }
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { if (!ExceptionHandler.isResetByPeer(cause)) { log.error(cause.getMessage(), cause); FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.valueOf(500)); ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); } }
private void writeErrorResponse(int statusCode, String message, ChannelHandlerContext ctx) { FullHttpResponse httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.valueOf(statusCode), Unpooled.copiedBuffer(message, Charset.forName(SentinelConfig.charset()))); httpResponse.headers().set("Content-Type", "text/plain; charset=" + SentinelConfig.charset()); ctx.write(httpResponse); ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); }
private void outputContent(ChannelHandlerContext ctx, FullHttpRequest request, int code, String content, String mimeType) { FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.valueOf(code), Unpooled.wrappedBuffer(content.getBytes(Charset.forName("UTF-8")))); response.headers().set(Names.CONTENT_TYPE, mimeType); response.headers().set(Names.CONTENT_LENGTH, response.content().readableBytes()); response.headers().set(Names.SERVER, SERVER_VS); ChannelFuture future = ctx.writeAndFlush(response); if (!HttpHeaders.isKeepAlive(request)) { future.addListener(ChannelFutureListener.CLOSE); } }
@Override protected HttpMessage createMessage(String[] initialLine) { return new DefaultHttpResponse( HttpVersion.valueOf(initialLine[0]), HttpResponseStatus.valueOf(Integer.parseInt(initialLine[1]), initialLine[2]), validateHeaders); }
protected void renderPage(Response response, ModelAndView modelAndView) { var sw = new StringWriter(); try { WebContext.blade().templateEngine().render(modelAndView, sw); ByteBuf buffer = Unpooled.wrappedBuffer(sw.toString().getBytes("utf-8")); FullHttpResponse fullHttpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.valueOf(response.statusCode()), buffer); response.body(new RawBody(fullHttpResponse)); } catch (Exception e) { log.error("Render view error", e); } }
protected void renderPage(Response response, ModelAndView modelAndView) { var sw = new StringWriter(); try { WebContext.blade().templateEngine().render(modelAndView, sw); ByteBuf buffer = Unpooled.wrappedBuffer(sw.toString().getBytes("utf-8")); FullHttpResponse fullHttpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.valueOf(response.statusCode()), buffer); response.body(new RawBody(fullHttpResponse)); } catch (Exception e) { log.error("Render view error", e); } }
public Void handleStreamResponse(Response response, InputStream body, ChannelHandlerContext context, boolean keepAlive) { var httpResponse = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.valueOf(response.statusCode())); httpResponse.headers().set(TRANSFER_ENCODING, HttpHeaderValues.CHUNKED); setDefaultHeaders(httpResponse.headers()); for (Map.Entry<String, String> next : response.headers().entrySet()) { httpResponse.headers().set(next.getKey(), next.getValue()); } context.write(response); context.write(new ChunkedStream(body)); ChannelFuture lastContentFuture = context.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT); if (!keepAlive) { lastContentFuture.addListener(ChannelFutureListener.CLOSE); } return null; }
public Void handleStreamResponse(Response response, InputStream body, ChannelHandlerContext context, boolean keepAlive) { var httpResponse = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.valueOf(response.statusCode())); httpResponse.headers().set(TRANSFER_ENCODING, HttpHeaderValues.CHUNKED); setDefaultHeaders(httpResponse.headers()); for (Map.Entry<String, String> next : response.headers().entrySet()) { httpResponse.headers().set(next.getKey(), next.getValue()); } context.write(response); context.write(new ChunkedStream(body)); ChannelFuture lastContentFuture = context.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT); if (!keepAlive) { lastContentFuture.addListener(ChannelFutureListener.CLOSE); } return null; }
@Override public FullHttpResponse onByteBuf(Object byteBuf) { var httpResponse = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.valueOf(response.statusCode())); for (Map.Entry<String, String> next : response.headers().entrySet()) { httpResponse.headers().set(next.getKey(), next.getValue()); } // Write the initial line and the header. if (request.keepAlive()) { httpResponse.headers().set(HttpConst.CONNECTION, KEEP_ALIVE); } context.write(httpResponse, context.voidPromise()); ChannelFuture lastContentFuture = context.writeAndFlush(byteBuf); if (!request.keepAlive()) { lastContentFuture.addListener(ChannelFutureListener.CLOSE); } return null; }
private FullHttpResponse createResponseByByteBuf(Response response, ByteBuf byteBuf) { Map<String, String> headers = response.headers(); var httpResponse = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.valueOf(response.statusCode()), byteBuf); httpResponse.headers().set(CONTENT_LENGTH, httpResponse.content().readableBytes()); setDefaultHeaders(httpResponse.headers()); if (response.cookiesRaw().size() > 0) { this.appendCookie(response, httpResponse); } for (Map.Entry<String, String> next : headers.entrySet()) { httpResponse.headers().set(HttpConst.getAsciiString(next.getKey()), next.getValue()); } return httpResponse; }
private FullHttpResponse createResponseByByteBuf(Response response, ByteBuf byteBuf) { Map<String, String> headers = response.headers(); var httpResponse = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.valueOf(response.statusCode()), byteBuf); httpResponse.headers().set(CONTENT_LENGTH, httpResponse.content().readableBytes()); setDefaultHeaders(httpResponse.headers()); if (response.cookiesRaw().size() > 0) { this.appendCookie(response, httpResponse); } for (Map.Entry<String, String> next : headers.entrySet()) { httpResponse.headers().set(HttpConst.getAsciiString(next.getKey()), next.getValue()); } return httpResponse; }
@Override public FullHttpResponse onByteBuf(Object byteBuf) { var httpResponse = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.valueOf(response.statusCode())); for (Map.Entry<String, String> next : response.headers().entrySet()) { httpResponse.headers().set(next.getKey(), next.getValue()); } // Write the initial line and the header. if (request.keepAlive()) { httpResponse.headers().set(HttpConst.CONNECTION, KEEP_ALIVE); } context.write(httpResponse, context.voidPromise()); ChannelFuture lastContentFuture = context.writeAndFlush(byteBuf); if (!request.keepAlive()) { lastContentFuture.addListener(ChannelFutureListener.CLOSE); } return null; }
@Override public MutableHttpResponse status(HttpStatus status, String reason) { HttpResponseStatus nettyStatus; if (reason == null) { nettyStatus = HttpResponseStatus.valueOf(status.getCode()); } else { nettyStatus = new HttpResponseStatus(status.getCode(), reason); } DefaultFullHttpResponse fullHttpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, nettyStatus); return new NettyMutableHttpResponse(fullHttpResponse, ConversionService.SHARED); }
private HttpResponseStatus getStatus(HttpResponse response) { int statusCode = response.getStatusCode() != null ? response.getStatusCode() : 200; if (!StringUtils.isEmpty(response.getReasonPhrase())) { return new HttpResponseStatus(statusCode, response.getReasonPhrase()); } else { return HttpResponseStatus.valueOf(statusCode); } }