Search in sources :

Example 11 with StringEncoder

use of io.netty.handler.codec.string.StringEncoder in project camel by apache.

the class NettySingleCodecTest method createRegistry.

@Override
protected JndiRegistry createRegistry() throws Exception {
    JndiRegistry registry = super.createRegistry();
    StringEncoder stringEncoder = new StringEncoder();
    StringDecoder stringDecoder = new StringDecoder();
    registry.bind("encoder", stringEncoder);
    registry.bind("decoder", stringDecoder);
    return registry;
}
Also used : JndiRegistry(org.apache.camel.impl.JndiRegistry) StringEncoder(io.netty.handler.codec.string.StringEncoder) StringDecoder(io.netty.handler.codec.string.StringDecoder)

Example 12 with StringEncoder

use of io.netty.handler.codec.string.StringEncoder in project flink by apache.

the class NettyClientServerSslTest method testValidSslConnection.

/**
	 * Verify valid ssl configuration and connection
	 *
	 */
@Test
public void testValidSslConnection() throws Exception {
    NettyProtocol protocol = new NettyProtocol() {

        @Override
        public ChannelHandler[] getServerChannelHandlers() {
            return new ChannelHandler[0];
        }

        @Override
        public ChannelHandler[] getClientChannelHandlers() {
            return new ChannelHandler[0];
        }
    };
    NettyConfig nettyConfig = new NettyConfig(InetAddress.getLoopbackAddress(), NetUtils.getAvailablePort(), NettyTestUtil.DEFAULT_SEGMENT_SIZE, 1, createSslConfig());
    NettyTestUtil.NettyServerAndClient serverAndClient = NettyTestUtil.initServerAndClient(protocol, nettyConfig);
    Channel ch = NettyTestUtil.connect(serverAndClient);
    // should be able to send text data
    ch.pipeline().addLast(new StringDecoder()).addLast(new StringEncoder());
    assertTrue(ch.writeAndFlush("test").await().isSuccess());
    NettyTestUtil.shutdown(serverAndClient);
}
Also used : StringEncoder(io.netty.handler.codec.string.StringEncoder) Channel(io.netty.channel.Channel) StringDecoder(io.netty.handler.codec.string.StringDecoder) ChannelHandler(io.netty.channel.ChannelHandler) Test(org.junit.Test)

Example 13 with StringEncoder

use of io.netty.handler.codec.string.StringEncoder in project flink by apache.

the class NettyClientServerSslTest method testSslHandshakeError.

/**
	 * Verify SSL handshake error when untrusted server certificate is used
	 *
	 */
@Test
public void testSslHandshakeError() throws Exception {
    NettyProtocol protocol = new NettyProtocol() {

        @Override
        public ChannelHandler[] getServerChannelHandlers() {
            return new ChannelHandler[0];
        }

        @Override
        public ChannelHandler[] getClientChannelHandlers() {
            return new ChannelHandler[0];
        }
    };
    Configuration config = createSslConfig();
    // Use a server certificate which is not present in the truststore
    config.setString(ConfigConstants.SECURITY_SSL_KEYSTORE, "src/test/resources/untrusted.keystore");
    NettyConfig nettyConfig = new NettyConfig(InetAddress.getLoopbackAddress(), NetUtils.getAvailablePort(), NettyTestUtil.DEFAULT_SEGMENT_SIZE, 1, config);
    NettyTestUtil.NettyServerAndClient serverAndClient = NettyTestUtil.initServerAndClient(protocol, nettyConfig);
    Channel ch = NettyTestUtil.connect(serverAndClient);
    ch.pipeline().addLast(new StringDecoder()).addLast(new StringEncoder());
    // Attempting to write data over ssl should fail
    assertFalse(ch.writeAndFlush("test").await().isSuccess());
    NettyTestUtil.shutdown(serverAndClient);
}
Also used : StringEncoder(io.netty.handler.codec.string.StringEncoder) Configuration(org.apache.flink.configuration.Configuration) Channel(io.netty.channel.Channel) StringDecoder(io.netty.handler.codec.string.StringDecoder) ChannelHandler(io.netty.channel.ChannelHandler) Test(org.junit.Test)

Example 14 with StringEncoder

use of io.netty.handler.codec.string.StringEncoder in project jackrabbit-oak by apache.

the class StandbyClient method connect.

void connect(String host, int port) throws Exception {
    final SslContext sslContext;
    if (secure) {
        sslContext = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
    } else {
        sslContext = null;
    }
    Bootstrap b = new Bootstrap().group(group).channel(NioSocketChannel.class).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, readTimeoutMs).option(ChannelOption.TCP_NODELAY, true).option(ChannelOption.SO_REUSEADDR, true).option(ChannelOption.SO_KEEPALIVE, true).handler(new ChannelInitializer<SocketChannel>() {

        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline p = ch.pipeline();
            if (sslContext != null) {
                p.addLast(sslContext.newHandler(ch.alloc()));
            }
            p.addLast(new ReadTimeoutHandler(readTimeoutMs, TimeUnit.MILLISECONDS));
            // Decoders
            p.addLast(new SnappyFramedDecoder(true));
            // Such a big max frame length is needed because blob
            // values are sent in one big message. In future
            // versions of the protocol, sending binaries in chunks
            // should be considered instead.
            p.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4));
            p.addLast(new ResponseDecoder());
            // Encoders
            p.addLast(new StringEncoder(CharsetUtil.UTF_8));
            p.addLast(new GetHeadRequestEncoder());
            p.addLast(new GetSegmentRequestEncoder());
            p.addLast(new GetBlobRequestEncoder());
            p.addLast(new GetReferencesRequestEncoder());
            // Handlers
            p.addLast(new GetHeadResponseHandler(headQueue));
            p.addLast(new GetSegmentResponseHandler(segmentQueue));
            p.addLast(new GetBlobResponseHandler(blobQueue));
            p.addLast(new GetReferencesResponseHandler(referencesQueue));
            // Exception handler
            p.addLast(new ExceptionHandler(clientId));
        }
    });
    channel = b.connect(host, port).sync().channel();
}
Also used : GetReferencesRequestEncoder(org.apache.jackrabbit.oak.segment.standby.codec.GetReferencesRequestEncoder) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) GetSegmentRequestEncoder(org.apache.jackrabbit.oak.segment.standby.codec.GetSegmentRequestEncoder) ChannelPipeline(io.netty.channel.ChannelPipeline) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) StringEncoder(io.netty.handler.codec.string.StringEncoder) ResponseDecoder(org.apache.jackrabbit.oak.segment.standby.codec.ResponseDecoder) GetBlobRequestEncoder(org.apache.jackrabbit.oak.segment.standby.codec.GetBlobRequestEncoder) GetHeadRequestEncoder(org.apache.jackrabbit.oak.segment.standby.codec.GetHeadRequestEncoder) Bootstrap(io.netty.bootstrap.Bootstrap) ReadTimeoutHandler(io.netty.handler.timeout.ReadTimeoutHandler) SnappyFramedDecoder(io.netty.handler.codec.compression.SnappyFramedDecoder) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) SslContext(io.netty.handler.ssl.SslContext)

Aggregations

StringEncoder (io.netty.handler.codec.string.StringEncoder)14 StringDecoder (io.netty.handler.codec.string.StringDecoder)12 ChannelPipeline (io.netty.channel.ChannelPipeline)6 Channel (io.netty.channel.Channel)4 ChannelHandler (io.netty.channel.ChannelHandler)4 SocketChannel (io.netty.channel.socket.SocketChannel)4 DelimiterBasedFrameDecoder (io.netty.handler.codec.DelimiterBasedFrameDecoder)4 ChannelFuture (io.netty.channel.ChannelFuture)3 LineBasedFrameDecoder (io.netty.handler.codec.LineBasedFrameDecoder)3 JndiRegistry (org.apache.camel.impl.JndiRegistry)3 Bootstrap (io.netty.bootstrap.Bootstrap)2 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)2 EventLoopGroup (io.netty.channel.EventLoopGroup)2 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)2 LoggingHandler (io.netty.handler.logging.LoggingHandler)2 SslContext (io.netty.handler.ssl.SslContext)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 ShutdownArgs (com.github.dreamhead.moco.bootstrap.ShutdownArgs)1