use of org.apache.http.conn.scheme.Scheme in project platformlayer by platformlayer.
the class MetricClientImpl method buildHttpClient.
private HttpClient buildHttpClient(CertificateAndKey certificateAndKey, List<String> trustKeys) {
int port = metricBaseUrl.getPort();
if (port == -1) {
String scheme = metricBaseUrl.getScheme();
if (scheme.equals("https")) {
port = 443;
} else if (scheme.equals("http")) {
port = 80;
} else {
throw new IllegalArgumentException("Unknown scheme: " + scheme);
}
}
SchemeSocketFactory schemeSocketFactory;
try {
KeyManager keyManager = new SimpleClientCertificateKeyManager(certificateAndKey);
TrustManager trustManager;
X509HostnameVerifier hostnameVerifier;
if (trustKeys != null) {
trustManager = new PublicKeyTrustManager(trustKeys);
hostnameVerifier = SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
} else {
trustManager = null;
hostnameVerifier = SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER;
}
javax.net.ssl.SSLSocketFactory sslSocketFactory = SslHelpers.buildSslSocketFactory(keyManager, trustManager);
schemeSocketFactory = new SSLSocketFactory(sslSocketFactory, hostnameVerifier);
} catch (GeneralSecurityException e) {
throw new IllegalArgumentException("Error building SSL client", e);
}
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("https", port, schemeSocketFactory));
PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager(schemeRegistry);
HttpClient httpClient = new DefaultHttpClient(connectionManager);
httpClient = new DecompressingHttpClient(httpClient);
return httpClient;
}
use of org.apache.http.conn.scheme.Scheme in project platformlayer by platformlayer.
the class ApacheCommonsHttpConfiguration method buildHttpClient.
HttpClient buildHttpClient(SslConfiguration sslConfiguration) {
HttpParams httpParams = null;
if (sslConfiguration == null || sslConfiguration.isEmpty()) {
sslConfiguration = null;
}
ClientConnectionManager connectionManager;
if (sslConfiguration != null) {
SchemeSocketFactory schemeSocketFactory;
try {
javax.net.ssl.SSLSocketFactory sslSocketFactory = sslConfiguration.getSslSocketFactory();
X509HostnameVerifier apacheHostnameVerifier = null;
if (sslConfiguration.getHostnameVerifier() != null) {
apacheHostnameVerifier = new ApacheHostnameVerifierAdapter(sslConfiguration.getHostnameVerifier());
} else {
apacheHostnameVerifier = new ApacheHostnameVerifierAdapter(SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
}
schemeSocketFactory = new SSLSocketFactory(sslSocketFactory, apacheHostnameVerifier);
} catch (GeneralSecurityException e) {
throw new IllegalArgumentException("Error building SSL client", e);
}
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("https", 443, schemeSocketFactory));
connectionManager = buildConnectionManager(schemeRegistry);
} else {
SchemeRegistry schemeRegistry = SchemeRegistryFactory.createDefault();
connectionManager = buildConnectionManager(schemeRegistry);
}
HttpClient httpClient = buildDefaultHttpClient(connectionManager, httpParams);
httpClient = wrapHttpClient(httpClient);
return httpClient;
}
use of org.apache.http.conn.scheme.Scheme in project robovm by robovm.
the class DefaultClientConnectionOperator method updateSecureConnection.
// openConnection
// non-javadoc, see interface ClientConnectionOperator
public void updateSecureConnection(OperatedClientConnection conn, HttpHost target, HttpContext context, HttpParams params) throws IOException {
if (conn == null) {
throw new IllegalArgumentException("Connection must not be null.");
}
if (target == null) {
throw new IllegalArgumentException("Target host must not be null.");
}
//@@@ is context allowed to be null?
if (params == null) {
throw new IllegalArgumentException("Parameters must not be null.");
}
if (!conn.isOpen()) {
throw new IllegalArgumentException("Connection must be open.");
}
final Scheme schm = schemeRegistry.getScheme(target.getSchemeName());
if (!(schm.getSocketFactory() instanceof LayeredSocketFactory)) {
throw new IllegalArgumentException("Target scheme (" + schm.getName() + ") must have layered socket factory.");
}
final LayeredSocketFactory lsf = (LayeredSocketFactory) schm.getSocketFactory();
final Socket sock;
try {
sock = lsf.createSocket(conn.getSocket(), target.getHostName(), schm.resolvePort(target.getPort()), true);
} catch (ConnectException ex) {
throw new HttpHostConnectException(target, ex);
}
prepareSocket(sock, context, params);
conn.update(sock, target, lsf.isSecure(sock), params);
//@@@ error handling: close the layered socket in case of exception?
}
use of org.apache.http.conn.scheme.Scheme in project robovm by robovm.
the class DefaultClientConnectionOperator method openConnection.
// non-javadoc, see interface ClientConnectionOperator
public void openConnection(OperatedClientConnection conn, HttpHost target, InetAddress local, HttpContext context, HttpParams params) throws IOException {
if (conn == null) {
throw new IllegalArgumentException("Connection must not be null.");
}
if (target == null) {
throw new IllegalArgumentException("Target host must not be null.");
}
//@@@ is context allowed to be null?
if (params == null) {
throw new IllegalArgumentException("Parameters must not be null.");
}
if (conn.isOpen()) {
throw new IllegalArgumentException("Connection must not be open.");
}
final Scheme schm = schemeRegistry.getScheme(target.getSchemeName());
final SocketFactory sf = schm.getSocketFactory();
final SocketFactory plain_sf;
final LayeredSocketFactory layered_sf;
if (sf instanceof LayeredSocketFactory) {
plain_sf = staticPlainSocketFactory;
layered_sf = (LayeredSocketFactory) sf;
} else {
plain_sf = sf;
layered_sf = null;
}
InetAddress[] addresses = InetAddress.getAllByName(target.getHostName());
for (int i = 0; i < addresses.length; ++i) {
Socket sock = plain_sf.createSocket();
conn.opening(sock, target);
try {
Socket connsock = plain_sf.connectSocket(sock, addresses[i].getHostAddress(), schm.resolvePort(target.getPort()), local, 0, params);
if (sock != connsock) {
sock = connsock;
conn.opening(sock, target);
}
/*
* prepareSocket is called on the just connected
* socket before the creation of the layered socket to
* ensure that desired socket options such as
* TCP_NODELAY, SO_RCVTIMEO, SO_LINGER will be set
* before any I/O is performed on the socket. This
* happens in the common case as
* SSLSocketFactory.createSocket performs hostname
* verification which requires that SSL handshaking be
* performed.
*/
prepareSocket(sock, context, params);
if (layered_sf != null) {
Socket layeredsock = layered_sf.createSocket(sock, target.getHostName(), schm.resolvePort(target.getPort()), true);
if (layeredsock != sock) {
conn.opening(layeredsock, target);
}
conn.openCompleted(sf.isSecure(layeredsock), params);
} else {
conn.openCompleted(sf.isSecure(sock), params);
}
break;
// BEGIN android-changed
// catch SocketException to cover any kind of connect failure
} catch (SocketException ex) {
if (i == addresses.length - 1) {
ConnectException cause = ex instanceof ConnectException ? (ConnectException) ex : new ConnectException(ex.getMessage(), ex);
throw new HttpHostConnectException(target, cause);
}
// END android-changed
} catch (ConnectTimeoutException ex) {
if (i == addresses.length - 1) {
throw ex;
}
}
}
}
use of org.apache.http.conn.scheme.Scheme in project robovm by robovm.
the class DefaultHttpRoutePlanner method determineRoute.
// non-javadoc, see interface HttpRoutePlanner
public HttpRoute determineRoute(HttpHost target, HttpRequest request, HttpContext context) throws HttpException {
if (request == null) {
throw new IllegalStateException("Request must not be null.");
}
// If we have a forced route, we can do without a target.
HttpRoute route = ConnRouteParams.getForcedRoute(request.getParams());
if (route != null)
return route;
if (target == null) {
throw new IllegalStateException("Target host must not be null.");
}
final InetAddress local = ConnRouteParams.getLocalAddress(request.getParams());
final HttpHost proxy = ConnRouteParams.getDefaultProxy(request.getParams());
final Scheme schm = schemeRegistry.getScheme(target.getSchemeName());
// as it is typically used for TLS/SSL, we assume that
// a layered scheme implies a secure connection
final boolean secure = schm.isLayered();
if (proxy == null) {
route = new HttpRoute(target, local, secure);
} else {
route = new HttpRoute(target, local, proxy, secure);
}
return route;
}
Aggregations