List of usage examples for org.apache.http.client.methods CloseableHttpResponse getFirstHeader
Header getFirstHeader(String str);
From source file:org.kontalk.client.DownloadClient.java
/** * Downloads to a directory represented by a {@link File} object, * determining the file name from the Content-Disposition header. * @param url URL of file//from w ww. j a v a2 s .c om * @param base base directory in which the download is saved * @param listener listener for download progress * @return the absolute file path of the downloaded file, or an empty string * if the file could not be downloaded */ public String download(String url, File base) { if (mHTTPClient == null) { mHTTPClient = createHTTPClient(mPrivateKey, mCertificate, mValidateCertificate); if (mHTTPClient == null) return ""; } LOGGER.info("downloading file from URL=" + url + "..."); mCurrentRequest = new HttpGet(url); // execute request CloseableHttpResponse response; try { response = mHTTPClient.execute(mCurrentRequest); } catch (IOException ex) { LOGGER.log(Level.WARNING, "can't execute request", ex); return ""; } try { int code = response.getStatusLine().getStatusCode(); // HTTP/1.1 200 OK -- other codes should throw Exceptions if (code != 200) { LOGGER.warning("invalid response code: " + code); return ""; } // get filename Header dispHeader = response.getFirstHeader("Content-Disposition"); if (dispHeader == null) { LOGGER.warning("no content header"); return ""; } String filename = parseContentDisposition(dispHeader.getValue()); // never trust incoming data filename = filename != null ? new File(filename).getName() : ""; if (filename.isEmpty()) { LOGGER.warning("no filename in content: " + dispHeader.getValue()); return ""; } // get file size long s = -1; Header lengthHeader = response.getFirstHeader("Content-Length"); if (lengthHeader == null) { LOGGER.warning("no length header"); } else { try { s = Long.parseLong(lengthHeader.getValue()); } catch (NumberFormatException ex) { LOGGER.log(Level.WARNING, "can' parse file size", ex); } } final long fileSize = s; mListener.updateProgress(s < 0 ? -2 : 0); // TODO should check for content-disposition parsing here // and choose another filename if necessary HttpEntity entity = response.getEntity(); if (entity == null) { LOGGER.warning("no entity in response"); return ""; } File destination = new File(base, filename); if (destination.exists()) { LOGGER.warning("file already exists: " + destination.getAbsolutePath()); return ""; } try (FileOutputStream out = new FileOutputStream(destination)) { CountingOutputStream cOut = new CountingOutputStream(out) { @Override protected synchronized void afterWrite(int n) { if (fileSize <= 0) return; // inform listener mListener.updateProgress((int) (this.getByteCount() / (fileSize * 1.0) * 100)); } }; entity.writeTo(cOut); } catch (IOException ex) { LOGGER.log(Level.WARNING, "can't download file", ex); return ""; } LOGGER.info("... download successful!"); return destination.getAbsolutePath(); } finally { try { response.close(); } catch (IOException ex) { LOGGER.log(Level.WARNING, "can't close response", ex); } } }
From source file:com.comcast.cdn.traffic_control.traffic_router.core.external.SteeringTest.java
License:asdf
@Test public void itUsesSteeredDeliveryServiceIdInRedirect() throws Exception { HttpGet httpGet = new HttpGet( "http://localhost:" + routerHttpPort + "/stuff?fakeClientIpAddress=12.34.56.78"); httpGet.addHeader("Host", "foo." + steeringDeliveryServiceId + ".bar"); CloseableHttpResponse response = null; try {/*w w w. j a va2 s. c om*/ response = httpClient.execute(httpGet); assertThat("Failed getting 302 for request " + httpGet.getFirstHeader("Host").getValue(), response.getStatusLine().getStatusCode(), equalTo(302)); assertThat(response.getFirstHeader("Location").getValue(), isIn(validLocations)); //System.out.println("itUsesSteered = "+response.getFirstHeader("Location").getValue()); } finally { if (response != null) { response.close(); } } }
From source file:com.comcast.cdn.traffic_control.traffic_router.core.external.SteeringTest.java
License:asdf
@Test public void itUsesTargetFiltersForSteering() throws Exception { HttpGet httpGet = new HttpGet("http://localhost:" + routerHttpPort + "/qwerytuiop/force-to-target-2/asdfghjkl?fakeClientIpAddress=12.34.56.78"); httpGet.addHeader("Host", "foo.steering-test-1.thecdn.example.com"); CloseableHttpResponse response = null; try {/*ww w . j ava2s . co m*/ response = httpClient.execute(httpGet); assertThat("Failed getting 302 for request " + httpGet.getFirstHeader("Host").getValue(), response.getStatusLine().getStatusCode(), equalTo(302)); assertThat(response.getFirstHeader("Location").getValue(), endsWith( ".steering-target-2.thecdn.example.com:8090/qwerytuiop/force-to-target-2/asdfghjkl?fakeClientIpAddress=12.34.56.78")); } finally { if (response != null) { response.close(); } } }
From source file:com.comcast.cdn.traffic_control.traffic_router.core.external.SteeringTest.java
License:asdf
@Test public void itUsesXtcSteeringOptionForOverride() throws Exception { HttpGet httpGet = new HttpGet("http://localhost:" + routerHttpPort + "/qwerytuiop/force-to-target-2/asdfghjkl?fakeClientIpAddress=12.34.56.78"); httpGet.addHeader("Host", "foo.steering-test-1.thecdn.example.com"); httpGet.addHeader("X-TC-Steering-Option", "steering-target-1"); CloseableHttpResponse response = null; try {/* w w w. j a v a 2 s .c o m*/ response = httpClient.execute(httpGet); assertThat("Failed getting 302 for request " + httpGet.getFirstHeader("Host").getValue(), response.getStatusLine().getStatusCode(), equalTo(302)); assertThat(response.getFirstHeader("Location").getValue(), endsWith( ".steering-target-1.thecdn.example.com:8090/qwerytuiop/force-to-target-2/asdfghjkl?fakeClientIpAddress=12.34.56.78")); } finally { if (response != null) { response.close(); } } }
From source file:com.comcast.cdn.traffic_control.traffic_router.core.external.SteeringTest.java
License:asdf
@Test public void itUsesWeightedDistributionForRequestPath() throws Exception { int count = 0; for (int weight : targetWeights.values()) { count += weight;//ww w.java 2s. c om } count *= 1000; if (count > 100000) { count = 100000; } Map<String, Integer> results = new HashMap<String, Integer>(); for (String steeredId : targetWeights.keySet()) { results.put(steeredId, 0); } //System.out.println("Going to execute " + count + " requests through steering delivery service '" + steeringDeliveryServiceId + "'"); for (int i = 0; i < count; i++) { String path = generateRandomPath(); HttpGet httpGet = new HttpGet( "http://localhost:" + routerHttpPort + path + "?fakeClientIpAddress=12.34.56.78"); httpGet.addHeader("Host", "foo." + steeringDeliveryServiceId + ".bar"); CloseableHttpResponse response = null; try { response = httpClient.execute(httpGet); assertThat("Did not get 302 for request '" + httpGet.getURI() + "'", response.getStatusLine().getStatusCode(), equalTo(302)); String location = response.getFirstHeader("Location").getValue(); for (String id : results.keySet()) { if (location.contains(id)) { results.put(id, results.get(id) + 1); } } } finally { if (response != null) { response.close(); } } } double totalWeight = 0; for (int weight : targetWeights.values()) { totalWeight += weight; } Map<String, Double> expectedHitRates = new HashMap<String, Double>(); for (String id : targetWeights.keySet()) { expectedHitRates.put(id, targetWeights.get(id) / totalWeight); } for (String id : results.keySet()) { int hits = results.get(id); double hitRate = (double) hits / count; assertThat(hitRate, closeTo(expectedHitRates.get(id), 0.009)); } }
From source file:com.comcast.cdn.traffic_control.traffic_router.core.external.SteeringTest.java
License:asdf
@Test public void z_itemsMigrateFromSmallerToLargerBucket() throws Exception { Map<String, String> domains = new HashMap<>(); Map<String, Integer> weights = new HashMap<>(); setupSteering(domains, weights, "internal/api/1.3/steering2.json"); List<String> randomPaths = new ArrayList<>(); for (int i = 0; i < 10000; i++) { randomPaths.add(generateRandomPath()); }//w w w . j a va 2s .c o m String smallerTarget = null; String largerTarget = null; for (String target : weights.keySet()) { if (smallerTarget == null && largerTarget == null) { smallerTarget = target; largerTarget = target; } if (weights.get(smallerTarget) > weights.get(target)) { smallerTarget = target; } if (weights.get(largerTarget) < weights.get(target)) { largerTarget = target; } } Map<String, List<String>> hashedPaths = new HashMap<>(); hashedPaths.put(smallerTarget, new ArrayList<String>()); hashedPaths.put(largerTarget, new ArrayList<String>()); for (String path : randomPaths) { HttpGet httpGet = new HttpGet( "http://localhost:" + routerHttpPort + path + "?fakeClientIpAddress=12.34.56.78"); httpGet.addHeader("Host", "foo." + steeringDeliveryServiceId + ".bar"); CloseableHttpResponse response = null; try { response = httpClient.execute(httpGet); assertThat("Did not get 302 for request '" + httpGet.getURI() + "'", response.getStatusLine().getStatusCode(), equalTo(302)); String location = response.getFirstHeader("Location").getValue(); for (String targetXmlId : hashedPaths.keySet()) { if (location.contains(targetXmlId)) { hashedPaths.get(targetXmlId).add(path); } } } finally { if (response != null) { response.close(); } } } // Change the steering attributes HttpPost httpPost = new HttpPost("http://localhost:" + testHttpPort + "/steering"); httpClient.execute(httpPost).close(); // steering is checked every 15 seconds by default. Thread.sleep(30 * 1000); Map<String, List<String>> rehashedPaths = new HashMap<>(); rehashedPaths.put(smallerTarget, new ArrayList<String>()); rehashedPaths.put(largerTarget, new ArrayList<String>()); for (String path : randomPaths) { HttpGet httpGet = new HttpGet( "http://localhost:" + routerHttpPort + path + "?fakeClientIpAddress=12.34.56.78"); httpGet.addHeader("Host", "foo." + steeringDeliveryServiceId + ".bar"); CloseableHttpResponse response = null; try { response = httpClient.execute(httpGet); assertThat("Did not get 302 for request '" + httpGet.getURI() + "'", response.getStatusLine().getStatusCode(), equalTo(302)); String location = response.getFirstHeader("Location").getValue(); for (String targetXmlId : rehashedPaths.keySet()) { if (location.contains(targetXmlId)) { rehashedPaths.get(targetXmlId).add(path); } } } finally { if (response != null) { response.close(); } } } assertThat(rehashedPaths.get(smallerTarget).size(), greaterThan(hashedPaths.get(smallerTarget).size())); assertThat(rehashedPaths.get(largerTarget).size(), lessThan(hashedPaths.get(largerTarget).size())); for (String path : hashedPaths.get(smallerTarget)) { assertThat(rehashedPaths.get(smallerTarget).contains(path), equalTo(true)); assertThat(rehashedPaths.get(largerTarget).contains(path), equalTo(false)); } }
From source file:org.esigate.DriverTest.java
/** * Ensure default ports are not added by esigate. * /*w w w. ja v a2s. c o m*/ * @throws Exception */ public void testRewriteRedirectResponseWithLocation() throws Exception { Properties properties = new Properties(); properties.put(Parameters.REMOTE_URL_BASE, "http://127.0.0.1"); properties.put(Parameters.PRESERVE_HOST, "true"); mockConnectionManager.setResponseHandler(new IResponseHandler() { @Override public HttpResponse execute(final HttpRequest httpRequest) throws IOException { if (!httpRequest.getLastHeader("Host").getValue().equals("www.foo.com")) { throw new IllegalArgumentException("Host must be www.foo.com"); } return new HttpResponseBuilder().status(HttpStatus.SC_MOVED_TEMPORARILY).entity("Found") .header("Location", "http://www.foo.com").build(); } }); Driver driver = createMockDriver(properties, mockConnectionManager); IncomingRequest request1 = TestUtils.createIncomingRequest("http://www.foo.com:80").build(); assertEquals("www.foo.com", request1.getLastHeader("Host").getValue()); CloseableHttpResponse driverResponse = driver.proxy("", request1); assertEquals("http://www.foo.com", driverResponse.getFirstHeader("Location").getValue()); }
From source file:org.esigate.DriverTest.java
public void testRewriteRedirectResponse() throws Exception { Properties properties = new Properties(); properties.put(Parameters.REMOTE_URL_BASE.getName(), "http://www.foo.com:8080/"); properties.put(Parameters.PRESERVE_HOST, "false"); request = TestUtils.createIncomingRequest("http://www.bar.com/foo/"); HttpResponse response = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), HttpStatus.SC_MOVED_TEMPORARILY, "Found"); response.addHeader("Location", "http://www.foo.com:8080/somewhere/"); mockConnectionManager.setResponse(response); Driver driver = createMockDriver(properties, mockConnectionManager); CloseableHttpResponse driverResponse = driver.proxy("/foo/", request.build()); assertEquals("http://www.bar.com/somewhere/", driverResponse.getFirstHeader("Location").getValue()); }
From source file:org.keycloak.testsuite.oauth.LoginStatusIframeEndpointTest.java
@Test public void checkIframe() throws IOException { CookieStore cookieStore = new BasicCookieStore(); try (CloseableHttpClient client = HttpClients.custom().setDefaultCookieStore(cookieStore).build()) { String redirectUri = URLEncoder.encode( suiteContext.getAuthServerInfo().getContextRoot() + "/auth/admin/master/console", "UTF-8"); HttpGet get = new HttpGet(suiteContext.getAuthServerInfo().getContextRoot() + "/auth/realms/master/protocol/openid-connect/auth?response_type=code&client_id=" + Constants.ADMIN_CONSOLE_CLIENT_ID + "&redirect_uri=" + redirectUri); CloseableHttpResponse response = client.execute(get); String s = IOUtils.toString(response.getEntity().getContent(), "UTF-8"); response.close();/*from w ww. j a v a 2 s . co m*/ String action = ActionURIUtils.getActionURIFromPageSource(s); HttpPost post = new HttpPost(action); List<NameValuePair> params = new LinkedList<>(); params.add(new BasicNameValuePair("username", "admin")); params.add(new BasicNameValuePair("password", "admin")); post.setHeader("Content-Type", "application/x-www-form-urlencoded"); post.setEntity(new UrlEncodedFormEntity(params)); response = client.execute(post); assertEquals("CP=\"This is not a P3P policy!\"", response.getFirstHeader("P3P").getValue()); Header setIdentityCookieHeader = null; Header setSessionCookieHeader = null; for (Header h : response.getAllHeaders()) { if (h.getName().equals("Set-Cookie")) { if (h.getValue().contains("KEYCLOAK_SESSION")) { setSessionCookieHeader = h; } else if (h.getValue().contains("KEYCLOAK_IDENTITY")) { setIdentityCookieHeader = h; } } } assertNotNull(setIdentityCookieHeader); assertTrue(setIdentityCookieHeader.getValue().contains("HttpOnly")); assertNotNull(setSessionCookieHeader); assertFalse(setSessionCookieHeader.getValue().contains("HttpOnly")); response.close(); Cookie sessionCookie = null; for (Cookie cookie : cookieStore.getCookies()) { if (cookie.getName().equals("KEYCLOAK_SESSION")) { sessionCookie = cookie; break; } } assertNotNull(sessionCookie); get = new HttpGet(suiteContext.getAuthServerInfo().getContextRoot() + "/auth/realms/master/protocol/openid-connect/login-status-iframe.html"); response = client.execute(get); assertEquals(200, response.getStatusLine().getStatusCode()); s = IOUtils.toString(response.getEntity().getContent(), "UTF-8"); assertTrue(s.contains("function getCookie()")); assertEquals("CP=\"This is not a P3P policy!\"", response.getFirstHeader("P3P").getValue()); response.close(); get = new HttpGet(suiteContext.getAuthServerInfo().getContextRoot() + "/auth/realms/master/protocol/openid-connect/login-status-iframe.html/init"); response = client.execute(get); assertEquals(403, response.getStatusLine().getStatusCode()); response.close(); get = new HttpGet(suiteContext.getAuthServerInfo().getContextRoot() + "/auth/realms/master/protocol/openid-connect/login-status-iframe.html/init?" + "client_id=invalid" + "&origin=" + suiteContext.getAuthServerInfo().getContextRoot()); response = client.execute(get); assertEquals(403, response.getStatusLine().getStatusCode()); response.close(); get = new HttpGet(suiteContext.getAuthServerInfo().getContextRoot() + "/auth/realms/master/protocol/openid-connect/login-status-iframe.html/init?" + "client_id=" + Constants.ADMIN_CONSOLE_CLIENT_ID + "&origin=http://invalid"); response = client.execute(get); assertEquals(403, response.getStatusLine().getStatusCode()); response.close(); get = new HttpGet(suiteContext.getAuthServerInfo().getContextRoot() + "/auth/realms/master/protocol/openid-connect/login-status-iframe.html/init?" + "client_id=" + Constants.ADMIN_CONSOLE_CLIENT_ID + "&origin=" + suiteContext.getAuthServerInfo().getContextRoot()); response = client.execute(get); assertEquals(204, response.getStatusLine().getStatusCode()); response.close(); } }