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); } } }
response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
/** * Sends a simple DELETE request to the given path. You only specify the $path part of * http://$host:$host/$path. * * @param path The $path to DELETE (http://$host:$host/$path) */ public void sendDeleteRequest(String path, FiniteDuration timeout) throws TimeoutException, InterruptedException { if (!path.startsWith("/")) { path = "/" + path; } HttpRequest getRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.DELETE, path); getRequest.headers().set(HttpHeaders.Names.HOST, host); getRequest.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE); sendRequest(getRequest, timeout); }
/** * Sends a simple GET request to the given path. You only specify the $path part of * http://$host:$host/$path. * * @param path The $path to GET (http://$host:$host/$path) */ public void sendGetRequest(String path, FiniteDuration timeout) throws TimeoutException, InterruptedException { if (!path.startsWith("/")) { path = "/" + path; } HttpRequest getRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, path); getRequest.headers().set(HttpHeaders.Names.HOST, host); getRequest.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE); sendRequest(getRequest, timeout); }
/** * Sends a simple PATCH request to the given path. You only specify the $path part of * http://$host:$host/$path. * * @param path The $path to PATCH (http://$host:$host/$path) */ public void sendPatchRequest(String path, FiniteDuration timeout) throws TimeoutException, InterruptedException { if (!path.startsWith("/")) { path = "/" + path; } HttpRequest getRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.PATCH, path); getRequest.headers().set(HttpHeaders.Names.HOST, host); getRequest.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE); sendRequest(getRequest, timeout); }
/** * Sets the "date" header for the HTTP response. * * @param response HTTP response */ public static void setDateHeader(FullHttpResponse response) { SimpleDateFormat dateFormatter = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US); dateFormatter.setTimeZone(GMT_TIMEZONE); Calendar time = new GregorianCalendar(); response.headers().set(DATE, dateFormatter.format(time.getTime())); }
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; }
/** * Sets the content type header for the HTTP Response. * * @param response HTTP response * @param file file to extract content type */ public static void setContentTypeHeader(HttpResponse response, File file) { String mimeType = MimeTypes.getMimeTypeForFileName(file.getName()); String mimeFinal = mimeType != null ? mimeType : MimeTypes.getDefaultMimeType(); response.headers().set(CONTENT_TYPE, mimeFinal); } }
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; }
/** * Sets the content type header for the HTTP Response. * * @param response HTTP response * @param file file to extract content type */ public static void setContentTypeHeader(HttpResponse response, File file) { String mimeType = MimeTypes.getMimeTypeForFileName(file.getName()); String mimeFinal = mimeType != null ? mimeType : MimeTypes.getDefaultMimeType(); response.headers().set(CONTENT_TYPE, mimeFinal); } }
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 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 ChannelFuture flush(Channel ch, HttpRequest req, HttpResponse res) { if (!HttpHeaders.isKeepAlive(req)) { return ch.writeAndFlush(res).addListener(ChannelFutureListener.CLOSE); } else { res.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE); return ch.writeAndFlush(res); } } }
public static ChannelFuture flush(ChannelHandlerContext ctx, HttpRequest request, HttpResponse response) { if (!HttpHeaders.isKeepAlive(request)) { return ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); } else { response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE); return ctx.writeAndFlush(response); } }
public static ChannelFuture flush(Channel ch, HttpRequest req, HttpResponse res) { if (!HttpHeaders.isKeepAlive(req)) { return ch.writeAndFlush(res).addListener(ChannelFutureListener.CLOSE); } else { res.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE); return ch.writeAndFlush(res); } } }
/** * 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); }
public static ChannelFuture flush(ChannelHandlerContext ctx, HttpRequest request, HttpResponse response) { if (!HttpHeaders.isKeepAlive(request)) { return ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); } else { response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE); return ctx.writeAndFlush(response); } }