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.apache.sling.etcd.client.impl.EtcdClientImpl.java

@Nonnull
private Response exec(@Nonnull HttpUriRequest method, int... expected) throws IOException {
    CloseableHttpResponse response = httpClient.execute(logMethod(method));
    try {/*from   www.j a  v a  2 s  . c o m*/
        StatusLine statusLine = response.getStatusLine();
        Map<String, List<String>> headers = extractHeaders(response.getAllHeaders());
        String body = (response.getEntity() != null) ? EntityUtils.toString(response.getEntity(), UTF8) : null;
        logResponse(statusLine, body, headers);
        checkStatus(statusLine, expected);
        if (body == null) {
            throw new IOException(String.format("No entity found in response %s",
                    formatStatusLine(response.getStatusLine())));
        }
        return new Response(statusLine.getStatusCode(), statusLine.getReasonPhrase(), headers, body);
    } finally {
        IOUtils.closeQuietly(response);
    }
}

From source file:com.threatconnect.sdk.conn.HttpRequestExecutor.java

@Override
public String execute(String path, HttpMethod type, Object obj) throws IOException {

    path += (path.contains("?") ? "&" : "?");
    path += "createActivityLog=" + this.conn.getConfig().isActivityLogEnabled();

    logger.trace("Path: " + path);
    String fullPath = this.conn.getConfig().getTcApiUrl() + path.replace("/api/", "/");

    logger.trace("Full: " + type + ": " + fullPath);
    HttpRequestBase httpBase = getBase(fullPath, type);
    if (obj != null)
        applyEntityAsJSON(httpBase, obj);

    logger.trace("RawPath: " + httpBase.getURI().getPath());
    logger.trace("Query: " + httpBase.getURI().getRawQuery());
    logger.trace("Path: " + path);

    String headerPath = httpBase.getURI().getRawPath() + "?" + httpBase.getURI().getRawQuery();
    logger.trace("HeaderPath: " + headerPath);
    ConnectionUtil.applyHeaders(this.conn.getConfig(), httpBase, type.toString(), headerPath);
    logger.trace("Request: " + httpBase.getRequestLine());
    long startMs = System.currentTimeMillis();
    CloseableHttpResponse response = this.conn.getApiClient().execute(httpBase);
    notifyListeners(type, fullPath, (System.currentTimeMillis() - startMs));
    String result = null;//  w w  w .  j  a  v  a  2 s . co m

    try {
        logger.trace(response.getStatusLine().toString());
        HttpEntity entity = response.getEntity();
        logger.trace("Response Headers: " + Arrays.toString(response.getAllHeaders()));
        logger.trace("Content Encoding: " + entity.getContentEncoding());
        if (entity != null) {
            result = EntityUtils.toString(entity, StandardCharsets.UTF_8);
            logger.trace("Result:" + result);
            EntityUtils.consume(entity);
        }
    } finally {
        response.close();
    }

    return result;
}

From source file:org.daybreak.coccinella.webmagic.CrawlerDownloader.java

@Override
public Page download(Request request, Task task) {
    Site site = null;/*  w w w.j av a  2s.c  o  m*/
    if (task != null) {
        site = task.getSite();
    }
    Set<Integer> acceptStatCode;
    String charset = null;
    Map<String, String> headers = null;
    if (site != null) {
        acceptStatCode = site.getAcceptStatCode();
        charset = site.getCharset();
        headers = site.getHeaders();
    } else {
        acceptStatCode = Sets.newHashSet(200);
    }
    logger.info("downloading page " + request.getUrl());
    RequestBuilder requestBuilder = null;
    if (request instanceof CrawlerRequest) {
        CrawlerRequest crawlerRequest = (CrawlerRequest) request;
        if (StringUtils.isNotBlank(crawlerRequest.getCrawler().getReferer())) {
            site.addHeader(HttpHeaders.REFERER, crawlerRequest.getCrawler().getReferer());
        }
        if (crawlerRequest.getCrawler().getMethod() == HttpMethod.GET) {
            requestBuilder = RequestBuilder.get().setUri(request.getUrl());
        } else if (crawlerRequest.getCrawler().getMethod() == HttpMethod.POST) {
            try {
                requestBuilder = RequestBuilder.post().setUri(crawlerRequest.getUrl())
                        .setEntity(crawlerRequest.createEntity());
            } catch (UnsupportedEncodingException ex) {
                logger.warn("The encoding is not supported: " + crawlerRequest.getCrawler().getEncode());
                return null;
            }
        }
    }

    if (requestBuilder == null) {
        return null;
    }

    if (headers != null) {
        for (Map.Entry<String, String> headerEntry : headers.entrySet()) {
            requestBuilder.addHeader(headerEntry.getKey(), headerEntry.getValue());
        }
    }
    RequestConfig.Builder requestConfigBuilder = RequestConfig.custom()
            .setConnectionRequestTimeout(site.getTimeOut()).setSocketTimeout(site.getTimeOut())
            .setConnectTimeout(site.getTimeOut()).setCookieSpec(CookieSpecs.BEST_MATCH);
    if (site != null && site.getHttpProxy() != null) {
        requestConfigBuilder.setProxy(site.getHttpProxy());
    }
    requestBuilder.setConfig(requestConfigBuilder.build());
    CloseableHttpResponse httpResponse = null;
    try {
        httpResponse = getHttpClient(site).execute(requestBuilder.build());
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        if (acceptStatCode.contains(statusCode)) {
            // ??
            Header[] resHeaders = httpResponse.getAllHeaders();
            for (int i = 0; i < resHeaders.length; i++) {
                if (resHeaders[i].getName().equals("Set-Cookie")) {
                    String cookie = resHeaders[i].getValue();
                    String cookieName = cookie.split("=")[0];
                    String cookieValue = cookie.split("=")[1].split(";")[0];
                    site.addCookie(cookieName, cookieValue);
                }
            }

            //charset
            if (charset == null) {
                String value = httpResponse.getEntity().getContentType().getValue();
                charset = UrlUtils.getCharset(value);
            }
            return handleResponse(request, charset, httpResponse, task);
        } else {
            logger.warn("code error " + statusCode + "\t" + request.getUrl());
            return null;
        }
    } catch (IOException e) {
        logger.warn("download page " + request.getUrl() + " error", e);
        if (site.getCycleRetryTimes() > 0) {
            return addToCycleRetry(request, site);
        }
        return null;
    } finally {
        try {
            if (httpResponse != null) {
                //ensure the connection is released back to pool
                EntityUtils.consume(httpResponse.getEntity());
            }
        } catch (IOException e) {
            logger.warn("close response fail", e);
        }
    }
}

From source file:io.mandrel.requests.http.ApacheHttpRequester.java

public Blob extractWebPage(Uri uri, CloseableHttpResponse result, HttpContext localContext)
        throws MalformedURLException, IOException {
    try {/*from w  w  w  .  ja v a2s .  c  o m*/
        Map<String, List<String>> headers = new HashMap<String, List<String>>();
        if (result.getAllHeaders() != null) {
            for (Header header : result.getAllHeaders()) {
                headers.put(header.getName(), Arrays.asList(header.getValue()));
            }
        }

        List<io.mandrel.requests.http.Cookie> cookies = null;
        if (localContext != null) {
            CookieStore store = (CookieStore) localContext.getAttribute(HttpClientContext.COOKIE_STORE);
            if (store.getCookies() != null) {
                cookies = store.getCookies().stream().filter(cookie -> cookie != null)
                        .map(cookie -> new io.mandrel.requests.http.Cookie(cookie.getName(), cookie.getValue(),
                                cookie.getDomain(), cookie.getPath(),
                                cookie.getExpiryDate() != null ? cookie.getExpiryDate().getTime() : 0,
                                cookie.getExpiryDate() != null ? (int) cookie.getExpiryDate().getTime() : 0,
                                cookie.isSecure(), false))
                        .collect(Collectors.toList());
            }
        }

        HttpFetchMetadata metadata = new HttpFetchMetadata().headers(headers).cookies(cookies);
        metadata.setUri(uri)
                .setStatusCode(result.getStatusLine() != null ? result.getStatusLine().getStatusCode() : 0)
                .setStatusText(
                        result.getStatusLine() != null ? result.getStatusLine().getReasonPhrase() : null);

        HttpEntity entity = result.getEntity();
        InputStream content = entity.getContent();
        try {
            long contentLength = entity.getContentLength();
            Blob blob = new Blob(new BlobMetadata().setUri(uri)
                    .setSize(contentLength < 0 ? null : contentLength).setFetchMetadata(metadata))
                            .payload(IOUtils.toByteArray(content));
            return blob;
        } catch (IOException ex) {
            // In case of an IOException the connection will be released
            // back to the connection manager automatically
            throw ex;
        } finally {
            // Closing the input stream will trigger connection release
            content.close();
        }
    } finally {
        result.close();
    }
}

From source file:com.wso2telco.entity.Endpoints.java

/**
 * Call oauth2./*from  ww w.j  a v  a  2 s .co m*/
 *
 * @param oauthURL the oauth url
 * @param msisdn the msisdn
 * @param ipAddress the ip address
 * @return the string
 */
private String callOauth2(String oauthURL, String msisdn, String ipAddress) {
    String redirectURL = null;

    try {
        HttpGet get = new HttpGet(oauthURL);

        CloseableHttpClient httpclient = null;
        CloseableHttpResponse response = null;

        httpclient = HttpClients.createDefault();

        get.setHeader("msisdn", msisdn);
        get.setHeader("ipaddress", ipAddress);

        response = httpclient.execute(get);

        if (log.isDebugEnabled()) {
            log.debug("Status = " + response.getStatusLine().toString());
        }

        Header[] headers = response.getAllHeaders();

        for (Header header : headers) {

            if (log.isDebugEnabled()) {
                log.debug("Key : " + header.getName() + " ,Value : " + header.getValue());
            }
            if (header.getName().equals("Location")) {
                redirectURL = header.getValue();
            }
        }

    } catch (IOException ex) {
        // // LOG.info("Error occurred " + ex);
        log.error("Error occurred " + ex);
    }
    return redirectURL;
}

From source file:eu.fusepool.p3.proxy.ProxyHandler.java

@Override
public void handle(String target, Request baseRequest, final HttpServletRequest inRequest,
        final HttpServletResponse outResponse) throws IOException, ServletException {
    final String targetUriString = targetBaseUri + inRequest.getRequestURI();
    final String requestUri = getFullRequestUrl(inRequest);
    //System.out.println(targetUriString);
    final URI targetUri;
    try {//  w  w w .  ja v  a  2s  .c  om
        targetUri = new URI(targetUriString);
    } catch (URISyntaxException ex) {
        throw new IOException(ex);
    }
    final String method = inRequest.getMethod();
    final HttpEntityEnclosingRequestBase outRequest = new HttpEntityEnclosingRequestBase() {

        @Override
        public String getMethod() {
            return method;
        }

    };
    outRequest.setURI(targetUri);
    String transformerUri = null;
    if (method.equals("POST")) {
        if (!"no-transform".equals(inRequest.getHeader("X-Fusepool-Proxy"))) {
            transformerUri = getTransformerUrl(requestUri);
        }
    }
    final Enumeration<String> headerNames = baseRequest.getHeaderNames();
    while (headerNames.hasMoreElements()) {
        final String headerName = headerNames.nextElement();
        if (headerName.equalsIgnoreCase("Content-Length") || headerName.equalsIgnoreCase("X-Fusepool-Proxy")
                || headerName.equalsIgnoreCase("Transfer-Encoding")) {
            continue;
        }
        final Enumeration<String> headerValues = baseRequest.getHeaders(headerName);
        if (headerValues.hasMoreElements()) {
            final String headerValue = headerValues.nextElement();
            outRequest.setHeader(headerName, headerValue);
        }
        while (headerValues.hasMoreElements()) {
            final String headerValue = headerValues.nextElement();
            outRequest.addHeader(headerName, headerValue);
        }
    }
    final Header[] outRequestHeaders = outRequest.getAllHeaders();
    //slow: outRequest.setEntity(new InputStreamEntity(inRequest.getInputStream()));
    final byte[] inEntityBytes = IOUtils.toByteArray(inRequest.getInputStream());
    if (inEntityBytes.length > 0) {
        outRequest.setEntity(new ByteArrayEntity(inEntityBytes));
    }
    final CloseableHttpResponse inResponse = httpclient.execute(outRequest);
    try {
        outResponse.setStatus(inResponse.getStatusLine().getStatusCode());
        final Header[] inResponseHeaders = inResponse.getAllHeaders();
        final Set<String> setHeaderNames = new HashSet();
        for (Header header : inResponseHeaders) {
            if (setHeaderNames.add(header.getName())) {
                outResponse.setHeader(header.getName(), header.getValue());
            } else {
                outResponse.addHeader(header.getName(), header.getValue());
            }
        }
        final HttpEntity entity = inResponse.getEntity();
        final ServletOutputStream os = outResponse.getOutputStream();
        if (entity != null) {
            //outResponse.setContentType(target);
            final InputStream instream = entity.getContent();
            try {
                IOUtils.copy(instream, os);
            } finally {
                instream.close();
            }
        }
        //without flushing this and no or too little byte jetty return 404
        os.flush();
    } finally {
        inResponse.close();
    }
    if (transformerUri != null) {
        Header locationHeader = inResponse.getFirstHeader("Location");
        if (locationHeader == null) {
            log.warn("Response to POST request to LDPC contains no Location header. URI: " + targetUriString);
        } else {
            startTransformation(locationHeader.getValue(), requestUri, transformerUri, inEntityBytes,
                    outRequestHeaders);
        }
    }
}

From source file:br.com.autonomiccs.apacheCloudStack.client.ApacheCloudStackClient.java

/**
 *  It creates an {@link HttpContext} object with a cookie store that will contain the cookies returned by the user in the {@link CloseableHttpResponse} that is received as parameter.
 *///from   w  w w. java2s .  c om
protected HttpContext createHttpContextWithCookies(CloseableHttpResponse loginResponse) {
    CookieStore cookieStore = new BasicCookieStore();
    createAndAddCookiesOnStoreForHeaders(cookieStore, loginResponse.getAllHeaders());
    HttpContext httpContext = new BasicHttpContext();
    httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
    return httpContext;
}

From source file:com.clickntap.vimeo.Vimeo.java

private VimeoResponse apiRequest(String endpoint, String methodName, Map<String, String> params, File file)
        throws IOException {
    CloseableHttpClient client = HttpClientBuilder.create().build();
    HttpRequestBase request = null;//w  w w. j a  v  a 2  s .co m
    String url = null;
    if (endpoint.startsWith("http")) {
        url = endpoint;
    } else {
        url = new StringBuffer(VIMEO_SERVER).append(endpoint).toString();
    }
    if (methodName.equals(HttpGet.METHOD_NAME)) {
        request = new HttpGet(url);
    } else if (methodName.equals(HttpPost.METHOD_NAME)) {
        request = new HttpPost(url);
    } else if (methodName.equals(HttpPut.METHOD_NAME)) {
        request = new HttpPut(url);
    } else if (methodName.equals(HttpDelete.METHOD_NAME)) {
        request = new HttpDelete(url);
    } else if (methodName.equals(HttpPatch.METHOD_NAME)) {
        request = new HttpPatch(url);
    }
    request.addHeader("Accept", "application/vnd.vimeo.*+json; version=3.2");
    request.addHeader("Authorization", new StringBuffer(tokenType).append(" ").append(token).toString());
    HttpEntity entity = null;
    if (params != null) {
        ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
        for (String key : params.keySet()) {
            postParameters.add(new BasicNameValuePair(key, params.get(key)));
        }
        entity = new UrlEncodedFormEntity(postParameters);
    } else if (file != null) {
        entity = new FileEntity(file, ContentType.MULTIPART_FORM_DATA);
    }
    if (entity != null) {
        if (request instanceof HttpPost) {
            ((HttpPost) request).setEntity(entity);
        } else if (request instanceof HttpPatch) {
            ((HttpPatch) request).setEntity(entity);
        } else if (request instanceof HttpPut) {
            ((HttpPut) request).setEntity(entity);
        }
    }
    CloseableHttpResponse response = client.execute(request);
    String responseAsString = null;
    int statusCode = response.getStatusLine().getStatusCode();
    if (methodName.equals(HttpPut.METHOD_NAME) || methodName.equals(HttpDelete.METHOD_NAME)) {
        JSONObject out = new JSONObject();
        for (Header header : response.getAllHeaders()) {
            out.put(header.getName(), header.getValue());
        }
        responseAsString = out.toString();
    } else if (statusCode != 204) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        response.getEntity().writeTo(out);
        responseAsString = out.toString("UTF-8");
        out.close();
    }
    JSONObject json = null;
    try {
        json = new JSONObject(responseAsString);
    } catch (Exception e) {
        json = new JSONObject();
    }
    VimeoResponse vimeoResponse = new VimeoResponse(json, statusCode);
    response.close();
    client.close();
    return vimeoResponse;
}

From source file:org.riotfamily.crawler.HttpClientPageLoader.java

public PageData loadPage(Href href) {
    String url = href.getResolvedUri();
    PageData pageData = new PageData(href);
    log.info("Loading page: " + url);
    HttpGet get = null;/*from   w w w .j  a  v a  2  s .c o m*/
    try {
        get = new HttpGet(url);
        if (StringUtils.hasText(href.getReferrerUrl())) {
            get.addHeader(ServletUtils.REFERER_HEADER, href.getReferrerUrl());
        }
        prepareMethod(get);
        CloseableHttpResponse httpResponse = client.execute(get);
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        pageData.setStatusCode(statusCode);
        if (statusCode == HttpStatus.SC_OK) {
            try {
                HttpEntity entity = httpResponse.getEntity();
                if (accept(entity)) {
                    String content = EntityUtils.toString(entity, Consts.UTF_8);
                    pageData.setHtml(content);
                    Header[] headers = httpResponse.getAllHeaders();
                    for (int i = 0; i < headers.length; i++) {
                        pageData.addHeader(headers[i].getName(), headers[i].getValue());
                    }
                }
            } finally {
                httpResponse.close();
            }
        } else {
            log.info("Status: " + statusCode);
            Header[] locationHeaders = httpResponse.getHeaders("Location");
            if (locationHeaders != null && locationHeaders.length == 1) {
                pageData.setRedirectUrl(locationHeaders[0].getValue());
            } else {
                pageData.setError(httpResponse.getStatusLine().toString());
            }
        }
    } catch (Exception e) {
        pageData.setError(e.getMessage());
        log.warn(e.getMessage());
    } finally {
        try {
            if (get != null) {
                get.releaseConnection();
            }
        } catch (Exception e) {
        }
    }
    return pageData;
}

From source file:de.codecentric.boot.admin.zuul.filters.route.SimpleHostRoutingFilter.java

private CloseableHttpResponse forward(CloseableHttpClient httpclient, String verb, String uri,
        HttpServletRequest request, MultiValueMap<String, String> headers, MultiValueMap<String, String> params,
        InputStream requestEntity) throws Exception {
    Map<String, Object> info = this.helper.debug(verb, uri, headers, params, requestEntity);
    URL host = RequestContext.getCurrentContext().getRouteHost();
    HttpHost httpHost = getHttpHost(host);
    uri = StringUtils.cleanPath((host.getPath() + uri).replaceAll("/{2,}", "/"));
    HttpRequest httpRequest;/*from  w  ww. ja v a 2 s .c  o m*/
    int contentLength = request.getContentLength();
    InputStreamEntity entity = new InputStreamEntity(requestEntity, contentLength,
            request.getContentType() != null ? ContentType.create(request.getContentType()) : null);
    switch (verb.toUpperCase()) {
    case "POST":
        HttpPost httpPost = new HttpPost(uri + this.helper.getQueryString(params));
        httpRequest = httpPost;
        httpPost.setEntity(entity);
        break;
    case "PUT":
        HttpPut httpPut = new HttpPut(uri + this.helper.getQueryString(params));
        httpRequest = httpPut;
        httpPut.setEntity(entity);
        break;
    case "PATCH":
        HttpPatch httpPatch = new HttpPatch(uri + this.helper.getQueryString(params));
        httpRequest = httpPatch;
        httpPatch.setEntity(entity);
        break;
    default:
        httpRequest = new BasicHttpRequest(verb, uri + this.helper.getQueryString(params));
        log.debug(uri + this.helper.getQueryString(params));
    }
    try {
        httpRequest.setHeaders(convertHeaders(headers));
        log.debug(httpHost.getHostName() + " " + httpHost.getPort() + " " + httpHost.getSchemeName());
        CloseableHttpResponse zuulResponse = forwardRequest(httpclient, httpHost, httpRequest);
        RequestContext.getCurrentContext().set("zuulResponse", zuulResponse);
        this.helper.appendDebug(info, zuulResponse.getStatusLine().getStatusCode(),
                revertHeaders(zuulResponse.getAllHeaders()));
        return zuulResponse;
    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        // httpclient.getConnectionManager().shutdown();
    }
}