Example usage for java.net HttpURLConnection getFollowRedirects

List of usage examples for java.net HttpURLConnection getFollowRedirects

Introduction

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

Prototype

public static boolean getFollowRedirects() 

Source Link

Document

Returns a boolean indicating whether or not HTTP redirects (3xx) should be automatically followed.

Usage

From source file:demo.ServerRunning.java

@Override
public Statement apply(final Statement base, FrameworkMethod method, Object target) {

    // Check at the beginning, so this can be used as a static field
    Assume.assumeTrue(serverOnline.get(port));

    RestTemplate client = new RestTemplate();
    boolean followRedirects = HttpURLConnection.getFollowRedirects();
    HttpURLConnection.setFollowRedirects(false);
    boolean online = false;
    try {/*from w ww .  j  a va 2s.c  om*/
        client.getForEntity(new UriTemplate(getUrl("/info")).toString(), String.class);
        online = true;
        logger.info("Basic connectivity test passed");
    } catch (RestClientException e) {
        logger.warn(String.format(
                "Not executing tests because basic connectivity test failed for hostName=%s, port=%d", hostName,
                port), e);
        Assume.assumeNoException(e);
    } finally {
        HttpURLConnection.setFollowRedirects(followRedirects);
        if (!online) {
            serverOnline.put(port, false);
        }
    }

    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            base.evaluate();
        }
    };

}

From source file:com.yangcong345.android.phone.manager.OkHttpStack.java

@Override
public synchronized HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
        throws IOException, AuthFailureError {
    String url = request.getUrl();

    if (mUrlRewriter != null) {
        String rewritten = mUrlRewriter.rewriteUrl(url);
        if (rewritten == null) {
            throw new IOException("URL blocked by rewriter: " + url);
        }// ww  w. j  a v a 2s .  com
        url = rewritten;
    }

    /*init request builder*/
    okhttp3.Request.Builder okRequestBuilder = new okhttp3.Request.Builder().url(url)
            .cacheControl(new CacheControl.Builder().maxAge(0, TimeUnit.SECONDS).build());

    /*set request headers*/
    HashMap<String, String> map = new HashMap<String, String>();
    map.putAll(request.getHeaders());
    map.putAll(additionalHeaders);
    for (String headerName : map.keySet()) {
        okRequestBuilder.addHeader(headerName, map.get(headerName));
    }

    /*set request method*/
    setRequestMethod(okRequestBuilder, request);

    okhttp3.Request okRequest = okRequestBuilder.build();

    /*init request client*/
    int timeoutMs = request.getTimeoutMs();
    OkHttpClient.Builder okClientBuilder = new OkHttpClient.Builder();
    okClientBuilder.followRedirects(HttpURLConnection.getFollowRedirects())
            .followSslRedirects(HttpURLConnection.getFollowRedirects())
            .connectTimeout(timeoutMs, TimeUnit.MILLISECONDS).readTimeout(timeoutMs, TimeUnit.MILLISECONDS);

    if (okRequest.isHttps() && mSslSocketFactory != null) {
        okClientBuilder.sslSocketFactory(mSslSocketFactory);
    }

    // Initialize HttpResponse with data from the HttpURLConnection.
    ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);

    OkHttpClient okClient = okClientBuilder.build();

    Response okResponse = okClient.newCall(okRequest).execute();
    int responseCode = okResponse.code();
    if (responseCode == -1) {
        // -1 is returned by getResponseCode() if the response code could not be retrieved.
        // Signal to the caller that something was wrong with the connection.
        throw new IOException("Could not retrieve response code from HttpUrlConnection.");
    }
    StatusLine responseStatus = new BasicStatusLine(protocolVersion, responseCode, okResponse.message());
    BasicHttpResponse response = new BasicHttpResponse(responseStatus);
    if (hasResponseBody(request.getMethod(), responseStatus.getStatusCode())) {
        response.setEntity(entityFromOkHttp(okResponse));
    }

    for (int i = 0; i < okResponse.headers().size(); i++) {
        String name = okResponse.headers().name(i);
        String value = okResponse.headers().value(i);
        Header h = new BasicHeader(name, value);
        response.addHeader(h);
    }
    return response;
}

From source file:org.apigw.authserver.ServerRunning.java

public Statement apply(final Statement base, FrameworkMethod method, Object target) {

    // Check at the beginning, so this can be used as a static field
    if (assumeOnline) {
        Assume.assumeTrue(serverOnline.get(port));
    } else {//from  ww w.j  av  a  2 s  .  co  m
        Assume.assumeTrue(serverOffline.get(port));
    }

    RestTemplate client = new RestTemplate();
    boolean followRedirects = HttpURLConnection.getFollowRedirects();
    HttpURLConnection.setFollowRedirects(false);
    boolean online = false;
    try {
        client.getForEntity(getUrl("/apigw-auth-server-web/login.jsp").toString(), String.class);
        online = true;
        logger.info("Basic connectivity test passed");
    } catch (RestClientException e) {
        logger.warn(String.format(
                "Not executing tests because basic connectivity test failed for hostName=%s, port=%d", hostName,
                port), e);
        if (assumeOnline) {
            Assume.assumeNoException(e);
        }
    } finally {
        HttpURLConnection.setFollowRedirects(followRedirects);
        if (online) {
            serverOffline.put(port, false);
            if (!assumeOnline) {
                Assume.assumeTrue(serverOffline.get(port));
            }

        } else {
            serverOnline.put(port, false);
        }
    }

    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            postForStatus("/apigw-auth-server-web/login.jsp", new LinkedMultiValueMap<String, String>());
            base.evaluate();
        }
    };

}

From source file:com.android.volley.toolbox.HurlStack.java

/**
 * Create an {@link HttpURLConnection} for the specified {@code url}.
 *//*from   ww w  .  ja  v a  2  s .  c o m*/
protected HttpURLConnection createConnection(URL url) throws IOException {
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();

    // Workaround for the M release HttpURLConnection not observing the
    // HttpURLConnection.setFollowRedirects() property.
    // https://code.google.com/p/android/issues/detail?id=194495
    connection.setInstanceFollowRedirects(HttpURLConnection.getFollowRedirects());

    return connection;
}

From source file:com.amazonaws.ipnreturnurlvalidation.SignatureUtilsForOutbound.java

/**
 * Fetches the public key certificate from the given url and caches it in
 * memory./*  w  w w  . j  ava 2 s  .  c  o m*/
 */
private String getPublicKeyCertificateAsString(String certificateUrl) throws SignatureException {
    // 1. Try to fetch from the in-memory cache
    String certificate = keyStore.get(certificateUrl);
    if (certificate != null)
        return certificate;

    // 2. If not found in cache, fetch it
    boolean followRedirects = HttpURLConnection.getFollowRedirects();
    HttpURLConnection.setFollowRedirects(false);
    try {
        certificate = URLReader.getUrlContents(certificateUrl);
    } catch (IOException e) {
        throw new SignatureException(e);
    } finally {
        HttpURLConnection.setFollowRedirects(followRedirects);
    }

    // 3. populate newly fetched certificate in cache.
    keyStore.put(certificateUrl, certificate);

    return certificate;
}

From source file:org.zend.sdklib.internal.target.ZendDevCloud.java

private boolean setFollowRedirect() {
    final boolean orginal = HttpURLConnection.getFollowRedirects();
    HttpURLConnection.setFollowRedirects(false);
    return orginal;
}