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

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

Introduction

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

Prototype

HeaderIterator headerIterator();

Source Link

Usage

From source file:com.gsma.mobileconnect.utils.RestClient.java

/**
 * Builds a RestResponse from the given HttpResponse
 *
 * @param request The original request./*from www  . j a v  a2s. com*/
 * @param closeableHttpResponse The HttpResponse to build the RestResponse for.
 * @return The RestResponse of the HttpResponse
 * @throws IOException
 */
private RestResponse buildRestResponse(HttpRequestBase request, CloseableHttpResponse closeableHttpResponse)
        throws IOException {
    String requestUri = request.getURI().toString();
    int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();

    HeaderIterator headers = closeableHttpResponse.headerIterator();
    List<KeyValuePair> headerList = new ArrayList<KeyValuePair>(3);
    while (headers.hasNext()) {
        Header header = headers.nextHeader();
        headerList.add(new KeyValuePair(header.getName(), header.getValue()));
    }

    HttpEntity httpEntity = closeableHttpResponse.getEntity();
    String responseData = EntityUtils.toString(httpEntity);

    return new RestResponse(requestUri, statusCode, headerList, responseData);
}

From source file:org.opendaylight.infrautils.diagstatus.shell.HttpClient.java

public HttpResponse sendRequest(HttpRequest request) throws Exception {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    if (httpclient == null) {
        throw new ClientProtocolException("Couldn't create an HTTP client");
    }//from  ww w.j  av a2 s.c  o m
    RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(request.getTimeout())
            .setConnectTimeout(request.getTimeout()).build();
    HttpRequestBase httprequest;
    String method = request.getMethod();
    if (method.equalsIgnoreCase("GET")) {
        httprequest = new HttpGet(request.getUri());
    } else if (method.equalsIgnoreCase("POST")) {
        httprequest = new HttpPost(request.getUri());
        if (request.getEntity() != null) {
            StringEntity sentEntity = new StringEntity(request.getEntity());
            sentEntity.setContentType(request.getContentType());
            ((HttpEntityEnclosingRequestBase) httprequest).setEntity(sentEntity);
        }
    } else if (method.equalsIgnoreCase("PUT")) {
        httprequest = new HttpPut(request.getUri());
        if (request.getEntity() != null) {
            StringEntity sentEntity = new StringEntity(request.getEntity());
            sentEntity.setContentType(request.getContentType());
            ((HttpEntityEnclosingRequestBase) httprequest).setEntity(sentEntity);
        }
    } else if (method.equalsIgnoreCase("DELETE")) {
        httprequest = new HttpDelete(request.getUri());
    } else {
        httpclient.close();
        throw new IllegalArgumentException(
                "This profile class only supports GET, POST, PUT, and DELETE methods");
    }
    httprequest.setConfig(requestConfig);
    // add request headers
    Iterator<String> headerIterator = request.getHeaders().keySet().iterator();
    while (headerIterator.hasNext()) {
        String header = headerIterator.next();
        Iterator<String> valueIterator = request.getHeaders().get(header).iterator();
        while (valueIterator.hasNext()) {
            httprequest.addHeader(header, valueIterator.next());
        }
    }
    CloseableHttpResponse response = httpclient.execute(httprequest);
    try {
        int httpResponseCode = response.getStatusLine().getStatusCode();
        HashMap<String, List<String>> headerMap = new HashMap<>();
        // copy response headers
        HeaderIterator it = response.headerIterator();
        while (it.hasNext()) {
            Header nextHeader = it.nextHeader();
            String name = nextHeader.getName();
            String value = nextHeader.getValue();
            if (headerMap.containsKey(name)) {
                headerMap.get(name).add(value);
            } else {
                List<String> list = new ArrayList<>();
                list.add(value);
                headerMap.put(name, list);
            }
        }
        if (httpResponseCode > 299) {
            return new HttpResponse(httpResponseCode, response.getStatusLine().getReasonPhrase(), headerMap);
        }
        Optional<HttpEntity> receivedEntity = Optional.ofNullable(response.getEntity());
        String httpBody = receivedEntity.isPresent() ? EntityUtils.toString(receivedEntity.get()) : null;
        return new HttpResponse(response.getStatusLine().getStatusCode(), httpBody, headerMap);
    } finally {
        response.close();
    }
}