Example usage for io.netty.channel ChannelHandlerContext channel

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

Introduction

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

Prototype

Channel channel();

Source Link

Document

Return the Channel which is bound to the ChannelHandlerContext .

Usage

From source file:com.addthis.meshy.ChannelState.java

License:Apache License

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
    meshy.updateLastEventTime();//from   ww  w. ja v  a 2 s . c o  m
    log.warn("Netty exception caught. Closing channel. ChannelState: {}", this, cause);
    ctx.channel().close();
}

From source file:com.addthis.meshy.ChannelState.java

License:Apache License

@Override
public void channelActive(ChannelHandlerContext ctx) {
    buffer = ctx.alloc().buffer(16384);/*from   w w w . j  ava 2  s.  c  o m*/
    meshy.updateLastEventTime();
    channelConnected();
    meshy.channelConnected(ctx.channel(), this);
}

From source file:com.addthis.meshy.ChannelState.java

License:Apache License

@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    try {/*from   w w w .  ja  va  2s  . co m*/
        meshy.updateLastEventTime();
        channelClosed();
        meshy.channelClosed(ctx.channel(), this);
    } finally {
        buffer.release();
    }
}

From source file:com.adobe.acs.livereload.impl.WebSocketServerHandler.java

License:Apache License

private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req) throws Exception {
    // Handle a bad request.
    if (!req.getDecoderResult().isSuccess()) {
        sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST));
        return;/*from  w ww .j a  va  2s .  com*/
    }

    // Allow only GET methods.
    if (req.getMethod() != GET) {
        sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, FORBIDDEN));
        return;
    }

    if ("/".equals(req.getUri()) || "/favicon.ico".equals(req.getUri())) {
        FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, NOT_FOUND);
        sendHttpResponse(ctx, req, res);
        return;
    }

    if (req.getUri().startsWith("/livereload.js")) {
        InputStream is = getClass().getResourceAsStream("/livereload.js");
        byte[] data = IOUtils.toByteArray(is);
        ByteBuf content = Unpooled.wrappedBuffer(data);

        FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, OK, content);

        res.headers().set(CONTENT_TYPE, "application/javascript");
        setContentLength(res, content.readableBytes());

        sendHttpResponse(ctx, req, res);
        return;
    }

    // Handshake
    WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(getWebSocketLocation(req),
            null, false);
    handshaker = wsFactory.newHandshaker(req);
    if (handshaker == null) {
        WebSocketServerHandshakerFactory.sendUnsupportedWebSocketVersionResponse(ctx.channel());
    } else {
        handshaker.handshake(ctx.channel(), req);
    }
}

From source file:com.adobe.acs.livereload.impl.WebSocketServerHandler.java

License:Apache License

private void handleWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame) {

    // Check for closing frame
    if (frame instanceof CloseWebSocketFrame) {
        handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame.retain());
        return;/*from www.  j av a 2 s . c  o m*/
    }
    if (frame instanceof PingWebSocketFrame) {
        ctx.channel().write(new PongWebSocketFrame(frame.content().retain()));
        return;
    }
    if (!(frame instanceof TextWebSocketFrame)) {
        throw new UnsupportedOperationException(
                String.format("%s frame types not supported", frame.getClass().getName()));
    }

    String request = ((TextWebSocketFrame) frame).text();
    try {
        JSONObject obj = new JSONObject(request);
        handleCommand(obj, ctx);

    } catch (JSONException e) {
        throw new IllegalArgumentException(String.format("%s is not a valid JSON object", request));
    }

}

From source file:com.adobe.acs.livereload.impl.WebSocketServerHandler.java

License:Apache License

private void handleCommand(JSONObject obj, ChannelHandlerContext ctx) throws JSONException {
    String cmd = obj.getString(COMMAND);
    Channel channel = ctx.channel();
    if (CMD_HELLO.equals(cmd)) {
        JSONObject result = new JSONObject();
        result.put(COMMAND, CMD_HELLO);//from  w ww  . j  a v a  2 s  .  co m
        JSONArray protocols = new JSONArray();
        protocols.put(PROTOCOL_VERSION_7);
        result.put(PROTOCOLS, protocols);
        result.put("serverName", "AEM Live Reload Server");

        channel.write(new TextWebSocketFrame(result.toString()));

        if (isSupported(obj)) {
            log.info("adding LiveReload channel");
            group.add(channel);
        }

        ChannelInfo info = new ChannelInfo(isSupported(obj));
        infos.put(channel, info);
    } else if (CMD_INFO.equals(cmd)) {
        ChannelInfo info = infos.get(channel);
        if (info != null) {
            String url = obj.getString(URL);
            try {
                URI uri = new URI(url);
                info.setUri(uri);

                log.info("added uri to channel info {}", info);

            } catch (URISyntaxException e) {
                log.warn("Unable to store uri " + url, e);
            }
        }
    } else {
        log.warn("Unknown command {}", cmd);
        log.info(obj.toString(2));
    }
}

From source file:com.adobe.acs.livereload.impl.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.getStatus().code() != HttpServletResponse.SC_OK) {
        ByteBuf buf = Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8);
        res.content().writeBytes(buf);//from  w  w  w.j  a  va  2s .  c  o  m
        buf.release();
        setContentLength(res, res.content().readableBytes());
    }

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

From source file:com.aerofs.baseline.http.Channels.java

License:Apache License

static void expectedClose(ChannelHandlerContext ctx, String logMessage) {
    LOGGER.trace("{}: close: {}", Channels.getHexText(ctx.channel()), logMessage);
    ctx.channel().close();//from   w w w .j  a  v  a  2s  .  c  o m
}

From source file:com.aerofs.baseline.http.Channels.java

License:Apache License

static void close(ChannelHandlerContext ctx, String logMessage, Object... logObjects) {
    close(ctx.channel(), logMessage, logObjects);
}

From source file:com.aerofs.baseline.http.Channels.java

License:Apache License

static void closeAndLogStack(ChannelHandlerContext ctx, String logMessage, Throwable t) {
    closeAndLogStack(ctx.channel(), logMessage, t);
}