use of com.github.davidmoten.odata.client.Path in project odata-client by davidmoten.
the class RequestHelper method uploader.
public static Optional<StreamUploaderSingleCall> uploader(ContextPath contextPath, ODataType item, String fieldName, HttpMethod method) {
Preconditions.checkNotNull(fieldName);
String editLink = (String) item.getUnmappedFields().get(fieldName + "@odata.mediaEditLink");
String contentType = (String) item.getUnmappedFields().get(fieldName + "@odata.mediaContentType");
if (editLink == null) {
return Optional.empty();
} else {
// TODO support relative editLink?
Context context = contextPath.context();
if (contentType == null) {
contentType = CONTENT_TYPE_APPLICATION_OCTET_STREAM;
}
Path path = new Path(editLink, contextPath.path().style());
return Optional.of(new StreamUploaderSingleCall(new ContextPath(context, path), contentType, method));
}
}
use of com.github.davidmoten.odata.client.Path in project odata-client by davidmoten.
the class Generator method writeEntityRequest.
private void writeEntityRequest(Schema schema, TEntityType entityType, Map<String, List<Action>> typeActions, Map<String, List<Function>> typeFunctions) {
EntityType t = new EntityType(entityType, names);
names.getDirectoryEntityRequest(schema).mkdirs();
// TODO only write out those requests needed
String simpleClassName = t.getSimpleClassNameEntityRequest();
Imports imports = new Imports(t.getFullClassNameEntityRequest());
Indent indent = new Indent();
StringWriter w = new StringWriter();
try (PrintWriter p = new PrintWriter(w)) {
p.format("package %s;\n\n", t.getPackageEntityRequest());
p.format(IMPORTSHERE);
p.format("@%s\n", imports.add(JsonIgnoreType.class));
// don't make class final because can get extended by EntitySet
p.format("public class %s extends %s {\n\n", simpleClassName, imports.add(EntityRequest.class) + "<" + imports.add(t.getFullClassNameEntity()) + ">");
indent.right();
// add constructor
//
p.format(//
"%spublic %s(%s contextPath, %s<%s> value) {\n", //
indent, //
simpleClassName, //
imports.add(ContextPath.class), imports.add(Optional.class), imports.add(Object.class));
//
p.format(//
"%ssuper(%s.class, contextPath, value, %s);\n", //
indent.right(), //
imports.add(t.getFullClassNameEntity()), t.isMediaEntityOrHasStreamProperty());
p.format("%s}\n", indent.left());
indent.left();
if (t.hasStream()) {
p.format("\n%s/**\n", indent);
p.format("%s * If returning a stream without using object metadata is not supported then", indent);
p.format("%s * returns {@code Optional.empty()}. Otherwise, returns a stream provider\n", indent);
p.format("%s * where the location of the stream is assumed to be the current path + {@code /$value}.\n", indent);
p.format("%s *\n", indent);
p.format("%s * @return StreamProvider if suitable metadata found otherwise returns\n", indent);
p.format("%s * {@code Optional.empty()}\n", indent);
p.format("%s */\n", indent);
p.format("%s@%s\n", indent, imports.add(JsonIgnore.class));
p.format("%spublic %s<%s> getStreamCurrentPath() {\n", indent, imports.add(Optional.class), imports.add(StreamProvider.class));
p.format("%sreturn %s.createStream(contextPath, null);\n", indent.right(), imports.add(RequestHelper.class));
p.format("%s}\n", indent.left());
}
// TODO also support navigation properties with complexTypes?
//
t.getNavigationProperties().stream().filter(x -> {
boolean isEntity = names.isEntityWithNamespace(names.getInnerType(names.getType(x)));
if (!isEntity) {
log("Unexpected entity with non-entity navigation property type: " + simpleClassName + "." + x.getName() + ". If you get this message then raise an issue on the github project for odata-client.");
}
return isEntity;
}).forEach(x -> {
indent.right();
final String returnClass;
String y = x.getType().get(0);
Schema sch = names.getSchema(names.getInnerType(y));
if (Names.isCollection(y)) {
returnClass = toClassName(x, imports);
} else {
returnClass = imports.add(names.getFullClassNameEntityRequestFromTypeWithNamespace(sch, y));
}
// if collection then add with id method (if has ids)
if (Names.isCollection(y)) {
// TODO use actual key name from metadata
String inner = names.getInnerType(y);
// TODO remove redundant check
if (names.isEntityWithNamespace(inner)) {
String entityRequestType = names.getFullClassNameEntityRequestFromTypeWithNamespace(sch, inner);
EntityType et = names.getEntityType(inner);
KeyInfo k = getKeyInfo(et, imports);
if (!k.isEmpty()) {
p.format("\n%spublic %s %s(%s) {\n", indent, imports.add(entityRequestType), Names.getIdentifier(x.getName()), k.typedParams);
p.format("%sreturn new %s(contextPath.addSegment(\"%s\")%s, %s.empty());\n", indent.right(), imports.add(entityRequestType), x.getName(), k.addKeys, imports.add(Optional.class));
p.format("%s}\n", indent.left());
}
}
}
//
p.format(//
"\n%spublic %s %s() {\n", //
indent, //
returnClass, Names.getGetterMethodWithoutGet(x.getName()));
if (isCollection(x)) {
p.format("%sreturn new %s(\n", indent.right(), toClassName(x, imports));
p.format("%scontextPath.addSegment(\"%s\"), %s.empty());\n", indent.right().right().right().right(), x.getName(), imports.add(Optional.class));
indent.left().left().left().left();
} else {
p.format("%sreturn new %s(contextPath.addSegment(\"%s\"), %s.empty());\n", indent.right(), returnClass, x.getName(), imports.add(Optional.class));
}
p.format("%s}\n", indent.left());
indent.left();
});
indent.right();
Set<String> methodNames = new HashSet<>();
writeBoundActionMethods(t, typeActions, imports, indent, p, methodNames);
writeBoundFunctionMethods(t, typeFunctions, imports, indent, p, methodNames);
indent.left();
p.format("\n}\n");
writeToFile(imports, w, t.getClassFileEntityRequest());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of com.github.davidmoten.odata.client.Path 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.Path 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.Path in project odata-client by davidmoten.
the class MicrosoftClientBuilder method createService.
@SuppressWarnings("resource")
private static <T> T createService(String baseUrl, Authenticator authenticator, //
long connectTimeoutMs, //
long readTimeoutMs, //
Optional<String> proxyHost, //
Optional<Integer> proxyPort, Optional<String> proxyUsername, Optional<String> proxyPassword, Optional<Supplier<CloseableHttpClient>> supplier, Optional<Function<HttpClientBuilder, HttpClientBuilder>> httpClientBuilderExtras, //
Creator<T> creator, //
String authenticationEndpoint, //
Function<? super HttpService, ? extends HttpService> httpServiceTransformer, List<SchemaInfo> schemas, PathStyle pathStyle) {
final Supplier<CloseableHttpClient> clientSupplier = createClientSupplier(connectTimeoutMs, readTimeoutMs, proxyHost, proxyPort, proxyUsername, proxyPassword, supplier, httpClientBuilderExtras);
Path basePath = new Path(baseUrl, pathStyle);
HttpService httpService = new //
ApacheHttpClientHttpService(//
basePath, //
clientSupplier, authenticator::authenticate);
httpService = httpServiceTransformer.apply(httpService);
return creator.create(new Context(Serializer.INSTANCE, httpService, createProperties(), schemas));
}
Aggregations