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:com.yeetor.androidcontrol.WSSocketHandler.java
License:Open Source License
private static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, DefaultFullHttpResponse res) {// w w w . ja va 2 s . c om // if (res.getStatus().code() != 200) { ByteBuf buf = Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8); res.content().writeBytes(buf); buf.release(); } // ?Keep-Alive ChannelFuture f = ctx.channel().writeAndFlush(res); if (!isKeepAlive(req) || res.getStatus().code() != 200) { f.addListener(ChannelFutureListener.CLOSE); } }
From source file:com.yeetor.server.HttpServer.java
License:Open Source License
public void writeResponse(ChannelHandlerContext ctx, HttpRequest request, HttpResponse response, Object... contents) {//ww w.j ava 2s . c om if (HttpUtil.isKeepAlive(request)) { response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE); } ctx.write(response); for (Object content : contents) { ctx.write(content); } ChannelFuture future = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT); if (!HttpUtil.isKeepAlive(request)) { future.addListener(ChannelFutureListener.CLOSE); } }
From source file:com.zank.websocket.server.ServerHandler.java
License:Apache License
private static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) { if (res.status().code() != 200) { ByteBuf buf = Unpooled.copiedBuffer(res.status().toString(), CharsetUtil.UTF_8); res.content().writeBytes(buf);//from w w w .ja v a 2 s. co m buf.release(); HttpHeaderUtil.setContentLength(res, res.content().readableBytes()); } ChannelFuture f = ctx.channel().writeAndFlush(res); if (!HttpHeaderUtil.isKeepAlive(req) || res.status().code() != 200) { f.addListener(ChannelFutureListener.CLOSE); } }
From source file:com.zhucode.longio.transport.netty.HttpHandler.java
License:Open Source License
private ChannelFuture sendForHttp(ChannelHandlerContext ctx, byte[] bytes) { FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(bytes)); response.headers().set(CONTENT_TYPE, "text/json; charset=utf-8"); response.headers().set(CONTENT_LENGTH, response.content().readableBytes()); boolean ka = ctx.attr(keepAlive).get(); if (!ka) {/*from w w w . java 2 s . com*/ return ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); } else { response.headers().set(CONNECTION, Values.KEEP_ALIVE); return ctx.writeAndFlush(response); } }
From source file:de.dfki.kiara.http.HttpHandler.java
License:Open Source License
@Override public ListenableFuture<Void> send(TransportMessage message) { if (message == null) { throw new NullPointerException("msg"); }//from w ww . j av a 2 s . c o m if (state != State.CONNECTED || channel == null) { throw new IllegalStateException("state=" + state.toString() + " channel=" + channel); } HttpMessage httpMsg; boolean keepAlive = true; if (message instanceof HttpRequestMessage) { HttpRequestMessage msg = (HttpRequestMessage) message; httpMsg = msg.finalizeRequest(); if (logger.isDebugEnabled()) { logger.debug("SEND CONTENT: {}", msg.getContent().content().toString(StandardCharsets.UTF_8)); } } else if (message instanceof HttpResponseMessage) { HttpResponseMessage msg = (HttpResponseMessage) message; httpMsg = msg.finalizeResponse(); keepAlive = HttpHeaders.isKeepAlive(httpMsg); if (logger.isDebugEnabled()) { logger.debug("SEND CONTENT: {}", msg.getContent().content().toString(StandardCharsets.UTF_8)); } } else { throw new IllegalArgumentException("msg is neither of type HttpRequestMessage nor HttpResponseMessage"); } ChannelFuture result = channel.writeAndFlush(httpMsg); if (!keepAlive) { // If keep-alive is off, close the connection once the content is fully written. channel.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); } return new ListenableConstantFutureAdapter<>(result, null); }
From source file:de.eightnine.rec.HttpStaticFileServerHandler.java
License:Apache License
@Override public void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception { if (!request.getDecoderResult().isSuccess()) { sendError(ctx, BAD_REQUEST);/*ww w.j av a 2 s . c o m*/ return; } if (request.getMethod() != GET) { sendError(ctx, METHOD_NOT_ALLOWED); return; } final String uri = request.getUri(); final String path = sanitizeUri(uri); if (path == null) { sendError(ctx, FORBIDDEN); return; } File file = new File(path); if (file.isHidden() || !file.exists()) { sendError(ctx, NOT_FOUND); return; } if (file.isDirectory()) { if (uri.endsWith("/")) { sendListing(ctx, file); } else { sendRedirect(ctx, uri + '/'); } return; } if (!file.isFile()) { sendError(ctx, FORBIDDEN); return; } // Cache Validation String ifModifiedSince = request.headers().get(IF_MODIFIED_SINCE); if (ifModifiedSince != null && !ifModifiedSince.isEmpty()) { SimpleDateFormat dateFormatter = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US); Date ifModifiedSinceDate = dateFormatter.parse(ifModifiedSince); // Only compare up to the second because the datetime format we send to the client // does not have milliseconds long ifModifiedSinceDateSeconds = ifModifiedSinceDate.getTime() / 1000; long fileLastModifiedSeconds = file.lastModified() / 1000; if (ifModifiedSinceDateSeconds == fileLastModifiedSeconds) { sendNotModified(ctx); return; } } RandomAccessFile raf; try { raf = new RandomAccessFile(file, "r"); } catch (FileNotFoundException ignore) { sendError(ctx, NOT_FOUND); return; } long fileLength = raf.length(); HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK); HttpHeaders.setContentLength(response, fileLength); setContentTypeHeader(response, file); setDateAndCacheHeaders(response, file); if (HttpHeaders.isKeepAlive(request)) { response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE); } // Write the initial line and the header. ctx.write(response); // Write the content. ChannelFuture sendFileFuture; if (ctx.pipeline().get(SslHandler.class) == null) { sendFileFuture = ctx.write(new DefaultFileRegion(raf.getChannel(), 0, fileLength), ctx.newProgressivePromise()); } else { sendFileFuture = ctx.write(new HttpChunkedInput(new ChunkedFile(raf, 0, fileLength, 8192)), ctx.newProgressivePromise()); } sendFileFuture.addListener(new ChannelProgressiveFutureListener() { @Override public void operationProgressed(ChannelProgressiveFuture future, long progress, long total) { if (total < 0) { // total unknown System.err.println(future.channel() + " Transfer progress: " + progress); } else { System.err.println(future.channel() + " Transfer progress: " + progress + " / " + total); } } @Override public void operationComplete(ChannelProgressiveFuture future) { System.err.println(future.channel() + " Transfer complete."); } }); // Write the end marker ChannelFuture lastContentFuture = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT); // 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:de.jackwhite20.apex.util.ChannelUtil.java
License:Open Source License
public static void closeOnFlush(Channel ch) { if (ch != null && ch.isActive()) { ch.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); }/* w w w. j a v a2 s . c om*/ }
From source file:de.jackwhite20.comix.handler.DownstreamHandler.java
License:Open Source License
@Override public void channelInactive(ChannelHandlerContext ctx) { if (upstreamChannel != null) { if (upstreamChannel.isActive()) { upstreamChannel.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); }//from w w w . jav a 2 s .c o m upstreamBytesOut = 0; downstreamBytesIn = 0; } }
From source file:de.jackwhite20.comix.handler.DownstreamHandler.java
License:Open Source License
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { Channel ch = ctx.channel();//from www . j av a 2 s .c o m if (ch.isActive()) { ch.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); } }
From source file:de.jackwhite20.comix.handler.HandshakeHandler.java
License:Open Source License
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { Channel ch = ctx.channel();/*from w ww . j av a 2s .c o m*/ if (ch.isActive()) { ch.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); } }