Example usage for org.apache.http.client.methods CloseableHttpResponse getFirstHeader

List of usage examples for org.apache.http.client.methods CloseableHttpResponse getFirstHeader

Introduction

In this page you can find the example usage for org.apache.http.client.methods CloseableHttpResponse getFirstHeader.

Prototype

Header getFirstHeader(String str);

Source Link

Usage

From source file:com.comcast.cdn.traffic_control.traffic_router.core.external.SteeringTest.java

License:asdf

@Test
public void itUsesNoMultiLocationFormatResponseWithHead() throws Exception {
    final List<String> paths = new ArrayList<String>();
    paths.add("/qwerytuiop/asdfghjkl?fakeClientIpAddress=12.34.56.78");
    paths.add("/qwerytuiop/asdfghjkl?fakeClientIpAddress=12.34.56.78&" + RouterFilter.REDIRECT_QUERY_PARAM
            + "=true");
    paths.add("/qwerytuiop/asdfghjkl?fakeClientIpAddress=12.34.56.78&" + RouterFilter.REDIRECT_QUERY_PARAM
            + "=TRUE");
    paths.add("/qwerytuiop/asdfghjkl?fakeClientIpAddress=12.34.56.78&" + RouterFilter.REDIRECT_QUERY_PARAM
            + "=TruE");
    paths.add("/qwerytuiop/asdfghjkl?fakeClientIpAddress=12.34.56.78&" + RouterFilter.REDIRECT_QUERY_PARAM
            + "=T");

    for (final String path : paths) {
        HttpHead httpHead = new HttpHead("http://localhost:" + routerHttpPort + path);
        httpHead.addHeader("Host", "tr.client-steering-test-1.thecdn.example.com");

        CloseableHttpResponse response = null;

        try {//from www.j a va 2  s  . c  o  m
            response = httpClient.execute(httpHead);
            String location = ".client-steering-target-2.thecdn.example.com:8090" + path;

            assertThat("Failed getting 302 for request " + httpHead.getFirstHeader("Host").getValue(),
                    response.getStatusLine().getStatusCode(), equalTo(302));
            assertThat(response.getFirstHeader("Location").getValue(), endsWith(location));
            assertThat("Failed getting null body for HEAD request", response.getEntity(), nullValue());
        } finally {
            if (response != null) {
                response.close();
            }
        }
    }
}

From source file:org.votingsystem.util.HttpHelper.java

public ResponseVS sendData(byte[] byteArray, ContentTypeVS contentType, String serverURL, String... headerNames)
        throws IOException {
    log.info("sendData - contentType: " + contentType + " - serverURL: " + serverURL);
    ResponseVS responseVS = null;/*www. j  a va2s. c o  m*/
    HttpPost httpPost = null;
    CloseableHttpResponse response = null;
    try {
        httpPost = new HttpPost(serverURL);
        ByteArrayEntity entity = null;
        if (contentType != null)
            entity = new ByteArrayEntity(byteArray, ContentType.create(contentType.getName()));
        else
            entity = new ByteArrayEntity(byteArray);
        httpPost.setEntity(entity);
        response = httpClient.execute(httpPost);
        ContentTypeVS responseContentType = null;
        Header header = response.getFirstHeader("Content-Type");
        if (header != null)
            responseContentType = ContentTypeVS.getByName(header.getValue());
        log.info("------------------------------------------------");
        log.info(response.getStatusLine().toString() + " - contentTypeVS: " + responseContentType
                + " - connManager stats: " + connManager.getTotalStats().toString());
        log.info("------------------------------------------------");
        byte[] responseBytes = EntityUtils.toByteArray(response.getEntity());
        responseVS = new ResponseVS(response.getStatusLine().getStatusCode(), responseBytes,
                responseContentType);
        if (headerNames != null && headerNames.length > 0) {
            List<String> headerValues = new ArrayList<String>();
            for (String headerName : headerNames) {
                org.apache.http.Header headerValue = response.getFirstHeader(headerName);
                if (headerValue != null)
                    headerValues.add(headerValue.getValue());
            }
            responseVS.setData(headerValues);
        }
    } catch (HttpHostConnectException ex) {
        log.log(Level.SEVERE, ex.getMessage(), ex);
        responseVS = new ResponseVS(ResponseVS.SC_ERROR,
                ContextVS.getInstance().getMessage("hostConnectionErrorMsg", serverURL));
    } catch (Exception ex) {
        log.log(Level.SEVERE, ex.getMessage(), ex);
        responseVS = new ResponseVS(ResponseVS.SC_ERROR, ex.getMessage());
        if (httpPost != null)
            httpPost.abort();
    } finally {
        try {
            if (response != null)
                response.close();
        } catch (Exception ex) {
            log.log(Level.SEVERE, ex.getMessage(), ex);
        }
        return responseVS;
    }
}

From source file:crawler.java.edu.uci.ics.crawler4j.fetcher.PageFetcher.java

public PageFetchResult fetchPage(WebURL webUrl)
        throws InterruptedException, IOException, PageBiggerThanMaxSizeException {
    // Getting URL, setting headers & content
    PageFetchResult fetchResult = new PageFetchResult();
    String toFetchURL = webUrl.getURL();
    HttpUriRequest request = null;/*from w w  w.j  a  v  a  2  s .co m*/
    try {
        request = newHttpUriRequest(toFetchURL);
        // Applying Politeness delay
        synchronized (mutex) {
            long now = (new Date()).getTime();
            if ((now - lastFetchTime) < config.getPolitenessDelay()) {
                Thread.sleep(config.getPolitenessDelay() - (now - lastFetchTime));
            }
            lastFetchTime = (new Date()).getTime();
        }

        CloseableHttpResponse response = httpClient.execute(request);
        fetchResult.setEntity(response.getEntity());
        fetchResult.setResponseHeaders(response.getAllHeaders());

        // Setting HttpStatus
        int statusCode = response.getStatusLine().getStatusCode();

        // If Redirect ( 3xx )
        if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY
                || statusCode == HttpStatus.SC_MULTIPLE_CHOICES || statusCode == HttpStatus.SC_SEE_OTHER
                || statusCode == HttpStatus.SC_TEMPORARY_REDIRECT || statusCode == 308) { // todo follow https://issues.apache.org/jira/browse/HTTPCORE-389

            Header header = response.getFirstHeader("Location");
            if (header != null) {
                String movedToUrl = URLCanonicalizer.getCanonicalURL(header.getValue(), toFetchURL);
                fetchResult.setMovedToUrl(movedToUrl);
            }
        } else if (statusCode >= 200 && statusCode <= 299) { // is 2XX, everything looks ok
            fetchResult.setFetchedUrl(toFetchURL);
            String uri = request.getURI().toString();
            if (!uri.equals(toFetchURL)) {
                if (!URLCanonicalizer.getCanonicalURL(uri).equals(toFetchURL)) {
                    fetchResult.setFetchedUrl(uri);
                }
            }

            // Checking maximum size
            if (fetchResult.getEntity() != null) {
                long size = fetchResult.getEntity().getContentLength();
                if (size == -1) {
                    Header length = response.getLastHeader("Content-Length");
                    if (length == null) {
                        length = response.getLastHeader("Content-length");
                    }
                    if (length != null) {
                        size = Integer.parseInt(length.getValue());
                    }
                }
                if (size > config.getMaxDownloadSize()) {
                    //fix issue #52 - consume entity
                    response.close();
                    throw new PageBiggerThanMaxSizeException(size);
                }
            }
        }

        fetchResult.setStatusCode(statusCode);
        return fetchResult;

    } finally { // occurs also with thrown exceptions
        if ((fetchResult.getEntity() == null) && (request != null)) {
            request.abort();
        }
    }
}

From source file:crawler.PageFetcher.java

public PageFetchResult fetchPage(WebURL webUrl)
        throws InterruptedException, IOException, PageBiggerThanMaxSizeException {
    // Getting URL, setting headers & content
    PageFetchResult fetchResult = new PageFetchResult();
    String toFetchURL = webUrl.getURL();
    HttpUriRequest request = null;//w w  w  .  ja  va 2s.co m
    try {
        request = newHttpUriRequest(toFetchURL);
        // Applying Politeness delay
        synchronized (mutex) {
            long now = (new Date()).getTime();
            if ((now - lastFetchTime) < config.getPolitenessDelay()) {
                Thread.sleep(config.getPolitenessDelay() - (now - lastFetchTime));
            }
            lastFetchTime = (new Date()).getTime();
        }

        CloseableHttpResponse response = httpClient.execute(request);
        fetchResult.setEntity(response.getEntity());
        fetchResult.setResponseHeaders(response.getAllHeaders());

        // Setting HttpStatus
        int statusCode = response.getStatusLine().getStatusCode();

        // If Redirect ( 3xx )
        if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY
                || statusCode == HttpStatus.SC_MULTIPLE_CHOICES || statusCode == HttpStatus.SC_SEE_OTHER
                || statusCode == HttpStatus.SC_TEMPORARY_REDIRECT || statusCode == 308) { // todo follow
            // https://issues.apache.org/jira/browse/HTTPCORE-389

            Header header = response.getFirstHeader("Location");
            if (header != null) {
                String movedToUrl = URLCanonicalizer.getCanonicalURL(header.getValue(), toFetchURL);
                fetchResult.setMovedToUrl(movedToUrl);
            }
        } else if (statusCode >= 200 && statusCode <= 299) { // is 2XX, everything looks ok
            fetchResult.setFetchedUrl(toFetchURL);
            String uri = request.getURI().toString();
            if (!uri.equals(toFetchURL)) {
                if (!URLCanonicalizer.getCanonicalURL(uri).equals(toFetchURL)) {
                    fetchResult.setFetchedUrl(uri);
                }
            }

            // Checking maximum size
            if (fetchResult.getEntity() != null) {
                long size = fetchResult.getEntity().getContentLength();
                if (size == -1) {
                    Header length = response.getLastHeader("Content-Length");
                    if (length == null) {
                        length = response.getLastHeader("Content-length");
                    }
                    if (length != null) {
                        size = Integer.parseInt(length.getValue());
                    }
                }
                if (size > config.getMaxDownloadSize()) {
                    //fix issue #52 - consume entity
                    response.close();
                    throw new PageBiggerThanMaxSizeException(size);
                }
            }
        }

        fetchResult.setStatusCode(statusCode);
        return fetchResult;

    } finally { // occurs also with thrown exceptions
        if ((fetchResult.getEntity() == null) && (request != null)) {
            request.abort();
        }
    }
}

From source file:com.mxhero.plugin.cloudstorage.onedrive.api.command.RefreshCommand.java

/**
 * Handler execute./*from w w w  . j av  a  2 s .  c  o  m*/
 *
 * @param handler the handler
 * @return the t
 * @throws ApiException the api exception
 */
private T handlerExecute(CommandHandler<T> handler) throws ApiException {
    CloseableHttpResponse response = null;
    CloseableHttpClient client = null;
    try {
        HttpUriRequest request = handler.request();
        request.setHeader("Authorization", "Bearer " + credential.getAccessToken());
        client = clientBuilder.build();
        response = client.execute(request);
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
            throw new AuthenticationException("401 for " + credential.getUserId());
        } else if (response.getStatusLine().getStatusCode() != HttpStatus.SC_NOT_FOUND
                && response.getStatusLine().getStatusCode() > 399) {
            throw new ApiException(response);
        } else if (response.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND
                && response.getFirstHeader("WWW-Authenticate") != null
                && response.getFirstHeader("WWW-Authenticate").getValue() != null
                && response.getFirstHeader("WWW-Authenticate").getValue().contains("expired_token")) {
            throw new AuthenticationException("401 for " + credential.getUserId());
        }
        return handler.response(response);
    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    } finally {
        if (response != null) {
            try {
                response.close();
            } catch (Exception e) {
            }
            ;
        }
    }
}

From source file:org.esigate.DriverTest.java

/**
 * 0000174: Redirect location with default port specified are incorrectly rewritten when preserveHost=true
 * <p>//from w  w  w. ja va  2  s .c om
 * http://www.esigate.org/mantisbt/view.php?id=174
 * 
 * <p>
 * Issue with default ports, which results in invalid url creation.
 * 
 * @throws Exception
 */
public void testRewriteRedirectResponseWithDefaultPortSpecifiedInLocation() throws Exception {
    Properties properties = new Properties();
    properties.put(Parameters.REMOTE_URL_BASE, "http://www.foo.com:8080");
    properties.put(Parameters.PRESERVE_HOST, "true");
    HttpResponse response = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1),
            HttpStatus.SC_MOVED_TEMPORARILY, "Found");
    // The backend server sets the port even if default (OK it should not
    // but some servers do it)
    response.addHeader("Location", "http://www.foo.com:80/foo/bar");
    mockConnectionManager.setResponse(response);
    Driver driver = createMockDriver(properties, mockConnectionManager);
    request = TestUtils.createIncomingRequest("http://www.foo.com:80/foo");
    // HttpClientHelper will use the Host
    // header to rewrite the request sent to the backend
    // http://www.foo.com/foo
    CloseableHttpResponse driverResponse = driver.proxy("/foo", request.build());
    // The test initially failed with an invalid Location:
    // http://www.foo.com:80:80/foo/bar
    assertEquals("http://www.foo.com:80/foo/bar", driverResponse.getFirstHeader("Location").getValue());
}

From source file:fr.smile.liferay.EsigatePortlet.java

/**
 * Proxy request to provider application
 *
 * @param request//from w  ww. ja v  a  2s.co  m
 * @param targetUrl
 * @param method
 * @return
 * @throws IOException
 * @throws PortletException
 */
private CloseableHttpResponse proxy(PortletRequest request, String targetUrl, String method)
        throws IOException, PortletException {
    Pair<String, String> baseUrls = getBaseUrl(request);

    String baseActionURL = baseUrls.getRight();
    String baseResourceURL = baseUrls.getLeft();

    if (targetUrl.startsWith(baseActionURL)) {
        targetUrl = targetUrl.replace(baseActionURL, "");
    }

    if (LOG.isDebugEnabled()) {
        LOG.debug("Proxy to " + targetUrl);
    }

    String provider = request.getPreferences().getValue(PREF_PROVIDER, null);
    String block = request.getPreferences().getValue(PREF_BLOCK, null);

    IncomingRequest incomingRequest = this.create(request, method);
    Driver driver = DriverFactory.getInstance(provider);
    String[] baseUrl = Parameters.REMOTE_URL_BASE.getValue(driver.getConfiguration().getProperties());
    LiferayUrlRewriter rewriter = new LiferayUrlRewriter(baseActionURL, baseResourceURL);
    ResourceFixupRenderer renderer = new ResourceFixupRenderer(baseUrl[0], targetUrl, rewriter);
    BlockRenderer r = new BlockRenderer(block, null);
    try {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Proxy to driver " + driver.getConfiguration().getInstanceName());
        }
        CloseableHttpResponse driverResponse = driver.proxy(targetUrl, incomingRequest, renderer, r);
        if (LOG.isDebugEnabled()) {
            LOG.debug("HTTP status code is " + driverResponse.getStatusLine().getStatusCode());
        }
        if (driverResponse.getStatusLine().getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY
                || driverResponse.getStatusLine().getStatusCode() == HttpStatus.SC_MOVED_PERMANENTLY
                || driverResponse.getStatusLine().getStatusCode() == HttpStatus.SC_TEMPORARY_REDIRECT) {

            String redirecturl = driverResponse.getFirstHeader("Location").getValue();
            LOG.debug("Redirect to " + redirecturl);
            if (redirecturl.contains(HTTP_BASE_INCOMING_URL)) {
                redirecturl = redirecturl.replace(HTTP_BASE_INCOMING_URL, "");
                LOG.debug("Rendering " + redirecturl);
                driverResponse = driver.render(redirecturl, incomingRequest, renderer, r);
            }

        }
        HttpEntity httpEntity = driverResponse.getEntity();
        if (httpEntity == null) {
            throw new RuntimeException("" + driverResponse.getStatusLine().getStatusCode() + "  "
                    + driverResponse.getStatusLine().getReasonPhrase());
        }

        return driverResponse;

    } catch (HttpErrorPage e) {
        throw new PortletException(e);

    }
}

From source file:com.comcast.cdn.traffic_control.traffic_router.core.external.SteeringTest.java

License:asdf

@Test
public void itUsesMultiLocationFormatResponse() throws Exception {
    final List<String> paths = new ArrayList<String>();
    paths.add("/qwerytuiop/asdfghjkl?fakeClientIpAddress=12.34.56.78");
    paths.add("/qwerytuiop/asdfghjkl?fakeClientIpAddress=12.34.56.78&" + RouterFilter.REDIRECT_QUERY_PARAM
            + "=true");
    paths.add("/qwerytuiop/asdfghjkl?fakeClientIpAddress=12.34.56.78&" + RouterFilter.REDIRECT_QUERY_PARAM
            + "=TRUE");
    paths.add("/qwerytuiop/asdfghjkl?fakeClientIpAddress=12.34.56.78&" + RouterFilter.REDIRECT_QUERY_PARAM
            + "=TruE");
    paths.add("/qwerytuiop/asdfghjkl?fakeClientIpAddress=12.34.56.78&" + RouterFilter.REDIRECT_QUERY_PARAM
            + "=T");

    for (final String path : paths) {
        HttpGet httpGet = new HttpGet("http://localhost:" + routerHttpPort + path);
        httpGet.addHeader("Host", "tr.client-steering-test-1.thecdn.example.com");

        CloseableHttpResponse response = null;

        try {//from  w w w . j av  a 2s. c o m
            response = httpClient.execute(httpGet);
            String location1 = ".client-steering-target-2.thecdn.example.com:8090" + path;
            String location2 = ".client-steering-target-1.thecdn.example.com:8090" + path;

            assertThat("Failed getting 302 for request " + httpGet.getFirstHeader("Host").getValue(),
                    response.getStatusLine().getStatusCode(), equalTo(302));
            assertThat(response.getFirstHeader("Location").getValue(), endsWith(location1));

            HttpEntity entity = response.getEntity();
            ObjectMapper objectMapper = new ObjectMapper(new JsonFactory());

            assertThat(entity.getContent(), not(nullValue()));

            JsonNode json = objectMapper.readTree(entity.getContent());

            assertThat(json.has("locations"), equalTo(true));
            assertThat(json.get("locations").size(), equalTo(2));
            assertThat(json.get("locations").get(0).asText(),
                    equalTo(response.getFirstHeader("Location").getValue()));
            assertThat(json.get("locations").get(1).asText(), endsWith(location2));
        } finally {
            if (response != null) {
                response.close();
            }
        }
    }
}

From source file:org.cloudfoundry.identity.uaa.integration.FormLoginIntegrationTests.java

@Test
public void testSuccessfulAuthenticationFlow() throws Exception {
    //request home page /
    String location = serverRunning.getBaseUrl() + "/";
    HttpGet httpget = new HttpGet(location);
    CloseableHttpResponse response = httpclient.execute(httpget);

    assertEquals(OK.value(), response.getStatusLine().getStatusCode());

    String body = EntityUtils.toString(response.getEntity());
    EntityUtils.consume(response.getEntity());
    response.close();/*from  w ww . ja  v  a  2s .co  m*/
    httpget.completed();

    assertTrue(body.contains("/login.do"));
    assertTrue(body.contains("username"));
    assertTrue(body.contains("password"));

    String csrf = IntegrationTestUtils.extractCookieCsrf(body);

    HttpUriRequest loginPost = RequestBuilder.post().setUri(serverRunning.getBaseUrl() + "/login.do")
            .addParameter("username", testAccounts.getUserName())
            .addParameter("password", testAccounts.getPassword())
            .addParameter(CookieBasedCsrfTokenRepository.DEFAULT_CSRF_COOKIE_NAME, csrf).build();

    response = httpclient.execute(loginPost);
    assertEquals(FOUND.value(), response.getStatusLine().getStatusCode());
    location = response.getFirstHeader("Location").getValue();
    response.close();

    httpget = new HttpGet(location);
    response = httpclient.execute(httpget);
    assertEquals(OK.value(), response.getStatusLine().getStatusCode());

    body = EntityUtils.toString(response.getEntity());
    response.close();
    assertTrue(body.contains("Sign Out"));
}