本文整理汇总了Java中io.netty.handler.proxy.HttpProxyHandler类的典型用法代码示例。如果您正苦于以下问题:Java HttpProxyHandler类的具体用法?Java HttpProxyHandler怎么用?Java HttpProxyHandler使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
HttpProxyHandler类属于io.netty.handler.proxy包,在下文中一共展示了HttpProxyHandler类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: newProxyHandler
import io.netty.handler.proxy.HttpProxyHandler; //导入依赖的package包/类
/**
* Return a new eventual {@link ProxyHandler}
*
* @return a new eventual {@link ProxyHandler}
*/
public final ProxyHandler newProxyHandler() {
InetSocketAddress proxyAddr = this.address.get();
String username = this.username;
String password = Objects.nonNull(username) && Objects.nonNull(this.password) ?
this.password.apply(username) : null;
switch (this.type) {
case HTTP:
return Objects.nonNull(username) && Objects.nonNull(password) ?
new HttpProxyHandler(proxyAddr, username, password) :
new HttpProxyHandler(proxyAddr);
case SOCKS4:
return Objects.nonNull(username) ? new Socks4ProxyHandler(proxyAddr, username) :
new Socks4ProxyHandler(proxyAddr);
case SOCKS5:
return Objects.nonNull(username) && Objects.nonNull(password) ?
new Socks5ProxyHandler(proxyAddr, username, password) :
new Socks5ProxyHandler(proxyAddr);
}
throw new IllegalArgumentException("Proxy type unsupported : " + this.type);
}
示例2: newProxyHandler
import io.netty.handler.proxy.HttpProxyHandler; //导入依赖的package包/类
public ProxyHandler newProxyHandler() {
switch (proxySetting.getType()) {
case ProxySetting.TYPE_HTTP:
if (proxySetting.getUser().isEmpty()) {
return new HttpProxyHandler(address);
} else {
return new HttpProxyHandler(address, proxySetting.getUser(), proxySetting.getPassword());
}
case ProxySetting.TYPE_SOCKS5:
if (proxySetting.getUser().isEmpty()) {
return new Socks5ProxyHandler(address);
} else {
return new Socks5ProxyHandler(address, proxySetting.getUser(), proxySetting.getPassword());
}
case ProxySetting.TYPE_SOCKS4:
if (proxySetting.getUser().isEmpty()) {
return new Socks4ProxyHandler(address);
} else {
return new Socks4ProxyHandler(address, proxySetting.getUser());
}
default:
throw new RuntimeException("unknown proxy type: " + proxySetting.getType());
}
}
示例3: addProxyHandlerIfNeeded
import io.netty.handler.proxy.HttpProxyHandler; //导入依赖的package包/类
private void addProxyHandlerIfNeeded(ChannelPipeline pipeline, HttpProxyConfig httpProxyConfig,
@Nullable String passwordOverride) throws Exception {
String proxyHost = httpProxyConfig.host();
if (proxyHost.isEmpty()) {
return;
}
int proxyPort = MoreObjects.firstNonNull(httpProxyConfig.port(), 80);
SocketAddress proxyAddress = new InetSocketAddress(proxyHost, proxyPort);
String username = httpProxyConfig.username();
if (username.isEmpty()) {
pipeline.addLast(new HttpProxyHandler(proxyAddress));
} else {
String password = getPassword(httpProxyConfig, passwordOverride);
pipeline.addLast(new HttpProxyHandler(proxyAddress, username, password));
}
}
示例4: send
import io.netty.handler.proxy.HttpProxyHandler; //导入依赖的package包/类
public void send(String url, ProxyConfiguration pc, Map<String, Long> data) {
if (!(eventLoopGroup.isShuttingDown() || eventLoopGroup.isShutdown())) {
String[] parts = url.split(":");
if (parts.length != 2) {
throw new IllegalArgumentException("Invalid URL: " + url);
}
int port = Integer.parseInt(parts[1]);
Bootstrap b = bootstrapCache.get(Pair.of(url, pc), k -> {
Bootstrap bb = new Bootstrap()
.group(eventLoopGroup)
.channel(NioSocketChannel.class)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, config.readInt(ConfigProperty.GRAPHITE_CONNECT_TIMEOUT))
.option(ChannelOption.TCP_NODELAY, true)
.option(ChannelOption.SO_SNDBUF, 5 * 1024 * 1024)
.option(ChannelOption.SO_TIMEOUT, config.readInt(ConfigProperty.GRAPHITE_CONNECT_TIMEOUT));
if (pc != ProxyConfiguration.NONE) {
InetSocketAddress sa = new InetSocketAddress(pc.getHost(), pc.getPort());
switch (pc.getProxyType()) {
case SOCKS5: {
bb.handler(new Socks5ProxyHandler(sa, pc.getUsername(), pc.getPassword()));
break;
}
case HTTP:
case HTTPS: {
if (pc.getProxyType() == ProxyType.HTTPS) {
bb.handler(sslContext.newHandler(PooledByteBufAllocator.DEFAULT));
}
HttpProxyHandler hph = pc.getUsername() != null ? new HttpProxyHandler(sa, pc.getUsername(), pc.getPassword()) :
new HttpProxyHandler(sa);
bb.handler(hph);
break;
}
default:
}
}
bb.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
}
});
return bb;
});
try {
LOG.info("!_! Connect.");
ChannelFuture cf = b.connect(parts[0], port);
LOG.info("!_! Sync.");
cf.sync();
LOG.info("!_! Get Channel.");
Channel c = cf.channel();
data.forEach((metric, timestamp) -> {
// TODO optimize
c.write(Unpooled.copiedBuffer(metric + " " + (timestamp / 1000) + "\n", Charsets.ISO_8859_1));
});
LOG.info("!_! Flush.");
c.flush();
LOG.info("!_! Close.");
c.close();
} catch (InterruptedException e) {
throw Exceptions.runtime(e);
}
}
}
示例5: initChannel
import io.netty.handler.proxy.HttpProxyHandler; //导入依赖的package包/类
@Override
protected void initChannel(SocketChannel ch) throws Exception {
// Add the generic handlers to the pipeline
// e.g. SSL handler
if (proxyServerConfiguration != null) {
if (proxyServerConfiguration.getProxyUsername() != null
&& proxyServerConfiguration.getProxyPassword() != null) {
ch.pipeline().addLast("proxyServer",
new HttpProxyHandler(proxyServerConfiguration.getInetSocketAddress(),
proxyServerConfiguration.getProxyUsername(),
proxyServerConfiguration.getProxyPassword()));
} else {
ch.pipeline()
.addLast("proxyServer", new HttpProxyHandler(proxyServerConfiguration.getInetSocketAddress()));
}
}
if (sslEngine != null) {
log.debug("adding ssl handler");
ch.pipeline().addLast("ssl", new SslHandler(this.sslEngine));
}
ch.pipeline().addLast("compressor", new CustomHttpContentCompressor(chunkDisabled));
ch.pipeline().addLast("decoder", new HttpResponseDecoder());
ch.pipeline().addLast("encoder", new HttpRequestEncoder());
ch.pipeline().addLast("chunkWriter", new ChunkedWriteHandler());
if (httpTraceLogEnabled) {
ch.pipeline().addLast(Constants.HTTP_TRACE_LOG_HANDLER,
new HTTPTraceLoggingHandler("tracelog.http.upstream", LogLevel.DEBUG));
}
if (followRedirect) {
if (log.isDebugEnabled()) {
log.debug("Follow Redirect is enabled, so adding the redirect handler to the pipeline.");
}
RedirectHandler redirectHandler = new RedirectHandler(sslEngine, httpTraceLogEnabled, maxRedirectCount
, chunkDisabled);
ch.pipeline().addLast(Constants.REDIRECT_HANDLER, redirectHandler);
}
handler = new TargetHandler();
ch.pipeline().addLast(Constants.TARGET_HANDLER, handler);
}
示例6: initChannel
import io.netty.handler.proxy.HttpProxyHandler; //导入依赖的package包/类
@Override
public void initChannel(SocketChannel channel) {
ChannelPipeline pipeline = channel.pipeline();
if (proxyConfiguration != null) {
if (proxyConfiguration.getType() == ProxyConfiguration.Type.HTTPS) {
pipeline.addLast(new HttpProxyHandler(proxyConfiguration.getProxyAddress()));
} else if (proxyConfiguration.getType() == ProxyConfiguration.Type.SOCKS5) {
pipeline.addLast(new Socks5ProxyHandler(proxyConfiguration.getProxyAddress()));
}
}
pipeline.addLast(httpClientConnectionHandler);
if (channel.attr(SECURE) != null && channel.attr(SECURE).get() != null && channel.attr(SECURE).get()) {
InetSocketAddress remoteAddress = channel.attr(REMOTE_SOCKET).get();
pipeline.addLast(nettySslContextFactory().createClientSslContext().newHandler(channel.alloc(), remoteAddress.getHostName(), remoteAddress.getPort()));
}
// add logging
if (mockServerLogger.isEnabled(TRACE)) {
pipeline.addLast(new LoggingHandler("NettyHttpClient -->"));
}
pipeline.addLast(new HttpClientCodec());
pipeline.addLast(new HttpContentDecompressor());
pipeline.addLast(new HttpObjectAggregator(Integer.MAX_VALUE));
pipeline.addLast(new MockServerClientCodec());
pipeline.addLast(httpClientHandler);
}