org.mockserver.httpclient.HttpClientInitializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mockserver-core Show documentation
Show all versions of mockserver-core Show documentation
Functionality used by all MockServer modules for matching and expectations
package org.mockserver.httpclient;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpClientCodec;
import io.netty.handler.codec.http.HttpContentDecompressor;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.proxy.HttpProxyHandler;
import io.netty.handler.proxy.Socks5ProxyHandler;
import org.mockserver.codec.MockServerBinaryClientCodec;
import org.mockserver.codec.MockServerHttpClientCodec;
import org.mockserver.logging.LoggingHandler;
import org.mockserver.logging.MockServerLogger;
import org.mockserver.proxyconfiguration.ProxyConfiguration;
import org.mockserver.socket.tls.NettySslContextFactory;
import java.net.InetSocketAddress;
import java.util.Map;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import static org.mockserver.httpclient.NettyHttpClient.REMOTE_SOCKET;
import static org.mockserver.httpclient.NettyHttpClient.SECURE;
import static org.slf4j.event.Level.TRACE;
@ChannelHandler.Sharable
public class HttpClientInitializer extends ChannelInitializer {
private final MockServerLogger mockServerLogger;
private final boolean forwardProxyClient;
private final boolean isHttp;
private final HttpClientConnectionErrorHandler httpClientConnectionHandler;
private final HttpClientHandler httpClientHandler;
private final Map proxyConfigurations;
private final NettySslContextFactory nettySslContextFactory;
HttpClientInitializer(Map proxyConfigurations, MockServerLogger mockServerLogger, boolean forwardProxyClient, NettySslContextFactory nettySslContextFactory, boolean isHttp) {
this.proxyConfigurations = proxyConfigurations;
this.mockServerLogger = mockServerLogger;
this.forwardProxyClient = forwardProxyClient;
this.isHttp = isHttp;
this.httpClientHandler = new HttpClientHandler();
this.httpClientConnectionHandler = new HttpClientConnectionErrorHandler();
this.nettySslContextFactory = nettySslContextFactory;
}
@Override
public void initChannel(SocketChannel channel) {
ChannelPipeline pipeline = channel.pipeline();
boolean secure = channel.attr(SECURE) != null && channel.attr(SECURE).get() != null && channel.attr(SECURE).get();
if (proxyConfigurations != null) {
if (secure && proxyConfigurations.containsKey(ProxyConfiguration.Type.HTTPS)) {
ProxyConfiguration proxyConfiguration = proxyConfigurations.get(ProxyConfiguration.Type.HTTPS);
if (isNotBlank(proxyConfiguration.getUsername()) && isNotBlank(proxyConfiguration.getPassword())) {
pipeline.addLast(new HttpProxyHandler(proxyConfiguration.getProxyAddress(), proxyConfiguration.getUsername(), proxyConfiguration.getPassword()));
} else {
pipeline.addLast(new HttpProxyHandler(proxyConfiguration.getProxyAddress()));
}
} else if (proxyConfigurations.containsKey(ProxyConfiguration.Type.SOCKS5)) {
ProxyConfiguration proxyConfiguration = proxyConfigurations.get(ProxyConfiguration.Type.SOCKS5);
if (isNotBlank(proxyConfiguration.getUsername()) && isNotBlank(proxyConfiguration.getPassword())) {
pipeline.addLast(new Socks5ProxyHandler(proxyConfiguration.getProxyAddress(), proxyConfiguration.getUsername(), proxyConfiguration.getPassword()));
} else {
pipeline.addLast(new Socks5ProxyHandler(proxyConfiguration.getProxyAddress()));
}
}
}
pipeline.addLast(httpClientConnectionHandler);
if (secure) {
InetSocketAddress remoteAddress = channel.attr(REMOTE_SOCKET).get();
pipeline.addLast(nettySslContextFactory.createClientSslContext(forwardProxyClient).newHandler(channel.alloc(), remoteAddress.getHostName(), remoteAddress.getPort()));
}
// add logging
if (MockServerLogger.isEnabled(TRACE)) {
pipeline.addLast(new LoggingHandler(HttpClientHandler.class.getName()));
}
if (isHttp) {
pipeline.addLast(new HttpClientCodec());
pipeline.addLast(new HttpContentDecompressor());
pipeline.addLast(new HttpObjectAggregator(Integer.MAX_VALUE));
pipeline.addLast(new MockServerHttpClientCodec(mockServerLogger, proxyConfigurations));
} else {
pipeline.addLast(new MockServerBinaryClientCodec());
}
pipeline.addLast(httpClientHandler);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy