/** * Send the "405 Method Not Allowed" response. * * @param ctx The channel context to write the response to. */ private static void sendMethodNotAllowed(ChannelHandlerContext ctx) { FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, METHOD_NOT_ALLOWED); // close the connection as soon as the error message is sent. ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); }
private static void sendNotFound(ChannelHandlerContext ctx) { FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, NOT_FOUND); // close the connection as soon as the error message is sent. ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); } }
/** * Writes a simple error response message. * * @param ctx The channel context to write the response to. * @param status The response status. */ private static void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) { FullHttpResponse response = new DefaultFullHttpResponse( HTTP_1_1, status, Unpooled.copiedBuffer("Failure: " + status + "\r\n", CharsetUtil.UTF_8)); HttpHeaders.setHeader(response, CONTENT_TYPE, "text/plain; charset=UTF-8"); // close the connection as soon as the error message is sent. ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); } }
private void sendError(ChannelHandlerContext ctx, String error) { if (ctx.channel().isActive()) { DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR, Unpooled.wrappedBuffer(error.getBytes(ConfigConstants.DEFAULT_CHARSET))); response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain"); response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes()); ctx.writeAndFlush(response); } } }
byte[] bytes = ExceptionUtils.stringifyException(t).getBytes(ENCODING); DefaultFullHttpResponse response = new DefaultFullHttpResponse( HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR, Unpooled.wrappedBuffer(bytes));
/** * Send the "304 Not Modified" response. This response can be used when the * file timestamp is the same as what the browser is sending up. * * @param ctx The channel context to write the response to. */ public static void sendNotModified(ChannelHandlerContext ctx) { FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, NOT_MODIFIED); setDateHeader(response); // close the connection as soon as the error message is sent. ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); }
/** * Send the "304 Not Modified" response. This response can be used when the * file timestamp is the same as what the browser is sending up. * * @param ctx The channel context to write the response to. */ public static void sendNotModified(ChannelHandlerContext ctx) { FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, NOT_MODIFIED); setDateHeader(response); // close the connection as soon as the error message is sent. ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); }
/** * Send the "304 Not Modified" response. This response can be used when the * file timestamp is the same as what the browser is sending up. * * @param ctx The channel context to write the response to. */ public static void sendNotModified(ChannelHandlerContext ctx) { FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, NOT_MODIFIED); setDateHeader(response); // close the connection as soon as the error message is sent. ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); }
public static HttpResponse getResponse(HttpResponseStatus status, @Nullable String message) { ByteBuf messageByteBuf = message == null ? Unpooled.buffer(0) : Unpooled.wrappedBuffer(message.getBytes(ENCODING)); FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, messageByteBuf); response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=" + ENCODING.name()); response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes()); return response; }
public static HttpResponse getResponse(HttpResponseStatus status, @Nullable String message) { ByteBuf messageByteBuf = message == null ? Unpooled.buffer(0) : Unpooled.wrappedBuffer(message.getBytes(ENCODING)); FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, messageByteBuf); response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=" + ENCODING.name()); response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes()); return response; }
public static HttpResponse getRedirectResponse(String redirectAddress, String path, HttpResponseStatus code) { checkNotNull(redirectAddress, "Redirect address"); checkNotNull(path, "Path"); String newLocation = String.format("%s%s", redirectAddress, path); HttpResponse redirectResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, code); redirectResponse.headers().set(HttpHeaders.Names.LOCATION, newLocation); redirectResponse.headers().set(HttpHeaders.Names.CONTENT_LENGTH, 0); return redirectResponse; }
public static HttpResponse getUnavailableResponse() { String result = "Service temporarily unavailable due to an ongoing leader election. Please refresh."; byte[] bytes = result.getBytes(ConfigConstants.DEFAULT_CHARSET); HttpResponse unavailableResponse = new DefaultFullHttpResponse( HttpVersion.HTTP_1_1, HttpResponseStatus.SERVICE_UNAVAILABLE, Unpooled.wrappedBuffer(bytes)); unavailableResponse.headers().set(HttpHeaders.Names.CONTENT_LENGTH, bytes.length); unavailableResponse.headers().set(HttpHeaders.Names.CONTENT_TYPE, MimeTypes.getMimeTypeForExtension("txt")); return unavailableResponse; }
public static HttpResponse getErrorResponse(Throwable throwable, HttpResponseStatus status) { byte[] bytes = ExceptionUtils.stringifyException(throwable).getBytes(ENCODING); FullHttpResponse response = new DefaultFullHttpResponse( HttpVersion.HTTP_1_1, status, Unpooled.wrappedBuffer(bytes)); response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=" + ENCODING.name()); response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes()); return response; } }
public static HttpResponse getUnavailableResponse() { String result = "Service temporarily unavailable due to an ongoing leader election. Please refresh."; byte[] bytes = result.getBytes(ConfigConstants.DEFAULT_CHARSET); HttpResponse unavailableResponse = new DefaultFullHttpResponse( HttpVersion.HTTP_1_1, HttpResponseStatus.SERVICE_UNAVAILABLE, Unpooled.wrappedBuffer(bytes)); unavailableResponse.headers().set(HttpHeaders.Names.CONTENT_LENGTH, bytes.length); unavailableResponse.headers().set(HttpHeaders.Names.CONTENT_TYPE, MimeTypes.getMimeTypeForExtension("txt")); return unavailableResponse; }
public static HttpResponse getUnavailableResponse() { String result = "Service temporarily unavailable due to an ongoing leader election. Please refresh."; byte[] bytes = result.getBytes(ConfigConstants.DEFAULT_CHARSET); HttpResponse unavailableResponse = new DefaultFullHttpResponse( HttpVersion.HTTP_1_1, HttpResponseStatus.SERVICE_UNAVAILABLE, Unpooled.wrappedBuffer(bytes)); unavailableResponse.headers().set(HttpHeaders.Names.CONTENT_LENGTH, bytes.length); unavailableResponse.headers().set(HttpHeaders.Names.CONTENT_TYPE, MimeTypes.getMimeTypeForExtension("txt")); return unavailableResponse; }
public static HttpResponse getErrorResponse(Throwable throwable, HttpResponseStatus status) { byte[] bytes = ExceptionUtils.stringifyException(throwable).getBytes(ENCODING); FullHttpResponse response = new DefaultFullHttpResponse( HttpVersion.HTTP_1_1, status, Unpooled.wrappedBuffer(bytes)); response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=" + ENCODING.name()); response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes()); return response; } }
/** * Writes a simple error response message. * * @param ctx The channel context to write the response to. * @param status The response status. */ public static void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) { FullHttpResponse response = new DefaultFullHttpResponse( HTTP_1_1, status, Unpooled.copiedBuffer("Failure: " + status + "\r\n", CharsetUtil.UTF_8)); response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8"); // close the connection as soon as the error message is sent. ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); }
@Override protected void channelRead0(ChannelHandlerContext ctx, RoutedRequest routed) throws Exception { HttpResponse response = new DefaultFullHttpResponse( HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(encodedText)); response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, encodedText.length); response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain"); KeepAliveWrite.flush(ctx, routed.getRequest(), response); } }
@Override protected void channelRead0(ChannelHandlerContext ctx, RoutedRequest routed) throws Exception { HttpResponse response = new DefaultFullHttpResponse( HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(encodedText)); response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, encodedText.length); response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain"); KeepAliveWrite.flush(ctx, routed.getRequest(), response); } }
private void sendError(ChannelHandlerContext ctx, String error) { if (ctx.channel().isActive()) { DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR, Unpooled.wrappedBuffer(error.getBytes(ConfigConstants.DEFAULT_CHARSET))); response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain"); response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes()); ctx.writeAndFlush(response); } } }