Search in sources :

Example 41 with StringEncoder

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

the class SocketStringEchoTest method testStringEcho.

private static void testStringEcho(ServerBootstrap sb, Bootstrap cb, boolean autoRead) throws Throwable {
    sb.childOption(ChannelOption.AUTO_READ, autoRead);
    cb.option(ChannelOption.AUTO_READ, autoRead);
    Promise<Void> serverDonePromise = ImmediateEventExecutor.INSTANCE.newPromise();
    Promise<Void> clientDonePromise = ImmediateEventExecutor.INSTANCE.newPromise();
    final StringEchoHandler sh = new StringEchoHandler(autoRead, serverDonePromise);
    final StringEchoHandler ch = new StringEchoHandler(autoRead, clientDonePromise);
    sb.childHandler(new ChannelInitializer<Channel>() {

        @Override
        public void initChannel(Channel sch) throws Exception {
            sch.pipeline().addLast("framer", new DelimiterBasedFrameDecoder(512, Delimiters.lineDelimiter()));
            sch.pipeline().addLast("decoder", new StringDecoder(CharsetUtil.ISO_8859_1));
            sch.pipeline().addBefore("decoder", "encoder", new StringEncoder(CharsetUtil.ISO_8859_1));
            sch.pipeline().addAfter("decoder", "handler", sh);
        }
    });
    cb.handler(new ChannelInitializer<Channel>() {

        @Override
        public void initChannel(Channel sch) throws Exception {
            sch.pipeline().addLast("framer", new DelimiterBasedFrameDecoder(512, Delimiters.lineDelimiter()));
            sch.pipeline().addLast("decoder", new StringDecoder(CharsetUtil.ISO_8859_1));
            sch.pipeline().addBefore("decoder", "encoder", new StringEncoder(CharsetUtil.ISO_8859_1));
            sch.pipeline().addAfter("decoder", "handler", ch);
        }
    });
    Channel sc = sb.bind().sync().channel();
    Channel cc = cb.connect(sc.localAddress()).sync().channel();
    for (String element : data) {
        String delimiter = random.nextBoolean() ? "\r\n" : "\n";
        cc.writeAndFlush(element + delimiter);
    }
    ch.donePromise.sync();
    sh.donePromise.sync();
    sh.channel.close().sync();
    ch.channel.close().sync();
    sc.close().sync();
    if (sh.exception.get() != null && !(sh.exception.get() instanceof IOException)) {
        throw sh.exception.get();
    }
    if (ch.exception.get() != null && !(ch.exception.get() instanceof IOException)) {
        throw ch.exception.get();
    }
    if (sh.exception.get() != null) {
        throw sh.exception.get();
    }
    if (ch.exception.get() != null) {
        throw ch.exception.get();
    }
}
Also used : StringEncoder(io.netty.handler.codec.string.StringEncoder) Channel(io.netty.channel.Channel) DelimiterBasedFrameDecoder(io.netty.handler.codec.DelimiterBasedFrameDecoder) StringDecoder(io.netty.handler.codec.string.StringDecoder) IOException(java.io.IOException) IOException(java.io.IOException)

Example 42 with StringEncoder

use of org.apache.flink.shaded.netty4.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 NoOpProtocol();
    Configuration config = createSslConfig();
    // Use a server certificate which is not present in the truststore
    config.setString(SecurityOptions.SSL_INTERNAL_KEYSTORE, "src/test/resources/untrusted.keystore");
    NettyTestUtil.NettyServerAndClient serverAndClient;
    try (NetUtils.Port port = NetUtils.getAvailablePort()) {
        NettyConfig nettyConfig = createNettyConfig(config, port);
        serverAndClient = NettyTestUtil.initServerAndClient(protocol, nettyConfig);
    }
    Assert.assertNotNull("serverAndClient is null due to fail to get a free port", serverAndClient);
    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(org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringEncoder) NetUtils(org.apache.flink.util.NetUtils) Configuration(org.apache.flink.configuration.Configuration) NettyServerAndClient(org.apache.flink.runtime.io.network.netty.NettyTestUtil.NettyServerAndClient) SocketChannel(org.apache.flink.shaded.netty4.io.netty.channel.socket.SocketChannel) Channel(org.apache.flink.shaded.netty4.io.netty.channel.Channel) StringDecoder(org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringDecoder) Test(org.junit.Test) SSLUtilsTest(org.apache.flink.runtime.net.SSLUtilsTest)

Example 43 with StringEncoder

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

the class NettyClientServerSslTest method testClientUntrustedCertificate.

@Test
public void testClientUntrustedCertificate() throws Exception {
    final Configuration serverConfig = createSslConfig();
    final Configuration clientConfig = createSslConfig();
    // give the client a different keystore / certificate
    clientConfig.setString(SecurityOptions.SSL_INTERNAL_KEYSTORE, "src/test/resources/untrusted.keystore");
    NettyServerAndClient serverAndClient;
    try (NetUtils.Port serverPort = NetUtils.getAvailablePort();
        NetUtils.Port clientPort = NetUtils.getAvailablePort()) {
        final NettyConfig nettyServerConfig = createNettyConfig(serverConfig, serverPort);
        final NettyConfig nettyClientConfig = createNettyConfig(clientConfig, clientPort);
        final NettyBufferPool bufferPool = new NettyBufferPool(1);
        final NettyProtocol protocol = new NoOpProtocol();
        final NettyServer server = NettyTestUtil.initServer(nettyServerConfig, protocol, bufferPool);
        final NettyClient client = NettyTestUtil.initClient(nettyClientConfig, protocol, bufferPool);
        serverAndClient = new NettyServerAndClient(server, client);
    }
    Assert.assertNotNull("serverAndClient is null due to fail to get a free port", serverAndClient);
    final 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 : Configuration(org.apache.flink.configuration.Configuration) SocketChannel(org.apache.flink.shaded.netty4.io.netty.channel.socket.SocketChannel) Channel(org.apache.flink.shaded.netty4.io.netty.channel.Channel) StringDecoder(org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringDecoder) StringEncoder(org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringEncoder) NetUtils(org.apache.flink.util.NetUtils) NettyServerAndClient(org.apache.flink.runtime.io.network.netty.NettyTestUtil.NettyServerAndClient) Test(org.junit.Test) SSLUtilsTest(org.apache.flink.runtime.net.SSLUtilsTest)

Example 44 with StringEncoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringEncoder in project dubbo by alibaba.

the class QosProcessHandler method decode.

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    if (in.readableBytes() < 1) {
        return;
    }
    // read one byte to guess protocol
    final int magic = in.getByte(in.readerIndex());
    ChannelPipeline p = ctx.pipeline();
    p.addLast(new LocalHostPermitHandler(acceptForeignIp));
    if (isHttp(magic)) {
        // no welcome output for http protocol
        if (welcomeFuture != null && welcomeFuture.isCancellable()) {
            welcomeFuture.cancel(false);
        }
        p.addLast(new HttpServerCodec());
        p.addLast(new HttpObjectAggregator(1048576));
        p.addLast(new HttpProcessHandler());
        p.remove(this);
    } else {
        p.addLast(new LineBasedFrameDecoder(2048));
        p.addLast(new StringDecoder(CharsetUtil.UTF_8));
        p.addLast(new StringEncoder(CharsetUtil.UTF_8));
        p.addLast(new IdleStateHandler(0, 0, 5 * 60));
        p.addLast(new TelnetIdleEventHandler());
        p.addLast(new TelnetProcessHandler());
        p.remove(this);
    }
}
Also used : StringEncoder(io.netty.handler.codec.string.StringEncoder) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) StringDecoder(io.netty.handler.codec.string.StringDecoder) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 45 with StringEncoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringEncoder in project tech by ffyyhh995511.

the class HelloServerInitializer method initChannel.

@Override
protected void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();
    // 以("\n")为结尾分割的 解码器
    pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
    // 字符串解码 和 编码
    pipeline.addLast("decoder", new StringDecoder());
    pipeline.addLast("encoder", new StringEncoder());
    // 自己的逻辑Handler
    pipeline.addLast("handler", new HelloServerHandler());
}
Also used : StringEncoder(io.netty.handler.codec.string.StringEncoder) DelimiterBasedFrameDecoder(io.netty.handler.codec.DelimiterBasedFrameDecoder) StringDecoder(io.netty.handler.codec.string.StringDecoder) ChannelPipeline(io.netty.channel.ChannelPipeline)

Aggregations

StringEncoder (io.netty.handler.codec.string.StringEncoder)40 StringDecoder (io.netty.handler.codec.string.StringDecoder)37 ChannelPipeline (io.netty.channel.ChannelPipeline)28 SocketChannel (io.netty.channel.socket.SocketChannel)13 ChannelFuture (io.netty.channel.ChannelFuture)12 EventLoopGroup (io.netty.channel.EventLoopGroup)9 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)9 LengthFieldBasedFrameDecoder (io.netty.handler.codec.LengthFieldBasedFrameDecoder)9 LengthFieldPrepender (io.netty.handler.codec.LengthFieldPrepender)9 Bootstrap (io.netty.bootstrap.Bootstrap)8 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)8 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)7 Channel (io.netty.channel.Channel)7 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)7 DelimiterBasedFrameDecoder (io.netty.handler.codec.DelimiterBasedFrameDecoder)7 LineBasedFrameDecoder (io.netty.handler.codec.LineBasedFrameDecoder)7 NettyServerAndClient (org.apache.flink.runtime.io.network.netty.NettyTestUtil.NettyServerAndClient)5 Channel (org.apache.flink.shaded.netty4.io.netty.channel.Channel)5 SocketChannel (org.apache.flink.shaded.netty4.io.netty.channel.socket.SocketChannel)5 StringDecoder (org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringDecoder)5