use of io.netty.handler.codec.string.StringEncoder in project x-pipe by ctripcorp.
the class ServerHandlersInit method initChannel.
protected void initChannel(SocketChannel socketChannel) throws Exception {
SslHandler sslHandler = SSLHandlerProvider.getSSLHandler();
ChannelPipeline pipeline = socketChannel.pipeline();
pipeline.addLast(sslHandler);
pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
pipeline.addLast(new StringDecoder());
pipeline.addLast(new StringEncoder());
// and then business logic.
pipeline.addLast(new SecureChatServerHandler());
}
use of io.netty.handler.codec.string.StringEncoder in project remote-integration-example by thingsboard.
the class CustomIntegration method init.
@Override
public void init(TbIntegrationInitParams params) throws Exception {
super.init(params);
JsonNode configuration = mapper.readTree(params.getConfiguration().getConfiguration().get("configuration").asText());
try {
bossGroup = new NioEventLoopGroup();
workGroup = new NioEventLoopGroup();
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup, workGroup);
bootstrap.channel(NioServerSocketChannel.class);
bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) {
socketChannel.pipeline().addLast(new StringEncoder(), new StringDecoder(), new LineBasedFrameDecoder(1024));
socketChannel.pipeline().addLast(new SimpleChannelInboundHandler<String>() {
@Override
protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
log.debug("Server received the message: {}", msg);
if (msg.startsWith("Hello to ThingsBoard!")) {
deviceName = msg.substring(msg.indexOf("[") + 1, msg.indexOf("]"));
ctx.writeAndFlush("Hello from ThingsBoard!");
initialized = true;
} else {
if (initialized) {
CustomResponse response = new CustomResponse();
process(new CustomIntegrationMsg(msg, response));
ctx.writeAndFlush(response.getResult());
} else {
log.warn("The flaw was not started correctly!");
}
}
}
});
}
});
int port = getBindPort(configuration);
serverChannel = bootstrap.bind(port).sync().channel();
client = new CustomClient(port, getMsgGeneratorIntervalMs(configuration));
} catch (Exception e) {
log.error("Failed to init TCP server!", e);
throw new RuntimeException();
}
}
use of io.netty.handler.codec.string.StringEncoder in project learn-simple by muggle0.
the class MyClientInitializer method initChannel.
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline pipeline = socketChannel.pipeline();
// jia 一堆处理器 策略模式
pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
pipeline.addLast(new LengthFieldPrepender(4));
pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));
pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8));
pipeline.addLast(new MyClientHandler());
}
use of io.netty.handler.codec.string.StringEncoder in project flink-mirror by flink-ci.
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);
}
use of io.netty.handler.codec.string.StringEncoder in project flink-mirror by flink-ci.
the class NettyClientServerSslTest method testSslPinningForValidFingerprint.
@Test
public void testSslPinningForValidFingerprint() throws Exception {
NettyProtocol protocol = new NoOpProtocol();
Configuration config = createSslConfig();
// pin the certificate based on internal cert
config.setString(SecurityOptions.SSL_INTERNAL_CERT_FINGERPRINT, SSLUtilsTest.getCertificateFingerprint(config, "flink.test"));
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());
assertTrue(ch.writeAndFlush("test").await().isSuccess());
NettyTestUtil.shutdown(serverAndClient);
}
Aggregations