Search in sources :

Example 6 with CurlRequest

use of org.platformlayer.ops.helpers.CurlRequest in project platformlayer by platformlayer.

the class HttpProxyHelper method chooseProxy.

private String chooseProxy(OpsTarget target, List<String> proxies) {
    String bestProxy = null;
    TimeSpan bestTime = null;
    for (String proxy : proxies) {
        // {
        // // We choose the fastest proxy that gives us a 200 response
        // String url = proxy + "acng-report.html";
        // CurlRequest request = new CurlRequest(url);
        // request.setTimeout(5);
        // try {
        // CurlResult result = request.executeRequest(target);
        // if (result.getHttpResult() != 200) {
        // log.info("Unexpected response code while testing proxy: " + proxy + ".  Code=" + result.getHttpResult());
        // continue;
        // }
        // TimeSpan timeTotal = result.getTimeTotal();
        // if (bestTime == null || timeTotal.isLessThan(bestTime)) {
        // bestProxy = proxy;
        // bestTime = timeTotal;
        // }
        // } catch (ProcessExecutionException e) {
        // log.info("Error while testing proxy: " + proxy, e);
        // }
        // }
        {
            // We choose the fastest proxy that gives us a 200 response
            String url = "http://ftp.debian.org/debian/dists/stable/Release.gpg";
            CurlRequest request = new CurlRequest(url);
            request.proxy = proxy;
            request.timeout = TimeSpan.FIVE_SECONDS;
            try {
                CurlResult result = request.executeRequest(target);
                if (result.getHttpResult() != 200) {
                    log.info("Unexpected response code while testing proxy: " + proxy + ".  Code=" + result.getHttpResult());
                    continue;
                }
                TimeSpan timeTotal = result.getTimeTotal();
                if (bestTime == null || timeTotal.isLessThan(bestTime)) {
                    bestProxy = proxy;
                    bestTime = timeTotal;
                }
            } catch (OpsException e) {
                log.info("Error while testing proxy: " + proxy, e);
            }
        }
    }
    return bestProxy;
}
Also used : TimeSpan(com.fathomdb.TimeSpan) CurlResult(org.platformlayer.ops.helpers.CurlResult) OpsException(org.platformlayer.ops.OpsException) CurlRequest(org.platformlayer.ops.helpers.CurlRequest)

Aggregations

CurlRequest (org.platformlayer.ops.helpers.CurlRequest)6 Command (org.platformlayer.ops.Command)4 OpsException (org.platformlayer.ops.OpsException)4 CurlResult (org.platformlayer.ops.helpers.CurlResult)3 IOException (java.io.IOException)2 RequestBuilder (org.openstack.client.common.RequestBuilder)2 CommandEnvironment (org.platformlayer.ops.CommandEnvironment)2 ProcessExecution (org.platformlayer.ops.process.ProcessExecution)2 TimeSpan (com.fathomdb.TimeSpan)1 Md5Hash (com.fathomdb.hash.Md5Hash)1 File (java.io.File)1 InputStream (java.io.InputStream)1 InetAddress (java.net.InetAddress)1 URISyntaxException (java.net.URISyntaxException)1 UnknownHostException (java.net.UnknownHostException)1 Map (java.util.Map)1 OpenstackStorageClient (org.openstack.client.storage.OpenstackStorageClient)1 ObjectProperties (org.openstack.model.storage.ObjectProperties)1 CasStoreMap (org.platformlayer.cas.CasStoreMap)1 CasStoreObject (org.platformlayer.cas.CasStoreObject)1