Example usage for io.netty.channel ChannelHandlerContext attr

List of usage examples for io.netty.channel ChannelHandlerContext attr

Introduction

In this page you can find the example usage for io.netty.channel ChannelHandlerContext attr.

Prototype

@Deprecated
@Override
<T> Attribute<T> attr(AttributeKey<T> key);

Source Link

Usage

From source file:com.barchart.netty.server.http.pipeline.HttpRequestChannelHandler.java

License:BSD License

@Override
public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable exception) throws Exception {

    final PooledHttpServerRequest request = ctx.attr(ATTR_REQUEST).get();

    if (request != null) {

        final PooledHttpServerResponse response = request.response();

        try {//from  ww w.  j  a  va2  s  . co  m

            if (!response.isFinished()) {

                response.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);

                server.errorHandler().onError(request, exception);

                // Bad handler, forgot to finish
                if (!response.isFinished()) {
                    response.finish();
                }

            }

        } finally {

            server.logger().error(request, exception);

        }

    } else {
        sendServerError(ctx, new ServerException(HttpResponseStatus.INTERNAL_SERVER_ERROR, exception));
    }

}

From source file:com.barchart.netty.server.http.pipeline.HttpRequestChannelHandler.java

License:BSD License

/**
 * Free any handlers related to the current request, record access and
 * notify handler of completion./*from   w ww .jav  a  2  s  . c o  m*/
 */
@Override
public PooledHttpServerRequest requestComplete(final ChannelHandlerContext ctx) {

    final PooledHttpServerRequest request = ctx.attr(ATTR_REQUEST).getAndRemove();

    if (request != null) {

        // Record to access log
        server.logger().access(request, System.currentTimeMillis() - request.requestTime());

        try {

            try {

                if (!request.response().isFinished()) {

                    request.response().close();

                    final RequestHandler handler = request.handler();

                    if (handler != null) {
                        handler.cancel(request);
                    }

                }

            } finally {

                final RequestHandler handler = request.handler();

                if (handler != null) {
                    handler.release(request);
                }

            }

        } finally {

            requestPool.release(request);

        }

    }

    return request;

}

From source file:com.butor.netty.handler.codec.ftp.cmd.ActivePassiveSocketManager.java

License:Apache License

public Socket getActiveSocket(ChannelHandlerContext ctx) {
    return ctx.attr(FTPAttrKeys.ACTIVE_SOCKET).get();
}

From source file:com.butor.netty.handler.codec.ftp.cmd.ActivePassiveSocketManager.java

License:Apache License

public void closeActiveSocket(ChannelHandlerContext ctx) {
    Socket activeSocket = ctx.attr(FTPAttrKeys.ACTIVE_SOCKET).get();
    if (null == activeSocket)
        return;/* w w w  . j a v a2 s  .com*/
    try {
        activeSocket.close();
    } catch (Exception e) {
        logger.warn("Exception thrown on closing active socket", e);
    } finally {
        activeSocket = null;
    }
}

From source file:com.butor.netty.handler.codec.ftp.cmd.ActivePassiveSocketManager.java

License:Apache License

public void openActiveSocket(ChannelHandlerContext ctx, InetSocketAddress addr) throws IOException {
    Socket activeSocket;/*from  ww  w.ja va  2  s .c o  m*/
    activeSocket = new Socket(addr.getAddress(), addr.getPort());
    ctx.attr(FTPAttrKeys.ACTIVE_SOCKET).set(activeSocket);
}

From source file:com.butor.netty.handler.codec.ftp.cmd.ActivePassiveSocketManager.java

License:Apache License

public ServerSocket getPassiveSocket(ChannelHandlerContext ctx) {
    return ctx.attr(FTPAttrKeys.PASSIVE_SOCKET).get();
}

From source file:com.butor.netty.handler.codec.ftp.cmd.ActivePassiveSocketManager.java

License:Apache License

public ServerSocket openPassiveSocket(ChannelHandlerContext ctx, int port, InetAddress addr)
        throws IOException {
    ServerSocket passiveSocket;/*from   w  ww. jav a 2s .  c o m*/
    passiveSocket = new ServerSocket(port, 50, addr);
    ctx.attr(FTPAttrKeys.PASSIVE_SOCKET).set(passiveSocket);
    return passiveSocket;
}

From source file:com.butor.netty.handler.codec.ftp.cmd.ActivePassiveSocketManager.java

License:Apache License

public void closePassiveSocket(ChannelHandlerContext ctx) {
    ServerSocket passiveSocket = ctx.attr(FTPAttrKeys.PASSIVE_SOCKET).getAndRemove();
    if (null == passiveSocket)
        return;// ww  w .  j  av a 2  s . c o m
    try {
        passiveSocket.close();
    } catch (Exception e) {
        logger.warn("Exception thrown on closing passive socket", e);
    }
}

From source file:com.butor.netty.handler.codec.ftp.cmd.CommandExecutionTemplate.java

License:Apache License

public final void executeCommand(ChannelHandlerContext ctx, String cmd, String args) {
    //TODO handle properly FTP States see p.53 RFC 959
    FTPCommand command = getFTPCommand(cmd);
    if (command != null) {
        Boolean loggedIn = ctx.attr(FTPAttrKeys.LOGGED_IN).setIfAbsent(false);
        loggedIn = ctx.attr(FTPAttrKeys.LOGGED_IN).get();
        boolean isLogonCommand = command instanceof LogonCommand;
        if ((!loggedIn && isLogonCommand) || (loggedIn && !isLogonCommand)) {
            command.execute(ctx, args);//from   w  w w.ja va 2s  .  c  om
            ctx.attr(FTPAttrKeys.LAST_COMMAND).set(command);
            return;
        }
    }

    logger.debug("Command not supported {} {}", cmd, args);
    CommandUtil.send("500 Command not supported", ctx);

}

From source file:com.butor.netty.handler.codec.ftp.impl.CwdCmd.java

License:Apache License

@Override
public void execute(ChannelHandlerContext ctx, String args) {
    ctx.attr(FTPAttrKeys.CWD).set(args);
    send("250 CWD command successful", ctx, args);
}