@Override public String toString() { StringBuilder buf = new StringBuilder(); buf.append(getClass().getSimpleName()); buf.append("(chunked: "); buf.append(isChunked()); buf.append(')'); buf.append(StringUtil.NEWLINE); buf.append(getProtocolVersion().getText()); buf.append(' '); buf.append(getStatus().toString()); buf.append(StringUtil.NEWLINE); appendHeaders(buf); // Remove the last newline. buf.setLength(buf.length() - StringUtil.NEWLINE.length()); return buf.toString(); } }
/** * Sets the HTTP response status header. */ public static void setStatus(int spdyVersion, SpdyHeadersFrame frame, HttpResponseStatus status) { frame.headers().set(HttpNames.STATUS, status.toString()); }
/** * Writes all error responses to the client. * * TODO REST-Server 1. collect error stats * * @param messageEvent - for retrieving the channel details * @param status - error code * @param message - error message */ public static void writeErrorResponse(MessageEvent messageEvent, HttpResponseStatus status, String message) { // Create the Response object HttpResponse response = new DefaultHttpResponse(HTTP_1_1, status); response.setHeader(CONTENT_TYPE, "text/plain; charset=UTF-8"); response.setContent(ChannelBuffers.copiedBuffer("Failure: " + status.toString() + ". " + message + "\r\n", CharsetUtil.UTF_8)); response.setHeader(CONTENT_LENGTH, response.getContent().readableBytes()); // Write the response to the Netty Channel messageEvent.getChannel().write(response); } }
private String getErrorHtml(HttpResponseStatus status, Throwable throwable) { String stackTrace = null; if (throwable != null) { StringWriter sw = new StringWriter(); PrintWriter s = new PrintWriter(sw); throwable.printStackTrace(s); stackTrace = sw.toString(); } return "<html>\n" + "<head>\n" + "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>\n" + "<title>Error " + status.toString() + "</title>\n" + "</head>\n" + "<body>\n" + "<h2>HTTP ERROR: " + status.getCode() + "</h2>\n" + "<p>Problem accessing [" + request.getMethod() + "]" + request.getUri() + ". Reason:\n" + "<pre> " + status.getReasonPhrase() + (stackTrace != null ? "\n\n" + stackTrace : "") + "</pre></p>\n" + "<hr /><i><small>Powered by FastcatSearch</small></i>\n" + "</body>\n" + "</html>"; }
private void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) { HttpResponse response = new DefaultHttpResponse(HTTP_1_1, status); response.setHeader(CONTENT_TYPE, "text/plain; charset=UTF-8"); response.setContent(ChannelBuffers.copiedBuffer( "Failure: " + status.toString() + "\r\n", CharsetUtil.UTF_8)); // Close the connection as soon as the error message is sent. ctx.getChannel().write(response).addListener(ChannelFutureListener.CLOSE); } }
protected void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) { HttpResponse response = new DefaultHttpResponse(HTTP_1_1, status); response.setHeader(CONTENT_TYPE, "text/plain; charset=UTF-8"); response.setContent(ChannelBuffers.copiedBuffer( "Failure: " + status.toString() + "\r\n", CharsetUtil.UTF_8)); // Close the connection as soon as the error message is sent. ctx.getChannel().write(response).addListener(ChannelFutureListener.CLOSE); } }
private void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) { HttpResponse response = new DefaultHttpResponse(HTTP_1_1, status); response.setHeader(CONTENT_TYPE, "text/plain; charset=UTF-8"); response.setContent(ChannelBuffers.copiedBuffer( "Failure: " + status.toString() + "\r\n", CharsetUtil.UTF_8)); // Close the connection as soon as the error message is sent. ctx.getChannel().write(response).addListener(ChannelFutureListener.CLOSE); }
private void sendError(final ChannelHandlerContext ctx, final HttpResponseStatus status) { final HttpResponse response = new DefaultHttpResponse(HTTP_1_1, status); setHeader(response, CONTENT_TYPE, "text/plain; charset=UTF-8"); response.setContent(ChannelBuffers.copiedBuffer( "Failure: " + status.toString() + "\r\n", CharsetUtil.UTF_8)); // Close the connection as soon as the error message is sent. ctx.getChannel().write(response).addListener(ChannelFutureListener.CLOSE); } }
@Override protected ChannelBuffer extractResponse(Object message) throws TTransportException { if (!(message instanceof HttpResponse)) { return null; } HttpResponse httpResponse = (HttpResponse) message; if (!httpResponse.getStatus().equals(HttpResponseStatus.OK)) { throw new TTransportException("HTTP response had non-OK status: " + httpResponse .getStatus().toString()); } ChannelBuffer content = httpResponse.getContent(); if (!content.readable()) { return null; } return content; }
private void sendHttpResponse(ChannelHandlerContext ctx, HttpRequest req, HttpResponse res) { // Generate an error page if response status code is not OK (200). if (res.getStatus().getCode() != 200) { res.setContent( ChannelBuffers.copiedBuffer( res.getStatus().toString(), CharsetUtil.UTF_8)); HttpHeaders.setContentLength(res, res.getContent().readableBytes()); } // Send the response and close the connection if necessary. ChannelFuture f = ctx.getChannel().write(res); if (!HttpHeaders.isKeepAlive(req) || res.getStatus().getCode() != 200) { f.addListener(ChannelFutureListener.CLOSE); } }
private void sendHttpResponse(ChannelHandlerContext ctx, HttpRequest req, HttpResponse res) { // Generate an error page if response status code is not OK (200). if (res.getStatus().getCode() != 200) { res.setContent( ChannelBuffers.copiedBuffer( res.getStatus().toString(), CharsetUtil.UTF_8)); setContentLength(res, res.getContent().readableBytes()); } // Send the response and close the connection if necessary. ChannelFuture f = ctx.getChannel().write(res); if (!isKeepAlive(req) || res.getStatus().getCode() != 200) { f.addListener(ChannelFutureListener.CLOSE); } }
private void sendHttpResponse(ChannelHandlerContext ctx, HttpRequest req, HttpResponse res) { // Generate an error page if response status code is not OK (200). if (res.getStatus().getCode() != HttpResponseStatus.OK.getCode()) { res.setContent( ChannelBuffers.copiedBuffer( res.getStatus().toString(), CharsetUtil.UTF_8)); setContentLength(res, res.getContent().readableBytes()); } // Send the response and close the connection if necessary. ChannelFuture f = ctx.getChannel().write(res); if (!isKeepAlive(req) || res.getStatus().getCode() != HttpResponseStatus.OK.getCode()) { f.addListener(ChannelFutureListener.CLOSE); } } }