Search in sources :

Example 11 with HttpResponse

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

the class RequestHelper method send.

public static void send(HttpMethod method, ContextPath contextPath, RequestOptions options, InputStream in, int length) {
    List<RequestHeader> h = cleanAndSupplementRequestHeaders(options, "minimal", true);
    ContextPath cp = contextPath.addQueries(options.getQueries());
    HttpService service = cp.context().service();
    final HttpResponse response = service.submit(method, cp.toUrl(), h, in, length, options);
    checkResponseCodeOk(cp, response);
}
Also used : ContextPath(com.github.davidmoten.odata.client.ContextPath) HttpService(com.github.davidmoten.odata.client.HttpService) RequestHeader(com.github.davidmoten.odata.client.RequestHeader) HttpResponse(com.github.davidmoten.odata.client.HttpResponse)

Example 12 with HttpResponse

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

the class RequestHelper method patchOrPut.

@SuppressWarnings("unused")
private static <T extends ODataEntityType> T patchOrPut(T entity, ContextPath contextPath, RequestOptions options, HttpMethod method) {
    Preconditions.checkArgument(method == HttpMethod.PUT || method == HttpMethod.PATCH);
    final String json;
    if (method == HttpMethod.PATCH) {
        json = Serializer.INSTANCE.serializeChangesOnly(entity);
    } else {
        json = Serializer.INSTANCE.serialize(entity);
    }
    // build the url
    ContextPath cp = contextPath.addQueries(options.getQueries());
    List<RequestHeader> h = cleanAndSupplementRequestHeaders(options, "minimal", true);
    final String url;
    String editLink = (String) entity.getUnmappedFields().get("@odata.editLink");
    // TODO get patch working when editLink present (does not work with MsGraph)
    if (editLink != null && false) {
        if (editLink.startsWith(HTTPS) || editLink.startsWith("http://")) {
            url = editLink;
        } else {
            // TOOD unit test relative url in editLink
            // from
            // http://docs.oasis-open.org/odata/odata-json-format/v4.01/cs01/odata-json-format-v4.01-cs01.html#_Toc499720582
            String context = (String) entity.getUnmappedFields().get("@odata.context");
            if (context != null) {
                try {
                    URL u = new URL(context);
                    String p = u.getPath();
                    String basePath = p.substring(0, p.lastIndexOf('/'));
                    url = basePath + "/" + editLink;
                } catch (MalformedURLException e) {
                    throw new ClientException(e);
                }
            } else {
                url = cp.context().service().getBasePath().toUrl() + "/" + editLink;
            }
        }
    } else {
        url = cp.toUrl();
    }
    // get the response
    HttpService service = cp.context().service();
    final HttpResponse response = service.submit(method, url, h, json, options);
    checkResponseCodeOk(cp, response);
    // original?
    return entity;
}
Also used : ContextPath(com.github.davidmoten.odata.client.ContextPath) MalformedURLException(java.net.MalformedURLException) HttpService(com.github.davidmoten.odata.client.HttpService) RequestHeader(com.github.davidmoten.odata.client.RequestHeader) HttpResponse(com.github.davidmoten.odata.client.HttpResponse) ClientException(com.github.davidmoten.odata.client.ClientException) URL(java.net.URL)

Example 13 with HttpResponse

use of com.github.davidmoten.odata.client.HttpResponse 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

HttpResponse (com.github.davidmoten.odata.client.HttpResponse)13 RequestHeader (com.github.davidmoten.odata.client.RequestHeader)12 ContextPath (com.github.davidmoten.odata.client.ContextPath)7 ClientException (com.github.davidmoten.odata.client.ClientException)3 HttpService (com.github.davidmoten.odata.client.HttpService)3 IOException (java.io.IOException)2 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 OutputStream (java.io.OutputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 MalformedURLException (java.net.MalformedURLException)1 ProtocolException (java.net.ProtocolException)1 List (java.util.List)1 GraphService (odata.msgraph.client.container.GraphService)1 HttpEntity (org.apache.http.HttpEntity)1 HttpEntityEnclosingRequest (org.apache.http.HttpEntityEnclosingRequest)1 RequestConfig (org.apache.http.client.config.RequestConfig)1 Builder (org.apache.http.client.config.RequestConfig.Builder)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 InputStreamEntity (org.apache.http.entity.InputStreamEntity)1