Search in sources :

Example 1 with Context

use of com.github.davidmoten.odata.client.Context 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));
    }
}
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) StreamUploaderSingleCall(com.github.davidmoten.odata.client.StreamUploaderSingleCall)

Example 2 with Context

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

the class Generator method writeContainer.

private void writeContainer(Schema schema, TEntityContainer t) {
    names.getDirectoryContainer(schema).mkdirs();
    String simpleClassName = names.getSimpleClassNameContainer(schema, t.getName());
    Imports imports = new Imports(names.getFullClassNameContainer(schema, t.getName()));
    Indent indent = new Indent();
    StringWriter w = new StringWriter();
    try (PrintWriter p = new PrintWriter(w)) {
        p.format("package %s;\n\n", names.getPackageContainer(schema));
        p.format(IMPORTSHERE);
        final String extension;
        if (t.getExtends() != null) {
            extension = " extends " + imports.add(names.getFullClassNameFromTypeWithNamespace(t.getExtends()));
        } else {
            extension = "";
        }
        p.format("public final class %s%s implements %s {\n\n", simpleClassName, extension, imports.add(HasContext.class));
        // TODO handle container extension
        // write fields
        p.format("%sprivate final %s contextPath;\n\n", indent.right(), imports.add(ContextPath.class));
        // write constructor
        p.format("%spublic %s(%s context) {\n", indent, simpleClassName, imports.add(Context.class));
        p.format("%sthis.contextPath = new %s(context, context.service().getBasePath());\n", indent.right(), imports.add(ContextPath.class));
        p.format("%s}\n", indent.left());
        p.format("\n%s@%s\n", indent, imports.add(Override.class));
        p.format("%spublic %s _context() {\n", indent, imports.add(Context.class));
        p.format("%sreturn contextPath.context();\n", indent.right());
        p.format("%s}\n", indent.left());
        p.format("\n%spublic %s _service() {\n", indent, imports.add(HttpService.class));
        p.format("%sreturn contextPath.context().service();\n", indent.right());
        p.format("%s}\n", indent.left());
        // write static testing method
        p.format("\n%sstatic final class ContainerBuilderImpl extends %s<%s> {\n", indent, imports.add(ContainerBuilder.class), simpleClassName);
        p.format("\n%s@%s\n", indent.right(), imports.add(Override.class));
        p.format("%spublic %s _create(%s context) {\n", indent, simpleClassName, imports.add(Context.class));
        p.format("%sreturn new %s(context);\n", indent.right(), simpleClassName);
        p.format("%s}\n", indent.left());
        p.format("%s}\n", indent.left());
        p.format("\n%spublic static %s<%s<%s>, %s> test() {\n", indent, imports.add(BuilderBase.class), imports.add(ContainerBuilder.class), simpleClassName, simpleClassName);
        p.format("%sreturn new ContainerBuilderImpl();\n", indent.right());
        p.format("%s}\n", indent.left());
        // write get methods from properties
        // 
        Util.filter(t.getEntitySetOrActionImportOrFunctionImport(), TEntitySet.class).forEach(x -> {
            Schema sch = names.getSchema(x.getEntityType());
            boolean addedWithEmptyKeys = false;
            if (names.isEntityWithNamespace(x.getEntityType())) {
                String entityRequestType = names.getFullClassNameEntityRequestFromTypeWithNamespace(sch, x.getEntityType());
                EntityType et = names.getEntityType(x.getEntityType());
                KeyInfo k = getKeyInfo(et, imports);
                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());
                addedWithEmptyKeys = k.isEmpty();
            }
            if (!addedWithEmptyKeys) {
                // don't add a collection method if there were no keys for the entity
                EntitySet es = new EntitySet(schema, t, x, names);
                p.format("\n%spublic %s %s() {\n", indent, imports.add(es.getFullClassNameEntitySet()), Names.getIdentifier(x.getName()));
                p.format("%sreturn new %s(\n", indent.right(), imports.add(es.getFullClassNameEntitySet()));
                p.format("%scontextPath.addSegment(\"%s\"));\n", indent.right().right().right().right(), x.getName());
                p.format("%s}\n", indent.left().left().left().left().left());
            }
        });
        // 
        Util.filter(t.getEntitySetOrActionImportOrFunctionImport(), // 
        TSingleton.class).forEach(x -> {
            String importedType = toClassName(x, imports);
            p.format("\n%spublic %s %s() {\n", indent, importedType, Names.getIdentifier(x.getName()));
            p.format("%sreturn new %s(contextPath.addSegment(\"%s\"), %s.empty());\n", indent.right(), importedType, x.getName(), imports.add(Optional.class));
            p.format("%s}\n", indent.left());
        });
        // write unbound actions
        Set<String> methodNames = new HashSet<>();
        // 
        Util.types(schema, // 
        TAction.class).filter(x -> !x.isIsBound()).forEach(x -> writeAction(imports, indent, p, new Action(x, names), methodNames));
        // 
        Util.types(schema, // 
        TFunction.class).filter(x -> !x.isIsBound()).forEach(x -> writeFunction(imports, indent, p, new Function(x, names), methodNames));
        p.format("\n}\n");
        File classFile = names.getClassFileContainer(schema, t.getName());
        writeToFile(imports, w, classFile);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) StreamProvider(com.github.davidmoten.odata.client.StreamProvider) Arrays(java.util.Arrays) HasNameJavaHasNullable(com.github.davidmoten.odata.client.generator.model.HasNameJavaHasNullable) HasContext(com.github.davidmoten.odata.client.HasContext) HttpService(com.github.davidmoten.odata.client.HttpService) TEnumType(org.oasisopen.odata.csdl.v4.TEnumType) Map(java.util.Map) Parameter(com.github.davidmoten.odata.client.generator.model.Action.Parameter) PrintWriter(java.io.PrintWriter) FunctionRequestReturningNonCollection(com.github.davidmoten.odata.client.FunctionRequestReturningNonCollection) SchemaAndType(com.github.davidmoten.odata.client.generator.Names.SchemaAndType) JacksonInject(com.fasterxml.jackson.annotation.JacksonInject) TEntitySet(org.oasisopen.odata.csdl.v4.TEntitySet) NameValue(com.github.davidmoten.odata.client.NameValue) RequestHelper(com.github.davidmoten.odata.client.internal.RequestHelper) Set(java.util.Set) ODataType(com.github.davidmoten.odata.client.ODataType) StandardCharsets(java.nio.charset.StandardCharsets) UncheckedIOException(java.io.UncheckedIOException) Stream(java.util.stream.Stream) Lists(com.github.davidmoten.guavamini.Lists) CollectionPageEntityRequest(com.github.davidmoten.odata.client.CollectionPageEntityRequest) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) SchemaInfo(com.github.davidmoten.odata.client.SchemaInfo) TProperty(org.oasisopen.odata.csdl.v4.TProperty) Checks(com.github.davidmoten.odata.client.internal.Checks) Files(java.nio.file.Files) TNavigationProperty(org.oasisopen.odata.csdl.v4.TNavigationProperty) Function(com.github.davidmoten.odata.client.generator.model.Function) StringWriter(java.io.StringWriter) TSingleton(org.oasisopen.odata.csdl.v4.TSingleton) HttpMethod(com.github.davidmoten.odata.client.HttpMethod) IOException(java.io.IOException) Include(com.fasterxml.jackson.annotation.JsonInclude.Include) ContainerBuilder(com.github.davidmoten.odata.client.TestingService.ContainerBuilder) File(java.io.File) ChangedFields(com.github.davidmoten.odata.client.internal.ChangedFields) NavigationProperty(com.github.davidmoten.odata.client.annotation.NavigationProperty) TNavigationPropertyBinding(org.oasisopen.odata.csdl.v4.TNavigationPropertyBinding) RequestOptions(com.github.davidmoten.odata.client.RequestOptions) ParameterMap(com.github.davidmoten.odata.client.internal.ParameterMap) TEnumTypeMember(org.oasisopen.odata.csdl.v4.TEnumTypeMember) HttpRequestOptions(com.github.davidmoten.odata.client.HttpRequestOptions) CollectionPage(com.github.davidmoten.odata.client.CollectionPage) FieldName(com.github.davidmoten.odata.client.generator.model.Structure.FieldName) ActionRequestNoReturn(com.github.davidmoten.odata.client.ActionRequestNoReturn) Action(com.github.davidmoten.odata.client.generator.model.Action) Field(com.github.davidmoten.odata.client.generator.model.Field) UploadStrategy(com.github.davidmoten.odata.client.UploadStrategy) TComplexType(org.oasisopen.odata.csdl.v4.TComplexType) JsonAnyGetter(com.fasterxml.jackson.annotation.JsonAnyGetter) ActionRequestReturningNonCollection(com.github.davidmoten.odata.client.ActionRequestReturningNonCollection) ReturnType(com.github.davidmoten.odata.client.generator.model.Action.ReturnType) UnmappedFields(com.github.davidmoten.odata.client.UnmappedFields) Collectors(java.util.stream.Collectors) EntityType(com.github.davidmoten.odata.client.generator.model.EntityType) JsonAnySetter(com.fasterxml.jackson.annotation.JsonAnySetter) Context(com.github.davidmoten.odata.client.Context) List(java.util.List) KeyElement(com.github.davidmoten.odata.client.generator.model.KeyElement) Method(com.github.davidmoten.odata.client.generator.model.Method) PropertyRef(com.github.davidmoten.odata.client.generator.model.PropertyRef) Structure(com.github.davidmoten.odata.client.generator.model.Structure) StreamUploaderChunked(com.github.davidmoten.odata.client.StreamUploaderChunked) Optional(java.util.Optional) ClientException(com.github.davidmoten.odata.client.ClientException) ContextPath(com.github.davidmoten.odata.client.ContextPath) JsonPropertyOrder(com.fasterxml.jackson.annotation.JsonPropertyOrder) ComplexType(com.github.davidmoten.odata.client.generator.model.ComplexType) Preconditions(com.github.davidmoten.guavamini.Preconditions) TypedObject(com.github.davidmoten.odata.client.internal.TypedObject) HashMap(java.util.HashMap) TActionFunctionParameter(org.oasisopen.odata.csdl.v4.TActionFunctionParameter) HashSet(java.util.HashSet) TActionFunctionReturnType(org.oasisopen.odata.csdl.v4.TActionFunctionReturnType) TFunction(org.oasisopen.odata.csdl.v4.TFunction) EntityRequest(com.github.davidmoten.odata.client.EntityRequest) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) TAction(org.oasisopen.odata.csdl.v4.TAction) Path(com.github.davidmoten.odata.client.Path) Schema(org.oasisopen.odata.csdl.v4.Schema) StreamUploaderSingleCall(com.github.davidmoten.odata.client.StreamUploaderSingleCall) EntitySet(com.github.davidmoten.odata.client.generator.model.EntitySet) ODataEntityType(com.github.davidmoten.odata.client.ODataEntityType) BuilderBase(com.github.davidmoten.odata.client.TestingService.BuilderBase) ActionRequestReturningNonCollectionUnwrapped(com.github.davidmoten.odata.client.ActionRequestReturningNonCollectionUnwrapped) TEntityContainer(org.oasisopen.odata.csdl.v4.TEntityContainer) UnmappedFieldsImpl(com.github.davidmoten.odata.client.internal.UnmappedFieldsImpl) StreamUploader(com.github.davidmoten.odata.client.StreamUploader) JsonInclude(com.fasterxml.jackson.annotation.JsonInclude) FunctionRequestReturningStream(com.github.davidmoten.odata.client.FunctionRequestReturningStream) JsonIgnoreType(com.fasterxml.jackson.annotation.JsonIgnoreType) CollectionPageNonEntityRequest(com.github.davidmoten.odata.client.CollectionPageNonEntityRequest) FunctionRequestReturningNonCollectionUnwrapped(com.github.davidmoten.odata.client.FunctionRequestReturningNonCollectionUnwrapped) Property(com.github.davidmoten.odata.client.annotation.Property) Collections(java.util.Collections) TEntityType(org.oasisopen.odata.csdl.v4.TEntityType) Action(com.github.davidmoten.odata.client.generator.model.Action) TAction(org.oasisopen.odata.csdl.v4.TAction) Schema(org.oasisopen.odata.csdl.v4.Schema) Function(com.github.davidmoten.odata.client.generator.model.Function) TFunction(org.oasisopen.odata.csdl.v4.TFunction) ContextPath(com.github.davidmoten.odata.client.ContextPath) TEntitySet(org.oasisopen.odata.csdl.v4.TEntitySet) EntitySet(com.github.davidmoten.odata.client.generator.model.EntitySet) ContainerBuilder(com.github.davidmoten.odata.client.TestingService.ContainerBuilder) StringWriter(java.io.StringWriter) PrintWriter(java.io.PrintWriter) HashSet(java.util.HashSet) HasContext(com.github.davidmoten.odata.client.HasContext) Context(com.github.davidmoten.odata.client.Context) TSingleton(org.oasisopen.odata.csdl.v4.TSingleton) Optional(java.util.Optional) BuilderBase(com.github.davidmoten.odata.client.TestingService.BuilderBase) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) TEntitySet(org.oasisopen.odata.csdl.v4.TEntitySet) EntityType(com.github.davidmoten.odata.client.generator.model.EntityType) ODataEntityType(com.github.davidmoten.odata.client.ODataEntityType) TEntityType(org.oasisopen.odata.csdl.v4.TEntityType) HttpService(com.github.davidmoten.odata.client.HttpService) HasContext(com.github.davidmoten.odata.client.HasContext) File(java.io.File)

Example 3 with Context

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

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

use of com.github.davidmoten.odata.client.Context 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));
}
Also used : Path(com.github.davidmoten.odata.client.Path) Context(com.github.davidmoten.odata.client.Context) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpService(com.github.davidmoten.odata.client.HttpService) ApacheHttpClientHttpService(com.github.davidmoten.odata.client.internal.ApacheHttpClientHttpService) ApacheHttpClientHttpService(com.github.davidmoten.odata.client.internal.ApacheHttpClientHttpService)

Aggregations

Context (com.github.davidmoten.odata.client.Context)5 Path (com.github.davidmoten.odata.client.Path)5 ContextPath (com.github.davidmoten.odata.client.ContextPath)4 HttpService (com.github.davidmoten.odata.client.HttpService)2 StreamUploaderSingleCall (com.github.davidmoten.odata.client.StreamUploaderSingleCall)2 JacksonInject (com.fasterxml.jackson.annotation.JacksonInject)1 JsonAnyGetter (com.fasterxml.jackson.annotation.JsonAnyGetter)1 JsonAnySetter (com.fasterxml.jackson.annotation.JsonAnySetter)1 JsonIgnore (com.fasterxml.jackson.annotation.JsonIgnore)1 JsonIgnoreType (com.fasterxml.jackson.annotation.JsonIgnoreType)1 JsonInclude (com.fasterxml.jackson.annotation.JsonInclude)1 Include (com.fasterxml.jackson.annotation.JsonInclude.Include)1 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)1 JsonPropertyOrder (com.fasterxml.jackson.annotation.JsonPropertyOrder)1 Lists (com.github.davidmoten.guavamini.Lists)1 Preconditions (com.github.davidmoten.guavamini.Preconditions)1 ActionRequestNoReturn (com.github.davidmoten.odata.client.ActionRequestNoReturn)1 ActionRequestReturningNonCollection (com.github.davidmoten.odata.client.ActionRequestReturningNonCollection)1 ActionRequestReturningNonCollectionUnwrapped (com.github.davidmoten.odata.client.ActionRequestReturningNonCollectionUnwrapped)1 ClientException (com.github.davidmoten.odata.client.ClientException)1