Java Code Examples for java.net.httpurlconnection#getFollowRedirects()
The following examples show how to use
java.net.httpurlconnection#getFollowRedirects() .
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 File: URLConnectionTest.java From j2objc with Apache License 2.0 | 6 votes |
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 File: TestRewriteValve.java From Tomcat8-Source-Read with MIT License | 5 votes |
@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 File: TestRewriteValve.java From Tomcat8-Source-Read with MIT License | 5 votes |
@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 File: TestRewriteValve.java From Tomcat8-Source-Read with MIT License | 5 votes |
@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 File: AccessTokenUtils.java From gitlab4j-api with MIT License | 5 votes |
/** * 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 File: RequestBuilder.java From jumblr with Apache License 2.0 | 5 votes |
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 File: TestMapperWebapps.java From Tomcat7.0.67 with Apache License 2.0 | 4 votes |
@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 File: TestMapperWebapps.java From tomcatsrc with Apache License 2.0 | 4 votes |
@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 File: AccessTokenUtils.java From gitlab4j-api with MIT License | 4 votes |
/** * 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 File: AccessTokenUtils.java From gitlab4j-api with MIT License | 4 votes |
/** * 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
Source File: ApigeeHttpURLConnection.java From apigee-android-sdk with Apache License 2.0 | 4 votes |
public static boolean getFollowRedirects() { return HttpURLConnection.getFollowRedirects(); }