Search in sources :

Example 16 with RequestHeader

use of com.github.davidmoten.odata.client.RequestHeader in project odata-client by davidmoten.

the class ApacheHttpClientHttpService method getResponse.

private HttpResponse getResponse(List<RequestHeader> requestHeaders, HttpRequestBase request, InputStream content, int length, HttpRequestOptions options) {
    Preconditions.checkNotNull(options);
    log.debug("{} from url {}", request.getMethod(), request.getURI());
    log.debug("requestHeaders={}", requestHeaders);
    for (RequestHeader header : requestHeadersModifier.apply(toUrl(request), requestHeaders)) {
        request.addHeader(header.name(), header.value());
    }
    try {
        if (content != null && request instanceof HttpEntityEnclosingRequest) {
            ((HttpEntityEnclosingRequest) request).setEntity(new InputStreamEntity(content, length));
            log.debug("content={}", content);
        }
        RequestConfig config = com.github.davidmoten.odata.client.Util.nvl(request.getConfig(), RequestConfig.DEFAULT);
        Builder builder = // 
        RequestConfig.copy(config);
        options.requestConnectTimeoutMs().ifPresent(x -> builder.setConnectTimeout(x.intValue()));
        options.requestReadTimeoutMs().ifPresent(x -> builder.setSocketTimeout(x.intValue()));
        config = builder.build();
        request.setConfig(config);
        log.debug("executing request");
        try (CloseableHttpResponse response = client.execute(request)) {
            int statusCode = response.getStatusLine().getStatusCode();
            log.debug("executed request, code={}", statusCode);
            HttpEntity entity = response.getEntity();
            final byte[] bytes;
            if (entity == null) {
                bytes = null;
            } else {
                bytes = Util.read(entity.getContent());
            }
            if (log.isDebugEnabled()) {
                log.debug("response text=\n{}", bytes == null ? "null" : new String(bytes, StandardCharsets.UTF_8));
            }
            return new HttpResponse(statusCode, bytes);
        }
    } catch (IOException e) {
        throw new ClientException(e);
    }
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) HttpEntity(org.apache.http.HttpEntity) Builder(org.apache.http.client.config.RequestConfig.Builder) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpResponse(com.github.davidmoten.odata.client.HttpResponse) IOException(java.io.IOException) InputStreamEntity(org.apache.http.entity.InputStreamEntity) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) RequestHeader(com.github.davidmoten.odata.client.RequestHeader) ClientException(com.github.davidmoten.odata.client.ClientException)

Aggregations

RequestHeader (com.github.davidmoten.odata.client.RequestHeader)16 HttpResponse (com.github.davidmoten.odata.client.HttpResponse)13 ContextPath (com.github.davidmoten.odata.client.ContextPath)8 ClientException (com.github.davidmoten.odata.client.ClientException)6 HttpService (com.github.davidmoten.odata.client.HttpService)4 IOException (java.io.IOException)4 URL (java.net.URL)4 HttpURLConnection (java.net.HttpURLConnection)3 RequestOptions (com.github.davidmoten.odata.client.RequestOptions)2 InputStream (java.io.InputStream)2 MalformedURLException (java.net.MalformedURLException)2 ArrayList (java.util.ArrayList)2 Preconditions (com.github.davidmoten.guavamini.Preconditions)1 AuthenticationEndpoint (com.github.davidmoten.microsoft.authentication.AuthenticationEndpoint)1 Context (com.github.davidmoten.odata.client.Context)1 HttpMethod (com.github.davidmoten.odata.client.HttpMethod)1 HttpRequestOptions (com.github.davidmoten.odata.client.HttpRequestOptions)1 ODataEntityType (com.github.davidmoten.odata.client.ODataEntityType)1 ODataType (com.github.davidmoten.odata.client.ODataType)1 Path (com.github.davidmoten.odata.client.Path)1