Example usage for java.net HttpURLConnection usingProxy

List of usage examples for java.net HttpURLConnection usingProxy

Introduction

In this page you can find the example usage for java.net HttpURLConnection usingProxy.

Prototype

public abstract boolean usingProxy();

Source Link

Document

Indicates if the connection is going through a proxy.

Usage

From source file:com.yahoo.druid.hadoop.HiveDatasourceInputFormat.java

private String getSegmentsToLoad(String dataSource, List<Interval> intervals, String overlordUrl)
        throws MalformedURLException {
    logger.info("CheckPost7");
    String urlStr = "http://" + overlordUrl + "/druid/indexer/v1/action";
    logger.info("Sending request to overlord at " + urlStr);

    Interval interval = intervals.get(0);

    String requestJson = getSegmentListUsedActionJson(interval.toString());
    logger.info("request json is " + requestJson);

    int numTries = 3;
    for (int trial = 0; trial < numTries; trial++) {
        try {/* w  ww  . jav  a  2 s .  c o m*/
            logger.info("attempt number {} to get list of segments from overlord", trial);
            Proxy proxy = new Proxy(Proxy.Type.HTTP,
                    new InetSocketAddress("httpproxy-prod.blue.ygrid.yahoo.com", 4080));
            URL url = new URL(String.format("%s/druid/coordinator/v1/metadata/datasources/%s/segments?full",
                    overlordUrl, dataSource));
            //new URL(urlStr);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection(proxy);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("content-type", "application/json");
            conn.setRequestProperty("Accept", "*/*");
            conn.setUseCaches(false);
            conn.setDoOutput(true);
            conn.setConnectTimeout(60000);
            conn.usingProxy();
            OutputStream out = conn.getOutputStream();
            out.write(requestJson.getBytes());
            out.close();
            int responseCode = conn.getResponseCode();
            if (responseCode == 200) {
                return IOUtils.toString(conn.getInputStream());
            } else {
                logger.warn(
                        "Attempt Failed to get list of segments from overlord. response code [%s] , response [%s]",
                        responseCode, IOUtils.toString(conn.getInputStream()));
            }
        } catch (Exception ex) {
            logger.warn("Exception in getting list of segments from overlord", ex);
        }

        try {
            Thread.sleep(5000); //wait before next trial
        } catch (InterruptedException ex) {
            Throwables.propagate(ex);
        }
    }

    throw new RuntimeException(
            String.format("failed to find list of segments, dataSource[%s], interval[%s], overlord[%s]",
                    dataSource, interval, overlordUrl));
}