@Override public void sendError(int status, String message) throws IOException { if (committed) { throw new IllegalStateException(); } HttpResponseStatus responseStatus = null; if (message != null) { responseStatus = new HttpResponseStatus(status, message); } else { responseStatus = HttpResponseStatus.valueOf(status); } DefaultHttpResponse response = new DefaultHttpResponse(HTTP_1_1, responseStatus); if (keepAlive) { // Add keep alive and content length if needed response.headers() .add(Names.CONNECTION, Values.KEEP_ALIVE) .add(Names.CONTENT_LENGTH, 0); } channel.write(response); committed = true; }
response.headers().add("Content-Type", "text/plain");
response.headers().add("Content-Type", "text/plain");
@Override public void sendError(int status, String message) throws IOException { if (committed) { throw new IllegalStateException(); } HttpResponseStatus responseStatus = null; if (message != null) { responseStatus = new HttpResponseStatus(status, message); } else { responseStatus = HttpResponseStatus.valueOf(status); } DefaultHttpResponse response = new DefaultHttpResponse(HTTP_1_1, responseStatus); if (keepAlive) { // Add keep alive and content length if needed response.headers() .add(Names.CONNECTION, Values.KEEP_ALIVE) .add(Names.CONTENT_LENGTH, 0); } channel.write(response); committed = true; }
public void invalidRequestSent(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception { Channel channel = ctx.getChannel(); if (channel == null || !channel.isOpen()) { log.debug("Not writing any response, channel is already closed.", e.getCause()); return; } final DefaultHttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_0, HttpResponseStatus.BAD_REQUEST); response.headers().add(HttpHeaders.Names.CONTENT_TYPE, "text/plain"); response.headers().add(HttpHeaders.Names.CONNECTION, "close"); final ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(); new ChannelBufferOutputStream(buffer).writeBytes("Your client has sent a malformed or illegal request.\n"); response.setContent(buffer); final ChannelFuture channelFuture = channel.write(response); channelFuture.addListener(ChannelFutureListener.CLOSE); }
/** * Writes a HTTP 404 response to the client. * * @param ctx * The Netty context. * @param requestedUri * The URI requested by the client. */ private void writeResourceNotFound(final ChannelHandlerContext ctx, String requestedUri) { // Write the HTTP header to the client. DefaultHttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_0, HttpResponseStatus.NOT_FOUND); response.headers().add("Content-Type", "text/plain"); // Send the 404 message to the client. ChannelBuffer buffer = ChannelBuffers.copiedBuffer("The requested resource does not exist: " + requestedUri, CharsetUtil.UTF_8); response.setContent(buffer); // Write the header. Use a new future because the future we've been // passed is for upstream. ChannelFuture headerFuture = Channels.future(ctx.getChannel()); Channels.write(ctx, headerFuture, response); // Wait for the previous operation to finish and then close the channel. headerFuture.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) { ctx.getChannel().close(); } }); }
/** * Writes a HTTP 410 response to the client. * * @param ctx * The Netty context. * @param requestedUri * The URI requested by the client. */ private void writeResourceGone(final ChannelHandlerContext ctx, String requestedUri) { // Write the HTTP header to the client. DefaultHttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_0, HttpResponseStatus.GONE); response.headers().add("Content-Type", "text/plain"); // Send the 410 message to the client. ChannelBuffer buffer = ChannelBuffers.copiedBuffer("The requested resource is no longer available: " + requestedUri, CharsetUtil.UTF_8); response.setContent(buffer); // Write the header. Use a new future because the future we've been // passed is for upstream. ChannelFuture headerFuture = Channels.future(ctx.getChannel()); Channels.write(ctx, headerFuture, response); // Wait for the previous operation to finish and then close the channel. headerFuture.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) { ctx.getChannel().close(); } }); }