Example usage for io.netty.channel ChannelHandlerContext fireChannelRead

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

Introduction

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

Prototype

@Override
    ChannelHandlerContext fireChannelRead(Object msg);

Source Link

Usage

From source file:org.janusgraph.graphdb.tinkerpop.gremlin.server.handler.SaslAndHMACAuthenticationHandlerTest.java

License:Apache License

@Test
public void testHttpChannelReadWhenAuthenticatorHasNotBeenAdded() throws Exception {
    final HMACAuthenticator hmacAuth = createMock(HMACAuthenticator.class);
    final SaslAndHMACAuthenticator authenticator = createMock(SaslAndHMACAuthenticator.class);
    final ChannelHandlerContext ctx = createMock(ChannelHandlerContext.class);
    final ChannelPipeline pipeline = createMock(ChannelPipeline.class);
    final HttpMessage msg = createMock(HttpMessage.class);
    final HttpHeaders headers = createMock(HttpHeaders.class);

    expect(authenticator.getHMACAuthenticator()).andReturn(hmacAuth);
    expect(authenticator.getSimpleAuthenticator()).andReturn(createMock(JanusGraphSimpleAuthenticator.class));
    expect(ctx.pipeline()).andReturn(pipeline).times(2);
    expect(pipeline.get("hmac_authenticator")).andReturn(null);
    expect(pipeline.addAfter(eq(PIPELINE_AUTHENTICATOR), eq("hmac_authenticator"), isA(ChannelHandler.class)))
            .andReturn(null);/*from  w  ww . jav a 2  s. c om*/
    expect(msg.headers()).andReturn(headers).times(2);
    expect(headers.get(isA(String.class))).andReturn(null).times(2);
    expect(ctx.fireChannelRead(eq(msg))).andReturn(ctx);
    replayAll();

    final SaslAndHMACAuthenticationHandler handler = new SaslAndHMACAuthenticationHandler(authenticator, null);
    handler.channelRead(ctx, msg);
}

From source file:org.janusgraph.graphdb.tinkerpop.gremlin.server.handler.SaslAndHMACAuthenticationHandlerTest.java

License:Apache License

@Test
public void testHttpChannelReadWhenAuthenticatorHasBeenAdded() throws Exception {
    final SaslAndHMACAuthenticator authenticator = createMock(SaslAndHMACAuthenticator.class);
    final HMACAuthenticator hmacAuth = createMock(HMACAuthenticator.class);
    final ChannelHandlerContext ctx = createMock(ChannelHandlerContext.class);
    final ChannelHandler mockHandler = createMock(ChannelHandler.class);
    final ChannelPipeline pipeline = createMock(ChannelPipeline.class);
    final HttpMessage msg = createMock(HttpMessage.class);
    final HttpHeaders headers = createMock(HttpHeaders.class);

    expect(authenticator.getHMACAuthenticator()).andReturn(hmacAuth);
    expect(authenticator.getSimpleAuthenticator()).andReturn(createMock(JanusGraphSimpleAuthenticator.class));
    expect(ctx.pipeline()).andReturn(pipeline);
    expect(pipeline.get("hmac_authenticator")).andReturn(mockHandler);
    expect(msg.headers()).andReturn(headers).times(2);
    expect(headers.get(isA(String.class))).andReturn(null).times(2);
    expect(ctx.fireChannelRead(eq(msg))).andReturn(ctx);
    replayAll();// w ww. j  a v  a 2 s .c o m

    final SaslAndHMACAuthenticationHandler handler = new SaslAndHMACAuthenticationHandler(authenticator, null);
    handler.channelRead(ctx, msg);
}

From source file:org.jboss.aerogear.simplepush.server.netty.NotificationHandler.java

License:Apache License

@Override
public void messageReceived(final ChannelHandlerContext ctx, final Object msg) throws Exception {
    if (msg instanceof FullHttpRequest) {
        final FullHttpRequest request = (FullHttpRequest) msg;
        final String requestUri = request.getUri();
        logger.debug(requestUri);//from   w ww.j  a v  a  2s  . c  o  m
        if (requestUri.startsWith(simplePushServer.config().endpointPrefix())) {
            handleHttpRequest(ctx, request);
        } else {
            ctx.fireChannelRead(ReferenceCountUtil.retain(msg));
        }
    } else {
        ctx.fireChannelRead(ReferenceCountUtil.retain(msg));
    }
}

From source file:org.jboss.aerogear.sync.client.netty.SyncClientHandler.java

License:Apache License

@Override
protected void channelRead0(final ChannelHandlerContext ctx, final WebSocketFrame frame) throws Exception {
    if (frame instanceof CloseWebSocketFrame) {
        logger.debug("Received closeFrame");
        ctx.close();//  w  w  w  . j  a  va  2 s  .  co m
        return;
    }

    if (frame instanceof TextWebSocketFrame) {
        logger.info("TextWebSocketFrame: " + ((TextWebSocketFrame) frame).text());
        final JsonNode json = JsonMapper.asJsonNode(((TextWebSocketFrame) frame).text());
        logger.info("json: " + json);
        switch (MessageType.from(json.get("msgType").asText())) {
        case PATCH:
            final PatchMessage<S> serverPatchMessage = syncEngine.patchMessageFromJson(json.toString());
            logger.info("Edits: " + serverPatchMessage);
            patch(serverPatchMessage);
            break;
        case UNKNOWN:
            unknownMessageType(ctx, json);
            break;
        }
    } else {
        ctx.fireChannelRead(frame);
    }
}

From source file:org.jboss.aerogear.sync.client.netty.WebSocketClientHandler.java

License:Apache License

@Override
public void channelRead0(final ChannelHandlerContext ctx, final Object msg) {
    final Channel ch = ctx.channel();
    if (!handshaker.isHandshakeComplete()) {
        handshaker.finishHandshake(ch, (FullHttpResponse) msg);
        handshakeFuture.setSuccess();/*  w  w  w  . java 2s  . com*/
        return;
    }

    if (msg instanceof FullHttpResponse) {
        FullHttpResponse response = (FullHttpResponse) msg;
        throw new IllegalStateException("Unexpected FullHttpResponse (getStatus=" + response.getStatus()
                + ", content=" + response.content().toString(CharsetUtil.UTF_8) + ')');
    }

    final WebSocketFrame frame = (WebSocketFrame) msg;
    if (frame instanceof TextWebSocketFrame) {
        ctx.fireChannelRead(frame.retain());
    } else if (frame instanceof CloseWebSocketFrame) {
        ch.close();
    }
}

From source file:org.jboss.aerogear.sync.DiffSyncClientHandler.java

License:Apache License

@Override
protected void messageReceived(final ChannelHandlerContext ctx, final WebSocketFrame frame) throws Exception {
    if (frame instanceof CloseWebSocketFrame) {
        logger.debug("Received closeFrame");
        ctx.close();/*from w w w . j  a v  a 2s.co m*/
        return;
    }

    if (frame instanceof TextWebSocketFrame) {
        logger.info("TextWebSocketFrame: " + ((TextWebSocketFrame) frame).text());
        final JsonNode json = JsonMapper.asJsonNode(((TextWebSocketFrame) frame).text());
        logger.info("json: " + json);
        switch (MessageType.from(json.get("msgType").asText())) {
        case PATCH:
            final PatchMessage serverPatchMessage = JsonMapper.fromJson(json.toString(),
                    DefaultPatchMessage.class);
            logger.info("Edits: " + serverPatchMessage);
            patch(serverPatchMessage);
            break;
        case UNKNOWN:
            unknownMessageType(ctx, json);
            break;
        }
    } else {
        ctx.fireChannelRead(frame);
    }
}

From source file:org.jboss.aerogear.sync.DiffSyncHandler.java

License:Apache License

@Override
protected void messageReceived(final ChannelHandlerContext ctx, final WebSocketFrame frame) throws Exception {
    if (frame instanceof CloseWebSocketFrame) {
        logger.debug("Received closeFrame");
        ctx.close();/*  w  w  w .  ja  va 2 s . co  m*/
        return;
    }

    if (frame instanceof TextWebSocketFrame) {
        final JsonNode json = JsonMapper.asJsonNode(((TextWebSocketFrame) frame).text());
        logger.info("Doc:" + json);
        switch (MessageType.from(json.get("msgType").asText())) {
        case ADD:
            final Document<String> doc = documentFromJson(json);
            final String clientId = json.get("clientId").asText();
            final PatchMessage patchMessage = addSubscriber(doc, clientId, ctx);
            ctx.attr(DOC_ADD).set(true);
            ctx.channel().writeAndFlush(textFrame(toJson(patchMessage)));
            break;
        case PATCH:
            final PatchMessage clientPatchMessage = JsonMapper.fromJson(json.toString(),
                    DefaultPatchMessage.class);
            checkForReconnect(clientPatchMessage.documentId(), clientPatchMessage.clientId(), ctx);
            logger.debug("Client Edits=" + clientPatchMessage);
            patch(clientPatchMessage);
            break;
        case DETACH:
            // detach the client from a specific document.
            break;
        case UNKNOWN:
            unknownMessageType(ctx, json);
            break;
        }
    } else {
        ctx.fireChannelRead(frame);
    }
}

From source file:org.jboss.aerogear.sync.server.netty.DiffSyncHandler.java

License:Apache License

@Override
protected void channelRead0(final ChannelHandlerContext ctx, final WebSocketFrame frame) throws Exception {
    if (frame instanceof CloseWebSocketFrame) {
        logger.debug("Received closeFrame");
        ctx.close();/* w  ww.ja  v  a 2 s  .co m*/
        return;
    }

    if (frame instanceof TextWebSocketFrame) {
        final JsonNode json = JsonMapper.asJsonNode(((TextWebSocketFrame) frame).text());
        logger.info("Doc:" + json);
        switch (MessageType.from(json.get("msgType").asText())) {
        case ADD:
            final Document<T> doc = syncEngine.documentFromJson(json);
            final String clientId = json.get("clientId").asText();
            final PatchMessage<S> patchMessage = addSubscriber(doc, clientId, ctx);
            ctx.attr(DOC_ADD).set(true);
            ctx.channel().writeAndFlush(textFrame(patchMessage.asJson()));
            break;
        case PATCH:
            final PatchMessage<S> clientPatchMessage = syncEngine.patchMessageFromJson(json.toString());
            checkForReconnect(clientPatchMessage.documentId(), clientPatchMessage.clientId(), ctx);
            logger.debug("Client Edits = " + clientPatchMessage);
            patch(clientPatchMessage);
            break;
        case DETACH:
            // detach the client from a specific document.
            break;
        case UNKNOWN:
            unknownMessageType(ctx, json);
            break;
        }
    } else {
        ctx.fireChannelRead(frame);
    }
}

From source file:org.jdiameter.client.impl.transport.tls.netty.DiameterMessageHandler.java

License:Open Source License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof IMessage) {
        IMessage m = (IMessage) msg;//  ww w. ja  va2 s . c  o  m
        logger.debug("Received message {} TLS Transport {}", m.getCommandCode(),
                this.parentConnection.getKey());
        try {
            logger.debug("Passing message on to parent {}", this.parentConnection.getKey());
            parentConnection.onMessageReceived(m);
            logger.debug("Finished passing message on to parent {}", this.parentConnection.getKey());
        } catch (AvpDataException e) {
            logger.debug("Garbage was received. Discarding. {}", this.parentConnection.getKey());
            parentConnection.onAvpDataException(e);
        } finally {
            if (autoRelease) {
                ReferenceCountUtil.release(msg);
            } else {
                ctx.fireChannelRead(m);
            }
        }
    }
}

From source file:org.jdiameter.client.impl.transport.tls.netty.StartTlsServerHandler.java

License:Open Source License

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override/*from  ww  w  . ja  va2  s. c o m*/
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    logger.debug("StartTlsServerHandler");
    ByteBuf buf = (ByteBuf) msg;
    byte[] bytes = new byte[buf.readableBytes()];
    buf.getBytes(buf.readerIndex(), bytes);

    if ("StartTlsRequest".equals(new String(bytes))) {
        logger.debug("Received StartTlsRequest");
        SslContext sslContext = SslContextFactory.getSslContextForServer(this.tlsTransportClient.getConfig());
        SSLEngine sslEngine = sslContext.newEngine(ctx.alloc());
        sslEngine.setUseClientMode(false);
        SslHandler sslHandler = new SslHandler(sslEngine, false);

        final ChannelPipeline pipeline = ctx.pipeline();

        pipeline.remove("decoder");
        pipeline.remove("msgHandler");
        pipeline.remove("encoder");
        pipeline.remove("inbandWriter");
        pipeline.remove(this);

        pipeline.addLast("sslHandler", sslHandler);

        sslHandler.handshakeFuture().addListener(new GenericFutureListener() {

            @Override
            public void operationComplete(Future future) throws Exception {
                if (future.isSuccess()) {
                    logger.debug("StartTls server handshake succesfull");

                    tlsTransportClient.setTlsHandshakingState(TlsHandshakingState.SHAKEN);

                    logger.debug("restoring all handlers");

                    pipeline.addLast("decoder",
                            new DiameterMessageDecoder(
                                    StartTlsServerHandler.this.tlsTransportClient.getParent(),
                                    StartTlsServerHandler.this.tlsTransportClient.getParser()));
                    pipeline.addLast("msgHandler", new DiameterMessageHandler(
                            StartTlsServerHandler.this.tlsTransportClient.getParent(), true));

                    pipeline.addLast("encoder", new DiameterMessageEncoder(
                            StartTlsServerHandler.this.tlsTransportClient.getParser()));
                    pipeline.addLast("inbandWriter", new InbandSecurityHandler());

                }
            }
        });

        ReferenceCountUtil.release(msg);
        logger.debug("Sending StartTlsResponse");
        ctx.writeAndFlush(Unpooled.wrappedBuffer("StartTlsResponse".getBytes()))
                .addListener(new GenericFutureListener() {

                    @Override
                    public void operationComplete(Future f) throws Exception {
                        if (!f.isSuccess()) {
                            logger.error(f.cause().getMessage(), f.cause());
                        }

                    }
                });
    } else {
        ctx.fireChannelRead(msg);
    }

}