Java Code Examples for java.net.HttpURLConnection#getFollowRedirects()

The following examples show how to use java.net.HttpURLConnection#getFollowRedirects() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: j2objc   File: URLConnectionTest.java    License: Apache License 2.0 6 votes vote down vote up
private void testFollowRedirects(String spec) throws Exception {
    URL url = new URL(spec);
    boolean originalValue = HttpURLConnection.getFollowRedirects();
    try {
        HttpURLConnection.setFollowRedirects(false);
        assertFalse(HttpURLConnection.getFollowRedirects());

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        assertFalse(connection.getInstanceFollowRedirects());

        HttpURLConnection.setFollowRedirects(true);
        assertTrue(HttpURLConnection.getFollowRedirects());

        HttpURLConnection connection2 = (HttpURLConnection) url.openConnection();
        assertTrue(connection2.getInstanceFollowRedirects());
    } finally {
        HttpURLConnection.setFollowRedirects(originalValue);
    }
}
 
Example 2
Source Project: Tomcat8-Source-Read   File: TestRewriteValve.java    License: MIT License 5 votes vote down vote up
@Test
public void testPermanentRedirect() throws Exception {
     // Disable the following of redirects for this test only
    boolean originalValue = HttpURLConnection.getFollowRedirects();
    HttpURLConnection.setFollowRedirects(false);
    try {
        doTestRedirect("RewriteRule ^/from/a$ /to/b [R=permanent]", "/redirect/from/a", "/redirect/to/b",
            301);
    } finally {
        HttpURLConnection.setFollowRedirects(originalValue);
    }
}
 
Example 3
Source Project: Tomcat8-Source-Read   File: TestRewriteValve.java    License: MIT License 5 votes vote down vote up
@Test
public void testSeeotherRedirect() throws Exception {
     // Disable the following of redirects for this test only
    boolean originalValue = HttpURLConnection.getFollowRedirects();
    HttpURLConnection.setFollowRedirects(false);
    try {
        doTestRedirect("RewriteRule ^/from/a$ /to/b [R=seeother]", "/redirect/from/a", "/redirect/to/b",
            303);
    } finally {
        HttpURLConnection.setFollowRedirects(originalValue);
    }
}
 
Example 4
Source Project: Tomcat8-Source-Read   File: TestRewriteValve.java    License: MIT License 5 votes vote down vote up
@Test
public void test307Redirect() throws Exception {
     // Disable the following of redirects for this test only
    boolean originalValue = HttpURLConnection.getFollowRedirects();
    HttpURLConnection.setFollowRedirects(false);
    try {
        doTestRedirect("RewriteRule ^/from/a$ /to/b [R=307]", "/redirect/from/a", "/redirect/to/b",
            307);
    } finally {
        HttpURLConnection.setFollowRedirects(originalValue);
    }
}
 
Example 5
Source Project: gitlab4j-api   File: AccessTokenUtils.java    License: MIT License 5 votes vote down vote up
/**
 * Logs out the user associated with the GitLab session cookie.
 *
 * @param baseUrl the GitLab server base URL
 * @param cookies the GitLab session cookie to logout
 * @throws GitLabApiException if any error occurs
 */
protected static final void logout(final String baseUrl, final String cookies) throws GitLabApiException {

    // Save so it can be restored later
    boolean savedFollowRedirects = HttpURLConnection.getFollowRedirects();

    try {

        // Must manually follow redirects
        if (savedFollowRedirects) {
            HttpURLConnection.setFollowRedirects(false);
        }

        String urlString = baseUrl + "/users/sign_out";
        URL url = new URL(urlString);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestProperty("User-Agent", USER_AGENT);
        connection.setRequestProperty("Cookie", cookies);
        connection.setRequestMethod("GET");
        connection.setReadTimeout(10000);
        connection.setConnectTimeout(10000);

        // Make sure a redirect was provided, otherwise it is a logout failure
        int responseCode = connection.getResponseCode();
        if (responseCode != 302) {
            throw new GitLabApiException("Logout failure, aborting!");
        }

    } catch (IOException ioe) {
        throw new GitLabApiException(ioe);
    } finally {
        if (savedFollowRedirects) {
            HttpURLConnection.setFollowRedirects(true);
        }
    }
}
 
Example 6
Source Project: jumblr   File: RequestBuilder.java    License: Apache License 2.0 5 votes vote down vote up
public String getRedirectUrl(String path) {
    OAuthRequest request = this.constructGet(path, null);
    sign(request);
    boolean presetVal = HttpURLConnection.getFollowRedirects();
    HttpURLConnection.setFollowRedirects(false);
    Response response = request.send();
    HttpURLConnection.setFollowRedirects(presetVal);
    if (response.getCode() == 301 || response.getCode() == 302) {
        return response.getHeader("Location");
    } else {
        throw new JumblrException(response);
    }
}
 
Example 7
Source Project: Tomcat7.0.67   File: TestMapperWebapps.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testRedirect() throws Exception {
    // Disable the following of redirects for this test only
    boolean originalValue = HttpURLConnection.getFollowRedirects();
    HttpURLConnection.setFollowRedirects(false);
    try {
        Tomcat tomcat = getTomcatInstance();

        // Use standard test webapp as ROOT
        File rootDir = new File("test/webapp-3.0");
        org.apache.catalina.Context root =
                tomcat.addWebapp(null, "", rootDir.getAbsolutePath());

        // Add a security constraint
        SecurityConstraint constraint = new SecurityConstraint();
        SecurityCollection collection = new SecurityCollection();
        collection.addPattern("/welcome-files/*");
        collection.addPattern("/welcome-files");
        constraint.addCollection(collection);
        constraint.addAuthRole("foo");
        root.addConstraint(constraint);

        // Also make examples available
        File examplesDir = new File(getBuildDirectory(), "webapps/examples");
        org.apache.catalina.Context examples  = tomcat.addWebapp(
                null, "/examples", examplesDir.getAbsolutePath());
        // Then block access to the examples to test redirection
        RemoteAddrValve rav = new RemoteAddrValve();
        rav.setDeny(".*");
        rav.setDenyStatus(404);
        examples.getPipeline().addValve(rav);

        tomcat.start();

        // Redirects within a web application
        doRedirectTest("/welcome-files", 401);
        doRedirectTest("/welcome-files/", 401);

        doRedirectTest("/jsp", 302);
        doRedirectTest("/jsp/", 404);

        doRedirectTest("/WEB-INF", 404);
        doRedirectTest("/WEB-INF/", 404);

        // Redirects between web applications
        doRedirectTest("/examples", 404);
        doRedirectTest("/examples/", 404);
    } finally {
        HttpURLConnection.setFollowRedirects(originalValue);
    }
}
 
Example 8
Source Project: tomcatsrc   File: TestMapperWebapps.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testRedirect() throws Exception {
    // Disable the following of redirects for this test only
    boolean originalValue = HttpURLConnection.getFollowRedirects();
    HttpURLConnection.setFollowRedirects(false);
    try {
        Tomcat tomcat = getTomcatInstance();

        // Use standard test webapp as ROOT
        File rootDir = new File("test/webapp-3.0");
        org.apache.catalina.Context root =
                tomcat.addWebapp(null, "", rootDir.getAbsolutePath());

        // Add a security constraint
        SecurityConstraint constraint = new SecurityConstraint();
        SecurityCollection collection = new SecurityCollection();
        collection.addPattern("/welcome-files/*");
        collection.addPattern("/welcome-files");
        constraint.addCollection(collection);
        constraint.addAuthRole("foo");
        root.addConstraint(constraint);

        // Also make examples available
        File examplesDir = new File(getBuildDirectory(), "webapps/examples");
        org.apache.catalina.Context examples  = tomcat.addWebapp(
                null, "/examples", examplesDir.getAbsolutePath());
        examples.setMapperContextRootRedirectEnabled(false);
        // Then block access to the examples to test redirection
        RemoteAddrValve rav = new RemoteAddrValve();
        rav.setDeny(".*");
        rav.setDenyStatus(404);
        examples.getPipeline().addValve(rav);

        tomcat.start();

        // Redirects within a web application
        doRedirectTest("/welcome-files", 401);
        doRedirectTest("/welcome-files/", 401);

        doRedirectTest("/jsp", 302);
        doRedirectTest("/jsp/", 404);

        doRedirectTest("/WEB-INF", 404);
        doRedirectTest("/WEB-INF/", 404);

        // Redirects between web applications
        doRedirectTest("/examples", 404);
        doRedirectTest("/examples/", 404);
    } finally {
        HttpURLConnection.setFollowRedirects(originalValue);
    }
}
 
Example 9
Source Project: gitlab4j-api   File: AccessTokenUtils.java    License: MIT License 4 votes vote down vote up
/**
 * Fetches the user's GitLab Feed token using HTML scraping.
 *
 * @param baseUrl the GitLab server base URL
 * @param username the user name the user to log in with
 * @param password the password of the provided username
 * @return the fetched Feed token
 * @throws GitLabApiException if any exception occurs
 */
public static final String getFeedToken(final String baseUrl, final String username,
        final String password) throws GitLabApiException {

    // Save the follow redirect state so it can be restored later
    boolean savedFollowRedirects = HttpURLConnection.getFollowRedirects();
    String cookies = null;

    try {

        // Must manually follow redirects
        if (savedFollowRedirects) {
            HttpURLConnection.setFollowRedirects(false);
        }

        /*******************************************************************************
         * Step 1: Login and get the session cookie. *
         *******************************************************************************/
        cookies = login(baseUrl, username, password);

        /*******************************************************************************
         * Step 2: Go to the /profile/personal_access_tokens page and fetch the        *
         *         Feed token.                                                         *
         *******************************************************************************/
        String urlString = baseUrl + "/profile/personal_access_tokens";
        URL url = new URL(urlString);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestProperty("User-Agent", USER_AGENT);
        connection.setRequestProperty("Cookie", cookies);
        connection.setReadTimeout(10000);
        connection.setConnectTimeout(10000);

        // Make sure the response code is 200, otherwise there is a failure
        int responseCode = connection.getResponseCode();
        if (responseCode != 200) {
            throw new GitLabApiException("Failure loading Access Tokens page, aborting!");
        }

        // Extract the Feed token from the page and return it
        String content = getContent(connection);
        Matcher matcher = FEED_TOKEN_PATTERN.matcher(content);
        if (!matcher.find()) {
            throw new GitLabApiException("Feed token not found, aborting!");
        }

        String feedToken = matcher.group(1);
        return (feedToken);

    } catch (IOException ioe) {
        throw new GitLabApiException(ioe);
    } finally {

        if (cookies != null) {
            try { logout(baseUrl, cookies); } catch (Exception ignore) {}
        }

        if (savedFollowRedirects) {
            HttpURLConnection.setFollowRedirects(true);
        }
    }
}
 
Example 10
Source Project: gitlab4j-api   File: AccessTokenUtils.java    License: MIT License 4 votes vote down vote up
/**
 * Fetches the GitLab health check access token using HTML scraping.
 *
 * @param baseUrl the GitLab server base URL
 * @param username the user name of an admin user to log in with
 * @param password the password of the provided username
 * @return the fetched health check access token
 * @throws GitLabApiException if any exception occurs
 */
public static final String getHealthCheckAccessToken(final String baseUrl, final String username,
        final String password) throws GitLabApiException {

    // Save the follow redirect state so it can be restored later
    boolean savedFollowRedirects = HttpURLConnection.getFollowRedirects();
    String cookies = null;

    try {

        // Must manually follow redirects
        if (savedFollowRedirects) {
            HttpURLConnection.setFollowRedirects(false);
        }

        /*******************************************************************************
         * Step 1: Login and get the session cookie. *
         *******************************************************************************/
        cookies = login(baseUrl, username, password);

        /*******************************************************************************
         * Step 2: Go to the /admin/health_check page and fetch the * health check
         * access token. *
         *******************************************************************************/
        String urlString = baseUrl + "/admin/health_check";
        URL url = new URL(urlString);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestProperty("User-Agent", USER_AGENT);
        connection.setRequestProperty("Cookie", cookies);
        connection.setReadTimeout(10000);
        connection.setConnectTimeout(10000);

        // Make sure the response code is 200, otherwise there is a failure
        int responseCode = connection.getResponseCode();
        if (responseCode != 200) {
            throw new GitLabApiException("Failure loading Health Check page, aborting!");
        }

        // Extract the personal access token from the page and return it
        String content = getContent(connection);
        Matcher matcher = HEALTH_CHECK_ACCESS_TOKEN_PATTERN.matcher(content);
        if (!matcher.find()) {
            throw new GitLabApiException("health-check-access-token not found, aborting!");
        }

        String healthCheckAccessToken = matcher.group(1);
        return (healthCheckAccessToken);

    } catch (IOException ioe) {
        throw new GitLabApiException(ioe);
    } finally {

        if (cookies != null) {
            try { logout(baseUrl, cookies); } catch (Exception ignore) {}
        }

        if (savedFollowRedirects) {
            HttpURLConnection.setFollowRedirects(true);
        }
    }
}
 
Example 11
public static boolean getFollowRedirects()
{
	return HttpURLConnection.getFollowRedirects();
}