Example usage for io.netty.channel ChannelFutureListener CLOSE

List of usage examples for io.netty.channel ChannelFutureListener CLOSE

Introduction

In this page you can find the example usage for io.netty.channel ChannelFutureListener CLOSE.

Prototype

ChannelFutureListener CLOSE

To view the source code for io.netty.channel ChannelFutureListener CLOSE.

Click Source Link

Document

A ChannelFutureListener that closes the Channel which is associated with the specified ChannelFuture .

Usage

From source file:HttpUploadServerHandler.java

License:Apache License

private void writeResponse(Channel channel) {
    // Convert the response content to a ChannelBuffer.
    ByteBuf buf = copiedBuffer(responseContent.toString(), CharsetUtil.UTF_8);
    responseContent.setLength(0);/*from   w ww  .  j av  a  2  s .  c om*/

    // Decide whether to close the connection or not.
    boolean close = HttpHeaders.Values.CLOSE.equalsIgnoreCase(request.headers().get(CONNECTION))
            || request.getProtocolVersion().equals(HttpVersion.HTTP_1_0)
                    && !HttpHeaders.Values.KEEP_ALIVE.equalsIgnoreCase(request.headers().get(CONNECTION));

    // Build the response object.
    FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, buf);
    response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");

    if (!close) {
        // There's no need to add 'Content-Length' header
        // if this is the last response.
        response.headers().set(CONTENT_LENGTH, String.valueOf(buf.readableBytes()));
    }

    Set<Cookie> cookies;
    String value = request.headers().get(COOKIE);
    if (value == null) {
        cookies = Collections.emptySet();
    } else {
        cookies = CookieDecoder.decode(value);
    }
    if (!cookies.isEmpty()) {
        // Reset the cookies if necessary.
        for (Cookie cookie : cookies) {
            response.headers().add(SET_COOKIE, ServerCookieEncoder.encode(cookie));
        }
    }
    // Write the response.
    ChannelFuture future = channel.write(response);
    // Close the connection after the write operation is done if necessary.
    if (close) {
        future.addListener(ChannelFutureListener.CLOSE);
    }
}

From source file:HelloWorldHttp1Handler.java

License:Apache License

@Override
public void messageReceived(ChannelHandlerContext ctx, HttpRequest req) throws Exception {
    if (HttpHeaderUtil.is100ContinueExpected(req)) {
        ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE));
    }/*from  w  w w .  j a  va 2  s.  c  o m*/
    boolean keepAlive = HttpHeaderUtil.isKeepAlive(req);

    ByteBuf content = ctx.alloc().buffer();
    content.writeBytes(HelloWorldHttp2Handler.RESPONSE_BYTES.duplicate());

    FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, content);
    response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");
    response.headers().set(CONTENT_LENGTH, response.content().readableBytes());

    if (!keepAlive) {
        ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
    } else {
        response.headers().set(CONNECTION, Values.KEEP_ALIVE);
        ctx.writeAndFlush(response);
    }
}

From source file:NettyHttpTransportSourceHandler.java

License:Apache License

/**
 * Closes the specified channel after all queued write requests are flushed.
 *//*from  ww  w  . j  av  a2  s  . c  o  m*/
static void closeOnFlush(Channel ch) {
    if (ch.isActive()) {
        ch.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
    }
}

From source file:adalightserver.http.HttpServer.java

License:Apache License

private static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) {
    // res.headers().set("Access-Control-Allow-Methods", "POST, OPTIONS, GET");
    // res.headers().set("Access-Control-Allow-Origin", "*");
    // res.headers().set("Access-Control-Allow-Headers", "*");

    // Generate an error page if response getStatus code is not OK (200).
    if (res.getStatus().code() != 200) {
        ByteBuf buf = Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8);
        res.content().writeBytes(buf);//  w  w w. j a v  a2  s  .  c  om
        buf.release();
        HttpHeaders.setContentLength(res, res.content().readableBytes());
    }
    // Send the response and close the connection if necessary.
    ChannelFuture f = ctx.channel().writeAndFlush(res);
    if (!HttpHeaders.isKeepAlive(req) || res.getStatus().code() != 200) {
        f.addListener(ChannelFutureListener.CLOSE);
    }
}

From source file:alluxio.network.protocol.RPCMessageDecoder.java

License:Apache License

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    LOG.error("Error in decoding message. Possible Client/DataServer version incompatibility: "
            + cause.getMessage());/*from   w ww .j  a  va  2s  .  c om*/
    // Return an error message to the client.
    ctx.channel().writeAndFlush(new RPCErrorResponse(RPCResponse.Status.DECODE_ERROR))
            .addListener(ChannelFutureListener.CLOSE);
}

From source file:alluxio.worker.netty.BlockDataServerHandler.java

License:Apache License

/**
 * Handles a {@link RPCBlockReadRequest} by reading the data through a {@link BlockReader}
 * provided by the block worker. This method assumes the data is available in the local storage
 * of the worker and returns an error status if the data is not available.
 *
 * @param ctx The context of this request which handles the result of this operation
 * @param req The initiating {@link RPCBlockReadRequest}
 * @throws IOException if an I/O error occurs when reading the data requested
 *//* w  w  w.j  a  v  a 2s . c o m*/
void handleBlockReadRequest(final ChannelHandlerContext ctx, final RPCBlockReadRequest req) throws IOException {
    final long blockId = req.getBlockId();
    final long offset = req.getOffset();
    final long len = req.getLength();
    final long lockId = req.getLockId();
    final long sessionId = req.getSessionId();

    BlockReader reader = null;
    DataBuffer buffer;
    try {
        req.validate();
        reader = mWorker.readBlockRemote(sessionId, blockId, lockId);
        final long fileLength = reader.getLength();
        validateBounds(req, fileLength);
        final long readLength = returnLength(offset, len, fileLength);
        buffer = getDataBuffer(req, reader, readLength);
        RPCBlockReadResponse resp = new RPCBlockReadResponse(blockId, offset, readLength, buffer,
                RPCResponse.Status.SUCCESS);
        ChannelFuture future = ctx.writeAndFlush(resp);
        future.addListener(ChannelFutureListener.CLOSE);
        future.addListener(new ClosableResourceChannelListener(reader));
        future.addListener(new ReleasableResourceChannelListener(buffer));
        mWorker.accessBlock(sessionId, blockId);
        LOG.info("Preparation for responding to remote block request for: {} done.", blockId);
    } catch (Exception e) {
        LOG.error("Exception reading block {}", blockId, e);
        RPCBlockReadResponse resp;
        if (e instanceof BlockDoesNotExistException) {
            resp = RPCBlockReadResponse.createErrorResponse(req, RPCResponse.Status.FILE_DNE);
        } else {
            resp = RPCBlockReadResponse.createErrorResponse(req, RPCResponse.Status.UFS_READ_FAILED);
        }
        ChannelFuture future = ctx.writeAndFlush(resp);
        future.addListener(ChannelFutureListener.CLOSE);
        if (reader != null) {
            reader.close();
        }
    }
}

From source file:alluxio.worker.netty.BlockDataServerHandler.java

License:Apache License

/**
 * Handles a {@link RPCBlockWriteRequest} by writing the data through a {@link BlockWriter}
 * provided by the block worker. This method takes care of requesting space and creating the
 * block if necessary./*  w  w  w . ja v a2 s  . c  o m*/
 *
 * @param ctx The context of this request which handles the result of this operation
 * @param req The initiating {@link RPCBlockWriteRequest}
 * @throws IOException if an I/O exception occurs when writing the data
 */
// TODO(hy): This write request handler is very simple in order to be stateless. Therefore, the
// block file is opened and closed for every request. If this is too slow, then this handler
// should be optimized to keep state.
void handleBlockWriteRequest(final ChannelHandlerContext ctx, final RPCBlockWriteRequest req)
        throws IOException {
    final long sessionId = req.getSessionId();
    final long blockId = req.getBlockId();
    final long offset = req.getOffset();
    final long length = req.getLength();
    final DataBuffer data = req.getPayloadDataBuffer();

    BlockWriter writer = null;
    try {
        req.validate();
        ByteBuffer buffer = data.getReadOnlyByteBuffer();

        if (offset == 0) {
            // This is the first write to the block, so create the temp block file. The file will only
            // be created if the first write starts at offset 0. This allocates enough space for the
            // write.
            mWorker.createBlockRemote(sessionId, blockId, mStorageTierAssoc.getAlias(0), length);
        } else {
            // Allocate enough space in the existing temporary block for the write.
            mWorker.requestSpace(sessionId, blockId, length);
        }
        writer = mWorker.getTempBlockWriterRemote(sessionId, blockId);
        writer.append(buffer);

        RPCBlockWriteResponse resp = new RPCBlockWriteResponse(sessionId, blockId, offset, length,
                RPCResponse.Status.SUCCESS);
        ChannelFuture future = ctx.writeAndFlush(resp);
        future.addListener(ChannelFutureListener.CLOSE);
        future.addListener(new ClosableResourceChannelListener(writer));
    } catch (Exception e) {
        LOG.error("Error writing remote block : {}", e.getMessage(), e);
        RPCBlockWriteResponse resp = RPCBlockWriteResponse.createErrorResponse(req,
                RPCResponse.Status.WRITE_ERROR);
        ChannelFuture future = ctx.writeAndFlush(resp);
        future.addListener(ChannelFutureListener.CLOSE);
        if (writer != null) {
            writer.close();
        }
    }
}

From source file:alluxio.worker.netty.UnderFileSystemDataServerHandler.java

License:Apache License

/**
 * Handles a {@link RPCFileReadRequest} by reading the data through an input stream provided by
 * the file worker. This method assumes the length to read is less than or equal to the unread
 * data in the file.//from   w  ww. j av  a 2s .  c  o m
 *
 * @param ctx The context of this request which handles the result of this operation
 * @param req The initiating {@link RPCFileReadRequest}
 * @throws IOException if an I/O error occurs when interacting with the UFS
 */
public void handleFileReadRequest(ChannelHandlerContext ctx, RPCFileReadRequest req) throws IOException {
    req.validate();

    long ufsFileId = req.getTempUfsFileId();
    long offset = req.getOffset();
    long length = req.getLength();
    byte[] data = new byte[(int) length];

    try {
        InputStream in = mWorker.getUfsInputStream(ufsFileId, offset);
        int bytesRead = 0;
        if (in != null) { // if we have not reached the end of the file
            while (bytesRead < length) {
                int read = in.read(data, bytesRead, (int) length - bytesRead);
                if (read == -1) {
                    break;
                }
                bytesRead += read;
            }
        }
        DataBuffer buf = bytesRead != 0 ? new DataByteBuffer(ByteBuffer.wrap(data, 0, bytesRead), bytesRead)
                : null;
        RPCFileReadResponse resp = new RPCFileReadResponse(ufsFileId, offset, bytesRead, buf,
                RPCResponse.Status.SUCCESS);
        ChannelFuture future = ctx.writeAndFlush(resp);
        future.addListener(ChannelFutureListener.CLOSE);
    } catch (Exception e) {
        LOG.error("Failed to read ufs file, may have been closed due to a client timeout.", e);
        RPCFileReadResponse resp = RPCFileReadResponse.createErrorResponse(req,
                RPCResponse.Status.UFS_READ_FAILED);
        ChannelFuture future = ctx.writeAndFlush(resp);
        future.addListener(ChannelFutureListener.CLOSE);
    }
}

From source file:alluxio.worker.netty.UnderFileSystemDataServerHandler.java

License:Apache License

/**
 * Handles a {@link RPCFileWriteRequest} by writing the data through an output stream provided
 * by the file worker. This method only allows appending data to the file and does not support
 * writing at arbitrary offsets.//from   w  ww .  j a  v a  2 s  . c o m
 *
 * @param ctx The context of this request which handles the result of this operation
 * @param req The initiating {@link RPCFileWriteRequest}
 * @throws IOException if an I/O error occurs when interacting with the UFS
 */
public void handleFileWriteRequest(ChannelHandlerContext ctx, RPCFileWriteRequest req) throws IOException {
    long ufsFileId = req.getTempUfsFileId();
    // Currently unused as only sequential write is supported
    long offset = req.getOffset();
    long length = req.getLength();
    final DataBuffer data = req.getPayloadDataBuffer();

    try {
        OutputStream out = mWorker.getUfsOutputStream(ufsFileId);
        // This channel will not be closed because the underlying stream should not be closed, the
        // channel will be cleaned up when the underlying stream is closed.
        WritableByteChannel channel = Channels.newChannel(out);
        channel.write(data.getReadOnlyByteBuffer());
        RPCFileWriteResponse resp = new RPCFileWriteResponse(ufsFileId, offset, length,
                RPCResponse.Status.SUCCESS);
        ChannelFuture future = ctx.writeAndFlush(resp);
        future.addListener(ChannelFutureListener.CLOSE);
    } catch (Exception e) {
        LOG.error("Failed to write ufs file.", e);
        RPCFileWriteResponse resp = RPCFileWriteResponse.createErrorResponse(req,
                RPCResponse.Status.UFS_WRITE_FAILED);
        ChannelFuture future = ctx.writeAndFlush(resp);
        future.addListener(ChannelFutureListener.CLOSE);
    }
}

From source file:app.WebSocketServerHandler.java

License:Apache License

private static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) {
    // Generate an error page if response getStatus code is not OK (200).
    if (res.status().code() != 200) {
        ByteBuf buf = Unpooled.copiedBuffer(res.status().toString(), CharsetUtil.UTF_8);
        res.content().writeBytes(buf);/*from ww  w  .  j av a2 s  .co m*/
        buf.release();
        HttpUtil.setContentLength(res, res.content().readableBytes());
    }

    // Send the response and close the connection if necessary.
    ChannelFuture f = ctx.channel().writeAndFlush(res);
    if (!HttpUtil.isKeepAlive(req) || res.status().code() != 200) {
        f.addListener(ChannelFutureListener.CLOSE);
    }
}