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.butor.netty.handler.codec.ftp.impl.ListCmd.java

License:Apache License

@Override
public void execute(ChannelHandlerContext ctx, String args) {
    Socket activeSocket = activePassiveSocketManager.getActiveSocket(ctx);
    ServerSocket passiveSocket = activePassiveSocketManager.getPassiveSocket(ctx);
    FTPCommand lastFTPCommand = ctx.attr(FTPAttrKeys.LAST_COMMAND).get();
    String lastCommand = lastFTPCommand != null ? lastFTPCommand.getCmd() : null;

    if ("PORT".equals(lastCommand)) {
        if (null == activeSocket) {
            send("503 Bad sequence of commands", ctx, args);
            return;
        }//from  w w w.  j  a  va 2  s .  c o m
        send("150 Opening binary mode data connection for LIST " + args, ctx, args);
        try {
            writeListToClient(activeSocket.getOutputStream());
            send("226 Transfer complete for LIST", ctx, args);
        } catch (IOException e1) {
            logger.warn("Exception thrown on writing through active socket: [" + activeSocket + "]", e1);
            send("552 Requested file action aborted", ctx, args);
        } finally {
            activePassiveSocketManager.closeActiveSocket(ctx);
        }
    } else if ("PASV".equals(lastCommand)) {
        if (null == passiveSocket) {
            send("503 Bad sequence of commands", ctx, args);
            return;
        }

        send("150 Opening binary mode data connection for LIST on port: " + passiveSocket.getLocalPort(), ctx,
                args);
        Socket clientSocket = null;
        try {
            clientSocket = passiveSocket.accept();
            writeListToClient(clientSocket.getOutputStream());
            clientSocket.getOutputStream().close();
            send("226 Transfer complete for LIST", ctx, args);
        } catch (IOException e1) {
            logger.warn("Exception thrown on writing through passive socket: [" + passiveSocket + "],"
                    + "accepted client socket: [" + clientSocket + "]", e1);
            send("552 Requested file action aborted", ctx, args);
        } finally {
            activePassiveSocketManager.closePassiveSocket(ctx);
        }
    } else
        send("503 Bad sequence of commands", ctx, args);
}

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

License:Apache License

@Override
public void execute(ChannelHandlerContext ctx, String args) {
    String curDir = ctx.attr(FTPAttrKeys.CWD).get();
    send(String.format("257 \"%s\" is current directory", curDir == null ? "/" : curDir), ctx, args);
}

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

License:Apache License

@Override
public void execute(ChannelHandlerContext ctx, String args) {
    FTPCommand lastFTPCommand = ctx.attr(FTPAttrKeys.LAST_COMMAND).get();
    String lastCommand = lastFTPCommand != null ? lastFTPCommand.getCmd() : null;
    Socket activeSocket = activePassiveSocketManager.getActiveSocket(ctx);
    ServerSocket passiveSocket = activePassiveSocketManager.getPassiveSocket(ctx);

    if ("PORT".equals(lastCommand)) {
        if (null == activeSocket)
            send("503 Bad sequence of commands", ctx, args);
        send("150 Opening binary mode data connection for " + args, ctx, args);
        try {/*from www  .  j  av  a 2  s.c  o m*/
            dataReceiver.receive(args, activeSocket.getInputStream());
            send("226 Transfer complete for STOR " + args, ctx, args);
        } catch (IOException e1) {
            logger.warn("Exception thrown on reading through active socket: [" + activeSocket + "]", e1);
            send("552 Requested file action aborted", ctx, args);
        } finally {
            activePassiveSocketManager.closeActiveSocket(ctx);
        }
    } else if ("PASV".equals(lastCommand)) {
        if (null == passiveSocket)
            send("503 Bad sequence of commands", ctx, args);
        send("150 Opening binary mode data connection for " + args, ctx, args);
        Socket clientSocket = null;
        try {
            clientSocket = passiveSocket.accept();
            dataReceiver.receive(args, clientSocket.getInputStream());
            send("226 Transfer complete for STOR " + args, ctx, args);
        } catch (IOException e1) {
            logger.warn("Exception thrown on reading through passive socket: [" + passiveSocket + "], "
                    + "accepted client socket: [" + clientSocket + "]", e1);
            send("552 Requested file action aborted", ctx, args);
        } finally {
            activePassiveSocketManager.closePassiveSocket(ctx);
        }
    } else
        send("503 Bad sequence of commands", ctx, args);
}

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

License:Apache License

@Override
public void execute(ChannelHandlerContext ctx, String args) {
    send("230 USER LOGGED IN", ctx, args);
    ctx.attr(FTPAttrKeys.LOGGED_IN).set(true);
}

From source file:com.chenyang.proxy.http.HttpSchemaHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext uaChannelCtx, final Object msg) throws Exception {

    if (msg instanceof HttpRequest) {
        HttpRequest httpRequest = (HttpRequest) msg;

        String originalHost = HostNamePortUtil.getHostName(httpRequest);
        int originalPort = HostNamePortUtil.getPort(httpRequest);
        HttpRemote apnProxyRemote = new HttpRemote(originalHost, originalPort);

        if (!HostAuthenticationUtil.isValidAddress(apnProxyRemote.getInetSocketAddress())) {
            HttpErrorUtil.writeAndFlush(uaChannelCtx.channel(), HttpResponseStatus.FORBIDDEN);
            return;
        }//from   w  w  w .  jav  a  2 s. com

        Channel uaChannel = uaChannelCtx.channel();

        HttpConnectionAttribute apnProxyConnectionAttribute = HttpConnectionAttribute.build(
                uaChannel.remoteAddress().toString(), httpRequest.getMethod().name(), httpRequest.getUri(),
                httpRequest.getProtocolVersion().text(),
                httpRequest.headers().get(HttpHeaders.Names.USER_AGENT), apnProxyRemote);

        uaChannelCtx.attr(HttpConnectionAttribute.ATTRIBUTE_KEY).set(apnProxyConnectionAttribute);
        uaChannel.attr(HttpConnectionAttribute.ATTRIBUTE_KEY).set(apnProxyConnectionAttribute);

        if (httpRequest.getMethod().equals(HttpMethod.CONNECT)) {
            if (uaChannelCtx.pipeline().get(HttpUserAgentForwardHandler.HANDLER_NAME) != null) {
                uaChannelCtx.pipeline().remove(HttpUserAgentForwardHandler.HANDLER_NAME);
            }
            if (uaChannelCtx.pipeline().get(HttpUserAgentTunnelHandler.HANDLER_NAME) == null) {
                uaChannelCtx.pipeline().addLast(HttpUserAgentTunnelHandler.HANDLER_NAME,
                        new HttpUserAgentTunnelHandler());
            }
        } else {
            if (uaChannelCtx.pipeline().get(HttpUserAgentForwardHandler.HANDLER_NAME) == null) {
                uaChannelCtx.pipeline().addLast(HttpUserAgentForwardHandler.HANDLER_NAME,
                        new HttpUserAgentForwardHandler());
            }
        }
    }

    uaChannelCtx.fireChannelRead(msg);
}

From source file:com.couchbase.client.core.endpoint.dcp.DCPConnection.java

License:Apache License

void consumed(short partition, int delta) {
    if (env.dcpConnectionBufferSize() > 0) {
        ChannelHandlerContext ctx = contexts.get(partition);
        if (ctx == null) {
            return;
        }//from w  ww .  j a va2 s .c o  m
        synchronized (ctx) {
            Attribute<Integer> attr = ctx.attr(CONSUMED_BYTES);
            Integer consumedBytes = attr.get();
            if (consumedBytes == null) {
                consumedBytes = 0;
            }
            consumedBytes += MINIMUM_HEADER_SIZE + delta;
            if (consumedBytes >= env.dcpConnectionBufferSize() * env.dcpConnectionBufferAckThreshold()) {
                ctx.writeAndFlush(createBufferAcknowledgmentRequest(ctx, consumedBytes));
                consumedBytes = 0;
            }
            attr.set(consumedBytes);
        }
    }
}

From source file:com.curlymaple.server.ServerHandler.java

License:Apache License

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    Channel channel = ctx.pipeline().channel();
    MemoryData md = new MemoryData(channel);
    ctx.attr(key).set(md);
}

From source file:com.curlymaple.server.ServerHandler.java

License:Apache License

@Override
protected void messageReceived(ChannelHandlerContext ctx, String msg) throws Exception {
    try {/*from  www.j  a va 2 s  . c o  m*/
        // logger.info(msg);
        if (null == msg || "".equals(msg)) {
            return;
        }
        JSONObject json = JSONObject.fromObject(msg);
        IC2SCommand command = MessageDispatcher.dispatcher(json);
        if (null == command) {
            return;
        }
        MemoryData md = ctx.attr(key).get();
        JSONObject param = JSONObject.fromObject(json.get("p"));
        command.execute(param, md);
    } catch (Exception e) {
        ctx.pipeline().channel().close();
        ctx.pipeline().channel().disconnect();
        logger.error("messageReceived", e);
    }
}

From source file:com.curlymaple.server.ServerHandler.java

License:Apache License

@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    MemoryData md = ctx.attr(key).get();
    LogicManager.getInstance().removeMemoryData(md);
}

From source file:com.ebay.jetstream.http.netty.server.KeepAliveHandler.java

License:MIT License

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    ctx.attr(CREATION_TIME).set(System.nanoTime());
    super.channelActive(ctx);
}