Search in sources :

Example 16 with ContextPath

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

Aggregations

ContextPath (com.github.davidmoten.odata.client.ContextPath)16 HttpResponse (com.github.davidmoten.odata.client.HttpResponse)7 Path (com.github.davidmoten.odata.client.Path)7 RequestHeader (com.github.davidmoten.odata.client.RequestHeader)7 Context (com.github.davidmoten.odata.client.Context)6 HttpService (com.github.davidmoten.odata.client.HttpService)6 Optional (java.util.Optional)6 StreamUploaderSingleCall (com.github.davidmoten.odata.client.StreamUploaderSingleCall)5 IOException (java.io.IOException)5 PrintWriter (java.io.PrintWriter)5 StringWriter (java.io.StringWriter)5 UncheckedIOException (java.io.UncheckedIOException)5 JsonIgnore (com.fasterxml.jackson.annotation.JsonIgnore)4 ClientException (com.github.davidmoten.odata.client.ClientException)4 JacksonInject (com.fasterxml.jackson.annotation.JacksonInject)3 JsonAnyGetter (com.fasterxml.jackson.annotation.JsonAnyGetter)3 JsonAnySetter (com.fasterxml.jackson.annotation.JsonAnySetter)3 JsonIgnoreType (com.fasterxml.jackson.annotation.JsonIgnoreType)3 JsonInclude (com.fasterxml.jackson.annotation.JsonInclude)3 Include (com.fasterxml.jackson.annotation.JsonInclude.Include)3