Search in sources :

Example 11 with RequestHeader

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

the class RequestHelper method sendChunk.

public static void sendChunk(HttpMethod method, HttpService service, String url, InputStream in, List<RequestHeader> requestHeaders, long startByte, long finishByte, long size, HttpRequestOptions options) {
    List<RequestHeader> h = new ArrayList<RequestHeader>(requestHeaders);
    h.add(RequestHeader.create("Content-Range", "bytes " + startByte + "-" + (finishByte - 1) + "/" + size));
    HttpResponse response = service.put(url, h, in, (int) (finishByte - startByte), options);
    checkResponseCode(url, response, 200, 204);
}
Also used : ArrayList(java.util.ArrayList) RequestHeader(com.github.davidmoten.odata.client.RequestHeader) HttpResponse(com.github.davidmoten.odata.client.HttpResponse)

Example 12 with RequestHeader

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

the class RequestHelper method cleanAndSupplementRequestHeaders.

public static List<RequestHeader> cleanAndSupplementRequestHeaders(List<RequestHeader> requestHeaders, String contentTypeOdataMetadataValue, boolean hasPayload) {
    List<RequestHeader> list = new ArrayList<>();
    list.add(RequestHeader.ODATA_VERSION);
    if (hasPayload) {
        if (!requestHeaders.stream().map(x -> x.name()).filter(x -> x.equals("Content-Type")).findFirst().isPresent()) {
            list.add(RequestHeader.CONTENT_TYPE_JSON);
        }
    }
    list.add(RequestHeader.ACCEPT_JSON);
    list.addAll(requestHeaders);
    // remove duplicates
    List<RequestHeader> list2 = new ArrayList<>();
    Set<RequestHeader> set = new HashSet<>();
    for (RequestHeader r : list) {
        if (!set.contains(r)) {
            list2.add(r);
        }
        set.add(r);
    }
    // remove overriden accept header
    if (list2.contains(RequestHeader.ACCEPT_JSON) && list2.stream().anyMatch(RequestHeader::isAcceptJsonWithMetadata)) {
        list2.remove(RequestHeader.ACCEPT_JSON);
    }
    // only use the last accept with metadata request header
    Optional<RequestHeader> m = // 
    list2.stream().filter(// 
    RequestHeader::isAcceptJsonWithMetadata).reduce((x, y) -> y);
    return list2.stream().filter(x -> !x.isAcceptJsonWithMetadata() || !m.isPresent() || x.equals(m.get())).collect(Collectors.toList());
}
Also used : HttpURLConnection(java.net.HttpURLConnection) ContextPath(com.github.davidmoten.odata.client.ContextPath) StreamProvider(com.github.davidmoten.odata.client.StreamProvider) URL(java.net.URL) Properties(com.github.davidmoten.odata.client.Properties) Preconditions(com.github.davidmoten.guavamini.Preconditions) HttpService(com.github.davidmoten.odata.client.HttpService) RequestHeader(com.github.davidmoten.odata.client.RequestHeader) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ByteArrayInputStream(java.io.ByteArrayInputStream) SchemaInfo(com.github.davidmoten.odata.client.SchemaInfo) Map(java.util.Map) Path(com.github.davidmoten.odata.client.Path) StreamUploaderSingleCall(com.github.davidmoten.odata.client.StreamUploaderSingleCall) MalformedURLException(java.net.MalformedURLException) UnmappedFields(com.github.davidmoten.odata.client.UnmappedFields) Set(java.util.Set) HttpMethod(com.github.davidmoten.odata.client.HttpMethod) ODataType(com.github.davidmoten.odata.client.ODataType) Serializer(com.github.davidmoten.odata.client.Serializer) ODataEntityType(com.github.davidmoten.odata.client.ODataEntityType) Collectors(java.util.stream.Collectors) Context(com.github.davidmoten.odata.client.Context) Base64(java.util.Base64) List(java.util.List) HttpResponse(com.github.davidmoten.odata.client.HttpResponse) Optional(java.util.Optional) RequestOptions(com.github.davidmoten.odata.client.RequestOptions) ClientException(com.github.davidmoten.odata.client.ClientException) HttpRequestOptions(com.github.davidmoten.odata.client.HttpRequestOptions) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) RequestHeader(com.github.davidmoten.odata.client.RequestHeader) HashSet(java.util.HashSet)

Example 13 with RequestHeader

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

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

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

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