List of usage examples for io.netty.channel ChannelFutureListener CLOSE
ChannelFutureListener CLOSE
To view the source code for io.netty.channel ChannelFutureListener CLOSE.
Click Source Link
From source file:org.apache.hyracks.http.server.HttpRequestCapacityController.java
License:Apache License
public static void reject(ChannelHandlerContext ctx) { HttpResponseEncoder encoder = new HttpResponseEncoder(); ChannelPromise promise = ctx.newPromise(); promise.addListener(ChannelFutureListener.CLOSE); DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.SERVICE_UNAVAILABLE); try {//from ww w . j a va2s. com encoder.write(ctx, response, ctx.voidPromise()); ctx.writeAndFlush(ctx.alloc().buffer(0), promise); } catch (Throwable th) {//NOSONAR try { LOGGER.log(Level.SEVERE, "Failure during request rejection", th); } catch (Throwable loggingFailure) {//NOSONAR } PromiseNotificationUtil.tryFailure(promise, th, null); } }
From source file:org.apache.hyracks.http.server.HttpRequestHandler.java
License:Apache License
@Override public Void call() throws Exception { try {/*from ww w . jav a 2s . c o m*/ ChannelFuture lastContentFuture = handle(); if (!HttpUtil.isKeepAlive(request.getHttpRequest())) { lastContentFuture.addListener(ChannelFutureListener.CLOSE); } } catch (Throwable th) { //NOSONAR LOGGER.log(Level.SEVERE, "Failure handling HTTP Request", th); ctx.close(); } finally { request.getHttpRequest().release(); } return null; }
From source file:org.apache.hyracks.http.server.HttpServerHandler.java
License:Apache License
protected void respond(ChannelHandlerContext ctx, HttpVersion httpVersion, HttpResponseStatus status) { DefaultHttpResponse response = new DefaultHttpResponse(httpVersion, status); ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); }
From source file:org.apache.qpid.jms.transports.netty.NettyServer.java
License:Apache License
private static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest request, FullHttpResponse response) {//w w w .j av a2 s. c o m // Generate an error page if response getStatus code is not OK (200). if (response.status().code() != 200) { ByteBuf buf = Unpooled.copiedBuffer(response.status().toString(), StandardCharsets.UTF_8); response.content().writeBytes(buf); buf.release(); HttpUtil.setContentLength(response, response.content().readableBytes()); } // Send the response and close the connection if necessary. ChannelFuture f = ctx.channel().writeAndFlush(response); if (!HttpUtil.isKeepAlive(request) || response.status().code() != 200) { f.addListener(ChannelFutureListener.CLOSE); } }
From source file:org.apache.rocketmq.namesrv.telnet.TelnetHandler.java
License:Apache License
@Override public void channelRead0(ChannelHandlerContext ctx, String request) throws Exception { // Generate and write a response. String response;/*w w w . j a v a 2s . c o m*/ boolean close = false; if (request.isEmpty()) { return; } else if ("bye".equals(request.toLowerCase())) { response = "Have a good day!\r\n"; close = true; } else { response = TelnetCommandUtil.doCmd(request); } // We do not need to write a ChannelBuffer here. // We know the encoder inserted at TelnetPipelineFactory will do the conversion. ChannelFuture future = ctx.write(response); // Close the connection after sending 'Have a good day!' // if the client has sent 'bye'. if (close) { future.addListener(ChannelFutureListener.CLOSE); } }
From source file:org.apache.tajo.HttpFileServerHandler.java
License:Apache License
@Override public void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception { if (request.getMethod() != HttpMethod.GET) { sendError(ctx, HttpResponseStatus.METHOD_NOT_ALLOWED); return;// w ww . j a v a 2 s . c om } final String path = sanitizeUri(request.getUri()); if (path == null) { sendError(ctx, HttpResponseStatus.FORBIDDEN); return; } File file = new File(path); if (file.isHidden() || !file.exists()) { sendError(ctx, HttpResponseStatus.NOT_FOUND); return; } if (!file.isFile()) { sendError(ctx, HttpResponseStatus.FORBIDDEN); return; } RandomAccessFile raf; try { raf = new RandomAccessFile(file, "r"); } catch (FileNotFoundException fnfe) { sendError(ctx, HttpResponseStatus.NOT_FOUND); return; } long fileLength = raf.length(); HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); HttpHeaders.setContentLength(response, fileLength); setContentTypeHeader(response); // Write the initial line and the header. ctx.write(response); // Write the content. ChannelFuture writeFuture; ChannelFuture lastContentFuture; if (ctx.pipeline().get(SslHandler.class) != null) { // Cannot use zero-copy with HTTPS. lastContentFuture = ctx.writeAndFlush(new HttpChunkedInput(new ChunkedFile(raf, 0, fileLength, 8192))); } else { // No encryption - use zero-copy. final FileRegion region = new DefaultFileRegion(raf.getChannel(), 0, fileLength); writeFuture = ctx.write(region); lastContentFuture = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT); writeFuture.addListener(new ChannelProgressiveFutureListener() { @Override public void operationProgressed(ChannelProgressiveFuture future, long progress, long total) throws Exception { LOG.trace(String.format("%s: %d / %d", path, progress, total)); } @Override public void operationComplete(ChannelProgressiveFuture future) throws Exception { region.release(); } }); } // Decide whether to close the connection or not. if (!HttpHeaders.isKeepAlive(request)) { // Close the connection when the whole content is written out. lastContentFuture.addListener(ChannelFutureListener.CLOSE); } }
From source file:org.apache.tajo.HttpFileServerHandler.java
License:Apache License
private static void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) { FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, Unpooled.copiedBuffer("Failure: " + status.toString() + "\r\n", CharsetUtil.UTF_8)); response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=UTF-8"); // Close the connection as soon as the error message is sent. ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); }
From source file:org.apache.tajo.pullserver.HttpDataServerHandler.java
License:Apache License
@Override public void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception { if (request.getMethod() != HttpMethod.GET) { sendError(ctx, HttpResponseStatus.METHOD_NOT_ALLOWED); return;/*from www . j av a 2 s.c om*/ } String base = ContainerLocalizer.USERCACHE + "/" + userName + "/" + ContainerLocalizer.APPCACHE + "/" + appId + "/output" + "/"; final Map<String, List<String>> params = new QueryStringDecoder(request.getUri()).parameters(); List<FileChunk> chunks = Lists.newArrayList(); List<String> taskIds = splitMaps(params.get("ta")); int sid = Integer.valueOf(params.get("sid").get(0)); int partitionId = Integer.valueOf(params.get("p").get(0)); for (String ta : taskIds) { File file = new File(base + "/" + sid + "/" + ta + "/output/" + partitionId); FileChunk chunk = new FileChunk(file, 0, file.length()); chunks.add(chunk); } FileChunk[] file = chunks.toArray(new FileChunk[chunks.size()]); // Write the content. if (file == null) { HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NO_CONTENT); if (!HttpHeaders.isKeepAlive(request)) { ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); } else { response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE); ctx.writeAndFlush(response); } } else { HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); ChannelFuture writeFuture = null; long totalSize = 0; for (FileChunk chunk : file) { totalSize += chunk.length(); } HttpHeaders.setContentLength(response, totalSize); if (HttpHeaders.isKeepAlive(request)) { response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE); } // Write the initial line and the header. writeFuture = ctx.write(response); for (FileChunk chunk : file) { writeFuture = sendFile(ctx, chunk); if (writeFuture == null) { sendError(ctx, HttpResponseStatus.NOT_FOUND); return; } } if (ctx.pipeline().get(SslHandler.class) == null) { writeFuture = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT); } else { ctx.flush(); } // Decide whether to close the connection or not. if (!HttpHeaders.isKeepAlive(request)) { // Close the connection when the whole content is written out. writeFuture.addListener(ChannelFutureListener.CLOSE); } } }
From source file:org.apache.tajo.pullserver.HttpDataServerHandler.java
License:Apache License
private void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) { FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, Unpooled.copiedBuffer("Failure: " + status.toString() + "\r\n", CharsetUtil.UTF_8)); response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=UTF-8"); // Close the connection as soon as the error message is sent. ctx.channel().writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); }