Example usage for java.net HttpURLConnection setInstanceFollowRedirects

List of usage examples for java.net HttpURLConnection setInstanceFollowRedirects

Introduction

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

Prototype

public void setInstanceFollowRedirects(boolean followRedirects) 

Source Link

Document

Sets whether HTTP redirects (requests with response code 3xx) should be automatically followed by this HttpURLConnection instance.

Usage

From source file:de.schildbach.wallet.ExchangeRatesProvider.java

private static Map<String, ExchangeRate> requestExchangeRates(final URL url, final String userAgent,
        final String source, final String... fields) {
    final long start = System.currentTimeMillis();

    HttpURLConnection connection = null;
    Reader reader = null;/*w w  w  . ja v a2s .  c o m*/

    try {
        connection = (HttpURLConnection) url.openConnection();

        connection.setInstanceFollowRedirects(false);
        connection.setConnectTimeout(Constants.HTTP_TIMEOUT_MS);
        connection.setReadTimeout(Constants.HTTP_TIMEOUT_MS);
        connection.addRequestProperty("User-Agent", userAgent);
        connection.addRequestProperty("Accept-Encoding", "gzip");
        connection.connect();

        final int responseCode = connection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            final String contentEncoding = connection.getContentEncoding();

            InputStream is = new BufferedInputStream(connection.getInputStream(), 1024);
            if ("gzip".equalsIgnoreCase(contentEncoding))
                is = new GZIPInputStream(is);

            reader = new InputStreamReader(is, Charsets.UTF_8);
            final StringBuilder content = new StringBuilder();
            final long length = Io.copy(reader, content);

            final Map<String, ExchangeRate> rates = new TreeMap<String, ExchangeRate>();

            final JSONObject head = new JSONObject(content.toString());
            for (final Iterator<String> i = head.keys(); i.hasNext();) {
                final String currencyCode = i.next();
                if (!"timestamp".equals(currencyCode)) {
                    final JSONObject o = head.getJSONObject(currencyCode);

                    for (final String field : fields) {
                        final String rateStr = o.optString(field, null);

                        if (rateStr != null) {
                            try {
                                final Fiat rate = Fiat.parseFiat(currencyCode, rateStr);

                                if (rate.signum() > 0) {
                                    rates.put(currencyCode, new ExchangeRate(
                                            new org.bitcoinj.utils.ExchangeRate(rate), source));
                                    break;
                                }
                            } catch (final NumberFormatException x) {
                                log.warn("problem fetching {} exchange rate from {} ({}): {}", currencyCode,
                                        url, contentEncoding, x.getMessage());
                            }
                        }
                    }
                }
            }

            log.info("fetched exchange rates from {} ({}), {} chars, took {} ms", url, contentEncoding, length,
                    System.currentTimeMillis() - start);

            return rates;
        } else {
            log.warn("http status {} when fetching exchange rates from {}", responseCode, url);
        }
    } catch (final Exception x) {
        log.warn("problem fetching exchange rates from " + url, x);
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (final IOException x) {
                // swallow
            }
        }

        if (connection != null)
            connection.disconnect();
    }

    return null;
}

From source file:org.apache.hadoop.yarn.client.TestRMFailover.java

static String getRefreshURL(String url) {
    String redirectUrl = null;//from w w w  .  j a  v  a 2s. c  o m
    try {
        HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
        // do not automatically follow the redirection
        // otherwise we get too many redirections exception
        conn.setInstanceFollowRedirects(false);
        redirectUrl = conn.getHeaderField("Refresh");
    } catch (Exception e) {
        // throw new RuntimeException(e);
    }
    return redirectUrl;
}

From source file:org.apache.hadoop.yarn.client.TestRMFailover.java

static String getRedirectURL(String url) {
    String redirectUrl = null;//w w w . j  a  v  a2 s .co m
    try {
        HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
        // do not automatically follow the redirection
        // otherwise we get too many redirections exception
        conn.setInstanceFollowRedirects(false);
        if (conn.getResponseCode() == HttpServletResponse.SC_TEMPORARY_REDIRECT) {
            redirectUrl = conn.getHeaderField("Location");
        }
    } catch (Exception e) {
        // throw new RuntimeException(e);
    }
    return redirectUrl;
}

From source file:eu.eubrazilcc.lvl.oauth2.AuthTest.java

private static HttpURLConnection doRequest(final OAuthClientRequest request) throws IOException {
    final URL url = new URL(request.getLocationUri());
    final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setInstanceFollowRedirects(true);
    conn.connect();//from  w w w  . java 2 s.  c o  m
    conn.getResponseCode();
    return conn;
}

From source file:net.doubledoordev.cmd.CurseModpackDownloader.java

public static String getFinalURL(String url) throws IOException {
    while (true) {
        HttpURLConnection con = null;
        try {// w ww  . j a v  a 2 s.c o m
            con = (HttpURLConnection) new URL(url).openConnection();
            con.setInstanceFollowRedirects(false);
            con.connect();
            if (con.getHeaderField("Location") == null)
                return url;
            url = con.getHeaderField("Location");
        } catch (IOException e) {
            return url;
        } finally {
            if (con != null)
                con.disconnect();
        }
    }
}

From source file:biz.mosil.webtools.MosilWeb.java

/**
 * ? InputStream/*  www  .j av a  2s.co m*/
 * */
public static final InputStream getInputStream(final String _url) {
    InputStream result = null;

    try {
        URL url = new URL(_url);
        URLConnection conn = url.openConnection();

        if (!(conn instanceof HttpURLConnection)) {
            throw new IOException("This URL Can't Connect!");
        }

        HttpURLConnection httpConn = (HttpURLConnection) conn;
        //????
        httpConn.setAllowUserInteraction(false);
        //??
        httpConn.setInstanceFollowRedirects(true);
        httpConn.setRequestMethod("GET");
        httpConn.connect();

        final int response = httpConn.getResponseCode();

        if (response == HttpURLConnection.HTTP_OK) {
            result = httpConn.getInputStream();
        }

    } catch (MalformedURLException _ex) {
        Log.e("getInputStream", "Malformed URL Exception: " + _ex.toString());
    } catch (IOException _ex) {
        Log.e("getInputStream", "IO Exception: " + _ex.toString());
    }
    return result;
}

From source file:ubic.basecode.ontology.OntologyLoader.java

/**
 * Load an ontology into memory. Use this type of model when fast access is critical and memory is available.
 * If load from URL fails, attempt to load from disk cache under @cacheName.
 * /* w  w  w. j  a v  a2s.  c  o m*/
 * @param  url
 * @param  spec      e.g. OWL_MEM_TRANS_INF
 * @param  cacheName unique name of this ontology, will be used to load from disk in case of failed url connection
 * @return
 */
public static OntModel loadMemoryModel(String url, OntModelSpec spec, String cacheName) {
    StopWatch timer = new StopWatch();
    timer.start();
    OntModel model = getMemoryModel(url, spec);

    URLConnection urlc = null;
    int tries = 0;
    while (tries < MAX_CONNECTION_TRIES) {
        try {
            urlc = new URL(url).openConnection();
            // help ensure mis-configured web servers aren't causing trouble.
            urlc.setRequestProperty("Accept", "application/rdf+xml");

            try {
                HttpURLConnection c = (HttpURLConnection) urlc;
                c.setInstanceFollowRedirects(true);
            } catch (ClassCastException e) {
                // not via http, using a FileURLConnection.
            }

            if (tries > 0) {
                log.info("Retrying connecting to " + url + " [" + tries + "/" + MAX_CONNECTION_TRIES
                        + " of max tries");
            } else {
                log.info("Connecting to " + url);
            }

            urlc.connect(); // Will error here on bad URL

            if (urlc instanceof HttpURLConnection) {
                String newUrl = urlc.getHeaderField("Location");

                if (StringUtils.isNotBlank(newUrl)) {
                    log.info("Redirect to " + newUrl);
                    urlc = new URL(newUrl).openConnection();
                    // help ensure mis-configured web servers aren't causing trouble.
                    urlc.setRequestProperty("Accept", "application/rdf+xml");
                    urlc.connect();
                }
            }

            break;
        } catch (IOException e) {
            // try to recover.
            log.error(e + " retrying?");
            tries++;
        }
    }

    if (urlc != null) {
        try (InputStream in = urlc.getInputStream();) {
            Reader reader;
            if (cacheName != null) {
                // write tmp to disk
                File tempFile = getTmpDiskCachePath(cacheName);
                if (tempFile == null) {
                    reader = new InputStreamReader(in);
                } else {
                    tempFile.getParentFile().mkdirs();
                    Files.copy(in, tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
                    reader = new FileReader(tempFile);
                }

            } else {
                // Skip the cache
                reader = new InputStreamReader(in);
            }

            assert reader != null;
            try (BufferedReader buf = new BufferedReader(reader);) {
                model.read(buf, url);
            }

            log.info("Load model: " + timer.getTime() + "ms");
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }

    if (cacheName != null) {

        File f = getDiskCachePath(cacheName);
        File tempFile = getTmpDiskCachePath(cacheName);
        File oldFile = getOldDiskCachePath(cacheName);

        if (model.isEmpty()) {
            // Attempt to load from disk cache

            if (f == null) {
                throw new RuntimeException(
                        "Ontology cache directory required to load from disk: ontology.cache.dir");
            }

            if (f.exists() && !f.isDirectory()) {
                try (BufferedReader buf = new BufferedReader(new FileReader(f));) {
                    model.read(buf, url);
                    // We successfully loaded the cached ontology. Copy the loaded ontology to oldFile
                    // so that we don't recreate indices during initialization based on a false change in
                    // the ontology.
                    Files.copy(f.toPath(), oldFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
                    log.info("Load model from disk: " + timer.getTime() + "ms");
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                    throw new RuntimeException(
                            "Ontology failed load from URL (" + url + ") and disk cache: " + cacheName);
                }
            } else {
                throw new RuntimeException("Ontology failed load from URL (" + url
                        + ") and disk cache does not exist: " + cacheName);
            }

        } else {
            // Model was successfully loaded into memory from URL with given cacheName
            // Save cache to disk (rename temp file)
            log.info("Caching ontology to disk: " + cacheName);
            if (f != null) {
                try {
                    // Need to compare previous to current so instead of overwriting we'll move the old file
                    f.createNewFile();
                    Files.move(f.toPath(), oldFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
                    Files.move(tempFile.toPath(), f.toPath(), StandardCopyOption.REPLACE_EXISTING);
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }
            } else {
                log.warn("Ontology cache directory required to save to disk: ontology.cache.dir");
            }
        }

    }

    assert !model.isEmpty();

    return model;
}

From source file:com.evilisn.DAO.CertMapper.java

public static byte[] httpGetBin(URI uri, boolean bActiveCheckUnknownHost) throws Exception

{

    InputStream is = null;/*from  ww  w  .  j a v a2  s.c  om*/

    InputStream is_temp = null;

    try {

        if (uri == null)
            return null;

        URL url = uri.toURL();

        if (bActiveCheckUnknownHost) {

            url.getProtocol();

            String host = url.getHost();

            int port = url.getPort();

            if (port == -1)

                port = url.getDefaultPort();

            InetSocketAddress isa = new InetSocketAddress(host, port);

            if (isa.isUnresolved()) {

                //fix JNLP popup error issue

                throw new UnknownHostException("Host Unknown:" + isa.toString());

            }

        }

        HttpURLConnection uc = (HttpURLConnection) url.openConnection();

        uc.setDoInput(true);

        uc.setAllowUserInteraction(false);

        uc.setInstanceFollowRedirects(true);

        setTimeout(uc);

        String contentEncoding = uc.getContentEncoding();

        int len = uc.getContentLength();

        // is = uc.getInputStream();

        if (contentEncoding != null && contentEncoding.toLowerCase().indexOf("gzip") != -1)

        {

            is_temp = uc.getInputStream();

            is = new GZIPInputStream(is_temp);

        }

        else if (contentEncoding != null && contentEncoding.toLowerCase().indexOf("deflate") != -1)

        {

            is_temp = uc.getInputStream();

            is = new InflaterInputStream(is_temp);

        }

        else

        {

            is = uc.getInputStream();

        }

        if (len != -1) {

            int ch = 0, i = 0;

            byte[] res = new byte[len];

            while ((ch = is.read()) != -1) {

                res[i++] = (byte) (ch & 0xff);

            }

            return res;

        } else {

            ArrayList<byte[]> buffer = new ArrayList<byte[]>();

            int buf_len = 1024;

            byte[] res = new byte[buf_len];

            int ch = 0, i = 0;

            while ((ch = is.read()) != -1) {

                res[i++] = (byte) (ch & 0xff);

                if (i == buf_len) {

                    //rotate

                    buffer.add(res);

                    i = 0;

                    res = new byte[buf_len];

                }

            }

            int total_len = buffer.size() * buf_len + i;

            byte[] buf = new byte[total_len];

            for (int j = 0; j < buffer.size(); j++) {

                System.arraycopy(buffer.get(j), 0, buf, j * buf_len, buf_len);

            }

            if (i > 0) {

                System.arraycopy(res, 0, buf, buffer.size() * buf_len, i);

            }

            return buf;

        }

    } catch (Exception e) {

        e.printStackTrace();

        return null;

    } finally {

        closeInputStream(is_temp);

        closeInputStream(is);

    }

}

From source file:de.jdellay.wallet.ExchangeRatesProvider.java

private static Map<String, ExchangeRate> requestExchangeRates(final URL url, float ccnBtcConversion,
        final String userAgent, final String source, final String... fields) {
    final long start = System.currentTimeMillis();

    HttpURLConnection connection = null;
    Reader reader = null;/*w  ww.j a  v a 2  s . co  m*/

    try {
        connection = (HttpURLConnection) url.openConnection();

        connection.setInstanceFollowRedirects(false);
        connection.setConnectTimeout(Constants.HTTP_TIMEOUT_MS);
        connection.setReadTimeout(Constants.HTTP_TIMEOUT_MS);
        connection.addRequestProperty("User-Agent", userAgent);
        connection.addRequestProperty("Accept-Encoding", "gzip");
        connection.connect();

        final int responseCode = connection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            final String contentEncoding = connection.getContentEncoding();

            InputStream is = new BufferedInputStream(connection.getInputStream(), 1024);
            if ("gzip".equalsIgnoreCase(contentEncoding))
                is = new GZIPInputStream(is);

            reader = new InputStreamReader(is, Constants.UTF_8);
            final StringBuilder content = new StringBuilder();
            final long length = Io.copy(reader, content);

            final Map<String, ExchangeRate> rates = new TreeMap<String, ExchangeRate>();

            final JSONObject head = new JSONObject(content.toString());
            for (final Iterator<String> i = head.keys(); i.hasNext();) {
                final String currencyCode = i.next();
                if (!"timestamp".equals(currencyCode)) {
                    final JSONObject o = head.getJSONObject(currencyCode);

                    for (final String field : fields) {
                        final String rate = o.optString(field, null);

                        if (rate != null) {
                            try {
                                BigDecimal btcRate = new BigDecimal(GenericUtils.toNanoCoins(rate, 0));
                                BigInteger ccnRate = btcRate.multiply(BigDecimal.valueOf(ccnBtcConversion))
                                        .toBigInteger();

                                if (ccnRate.signum() > 0) {
                                    rates.put(currencyCode, new ExchangeRate(currencyCode, ccnRate, source));
                                    break;
                                }
                            } catch (final ArithmeticException x) {
                                log.warn("problem fetching {} exchange rate from {} ({}): {}", currencyCode,
                                        url, contentEncoding, x.getMessage());
                            }
                        }
                    }
                }
            }

            log.info("fetched exchange rates from {} ({}), {} chars, took {} ms", url, contentEncoding, length,
                    System.currentTimeMillis() - start);

            return rates;
        } else {
            log.warn("http status {} when fetching {}", responseCode, url);
        }
    } catch (final Exception x) {
        log.warn("problem fetching exchange rates from " + url, x);
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (final IOException x) {
                // swallow
            }
        }

        if (connection != null)
            connection.disconnect();
    }

    return null;
}

From source file:gribbit.util.RequestBuilder.java

/**
 * Make a GET or POST request, handling up to 6 redirects, and return the response. If isBinaryResponse is true,
 * returns a byte[] array, otherwise returns the response as a String.
 *///from   ww w.j a v  a 2s.com
private static Object makeRequest(String url, String[] keyValuePairs, boolean isGET, boolean isBinaryResponse,
        int redirDepth) {
    if (redirDepth > 6) {
        throw new IllegalArgumentException("Too many redirects");
    }
    HttpURLConnection connection = null;
    try {
        // Add the URL query params if this is a GET request
        String reqURL = isGET ? url + "?" + WebUtils.buildQueryString(keyValuePairs) : url;
        connection = (HttpURLConnection) new URL(reqURL).openConnection();
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setInstanceFollowRedirects(false);
        connection.setRequestMethod(isGET ? "GET" : "POST");
        connection.setUseCaches(false);
        connection.setRequestProperty("User-Agent",
                "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) "
                        + "Chrome/43.0.2357.125 Safari/537.36");
        if (!isGET) {
            // Send the body if this is a POST request
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setRequestProperty("charset", "utf-8");
            String params = WebUtils.buildQueryString(keyValuePairs);
            connection.setRequestProperty("Content-Length", Integer.toString(params.length()));
            try (DataOutputStream w = new DataOutputStream(connection.getOutputStream())) {
                w.writeBytes(params);
                w.flush();
            }
        }
        if (connection.getResponseCode() == HttpResponseStatus.FOUND.code()) {
            // Follow a redirect. For safety, the params are not passed on, and the method is forced to GET.
            return makeRequest(connection.getHeaderField("Location"), /* keyValuePairs = */null, /* isGET = */
                    true, isBinaryResponse, redirDepth + 1);
        } else if (connection.getResponseCode() == HttpResponseStatus.OK.code()) {
            // For 200 OK, return the text of the response
            if (isBinaryResponse) {
                ByteArrayOutputStream output = new ByteArrayOutputStream(32768);
                IOUtils.copy(connection.getInputStream(), output);
                return output.toByteArray();
            } else {
                StringWriter writer = new StringWriter(1024);
                IOUtils.copy(connection.getInputStream(), writer, "UTF-8");
                return writer.toString();
            }
        } else {
            throw new IllegalArgumentException(
                    "Got non-OK HTTP response code: " + connection.getResponseCode());
        }
    } catch (Exception e) {
        throw new IllegalArgumentException(
                "Exception during " + (isGET ? "GET" : "POST") + " request: " + e.getMessage(), e);
    } finally {
        if (connection != null) {
            try {
                connection.disconnect();
            } catch (Exception e) {
            }
        }
    }
}