Search in sources :

Example 11 with ContextPath

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

the class RequestHelper method getWithParametricType.

public static <T, S> T getWithParametricType(ContextPath contextPath, Class<T> cls, Class<S> parametricTypeClass, RequestOptions options) {
    // build the url
    ContextPath cp = contextPath.addQueries(options.getQueries());
    List<RequestHeader> h = cleanAndSupplementRequestHeaders(options, "minimal", false);
    // get the response
    HttpResponse response = cp.context().service().get(cp.toUrl(), h, options);
    checkResponseCode(cp, response, HttpURLConnection.HTTP_OK);
    // deserialize
    String text = response.getText();
    Class<? extends T> c = getSubClass(cp, contextPath.context().schemas(), cls, text);
    // FileAttachment which is a subclass of Attachment)
    return cp.context().serializer().deserializeWithParametricType(text, c, parametricTypeClass, contextPath, false);
}
Also used : ContextPath(com.github.davidmoten.odata.client.ContextPath) RequestHeader(com.github.davidmoten.odata.client.RequestHeader) HttpResponse(com.github.davidmoten.odata.client.HttpResponse)

Example 12 with ContextPath

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

the class RequestHelper method createStreamForEdmStream.

public static Optional<StreamProvider> createStreamForEdmStream(ContextPath contextPath, ODataType item, String fieldName, String base64) {
    Preconditions.checkNotNull(fieldName);
    String readLink = (String) item.getUnmappedFields().get(fieldName + "@odata.mediaReadLink");
    String contentType = (String) item.getUnmappedFields().get(fieldName + "@odata.mediaContentType");
    if (readLink == null && base64 == null) {
        return Optional.empty();
    } else {
        if (contentType == null) {
            contentType = CONTENT_TYPE_APPLICATION_OCTET_STREAM;
        }
        // TODO support relative editLink?
        Context context = contextPath.context();
        // path won't be used if readLink is null because base64 is not null
        // $value is not appended for a stream property
        Path path = new Path(readLink, contextPath.path().style());
        return Optional.of(new // 
        StreamProvider(// 
        new ContextPath(context, path), // 
        RequestOptions.EMPTY, // 
        contentType, base64));
    }
}
Also used : Context(com.github.davidmoten.odata.client.Context) ContextPath(com.github.davidmoten.odata.client.ContextPath) Path(com.github.davidmoten.odata.client.Path) ContextPath(com.github.davidmoten.odata.client.ContextPath)

Example 13 with ContextPath

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

the class RequestHelper method createStream.

// for HasStream case (only for entities, not for complexTypes)
public static Optional<StreamProvider> createStream(ContextPath contextPath, ODataEntityType entity) {
    String editLink;
    String contentType;
    if (entity == null) {
        editLink = null;
        contentType = null;
    } else {
        editLink = (String) entity.getUnmappedFields().get("@odata.mediaEditLink");
        if (editLink == null) {
            editLink = (String) entity.getUnmappedFields().get("@odata.editLink");
        }
        contentType = (String) entity.getUnmappedFields().get("@odata.mediaContentType");
    }
    if (editLink == null && contextPath.context().propertyIsFalse(Properties.ATTEMPT_STREAM_WHEN_NO_METADATA)) {
        return Optional.empty();
    } else {
        if (contentType == null) {
            contentType = CONTENT_TYPE_APPLICATION_OCTET_STREAM;
        }
        // TODO support relative editLink?
        Context context = contextPath.context();
        if (editLink == null) {
            editLink = contextPath.toUrl();
        }
        if (!editLink.startsWith(HTTPS)) {
            // TODO should use the base path from @odata.context field?
            editLink = concatenate(contextPath.context().service().getBasePath().toUrl(), editLink);
        }
        if (contextPath.context().propertyIsTrue(Properties.MODIFY_STREAM_EDIT_LINK) && entity != null) {
            // Bug fix for Microsoft Graph only?
            // When a collection is returned the editLink is terminated with the subclass if
            // the collection type has subclasses. For example when a collection of
            // Attachment (with full metadata) is requested the editLink of an individual
            // attachment may end in /itemAttachment to indicate the type of the attachment.
            // To get the $value download working we need to remove that type cast.
            int i = endsWith(editLink, "/" + entity.odataTypeName());
            if (i == -1) {
                i = endsWith(editLink, "/" + entity.odataTypeName() + "/$value");
            }
            if (i == -1) {
                i = endsWith(editLink, "/" + entity.odataTypeName() + "/%24value");
            }
            if (i != -1) {
                editLink = editLink.substring(0, i);
            }
        }
        Path path = new Path(editLink, contextPath.path().style());
        if (!path.toUrl().endsWith("/$value")) {
            path = path.addSegment("$value");
        }
        return Optional.of(new // 
        StreamProvider(// 
        new ContextPath(context, path), // 
        RequestOptions.EMPTY, // 
        contentType, null));
    }
}
Also used : Context(com.github.davidmoten.odata.client.Context) ContextPath(com.github.davidmoten.odata.client.ContextPath) Path(com.github.davidmoten.odata.client.Path) ContextPath(com.github.davidmoten.odata.client.ContextPath)

Example 14 with ContextPath

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

the class RequestHelper method submitAny.

public static <T> T submitAny(HttpMethod method, Object object, ContextPath contextPath, Class<T> responseClass, RequestOptions options) {
    // build the url
    ContextPath cp = contextPath.addQueries(options.getQueries());
    String json;
    if (object == null) {
        json = "";
    } else {
        json = Serializer.INSTANCE.serialize(object);
    }
    List<RequestHeader> h = cleanAndSupplementRequestHeaders(options, "minimal", true);
    // get the response
    HttpResponse response = cp.context().service().post(cp.toUrl(), h, json, options);
    // TODO could be tightened to 201 for POST create but POST Action calls need to
    // accept any successful code
    checkResponseCodeOk(cp, response);
    String text = response.getText();
    // deserialize
    Class<? extends T> c = getSubClass(cp, contextPath.context().schemas(), responseClass, text);
    // FileAttachment which is a subclass of Attachment)
    return cp.context().serializer().deserialize(text, c, contextPath, false);
}
Also used : ContextPath(com.github.davidmoten.odata.client.ContextPath) RequestHeader(com.github.davidmoten.odata.client.RequestHeader) HttpResponse(com.github.davidmoten.odata.client.HttpResponse)

Example 15 with ContextPath

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

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