List of usage examples for java.net HttpURLConnection HTTP_MOVED_TEMP
int HTTP_MOVED_TEMP
To view the source code for java.net HttpURLConnection HTTP_MOVED_TEMP.
Click Source Link
From source file:org.niord.proxy.rest.AbstractNiordService.java
/** * Creates a HTTP connection to the given URL and handles redirects. * @param url the URL/*from www . ja v a 2 s . c o m*/ * @return a HTTP connection to the given URL and handles redirects. */ HttpURLConnection createHttpUrlConnection(String url) throws IOException { HttpURLConnection con = newHttpUrlConnection(url); int status = con.getResponseCode(); if (status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_MOVED_PERM || status == HttpURLConnection.HTTP_SEE_OTHER) { // get redirect url from "location" header field String redirectUrl = con.getHeaderField("Location"); // open the new connection again con = newHttpUrlConnection(redirectUrl); } return con; }
From source file:org.jboss.test.security.test.WebJASPIFormUnitTestCase.java
public void testFormAuthSuccess() throws Exception { GetMethod indexGet = new GetMethod(baseURLNoAuth + "jaspi-web-form/"); int responseCode = httpConn.executeMethod(indexGet); String body = indexGet.getResponseBodyAsString(); assertTrue("Get OK(" + responseCode + ")", responseCode == HttpURLConnection.HTTP_OK); assertTrue("Redirected to login page", body.indexOf("j_security_check") > 0); HttpState state = httpConn.getState(); Cookie[] cookies = state.getCookies(); String sessionID = null;/*www .j a v a 2 s.co m*/ for (int c = 0; c < cookies.length; c++) { Cookie k = cookies[c]; if (k.getName().equalsIgnoreCase("JSESSIONID")) sessionID = k.getValue(); } getLog().debug("Saw JSESSIONID=" + sessionID); // Submit the login form PostMethod formPost = new PostMethod(baseURLNoAuth + "jaspi-web-form/j_security_check"); formPost.addRequestHeader("Referer", baseURLNoAuth + "jaspi-web-form/login.jsp"); formPost.addParameter("j_username", username); formPost.addParameter("j_password", password); responseCode = httpConn.executeMethod(formPost); String response = formPost.getStatusText(); log.debug("responseCode=" + responseCode + ", response=" + response); assertTrue("Saw HTTP_MOVED_TEMP", responseCode == HttpURLConnection.HTTP_MOVED_TEMP); // Follow the redirect to the SecureServlet Header location = formPost.getResponseHeader("Location"); String indexURI = location.getValue(); GetMethod war1Index = new GetMethod(indexURI); responseCode = httpConn.executeMethod(war1Index); response = war1Index.getStatusText(); log.debug("responseCode=" + responseCode + ", response=" + response); assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK); body = war1Index.getResponseBodyAsString(); if (body.indexOf("j_security_check") > 0) fail("get of " + indexURI + " redirected to login page"); if (body.indexOf("Hi Anil") < 0) fail("index.html not seen"); }
From source file:io.github.hidroh.materialistic.accounts.UserServicesClient.java
@Override public void login(String username, String password, boolean createAccount, Callback callback) { execute(postLogin(username, password, createAccount)) .map(response -> response.code() == HttpURLConnection.HTTP_MOVED_TEMP) .observeOn(AndroidSchedulers.mainThread()).subscribe(callback::onDone, callback::onError); }
From source file:org.jboss.test.cluster.test.FormAuthFailoverTestCase.java
/** * Test ability for a FORM auth based app to have failover without requiring * a new sign-on/*www. ja va2 s . c o m*/ * * @throws Exception */ public void testFormAuthFailover() throws Exception { log.info("+++ testFormAuthFailover"); String[] httpURLs = super.getHttpURLs(); String serverA = httpURLs[0]; String serverB = httpURLs[1]; log.info(System.getProperties()); log.info("serverA: " + serverA); log.info("serverB: " + serverB); // Start by accessing the secured index.html of war1 HttpClient httpConn = new HttpClient(); GetMethod indexGet = new GetMethod(serverA + "/war1/index.html"); int responseCode = httpConn.executeMethod(indexGet); String body = indexGet.getResponseBodyAsString(); assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK); assertTrue("Redirected to login page", body.indexOf("j_security_check") > 0); HttpState state = httpConn.getState(); Cookie[] cookies = state.getCookies(); String sessionID = null; for (int c = 0; c < cookies.length; c++) { Cookie k = cookies[c]; if (k.getName().equalsIgnoreCase("JSESSIONID")) sessionID = k.getValue(); } log.debug("Saw JSESSIONID=" + sessionID); // Submit the login form PostMethod formPost = new PostMethod(serverA + "/war1/j_security_check"); formPost.addRequestHeader("Referer", serverA + "/war1/login.html"); formPost.addParameter("j_username", "jduke"); formPost.addParameter("j_password", "theduke"); responseCode = httpConn.executeMethod(formPost.getHostConfiguration(), formPost, state); String response = formPost.getStatusText(); log.debug("responseCode=" + responseCode + ", response=" + response); assertTrue("Saw HTTP_MOVED_TEMP(" + responseCode + ")", responseCode == HttpURLConnection.HTTP_MOVED_TEMP); // Follow the redirect to the index.html page Header location = formPost.getResponseHeader("Location"); String indexURI = location.getValue(); GetMethod war1Index = new GetMethod(indexURI); responseCode = httpConn.executeMethod(war1Index.getHostConfiguration(), war1Index, state); assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK); body = war1Index.getResponseBodyAsString(); if (body.indexOf("j_security_check") > 0) fail("get of " + indexURI + " redirected to login page"); cookies = state.getCookies(); sessionID = null; for (int c = 0; c < cookies.length; c++) { Cookie k = cookies[c]; if (k.getName().equalsIgnoreCase("JSESSIONID")) { sessionID = k.getValue(); if (serverA.equals(serverB) == false) { // Make a session cookie to send to serverB Cookie copy = copyCookie(k, serverB); state.addCookie(copy); log.debug("Added state cookie: " + copy); } } } assertTrue("Saw JSESSIONID", sessionID != null); log.debug("Saw JSESSIONID=" + sessionID); // Now try getting the war1 index on the second server log.debug("Prepare /war1/index.html get"); GetMethod war2Index = new GetMethod(serverB + "/war2/index.html"); responseCode = httpConn.executeMethod(war2Index.getHostConfiguration(), war2Index, state); response = war2Index.getStatusText(); log.debug("responseCode=" + responseCode + ", response=" + response); assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK); body = war2Index.getResponseBodyAsString(); log.debug("body: " + body); if (body.indexOf("j_security_check") > 0) fail("get of /war1/index.html redirected to login page"); /* Access a secured servlet that calls a secured ejb in war2 to test propagation of the identity to the ejb container. */ GetMethod war2Servlet = new GetMethod(serverB + "/war1/EJBServlet"); responseCode = httpConn.executeMethod(war2Servlet.getHostConfiguration(), war2Servlet, state); response = war2Servlet.getStatusText(); log.debug("responseCode=" + responseCode + ", response=" + response); assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK); body = war2Servlet.getResponseBodyAsString(); log.debug("body: " + body); if (body.indexOf("j_security_check") > 0) fail("get of /war1/EJBServlet redirected to login page"); }
From source file:com.alexoree.jenkins.Main.java
private static String download(String localName, String remoteUrl) throws Exception { URL obj = new URL(remoteUrl); HttpURLConnection conn = (HttpURLConnection) obj.openConnection(); conn.setReadTimeout(5000);/*from w ww .j a v a 2 s. c om*/ System.out.println("Request URL ... " + remoteUrl); boolean redirect = false; // normally, 3xx is redirect int status = conn.getResponseCode(); if (status != HttpURLConnection.HTTP_OK) { if (status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_MOVED_PERM || status == HttpURLConnection.HTTP_SEE_OTHER) redirect = true; } if (redirect) { // get redirect url from "location" header field String newUrl = conn.getHeaderField("Location"); // get the cookie if need, for login String cookies = conn.getHeaderField("Set-Cookie"); // open the new connnection again conn = (HttpURLConnection) new URL(newUrl).openConnection(); String version = newUrl.substring(newUrl.lastIndexOf("/", newUrl.lastIndexOf("/") - 1) + 1, newUrl.lastIndexOf("/")); String pluginname = localName.substring(localName.lastIndexOf("/") + 1); String ext = ""; if (pluginname.endsWith(".war")) ext = ".war"; else ext = ".hpi"; pluginname = pluginname.replace(ext, ""); localName = localName.replace(pluginname + ext, "/download/plugins/" + pluginname + "/" + version + "/"); new File(localName).mkdirs(); localName += pluginname + ext; System.out.println("Redirect to URL : " + newUrl); } if (new File(localName).exists()) { System.out.println(localName + " exists, skipping"); return localName; } byte[] buffer = new byte[2048]; FileOutputStream baos = new FileOutputStream(localName); InputStream inputStream = conn.getInputStream(); int totalBytes = 0; int read = inputStream.read(buffer); while (read > 0) { totalBytes += read; baos.write(buffer, 0, read); read = inputStream.read(buffer); } System.out.println("Retrieved " + totalBytes + "bytes"); return localName; }
From source file:org.jenkinsci.plugins.mber.FileDownloadCallable.java
public static String followRedirects(final String url) throws IOException { // Turn off auto redirect follow so we can read the final URL ourselves. // The internal parsing doesn't work with some of the headers used by Amazon. final HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); connection.setUseCaches(false);//from www . j a v a 2s . co m connection.setInstanceFollowRedirects(false); connection.connect(); // Pull the redirect URL out of the "Location" header. Follow it recursively since it might be chained. if (connection.getResponseCode() == HttpURLConnection.HTTP_MOVED_PERM || connection.getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP) { final String redirectURL = connection.getHeaderField("Location"); return followRedirects(redirectURL); } return url; }
From source file:org.jboss.test.cluster.defaultcfg.web.test.PersistentManagerFormAuthTestCase.java
public void testFormAuthentication() throws Exception { String url = baseURLNoAuth + "/http-formauth-persistent/"; // Start by accessing the secured index.html of war1 HttpClient httpConn = new HttpClient(); // Try to access protected resource GetMethod indexGet = new GetMethod(url + "index.jsp"); int responseCode = httpConn.executeMethod(indexGet); String body = indexGet.getResponseBodyAsString(); assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK); assertTrue("Redirected to login page", body.indexOf("j_security_check") > 0); // Submit the login form PostMethod formPost = new PostMethod(url + "j_security_check"); formPost.addRequestHeader("Referer", url + "login.html"); formPost.addParameter("j_username", "admin"); formPost.addParameter("j_password", "admin"); responseCode = httpConn.executeMethod(formPost.getHostConfiguration(), formPost, httpConn.getState()); assertTrue("Saw HTTP_MOVED_TEMP(" + responseCode + ")", responseCode == HttpURLConnection.HTTP_MOVED_TEMP); // Follow the redirect to the index.html page Header location = formPost.getResponseHeader("Location"); String indexURI = location.getValue(); GetMethod warIndex = new GetMethod(indexURI); responseCode = httpConn.executeMethod(warIndex.getHostConfiguration(), warIndex, httpConn.getState()); assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK); body = warIndex.getResponseBodyAsString(); if (body.indexOf("j_security_check") > 0) fail("get of " + indexURI + " redirected to login page"); }
From source file:org.jboss.test.web.test.ssl.SSLUnitTestCase.java
private void doHttpRedirect(String httpNoAuth) throws Exception { log.info("+++ testHttpRedirect, httpNoAuth=" + httpNoAuth); // Start by accessing the secured index.html of war1 HttpClient httpConn = new HttpClient(); String url = httpNoAuth + "clientcert-auth/unrestricted/SecureServlet"; log.info("Accessing: " + url); GetMethod get = new GetMethod(url); int responseCode = httpConn.executeMethod(get); String status = get.getStatusText(); log.debug(status);/*w w w. j a v a 2 s . c om*/ assertTrue("Get HTTP_MOVED_TEMP(" + responseCode + ")", responseCode == HttpURLConnection.HTTP_MOVED_TEMP); Header hdr = get.getResponseHeader("Location"); url = hdr.getValue(); get = new GetMethod(url); responseCode = httpConn.executeMethod(get); status = get.getStatusText(); log.debug(status); assertTrue("Get OK(" + responseCode + ")", responseCode == HttpURLConnection.HTTP_OK); }
From source file:io.github.hidroh.materialistic.accounts.UserServicesClient.java
@Override public boolean voteUp(Context context, String itemId, Callback callback) { Pair<String, String> credentials = AppUtils.getCredentials(context); if (credentials == null) { return false; }//from ww w . j av a2 s.c o m Toast.makeText(context, R.string.sending, Toast.LENGTH_SHORT).show(); execute(postVote(credentials.first, credentials.second, itemId)) .map(response -> response.code() == HttpURLConnection.HTTP_MOVED_TEMP) .observeOn(AndroidSchedulers.mainThread()).subscribe(callback::onDone, callback::onError); return true; }
From source file:org.apache.hadoop.mapred.TestJobRetire.java
private JobID validateJobRetire(JobConf jobConf, Path inDir, Path outDir, JobTracker jobtracker) throws IOException { RunningJob rj = UtilsForTests.runJob(jobConf, inDir, outDir, 0, 0); rj.waitForCompletion();//from w w w . ja v a2 s .co m assertTrue(rj.isSuccessful()); JobID id = rj.getID(); //wait for job to get retired waitTillRetire(id, jobtracker); RetireJobInfo retired = jobtracker.retireJobs.get(id); assertTrue("History url not set", retired.getHistoryFile() != null && retired.getHistoryFile().length() > 0); assertNotNull("Job is not in cache", jobtracker.getJobStatus(id)); // get the job conf filename String name = jobtracker.getLocalJobFilePath(id); File file = new File(name); assertFalse("JobConf file not deleted", file.exists()); //test redirection URL jobUrl = new URL(rj.getTrackingURL()); HttpURLConnection conn = (HttpURLConnection) jobUrl.openConnection(); conn.setInstanceFollowRedirects(false); conn.connect(); assertEquals(HttpURLConnection.HTTP_MOVED_TEMP, conn.getResponseCode()); conn.disconnect(); URL redirectedUrl = new URL(conn.getHeaderField("Location")); conn = (HttpURLConnection) redirectedUrl.openConnection(); conn.connect(); assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode()); conn.disconnect(); return id; }