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

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

Introduction

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

Prototype

Header[] getAllHeaders();

Source Link

Usage

From source file:org.wso2.msf4j.delegates.client.MSF4JClientResponseContext.java

/**
 * Constructor of MSF4JClientResponseContext.
 *//*from  w  ww . j  a v  a2 s  .co m*/
public MSF4JClientResponseContext(CloseableHttpResponse response) throws IOException {
    this.statusCode = response.getStatusLine().getStatusCode();
    for (Header header : response.getAllHeaders()) {
        headers.add(header.getName(), header.getValue());
    }
    this.entityStream = response.getEntity().getContent();
}

From source file:com.googlecode.webutilities.servlets.WebProxyServlet.java

private void copyResponse(CloseableHttpResponse fromResponse, HttpServletResponse toResponse)
        throws IOException {
    toResponse.setStatus(fromResponse.getStatusLine().getStatusCode());
    for (Header header : fromResponse.getAllHeaders()) {
        toResponse.setHeader(header.getName(), header.getValue());
    }//  w ww.j av a  2s. com
    HttpEntity entity = fromResponse.getEntity();
    IOUtils.copy(entity.getContent(), toResponse.getOutputStream());
}

From source file:net.siegmar.japtproxy.fetcher.FetcherHttp.java

/**
 * {@inheritDoc}/*from  w  ww . j  a  va  2s . c om*/
 */
@Override
public FetchedResourceHttp fetch(final URL targetResource, final long lastModified,
        final String originalUserAgent) throws IOException, ResourceUnavailableException {

    final HttpGet httpGet = new HttpGet(targetResource.toExternalForm());

    httpGet.addHeader(HttpHeaderConstants.USER_AGENT,
            StringUtils.trim(StringUtils.defaultString(originalUserAgent) + " " + Util.USER_AGENT));

    if (lastModified != 0) {
        final String lastModifiedSince = Util.getRfc822DateFromTimestamp(lastModified);
        LOG.debug("Setting If-Modified-Since: {}", lastModifiedSince);
        httpGet.setHeader(HttpHeaderConstants.IF_MODIFIED_SINCE, lastModifiedSince);
    }

    CloseableHttpResponse httpResponse = null;
    try {
        httpResponse = httpClient.execute(httpGet);

        if (LOG.isDebugEnabled()) {
            logResponseHeader(httpResponse.getAllHeaders());
        }

        final int retCode = httpResponse.getStatusLine().getStatusCode();
        if (retCode == HttpServletResponse.SC_NOT_FOUND) {
            httpGet.releaseConnection();
            throw new ResourceUnavailableException("Resource '" + targetResource + " not found");
        }

        if (retCode != HttpServletResponse.SC_OK && retCode != HttpServletResponse.SC_NOT_MODIFIED) {
            throw new IOException("Invalid status code returned: " + httpResponse.getStatusLine());
        }

        final FetchedResourceHttp fetchedResourceHttp = new FetchedResourceHttp(httpResponse);
        fetchedResourceHttp.setModified(lastModified == 0 || retCode != HttpServletResponse.SC_NOT_MODIFIED);

        if (LOG.isDebugEnabled()) {
            final long fetchedTimestamp = fetchedResourceHttp.getLastModified();
            if (fetchedTimestamp != 0) {
                LOG.debug("Response status code: {}, Last modified: {}", retCode,
                        Util.getSimpleDateFromTimestamp(fetchedTimestamp));
            } else {
                LOG.debug("Response status code: {}", retCode);
            }
        }

        return fetchedResourceHttp;
    } catch (final IOException e) {
        // Closing only in case of an exception - otherwise closed by FetchedResourceHttp
        if (httpResponse != null) {
            httpResponse.close();
        }

        throw e;
    }
}

From source file:com.agileapes.couteau.http.client.impl.DefaultLatentHttpClient.java

private HttpResponse getResponse(CloseableHttpResponse response, HttpRequest httpRequest) {
    final ArrayList<HttpHeader> headers = new ArrayList<HttpHeader>();
    for (Header header : response.getAllHeaders()) {
        headers.add(new ImmutableHttpHeader(header.getName(), header.getValue()));
    }//from  w  w w . ja v  a  2  s  .  co m
    return new ImmutableHttpResponse(response.getEntity(), response.getStatusLine().getStatusCode(),
            response.getStatusLine().getReasonPhrase(), headers, httpRequest, response);
}

From source file:com.vmware.vim25.ws.ApacheHttpClient.java

private InputStream post(String payload) {
    CloseableHttpClient httpclient;//from  w  w  w.j a v a 2  s. com
    RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(this.connectTimeout)
            .setSocketTimeout(this.readTimeout).build();
    if (trustAllSSL) {
        httpclient = HttpClients.custom().setSSLSocketFactory(ApacheTrustSelfSigned.trust()).build();

    } else {
        httpclient = HttpClients.createDefault();
    }
    HttpPost httpPost;
    StringEntity stringEntity;
    try {
        stringEntity = new StringEntity(payload);
        log.trace("Converted payload to String entity.");
    } catch (UnsupportedEncodingException e) {
        log.error("Failed to convert payload to StringEntity. Unsupported Encoding Exception caught. Payload: "
                + payload, e);
        return null;
    }
    try {
        httpPost = new HttpPost(this.baseUrl.toURI());
    } catch (URISyntaxException e) {
        log.error("Malformed URI sent: " + this.baseUrl.toString(), e);
        return null;
    }
    httpPost.setConfig(requestConfig);
    httpPost.setHeader(SoapAction.SOAP_ACTION_HEADER.toString(), soapAction);
    httpPost.setHeader("Content-Type", "text/xml; charset=utf-8");
    if (cookie != null) {
        log.trace("Setting Cookie.");
        httpPost.setHeader("Cookie", cookie);
    }
    httpPost.setEntity(stringEntity);
    try {
        CloseableHttpResponse response = httpclient.execute(httpPost);
        InputStream inputStream = response.getEntity().getContent();
        if (cookie == null) {

            Header[] headers = response.getAllHeaders();
            for (Header header : headers) {
                if (header.getName().equals("Set-Cookie")) {
                    cookie = header.getValue();
                    break;
                }
            }
        }
        return inputStream;
    } catch (IOException e) {
        log.error("", e);
    }
    return null;
}

From source file:com.yahoo.gondola.container.client.ApacheHttpComponentProxyClient.java

private Response getResponse(CloseableHttpResponse proxiedResponse) throws IOException {
    Response.ResponseBuilder builder = Response.status(proxiedResponse.getStatusLine().getStatusCode());

    if (proxiedResponse.getEntity() != null) {
        builder.entity(EntityUtils.toString(proxiedResponse.getEntity()));
        for (Header header : proxiedResponse.getAllHeaders()) {
            builder.header(header.getName(), header.getValue());
        }/*ww w.  j  av a  2  s .co m*/
    }
    return builder.build();
}

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.j a  va2s  .c  om
    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.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 va  2  s . c  om
    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:org.openhim.mediator.engine.MediatorServerTest.java

private CloseableHttpResponse executeHTTPRequest(String method, String path, String body,
        Map<String, String> headers, Map<String, String> params) throws URISyntaxException, IOException {
    URIBuilder builder = new URIBuilder().setScheme("http").setHost(testConfig.getServerHost())
            .setPort(testConfig.getServerPort()).setPath(path);

    if (params != null) {
        Iterator<String> iter = params.keySet().iterator();
        while (iter.hasNext()) {
            String param = iter.next();
            builder.addParameter(param, params.get(param));
        }//from w  w  w  . j  av a 2 s.c  o m
    }

    HttpUriRequest uriReq;
    switch (method) {
    case "GET":
        uriReq = new HttpGet(builder.build());
        break;
    case "POST":
        uriReq = new HttpPost(builder.build());
        StringEntity entity = new StringEntity(body);
        if (body.length() > 1024) {
            //always test big requests chunked
            entity.setChunked(true);
        }
        ((HttpPost) uriReq).setEntity(entity);
        break;
    case "PUT":
        uriReq = new HttpPut(builder.build());
        StringEntity putEntity = new StringEntity(body);
        ((HttpPut) uriReq).setEntity(putEntity);
        break;
    case "DELETE":
        uriReq = new HttpDelete(builder.build());
        break;
    default:
        throw new UnsupportedOperationException(method + " requests not supported");
    }

    if (headers != null) {
        Iterator<String> iter = headers.keySet().iterator();
        while (iter.hasNext()) {
            String header = iter.next();
            uriReq.addHeader(header, headers.get(header));
        }
    }

    RequestConfig.Builder reqConf = RequestConfig.custom().setConnectTimeout(1000)
            .setConnectionRequestTimeout(1000);
    CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(reqConf.build()).build();

    CloseableHttpResponse response = client.execute(uriReq);

    boolean foundContentType = false;
    for (Header hdr : response.getAllHeaders()) {
        if ("content-type".equalsIgnoreCase(hdr.getName())) {
            assertTrue(hdr.getValue().contains("application/json+openhim"));
            foundContentType = true;
        }
    }
    assertTrue("Content-Type must be included in the response", foundContentType);

    return response;
}

From source file:org.tinymediamanager.scraper.util.Url.java

/**
 * Gets the input stream.//from   w  ww.java2  s .  co  m
 * 
 * @return the input stream
 * @throws IOException
 *           Signals that an I/O exception has occurred.
 */
public InputStream getInputStream() throws IOException, InterruptedException {
    // workaround for local files
    if (url.startsWith("file:")) {
        String newUrl = url.replace("file:/", "");
        File file = new File(newUrl);
        return new FileInputStream(file);
    }

    BasicHttpContext localContext = new BasicHttpContext();
    ByteArrayInputStream is = null;

    // replace our API keys for logging...
    String logUrl = url.replaceAll("api_key=\\w+", "api_key=<API_KEY>").replaceAll("api/\\d+\\w+",
            "api/<API_KEY>");
    LOGGER.debug("getting " + logUrl);
    HttpGet httpget = new HttpGet(uri);
    RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(10000)
            .build();
    httpget.setConfig(requestConfig);

    // set custom headers
    for (Header header : headersRequest) {
        httpget.addHeader(header);
    }

    CloseableHttpResponse response = null;
    try {
        response = client.execute(httpget, localContext);
        headersResponse = response.getAllHeaders();
        entity = response.getEntity();
        responseStatus = response.getStatusLine();
        if (entity != null) {
            is = new ByteArrayInputStream(EntityUtils.toByteArray(entity));
        }
        EntityUtils.consume(entity);
    } catch (InterruptedIOException e) {
        LOGGER.info("aborted request (" + e.getMessage() + "): " + logUrl);
        throw e;
    } catch (UnknownHostException e) {
        LOGGER.error("proxy or host not found/reachable", e);
        throw e;
    } catch (Exception e) {
        LOGGER.error("Exception getting url " + logUrl, e);
    } finally {
        if (response != null) {
            response.close();
        }
    }
    return is;
}