Adding support for specifying a Proxy to use for the connection#38
Adding support for specifying a Proxy to use for the connection#38davydotcom wants to merge 1 commit into
Conversation
Signed-off-by: David Estes <davydotcom@gmail.com>
| SSLContext sslcontext = SSLContext.getInstance("TLS"); | ||
| sslcontext.init(null, new TrustManager[]{noCaTrustManager}, null); | ||
| sslsf = new SSLConnectionSocketFactory(sslcontext, NoopHostnameVerifier.INSTANCE); | ||
| sslsf = new SSLConnectionSocketFactory(sslcontext, NoopHostnameVerifier.INSTANCE) { |
There was a problem hiding this comment.
Why no socks handling for secure ssl, i.e. ssl with a valid certificate?
Also, this seems like duplicate code?
Why not do something like:
static class ProxyConnectionSocketFactory implements ConnectionSocketFactory {
private final ConnectionSocketFactory delegate;
private final ProxyInfo proxyInfo;
ProxyConnectionSocketFactory(ConnectionSocketFactory delegate, ProxyInfo proxyInfo) {
this.delegate = delegate;
this.proxyInfo = proxyInfo;
}
/**
* Wraps the given factory with SOCKS proxy support if needed.
* Returns the original factory unchanged if proxy is null or not SOCKS.
*/
static ConnectionSocketFactory wrapIfNeeded(ConnectionSocketFactory factory, ProxyInfo proxyInfo) {
if (proxyInfo != null
&& proxyInfo.getProxyType() == ProxyInfo.ProxyType.SOCKS
&& proxyInfo.getHost() != null
&& proxyInfo.getProxyPort() != null) {
return new ProxyConnectionSocketFactory(factory, proxyInfo);
}
return factory;
}
// ... delegate methods ...
} Use it like this:
private Registry createConnectionSocketFactoryRegistry() {
ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory();
ConnectionSocketFactory sslsf = createSslSocketFactory();
return RegistryBuilder.<ConnectionSocketFactory>create()
.register(HTTP_PROTOCOL, ProxyConnectionSocketFactory.wrapIfNeeded(plainsf, proxyInfo))
.register(HTTPS_PROTOCOL, ProxyConnectionSocketFactory.wrapIfNeeded(sslsf, proxyInfo))
.build();
} | package org.ovirt.engine.sdk4; | ||
|
|
||
| public class ProxyInfo { | ||
| protected String host; |
There was a problem hiding this comment.
why are some fields protected?
| protected String user; | ||
| protected String password; | ||
| private String proxyDomain; | ||
| private String proxyWorkstation; |
There was a problem hiding this comment.
proxyWorkstation isn't set in the constructor? Any reason for that?
| } | ||
|
|
||
|
|
||
| //create an enum for proxy type |
| protected String user; | ||
| protected String password; | ||
| protected String token; | ||
|
|
| private String ssoTokenName = null; | ||
| private String ssoUrl = null; | ||
| private String ssoRevokeUrl = null; | ||
|
|
| if(proxyInfo.getHost() != null && proxyInfo.getProxyPort() != null) { | ||
| clientBuilder.setProxy(new HttpHost(proxyInfo.getHost(), proxyInfo.getProxyPort())); | ||
| if(proxyInfo.getUser() != null) { //authenticated proxy | ||
| NTCredentials ntCreds = new NTCredentials(proxyInfo.getUser(), proxyInfo.getPassword(), proxyInfo.getProxyWorkstation(), proxyInfo.getProxyDomain()); |
There was a problem hiding this comment.
NTCredentials feels oddly specific? I would say the majority of proxies doesn't use windows domain auth?
| private ProxyType proxyType; | ||
|
|
||
|
|
||
| public ProxyInfo(String host, Integer proxyPort, String user, String password, String proxyDomain, ProxyType proxyType) { |
There was a problem hiding this comment.
I would add some validation to the constructor to check for mandatory fields, this would make it more user friendly.
| clientBuilder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy()); | ||
| } | ||
| } | ||
| } else if(proxyInfo.getProxyType() == ProxyInfo.ProxyType.SOCKS) { |
There was a problem hiding this comment.
Just remove this if, just having this branch for that comment is kind of useless.
This library does not allow you to specify a Proxy for the connection. This adds support for passing Proxy Information for both SOCKS or HTTP proxies