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