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.nectarframework.base.service.nanohttp.NanoHttpService.java

private Response serveProxy(ProxyResolution proxyResolution, String uri, Method method,
        Map<String, String> headers, Map<String, List<String>> parms, String queryParameterString,
        Map<String, String> files) {

    String remoteTarget = uri.substring(proxyResolution.getPath().length() + 1);
    if (!remoteTarget.startsWith("/")) {
        remoteTarget = "/" + remoteTarget;
    }//from  w  ww.j  a  va 2s. c  o m

    CloseableHttpClient httpclient = HttpClients.createDefault();
    URIBuilder remoteUri = new URIBuilder();
    remoteUri.setScheme("http");
    remoteUri.setHost(proxyResolution.getHost());
    remoteUri.setPort(proxyResolution.getPort());
    remoteUri.setPath(proxyResolution.getRequestPath() + remoteTarget);
    remoteUri.setCharset(Charset.defaultCharset());
    for (String k : parms.keySet()) {
        remoteUri.addParameter(k, parms.get(k).get(0));
    }

    HttpGet httpget;
    try {
        httpget = new HttpGet(remoteUri.build());
    } catch (URISyntaxException e) {
        Log.warn(e);
        return newFixedLengthResponse(Status.INTERNAL_ERROR, NanoHttpService.MIME_PLAINTEXT,
                "SERVER INTERNAL ERROR: URISyntaxException" + e.getMessage());
    }

    CloseableHttpResponse response = null;
    Response resp = null;
    try {
        response = httpclient.execute(httpget);

        HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream is = entity.getContent();
            long isl = entity.getContentLength();
            resp = new Response(Status.lookup(response.getStatusLine().getStatusCode()), null, is, isl);
        } else {
            resp = new Response(Status.lookup(response.getStatusLine().getStatusCode()), null, null, 0);
        }

        Header[] remoteHeaders = response.getAllHeaders();
        for (Header h : remoteHeaders) {
            resp.addHeader(h.getName(), h.getValue());
        }

        resp.setProxyResponse(response);

    } catch (IOException e) {
        Log.warn(e);
        return newFixedLengthResponse(Status.INTERNAL_ERROR, NanoHttpService.MIME_PLAINTEXT,
                "SERVER INTERNAL ERROR: IOException" + e.getMessage());
    }

    return resp;
}

From source file:org.esigate.Driver.java

/**
 * Performs rendering on an HttpResponse.
 * <p>/*from   www .j a v a 2  s. c o m*/
 * Rendering is only performed if page can be parsed.
 * 
 * @param pageUrl
 *            The remove url from which the body was retrieved.
 * @param originalRequest
 *            The request received by esigate.
 * @param response
 *            The response which will be rendered.
 * @param renderers
 *            list of renderers to apply.
 * @return The rendered response, or the original response if if was not parsed.
 * @throws HttpErrorPage
 * @throws IOException
 */
private CloseableHttpResponse performRendering(String pageUrl, DriverRequest originalRequest,
        CloseableHttpResponse response, Renderer[] renderers) throws HttpErrorPage, IOException {

    if (!contentTypeHelper.isTextContentType(response)) {
        LOG.debug("'{}' is binary on no transformation to apply: was forwarded without modification.", pageUrl);
        return response;
    }

    LOG.debug("'{}' is text : will apply renderers.", pageUrl);

    // Get response body
    String currentValue = HttpResponseUtils.toString(response, this.eventManager);

    // Perform rendering
    currentValue = performRendering(pageUrl, originalRequest, response, currentValue, renderers);

    // Generate the new response.
    HttpEntity transformedHttpEntity = new StringEntity(currentValue, ContentType.get(response.getEntity()));
    CloseableHttpResponse transformedResponse = BasicCloseableHttpResponse
            .adapt(new BasicHttpResponse(response.getStatusLine()));
    transformedResponse.setHeaders(response.getAllHeaders());
    transformedResponse.setEntity(transformedHttpEntity);
    return transformedResponse;

}

From source file:org.commonjava.propulsor.client.http.ClientHttpSupport.java

public Map<String, String> head(final String path, final int... responseCodes) throws ClientHttpException {
    connect();/*  w w  w . j  a v  a  2s.c om*/

    HttpHead request = null;
    CloseableHttpResponse response = null;
    CloseableHttpClient client = null;

    try {
        request = newJsonHead(buildUrl(baseUrl, path));
        client = newClient();
        response = client.execute(request);

        final StatusLine sl = response.getStatusLine();
        if (!validResponseCode(sl.getStatusCode(), responseCodes)) {
            if (sl.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
                return null;
            }

            throw new ClientHttpException(sl.getStatusCode(),
                    "Error executing HEAD: %s. Status was: %d %s (%s)", path, sl.getStatusCode(),
                    sl.getReasonPhrase(), sl.getProtocolVersion());
        }

        final Map<String, String> headers = new HashMap<>();
        for (final Header header : response.getAllHeaders()) {
            final String name = header.getName().toLowerCase();

            if (!headers.containsKey(name)) {
                headers.put(name, header.getValue());
            }
        }

        return headers;
    } catch (final IOException e) {
        throw new ClientHttpException("Client request failed: %s", e, e.getMessage());
    } finally {
        cleanupResources(request, response, client);
    }
}

From source file:org.commonjava.indy.client.core.IndyClientHttp.java

public Map<String, String> head(final String path, final int... responseCodes) throws IndyClientException {
    connect();//from www  .  ja va 2  s .c o  m

    HttpHead request = null;
    CloseableHttpResponse response = null;
    CloseableHttpClient client = null;

    try {
        request = newJsonHead(buildUrl(baseUrl, path));
        client = newClient();
        response = client.execute(request, newContext());

        final StatusLine sl = response.getStatusLine();
        if (!validResponseCode(sl.getStatusCode(), responseCodes)) {
            if (sl.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
                return null;
            }

            throw new IndyClientException(sl.getStatusCode(),
                    "Error executing HEAD: %s. Status was: %d %s (%s)", path, sl.getStatusCode(),
                    sl.getReasonPhrase(), sl.getProtocolVersion());
        }

        final Map<String, String> headers = new HashMap<>();
        for (final Header header : response.getAllHeaders()) {
            final String name = header.getName().toLowerCase();

            if (!headers.containsKey(name)) {
                headers.put(name, header.getValue());
            }
        }

        return headers;
    } catch (final IOException e) {
        throw new IndyClientException("Indy request failed: %s", e, e.getMessage());
    } finally {
        cleanupResources(request, response, client);
    }
}

From source file:PostTest.PostTest.java

public Map<?, ?> getGetData(String url, Object object) throws UnsupportedEncodingException, IOException {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    Map returnMap = new HashMap();
    try {//from  w w  w .  j  ava2s  .c om
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpResponse response = httpclient.execute(httpGet);
        try {
            int status = response.getStatusLine().getStatusCode();
            returnMap.put("status", status);
            // ?
            if (status == HttpStatus.SC_OK) {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    // start ??
                    InputStream is = entity.getContent();
                    BufferedReader in = new BufferedReader(new InputStreamReader(is));
                    StringBuffer buffer = new StringBuffer();
                    String line = "";
                    while ((line = in.readLine()) != null) {
                        buffer.append(line);
                    }
                    // end ??
                    returnMap.put("data", buffer.toString());
                }

            }
            // 
            else {
                returnMap.put("StatusCode", response.getStatusLine().getStatusCode());
                returnMap.put("ReasonPhrase", response.getStatusLine().getReasonPhrase());
                Header[] headers = response.getAllHeaders();
                for (Header header : headers) {
                    returnMap.put(header.getName(), header.getValue());
                }
            }

        } finally {
            response.close();
        }
    } finally {
        httpclient.close();
    }
    return returnMap;
}

From source file:PostTest.PostTest.java

public Map<?, ?> getGetData(String url) throws UnsupportedEncodingException, IOException {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    Map returnMap = new HashMap();
    try {/*from   w w  w  . ja  v  a2 s.  c  om*/
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpResponse response = httpclient.execute(httpGet);
        try {
            int status = response.getStatusLine().getStatusCode();
            returnMap.put("status", status);
            // ?
            if (status == HttpStatus.SC_OK) {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    // start ??
                    InputStream is = entity.getContent();
                    BufferedReader in = new BufferedReader(new InputStreamReader(is));
                    StringBuffer buffer = new StringBuffer();
                    String line = "";
                    while ((line = in.readLine()) != null) {
                        buffer.append(line);
                    }
                    // end ??
                    returnMap.put("data", buffer.toString());
                }

            }
            // 
            else {
                returnMap.put("StatusCode", response.getStatusLine().getStatusCode());
                returnMap.put("ReasonPhrase", response.getStatusLine().getReasonPhrase());
                Header[] headers = response.getAllHeaders();
                for (Header header : headers) {
                    returnMap.put(header.getName(), header.getValue());
                }
            }

        } finally {
            response.close();
        }
    } finally {
        httpclient.close();
    }
    return returnMap;
}

From source file:PostTest.PostTest.java

public Map getPostData(String url, Map<String, String> para) throws ClientProtocolException, IOException {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    Map returnMap = new HashMap();
    try {/* w  w w .ja va  2 s.  com*/
        HttpPost httppost = new HttpPost(url);
        try {
            if (para != null) {
                List pl = new ArrayList();
                for (Iterator iterator = para.keySet().iterator(); iterator.hasNext();) {
                    String key = (String) iterator.next();

                    Object ov = para.get(key);
                    if (ov instanceof Integer) {
                        ov = String.valueOf(ov);
                    }

                    pl.add(new BasicNameValuePair(key, ov + ""));
                }
                httppost.setEntity(new UrlEncodedFormEntity(pl));
            }
        } catch (Throwable e) {
            throw new RuntimeException("para=" + para, e);
        }
        CloseableHttpResponse response = httpclient.execute(httppost);
        try {
            int status = response.getStatusLine().getStatusCode();
            returnMap.put("status", status);
            // ?
            if (status == HttpStatus.SC_OK) {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    // start ??
                    InputStream is = entity.getContent();
                    BufferedReader in = new BufferedReader(new InputStreamReader(is));
                    StringBuffer buffer = new StringBuffer();
                    String line = "";
                    while ((line = in.readLine()) != null) {
                        buffer.append(line);
                    }
                    // end ??
                    returnMap.put("data", buffer.toString());
                }

            }
            // 
            else {
                returnMap.put("StatusCode", response.getStatusLine().getStatusCode());
                returnMap.put("ReasonPhrase", response.getStatusLine().getReasonPhrase());
                Header[] headers = response.getAllHeaders();
                for (Header header : headers) {
                    returnMap.put(header.getName(), header.getValue());
                }
            }
            // System.out.println( EntityUtils.toString(response.getEntity()));

        } finally {
            response.close();
        }
    } finally {
        httpclient.close();
    }
    return returnMap;
}

From source file:org.aliuge.crawler.fetcher.DefaultFetcher.java

public PageFetchResult fetch(WebURL webUrl, boolean proxy) {
    PageFetchResult fetchResult = new PageFetchResult();

    String toFetchURL = webUrl.getUrl();
    HttpGet get = new HttpGet(toFetchURL);
    get.addHeader("Accept-Encoding", "gzip");
    get.addHeader("User-Agent", config.getAgent());

    RequestConfig requestConfig = null;//from w  w w  .  java  2s  .c  om
    CloseableHttpResponse response = null;

    synchronized (mutex) {
        long now = (new Date()).getTime();
        if (now - lastFetchTime < ((FetchConfig) config).getDelayBetweenRequests()) {
            try {
                Thread.sleep(((FetchConfig) config).getDelayBetweenRequests() - (now - lastFetchTime));
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        lastFetchTime = (new Date()).getTime();
    }
    int statusCode = 0;
    int count = 5;

    while (statusCode != HttpStatus.SC_OK && count-- > 0) {
        HttpHost proxyHost = null;
        if (proxy) {
            proxyHost = getProxyIp();

            if (proxyHost != null)
                requestConfig = RequestConfig.copy(defaultRequestConfig).setSocketTimeout(10000)
                        .setConnectTimeout(10000).setProxy(proxyHost).build();
        }

        get.setConfig(requestConfig);

        try {
            response = httpClient.execute(get);
            statusCode = response.getStatusLine().getStatusCode();
            fetchResult.setEntity(response.getEntity());
            fetchResult.setResponseHeaders(response.getAllHeaders());
        } catch (IOException e) {
            // e.printStackTrace();
            // log.info("Fatal transport error: " + e.getMessage()+
            // " while fetching " + toFetchURL + " (link found in doc #"+
            // webUrl.getParentDocid() + ")");
            addFailedProxy(proxyHost.toHostString());
            /*
             * if (null != get) get.abort();
             * fetchResult.setStatusCode(CustomFetchStatus
             * .FatalTransportError);
             */
            // return fetchResult;
        }
    }

    fetchResult.setStatusCode(statusCode);

    fetchResult.setFetchedUrl(toFetchURL);

    if (fetchResult.getStatusCode() == HttpStatus.SC_OK) {

        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());
            } else {
                size = -1;
            }
        }
        if (size > ((FetchConfig) config).getMaxDownloadSizePerPage()) {
            fetchResult.setStatusCode(CustomFetchStatus.PageTooBig);
            get.abort();
            return fetchResult;
        }
        // fetchResult.setStatusCode(HttpStatus.SC_OK);
        return fetchResult;
    }
    get.abort();

    fetchResult.setStatusCode(CustomFetchStatus.UnknownError);
    return fetchResult;
}

From source file:com.intuit.karate.http.apache.ApacheHttpClient.java

@Override
protected HttpResponse makeHttpRequest(HttpEntity entity, long startTime) {
    if (entity != null) {
        requestBuilder.setEntity(entity);
        requestBuilder.setHeader(entity.getContentType());
    }/*from  w  w  w  .j  av  a2  s . c  om*/
    HttpUriRequest httpRequest = requestBuilder.build();
    CloseableHttpClient client = clientBuilder.build();
    BasicHttpContext context = new BasicHttpContext();
    context.setAttribute(URI_CONTEXT_KEY, getRequestUri());
    CloseableHttpResponse httpResponse;
    byte[] bytes;
    try {
        httpResponse = client.execute(httpRequest, context);
        HttpEntity responseEntity = httpResponse.getEntity();
        InputStream is = responseEntity.getContent();
        bytes = IOUtils.toByteArray(is);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    long responseTime = getResponseTime(startTime);
    HttpResponse response = new HttpResponse();
    response.setTime(responseTime);
    response.setUri(getRequestUri());
    response.setBody(bytes);
    response.setStatus(httpResponse.getStatusLine().getStatusCode());
    for (Cookie c : cookieStore.getCookies()) {
        com.intuit.karate.http.Cookie cookie = new com.intuit.karate.http.Cookie(c.getName(), c.getValue());
        cookie.put(DOMAIN, c.getDomain());
        cookie.put(PATH, c.getPath());
        if (c.getExpiryDate() != null) {
            cookie.put(EXPIRES, c.getExpiryDate().getTime() + "");
        }
        cookie.put(PERSISTENT, c.isPersistent() + "");
        cookie.put(SECURE, c.isSecure() + "");
        response.addCookie(cookie);
    }
    for (Header header : httpResponse.getAllHeaders()) {
        response.addHeader(header.getName(), header.getValue());
    }
    return response;
}