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);
}
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());
}
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);
}
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);
}
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;
}
Aggregations