use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpObjectAggregator in project protools by SeanDragon.
the class HttpClientChannelPoolHandler method channelCreated.
@Override
public void channelCreated(Channel channel) {
NioSocketChannel nioSocketChannel = (NioSocketChannel) channel;
nioSocketChannel.config().setTcpNoDelay(true).setKeepAlive(true);
final ChannelPipeline p = nioSocketChannel.pipeline();
// HTTPS
if (sslCtx != null) {
p.addLast(sslCtx.newHandler(channel.alloc()));
}
p.addLast(new HttpClientCodec(Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE));
p.addLast(new HttpObjectAggregator(Integer.MAX_VALUE));
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpObjectAggregator in project cdap by caskdata.
the class NettyRouterPipelineTest method testHttpPipelining.
@Test
public void testHttpPipelining() throws Exception {
final BlockingQueue<HttpResponseStatus> responseStatuses = new LinkedBlockingQueue<>();
EventLoopGroup eventGroup = new NioEventLoopGroup();
Bootstrap bootstrap = new Bootstrap().channel(NioSocketChannel.class).group(eventGroup).handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("codec", new HttpClientCodec());
pipeline.addLast("aggregator", new HttpObjectAggregator(1048576));
pipeline.addLast("handler", new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof HttpResponse) {
responseStatuses.add(((HttpResponse) msg).status());
}
ReferenceCountUtil.release(msg);
}
});
}
});
// Create a connection and make five consecutive HTTP call without waiting for the first to respond
Channel channel = bootstrap.connect(HOSTNAME, ROUTER.getServiceMap().get(GATEWAY_NAME)).sync().channel();
for (int i = 0; i < 5; i++) {
HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/v1/sleep?sleepMillis=3000");
request.headers().set(HttpHeaderNames.HOST, HOSTNAME);
channel.writeAndFlush(request);
}
// Should get the first response as normal one
HttpResponseStatus status = responseStatuses.poll(5, TimeUnit.SECONDS);
Assert.assertEquals(HttpResponseStatus.OK, status);
// The rest four should be failure responses
for (int i = 0; i < 4; i++) {
Assert.assertEquals(HttpResponseStatus.NOT_IMPLEMENTED, responseStatuses.poll(1, TimeUnit.SECONDS));
}
eventGroup.shutdownGracefully();
channel.close();
Assert.assertTrue(responseStatuses.isEmpty());
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpObjectAggregator in project xian by happyyangyuan.
the class DefaultInitializer method initInboundHandlers.
private void initInboundHandlers(ChannelPipeline p) {
p.addLast("httpRequestDecoder", new HttpRequestDecoder());
p.addLast("httpAggregator", new HttpObjectAggregator(Config.getClientMaxBodySize()));
p.addLast("reqReceived", new ReqReceived());
p.addLast("reqDecoderAux", new RequestDecoderAux());
p.addLast("businessHandler", new BusinessHandler());
p.addLast("reqSubmitted", new ReqSubmitted());
p.addLast("exceptionHandler", new DefaultExceptionHandler());
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpObjectAggregator in project autobahn-java by crossbario.
the class NettyWebSocket method connect.
@Override
public void connect(ITransportHandler transportHandler, TransportOptions options) throws Exception {
if (options == null) {
if (mOptions == null) {
options = new TransportOptions();
} else {
options = new TransportOptions();
options.setAutoPingInterval(mOptions.getAutoPingInterval());
options.setAutoPingTimeout(mOptions.getAutoPingTimeout());
options.setMaxFramePayloadSize(mOptions.getMaxFramePayloadSize());
}
}
URI uri;
uri = new URI(mUri);
int port = validateURIAndGetPort(uri);
String scheme = uri.getScheme();
String host = uri.getHost();
final SslContext sslContext = getSSLContext(scheme);
WebSocketClientHandshaker handshaker = WebSocketClientHandshakerFactory.newHandshaker(uri, WebSocketVersion.V13, mSerializers, true, new DefaultHttpHeaders(), options.getMaxFramePayloadSize());
mHandler = new NettyWebSocketClientHandler(handshaker, this, transportHandler);
EventLoopGroup group = new NioEventLoopGroup();
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(group);
bootstrap.channel(NioSocketChannel.class);
TransportOptions opt = options;
bootstrap.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline channelPipeline = ch.pipeline();
if (sslContext != null) {
channelPipeline.addLast(sslContext.newHandler(ch.alloc(), host, port));
}
channelPipeline.addLast(new HttpClientCodec(), new HttpObjectAggregator(8192), WebSocketClientCompressionHandler.INSTANCE, new IdleStateHandler(opt.getAutoPingInterval() + opt.getAutoPingTimeout(), opt.getAutoPingInterval(), 0, TimeUnit.SECONDS), mHandler);
}
});
mChannel = bootstrap.connect(uri.getHost(), port).sync().channel();
mHandler.getHandshakeFuture().sync();
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpObjectAggregator in project activemq-artemis by apache.
the class NettyConnector method start.
@Override
public synchronized void start() {
if (channelClazz != null) {
return;
}
if (remotingThreads == -1) {
// Default to number of cores * 3
remotingThreads = Runtime.getRuntime().availableProcessors() * 3;
}
String connectorType;
if (useEpoll && Epoll.isAvailable()) {
if (useGlobalWorkerPool) {
group = SharedEventLoopGroup.getInstance((threadFactory -> new EpollEventLoopGroup(remotingThreads, threadFactory)));
} else {
group = new EpollEventLoopGroup(remotingThreads);
}
connectorType = EPOLL_CONNECTOR_TYPE;
channelClazz = EpollSocketChannel.class;
logger.debug("Connector " + this + " using native epoll");
} else if (useKQueue && KQueue.isAvailable()) {
if (useGlobalWorkerPool) {
group = SharedEventLoopGroup.getInstance((threadFactory -> new KQueueEventLoopGroup(remotingThreads, threadFactory)));
} else {
group = new KQueueEventLoopGroup(remotingThreads);
}
connectorType = KQUEUE_CONNECTOR_TYPE;
channelClazz = KQueueSocketChannel.class;
logger.debug("Connector " + this + " using native kqueue");
} else {
if (useGlobalWorkerPool) {
channelClazz = NioSocketChannel.class;
group = SharedEventLoopGroup.getInstance((threadFactory -> new NioEventLoopGroup(remotingThreads, threadFactory)));
} else {
channelClazz = NioSocketChannel.class;
group = new NioEventLoopGroup(remotingThreads);
}
connectorType = NIO_CONNECTOR_TYPE;
channelClazz = NioSocketChannel.class;
logger.debug("Connector + " + this + " using nio");
}
// if we are a servlet wrap the socketChannelFactory
bootstrap = new Bootstrap();
bootstrap.channel(channelClazz);
bootstrap.group(group);
bootstrap.option(ChannelOption.TCP_NODELAY, tcpNoDelay);
if (connectTimeoutMillis != -1) {
bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeoutMillis);
}
if (tcpReceiveBufferSize != -1) {
bootstrap.option(ChannelOption.SO_RCVBUF, tcpReceiveBufferSize);
}
if (tcpSendBufferSize != -1) {
bootstrap.option(ChannelOption.SO_SNDBUF, tcpSendBufferSize);
}
final int writeBufferLowWaterMark = this.writeBufferLowWaterMark != -1 ? this.writeBufferLowWaterMark : WriteBufferWaterMark.DEFAULT.low();
final int writeBufferHighWaterMark = this.writeBufferHighWaterMark != -1 ? this.writeBufferHighWaterMark : WriteBufferWaterMark.DEFAULT.high();
final WriteBufferWaterMark writeBufferWaterMark = new WriteBufferWaterMark(writeBufferLowWaterMark, writeBufferHighWaterMark);
bootstrap.option(ChannelOption.WRITE_BUFFER_WATER_MARK, writeBufferWaterMark);
bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
bootstrap.option(ChannelOption.SO_REUSEADDR, true);
channelGroup = new DefaultChannelGroup("activemq-connector", GlobalEventExecutor.INSTANCE);
final String realKeyStorePath;
final String realKeyStoreProvider;
final String realKeyStorePassword;
final String realTrustStorePath;
final String realTrustStoreProvider;
final String realTrustStorePassword;
if (sslEnabled) {
// HORNETQ-680 - override the server-side config if client-side system properties are set
realKeyStorePath = Stream.of(System.getProperty(JAVAX_KEYSTORE_PATH_PROP_NAME), System.getProperty(ACTIVEMQ_KEYSTORE_PATH_PROP_NAME), keyStorePath).map(v -> useDefaultSslContext ? keyStorePath : v).filter(Objects::nonNull).findFirst().orElse(null);
realKeyStorePassword = Stream.of(System.getProperty(JAVAX_KEYSTORE_PASSWORD_PROP_NAME), System.getProperty(ACTIVEMQ_KEYSTORE_PASSWORD_PROP_NAME), keyStorePassword).map(v -> useDefaultSslContext ? keyStorePassword : v).filter(Objects::nonNull).findFirst().orElse(null);
realKeyStoreProvider = Stream.of(System.getProperty(ACTIVEMQ_KEYSTORE_PROVIDER_PROP_NAME), keyStoreProvider).map(v -> useDefaultSslContext ? keyStoreProvider : v).filter(Objects::nonNull).findFirst().orElse(null);
realTrustStorePath = Stream.of(System.getProperty(JAVAX_TRUSTSTORE_PATH_PROP_NAME), System.getProperty(ACTIVEMQ_TRUSTSTORE_PATH_PROP_NAME), trustStorePath).map(v -> useDefaultSslContext ? trustStorePath : v).filter(Objects::nonNull).findFirst().orElse(null);
realTrustStorePassword = Stream.of(System.getProperty(JAVAX_TRUSTSTORE_PASSWORD_PROP_NAME), System.getProperty(ACTIVEMQ_TRUSTSTORE_PASSWORD_PROP_NAME), trustStorePassword).map(v -> useDefaultSslContext ? trustStorePassword : v).filter(Objects::nonNull).findFirst().orElse(null);
realTrustStoreProvider = Stream.of(System.getProperty(ACTIVEMQ_TRUSTSTORE_PROVIDER_PROP_NAME), trustStoreProvider).map(v -> useDefaultSslContext ? trustStoreProvider : v).filter(Objects::nonNull).findFirst().orElse(null);
} else {
realKeyStorePath = null;
realKeyStoreProvider = null;
realKeyStorePassword = null;
realTrustStorePath = null;
realTrustStoreProvider = null;
realTrustStorePassword = null;
}
bootstrap.handler(new ChannelInitializer<Channel>() {
@Override
public void initChannel(Channel channel) throws Exception {
final ChannelPipeline pipeline = channel.pipeline();
if (sslEnabled && !useServlet) {
SSLEngine engine;
if (sslProvider.equals(TransportConstants.OPENSSL_PROVIDER)) {
engine = loadOpenSslEngine(channel.alloc(), realKeyStoreProvider, realKeyStorePath, realKeyStorePassword, realTrustStoreProvider, realTrustStorePath, realTrustStorePassword);
} else {
engine = loadJdkSslEngine(useDefaultSslContext, realKeyStoreProvider, realKeyStorePath, realKeyStorePassword, realTrustStoreProvider, realTrustStorePath, realTrustStorePassword);
}
engine.setUseClientMode(true);
engine.setWantClientAuth(true);
// setting the enabled cipher suites resets the enabled protocols so we need
// to save the enabled protocols so that after the customer cipher suite is enabled
// we can reset the enabled protocols if a customer protocol isn't specified
String[] originalProtocols = engine.getEnabledProtocols();
if (enabledCipherSuites != null) {
try {
engine.setEnabledCipherSuites(SSLSupport.parseCommaSeparatedListIntoArray(enabledCipherSuites));
} catch (IllegalArgumentException e) {
ActiveMQClientLogger.LOGGER.invalidCipherSuite(SSLSupport.parseArrayIntoCommandSeparatedList(engine.getSupportedCipherSuites()));
throw e;
}
}
if (enabledProtocols != null) {
try {
engine.setEnabledProtocols(SSLSupport.parseCommaSeparatedListIntoArray(enabledProtocols));
} catch (IllegalArgumentException e) {
ActiveMQClientLogger.LOGGER.invalidProtocol(SSLSupport.parseArrayIntoCommandSeparatedList(engine.getSupportedProtocols()));
throw e;
}
} else {
engine.setEnabledProtocols(originalProtocols);
}
if (verifyHost) {
SSLParameters sslParameters = engine.getSSLParameters();
sslParameters.setEndpointIdentificationAlgorithm("HTTPS");
engine.setSSLParameters(sslParameters);
}
SslHandler handler = new SslHandler(engine);
pipeline.addLast("ssl", handler);
}
if (httpEnabled) {
pipeline.addLast(new HttpRequestEncoder());
pipeline.addLast(new HttpResponseDecoder());
pipeline.addLast(new HttpObjectAggregator(Integer.MAX_VALUE));
pipeline.addLast(new HttpHandler());
}
if (httpUpgradeEnabled) {
// prepare to handle a HTTP 101 response to upgrade the protocol.
final HttpClientCodec httpClientCodec = new HttpClientCodec();
pipeline.addLast(httpClientCodec);
pipeline.addLast("http-upgrade", new HttpUpgradeHandler(pipeline, httpClientCodec));
}
protocolManager.addChannelHandlers(pipeline);
pipeline.addLast(new ActiveMQClientChannelHandler(channelGroup, handler, new Listener()));
}
});
if (batchDelay > 0) {
flusher = new BatchFlusher();
batchFlusherFuture = scheduledThreadPool.scheduleWithFixedDelay(flusher, batchDelay, batchDelay, TimeUnit.MILLISECONDS);
}
ActiveMQClientLogger.LOGGER.startedNettyConnector(connectorType, TransportConstants.NETTY_VERSION, host, port);
}
Aggregations