Search in sources :

Example 1 with TypeIdAnnotation

use of org.mule.metadata.api.annotation.TypeIdAnnotation in project mule by mulesoft.

the class SoapInvokeOperationDeclarer method declareRequestParameters.

/**
 * Given the Invoke Operation Declarer declares the parameters for the soap request.
 *
 * @param operation the invoke operation declarer.
 * @param loader    a {@link ClassTypeLoader} to load some parameters types.
 */
private void declareRequestParameters(OperationDeclarer operation, ClassTypeLoader loader) {
    ParameterGroupDeclarer message = operation.onParameterGroup(MESSAGE_GROUP).withDslInlineRepresentation(true).withLayout(getLayout(1));
    MetadataType binaryType = loader.load(InputStream.class);
    ObjectType attachments = TYPE_BUILDER.objectType().openWith(TYPE_BUILDER.binaryType().id(InputStream.class.getName()).with(new TypedValueTypeAnnotation())).with(new TypeIdAnnotation(Map.class.getName())).build();
    message.withOptionalParameter(BODY_PARAM).ofDynamicType(binaryType).withRole(PRIMARY_CONTENT).defaultingTo(PAYLOAD).withLayout(getLayout(3)).withDisplayModel(DisplayModel.builder().summary("The XML body to include in the SOAP message, with all the required parameters.").build());
    message.withOptionalParameter(HEADERS_PARAM).ofDynamicType(binaryType).withRole(CONTENT).withLayout(getLayout(4)).withDisplayModel(DisplayModel.builder().displayName(HEADERS_DISPLAY_NAME).summary("The XML headers to include in the SOAP message.").build());
    message.withOptionalParameter(ATTACHMENTS_PARAM).ofDynamicType(attachments).withRole(CONTENT).withLayout(getLayout(5)).withDisplayModel(DisplayModel.builder().summary("The attachments to include in the SOAP request.").build());
    operation.onParameterGroup(TRANSPORT_GROUP).withLayout(getLayout(2)).withOptionalParameter(TRANSPORT_HEADERS_PARAM).ofType(TYPE_BUILDER.objectType().openWith(loader.load(String.class)).with(new TypeIdAnnotation(Map.class.getName())).build()).withDsl(ParameterDslConfiguration.getDefaultInstance()).withLayout(LayoutModel.builder().order(2).tabName(TRANSPORT).build()).withDisplayModel(DisplayModel.builder().displayName(HEADERS_DISPLAY_NAME).summary("The headers to set in the transport configuration.").build());
}
Also used : ObjectType(org.mule.metadata.api.model.ObjectType) InputStream(java.io.InputStream) TypedValueTypeAnnotation(org.mule.runtime.extension.api.declaration.type.annotation.TypedValueTypeAnnotation) MetadataType(org.mule.metadata.api.model.MetadataType) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ParameterGroupDeclarer(org.mule.runtime.api.meta.model.declaration.fluent.ParameterGroupDeclarer) TypeIdAnnotation(org.mule.metadata.api.annotation.TypeIdAnnotation)

Example 2 with TypeIdAnnotation

use of org.mule.metadata.api.annotation.TypeIdAnnotation in project mule by mulesoft.

the class NullSafeValueResolverWrapper method of.

/**
 * Creates a new instance
 *
 * @param delegate the {@link ValueResolver} to wrap
 * @param type the type of the value this resolver returns
 * @param reflectionCache the cache for expensive reflection lookups
 * @param muleContext the current {@link MuleContext}
 * @param <T> the generic type of the produced values
 * @return a new null safe {@link ValueResolver}
 * @throws IllegalParameterModelDefinitionException if used on parameters of not supported types
 */
public static <T> ValueResolver<T> of(ValueResolver<T> delegate, MetadataType type, ReflectionCache reflectionCache, MuleContext muleContext, ObjectTypeParametersResolver parametersResolver) {
    checkArgument(delegate != null, "delegate cannot be null");
    Reference<ValueResolver> wrappedResolver = new Reference<>();
    type.accept(new MetadataTypeVisitor() {

        @Override
        public void visitObject(ObjectType objectType) {
            Class clazz = getType(objectType);
            if (isMap(objectType)) {
                ValueResolver<?> fallback = MapValueResolver.of(clazz, emptyList(), emptyList(), reflectionCache, muleContext);
                wrappedResolver.set(new NullSafeValueResolverWrapper(delegate, fallback, muleContext));
                return;
            }
            String requiredFields = objectType.getFields().stream().filter(f -> f.isRequired() && !isFlattenedParameterGroup(f)).map(MetadataTypeUtils::getLocalPart).collect(joining(", "));
            if (!isBlank(requiredFields)) {
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug(format("Class '%s' cannot be used with NullSafe Wrapper since it contains non optional fields: [%s]", clazz.getName(), requiredFields));
                }
                wrappedResolver.set(delegate);
                return;
            }
            ResolverSet resolverSet = new ResolverSet(muleContext);
            for (Field field : getFields(clazz)) {
                ValueResolver<?> fieldResolver = null;
                ObjectFieldType objectField = objectType.getFieldByName(getAlias(field)).orElse(null);
                if (objectField == null) {
                    continue;
                }
                Optional<String> defaultValue = getDefaultValue(objectField);
                // TODO MULE-13066 Extract ParameterResolver logic into a centralized resolver
                if (defaultValue.isPresent()) {
                    fieldResolver = getFieldDefaultValueValueResolver(objectField, muleContext);
                } else if (isFlattenedParameterGroup(objectField)) {
                    DefaultObjectBuilder groupBuilder = new DefaultObjectBuilder<>(getType(objectField.getValue()));
                    resolverSet.add(field.getName(), new ObjectBuilderValueResolver<T>(groupBuilder, muleContext));
                    ObjectType childGroup = (ObjectType) objectField.getValue();
                    parametersResolver.resolveParameters(childGroup, groupBuilder);
                    parametersResolver.resolveParameterGroups(childGroup, groupBuilder);
                } else {
                    NullSafe nullSafe = field.getAnnotation(NullSafe.class);
                    if (nullSafe != null) {
                        MetadataType nullSafeType;
                        if (Object.class.equals(nullSafe.defaultImplementingType())) {
                            nullSafeType = objectField.getValue();
                        } else {
                            nullSafeType = new BaseTypeBuilder(JAVA).objectType().with(new TypeIdAnnotation(nullSafe.defaultImplementingType().getName())).build();
                        }
                        fieldResolver = NullSafeValueResolverWrapper.of(new StaticValueResolver<>(null), nullSafeType, reflectionCache, muleContext, parametersResolver);
                    }
                    if (field.getAnnotation(ConfigOverride.class) != null) {
                        ValueResolver<?> fieldDelegate = fieldResolver != null ? fieldResolver : new StaticValueResolver<>(null);
                        fieldResolver = ConfigOverrideValueResolverWrapper.of(fieldDelegate, field.getName(), reflectionCache, muleContext);
                    }
                }
                if (fieldResolver != null) {
                    resolverSet.add(field.getName(), fieldResolver);
                }
            }
            ObjectBuilder<T> objectBuilder = new DefaultResolverSetBasedObjectBuilder<T>(clazz, resolverSet);
            wrappedResolver.set(new NullSafeValueResolverWrapper(delegate, new ObjectBuilderValueResolver(objectBuilder, muleContext), muleContext));
        }

        @Override
        public void visitArrayType(ArrayType arrayType) {
            Class collectionClass = getType(arrayType);
            ValueResolver<?> fallback = CollectionValueResolver.of(collectionClass, emptyList());
            wrappedResolver.set(new NullSafeValueResolverWrapper(delegate, fallback, muleContext));
        }

        @Override
        protected void defaultVisit(MetadataType metadataType) {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug(format("Class '%s' cannot be used with NullSafe Wrapper since it is of a simple type", getType(metadataType).getName()));
            }
            wrappedResolver.set(delegate);
        }
    });
    return wrappedResolver.get();
}
Also used : ConfigOverride(org.mule.runtime.extension.api.annotation.param.ConfigOverride) MetadataTypeUtils.getDefaultValue(org.mule.metadata.api.utils.MetadataTypeUtils.getDefaultValue) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) LoggerFactory(org.slf4j.LoggerFactory) JAVA(org.mule.metadata.api.model.MetadataFormat.JAVA) Preconditions.checkArgument(org.mule.runtime.api.util.Preconditions.checkArgument) LifecycleUtils.initialiseIfNeeded(org.mule.runtime.core.api.lifecycle.LifecycleUtils.initialiseIfNeeded) BaseTypeBuilder(org.mule.metadata.api.builder.BaseTypeBuilder) IntrospectionUtils.getFields(org.mule.runtime.module.extension.internal.util.IntrospectionUtils.getFields) MetadataTypeUtils(org.mule.metadata.api.utils.MetadataTypeUtils) ExtensionMetadataTypeUtils.isFlattenedParameterGroup(org.mule.runtime.extension.api.util.ExtensionMetadataTypeUtils.isFlattenedParameterGroup) ArrayType(org.mule.metadata.api.model.ArrayType) MuleContext(org.mule.runtime.core.api.MuleContext) DefaultObjectBuilder(org.mule.runtime.module.extension.internal.runtime.objectbuilder.DefaultObjectBuilder) DefaultResolverSetBasedObjectBuilder(org.mule.runtime.module.extension.internal.runtime.objectbuilder.DefaultResolverSetBasedObjectBuilder) IntrospectionUtils.getAlias(org.mule.runtime.module.extension.internal.util.IntrospectionUtils.getAlias) MuleException(org.mule.runtime.api.exception.MuleException) TypeIdAnnotation(org.mule.metadata.api.annotation.TypeIdAnnotation) ResolverUtils.getFieldDefaultValueValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.ResolverUtils.getFieldDefaultValueValueResolver) ExtensionMetadataTypeUtils.isMap(org.mule.runtime.extension.api.util.ExtensionMetadataTypeUtils.isMap) IllegalParameterModelDefinitionException(org.mule.runtime.extension.api.exception.IllegalParameterModelDefinitionException) ObjectType(org.mule.metadata.api.model.ObjectType) Logger(org.slf4j.Logger) Collections.emptyList(java.util.Collections.emptyList) ReflectionCache(org.mule.runtime.module.extension.internal.util.ReflectionCache) Initialisable(org.mule.runtime.api.lifecycle.Initialisable) Field(java.lang.reflect.Field) String.format(java.lang.String.format) Collectors.joining(java.util.stream.Collectors.joining) MetadataTypeVisitor(org.mule.metadata.api.visitor.MetadataTypeVisitor) ObjectBuilder(org.mule.runtime.module.extension.internal.runtime.objectbuilder.ObjectBuilder) ObjectFieldType(org.mule.metadata.api.model.ObjectFieldType) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) Reference(org.mule.runtime.api.util.Reference) NullSafe(org.mule.runtime.extension.api.annotation.param.NullSafe) MetadataType(org.mule.metadata.api.model.MetadataType) Optional(java.util.Optional) JavaTypeUtils.getType(org.mule.metadata.java.api.utils.JavaTypeUtils.getType) BaseTypeBuilder(org.mule.metadata.api.builder.BaseTypeBuilder) MetadataTypeVisitor(org.mule.metadata.api.visitor.MetadataTypeVisitor) TypeIdAnnotation(org.mule.metadata.api.annotation.TypeIdAnnotation) ArrayType(org.mule.metadata.api.model.ArrayType) ObjectType(org.mule.metadata.api.model.ObjectType) Field(java.lang.reflect.Field) ResolverUtils.getFieldDefaultValueValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.ResolverUtils.getFieldDefaultValueValueResolver) ConfigOverride(org.mule.runtime.extension.api.annotation.param.ConfigOverride) Optional(java.util.Optional) Reference(org.mule.runtime.api.util.Reference) ConfigOverride(org.mule.runtime.extension.api.annotation.param.ConfigOverride) MetadataType(org.mule.metadata.api.model.MetadataType) DefaultObjectBuilder(org.mule.runtime.module.extension.internal.runtime.objectbuilder.DefaultObjectBuilder) DefaultResolverSetBasedObjectBuilder(org.mule.runtime.module.extension.internal.runtime.objectbuilder.DefaultResolverSetBasedObjectBuilder) ObjectBuilder(org.mule.runtime.module.extension.internal.runtime.objectbuilder.ObjectBuilder) NullSafe(org.mule.runtime.extension.api.annotation.param.NullSafe) MetadataTypeUtils(org.mule.metadata.api.utils.MetadataTypeUtils) ObjectFieldType(org.mule.metadata.api.model.ObjectFieldType) DefaultObjectBuilder(org.mule.runtime.module.extension.internal.runtime.objectbuilder.DefaultObjectBuilder)

Example 3 with TypeIdAnnotation

use of org.mule.metadata.api.annotation.TypeIdAnnotation in project mule by mulesoft.

the class NullSafeModelValidator method validate.

@Override
public void validate(ExtensionModel extensionModel, ProblemsReporter problemsReporter) {
    ReflectionCache reflectionCache = new ReflectionCache();
    TypeLoader typeLoader = ExtensionsTypeLoaderFactory.getDefault().createTypeLoader();
    new ExtensionWalker() {

        @Override
        public void onParameter(ParameterizedModel owner, ParameterGroupModel groupModel, ParameterModel model) {
            model.getType().accept(new MetadataTypeVisitor() {

                @Override
                public void visitObject(ObjectType objectType) {
                    if (objectType.getMetadataFormat().equals(JAVA) && !isMap(objectType)) {
                        objectType.getAnnotation(TypeIdAnnotation.class).map(TypeIdAnnotation::getValue).ifPresent(typeId -> typeLoader.load(typeId).ifPresent(fieldMetadataType -> objectType.getFields().stream().filter(f -> f.getAnnotation(NullSafeTypeAnnotation.class).isPresent()).forEach(f -> validateField(getLocalPart(f), f, getType(fieldMetadataType), f.getAnnotation(NullSafeTypeAnnotation.class).get()))));
                    }
                }

                private void validateField(String fieldName, ObjectFieldType field, Class<?> declaringClass, NullSafeTypeAnnotation nullSafeTypeAnnotation) {
                    Class<?> nullSafeType = nullSafeTypeAnnotation.getType();
                    Class<?> fieldType = getType(field.getValue());
                    boolean hasDefaultOverride = nullSafeTypeAnnotation.hasDefaultOverride();
                    field.getValue().accept(new BasicTypeMetadataVisitor() {

                        @Override
                        protected void visitBasicType(MetadataType metadataType) {
                            problemsReporter.addError(new Problem(extensionModel, format("Field '%s' in class '%s' is annotated with '@%s' but is of type '%s'. That annotation can only be " + "used with complex types (Pojos, Lists, Maps)", fieldName, declaringClass.getName(), NullSafe.class.getSimpleName(), fieldType.getName())));
                        }

                        @Override
                        public void visitArrayType(ArrayType arrayType) {
                            if (hasDefaultOverride) {
                                problemsReporter.addError(new Problem(extensionModel, format("Field '%s' in class '%s' is annotated with '@%s' is of type '%s'" + " but a 'defaultImplementingType' was provided." + " Type override is not allowed for Collections", fieldName, declaringClass.getName(), NullSafe.class.getSimpleName(), fieldType.getName())));
                            }
                        }

                        @Override
                        public void visitObject(ObjectType objectType) {
                            String requiredFields = objectType.getFields().stream().filter(f -> f.isRequired() && !isFlattenedParameterGroup(f)).map(MetadataTypeUtils::getLocalPart).collect(joining(", "));
                            if (!isBlank(requiredFields) && isCompiletime(extensionModel)) {
                                problemsReporter.addError(new Problem(model, format("Class '%s' cannot be used with '@%s' parameter since it contains non optional fields: [%s]", getId(objectType).orElse(""), NullSafe.class.getSimpleName(), requiredFields)));
                            }
                            if (objectType.isOpen()) {
                                if (hasDefaultOverride) {
                                    problemsReporter.addError(new Problem(model, format("Field '%s' in class '%s' is annotated with '@%s' is of type '%s'" + " but a 'defaultImplementingType' was provided." + " Type override is not allowed for Maps", fieldName, declaringClass.getName(), NullSafe.class.getSimpleName(), fieldType.getName())));
                                }
                                return;
                            }
                            if (hasDefaultOverride && isInstantiable(fieldType, reflectionCache)) {
                                problemsReporter.addError(new Problem(model, format("Field '%s' in class '%s' is annotated with '@%s' is of concrete type '%s'," + " but a 'defaultImplementingType' was provided." + " Type override is not allowed for concrete types", fieldName, declaringClass.getName(), NullSafe.class.getSimpleName(), fieldType.getName())));
                            }
                            if (!isInstantiable(nullSafeType, reflectionCache)) {
                                problemsReporter.addError(new Problem(model, format("Field '%s' in class '%s' is annotated with '@%s' but is of type '%s'. That annotation can only be " + "used with complex instantiable types (Pojos, Lists, Maps)", fieldName, declaringClass.getName(), NullSafe.class.getSimpleName(), nullSafeType.getName())));
                            }
                            if (hasDefaultOverride && !fieldType.isAssignableFrom(nullSafeType)) {
                                problemsReporter.addError(new Problem(model, format("Field '%s' in class '%s' is annotated with '@%s' of type '%s', but provided type '%s" + " is not a subtype of the parameter's type", fieldName, declaringClass.getName(), NullSafe.class.getSimpleName(), fieldType.getName(), nullSafeType.getName())));
                            }
                        }
                    });
                }
            });
        }
    }.walk(extensionModel);
}
Also used : ExtensionMetadataTypeUtils.getId(org.mule.runtime.extension.api.util.ExtensionMetadataTypeUtils.getId) ParameterModel(org.mule.runtime.api.meta.model.parameter.ParameterModel) ExtensionsTypeLoaderFactory(org.mule.runtime.extension.api.declaration.type.ExtensionsTypeLoaderFactory) JAVA(org.mule.metadata.api.model.MetadataFormat.JAVA) MetadataTypeUtils(org.mule.metadata.api.utils.MetadataTypeUtils) ExtensionMetadataTypeUtils.isFlattenedParameterGroup(org.mule.runtime.extension.api.util.ExtensionMetadataTypeUtils.isFlattenedParameterGroup) ModelValidationUtils.isCompiletime(org.mule.runtime.module.extension.internal.loader.validation.ModelValidationUtils.isCompiletime) ArrayType(org.mule.metadata.api.model.ArrayType) BasicTypeMetadataVisitor(org.mule.metadata.api.visitor.BasicTypeMetadataVisitor) ParameterGroupModel(org.mule.runtime.api.meta.model.parameter.ParameterGroupModel) IntrospectionUtils.isInstantiable(org.mule.runtime.module.extension.internal.util.IntrospectionUtils.isInstantiable) Problem(org.mule.runtime.extension.api.loader.Problem) MetadataTypeUtils.getLocalPart(org.mule.metadata.api.utils.MetadataTypeUtils.getLocalPart) TypeIdAnnotation(org.mule.metadata.api.annotation.TypeIdAnnotation) ExtensionModelValidator(org.mule.runtime.extension.api.loader.ExtensionModelValidator) ExtensionMetadataTypeUtils.isMap(org.mule.runtime.extension.api.util.ExtensionMetadataTypeUtils.isMap) ObjectType(org.mule.metadata.api.model.ObjectType) ParameterizedModel(org.mule.runtime.api.meta.model.parameter.ParameterizedModel) ProblemsReporter(org.mule.runtime.extension.api.loader.ProblemsReporter) NullSafeTypeAnnotation(org.mule.runtime.extension.api.declaration.type.annotation.NullSafeTypeAnnotation) ReflectionCache(org.mule.runtime.module.extension.internal.util.ReflectionCache) String.format(java.lang.String.format) Collectors.joining(java.util.stream.Collectors.joining) MetadataTypeVisitor(org.mule.metadata.api.visitor.MetadataTypeVisitor) TypeLoader(org.mule.metadata.api.TypeLoader) ExtensionModel(org.mule.runtime.api.meta.model.ExtensionModel) ObjectFieldType(org.mule.metadata.api.model.ObjectFieldType) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) ExtensionWalker(org.mule.runtime.api.meta.model.util.ExtensionWalker) NullSafe(org.mule.runtime.extension.api.annotation.param.NullSafe) MetadataType(org.mule.metadata.api.model.MetadataType) JavaTypeUtils.getType(org.mule.metadata.java.api.utils.JavaTypeUtils.getType) ReflectionCache(org.mule.runtime.module.extension.internal.util.ReflectionCache) ExtensionWalker(org.mule.runtime.api.meta.model.util.ExtensionWalker) MetadataType(org.mule.metadata.api.model.MetadataType) TypeLoader(org.mule.metadata.api.TypeLoader) NullSafeTypeAnnotation(org.mule.runtime.extension.api.declaration.type.annotation.NullSafeTypeAnnotation) MetadataTypeVisitor(org.mule.metadata.api.visitor.MetadataTypeVisitor) MetadataTypeUtils(org.mule.metadata.api.utils.MetadataTypeUtils) BasicTypeMetadataVisitor(org.mule.metadata.api.visitor.BasicTypeMetadataVisitor) ArrayType(org.mule.metadata.api.model.ArrayType) ObjectType(org.mule.metadata.api.model.ObjectType) ParameterModel(org.mule.runtime.api.meta.model.parameter.ParameterModel) ParameterizedModel(org.mule.runtime.api.meta.model.parameter.ParameterizedModel) ParameterGroupModel(org.mule.runtime.api.meta.model.parameter.ParameterGroupModel) Problem(org.mule.runtime.extension.api.loader.Problem) ObjectFieldType(org.mule.metadata.api.model.ObjectFieldType)

Aggregations

TypeIdAnnotation (org.mule.metadata.api.annotation.TypeIdAnnotation)3 MetadataType (org.mule.metadata.api.model.MetadataType)3 ObjectType (org.mule.metadata.api.model.ObjectType)3 String.format (java.lang.String.format)2 Collectors.joining (java.util.stream.Collectors.joining)2 StringUtils.isBlank (org.apache.commons.lang3.StringUtils.isBlank)2 ArrayType (org.mule.metadata.api.model.ArrayType)2 JAVA (org.mule.metadata.api.model.MetadataFormat.JAVA)2 ObjectFieldType (org.mule.metadata.api.model.ObjectFieldType)2 MetadataTypeUtils (org.mule.metadata.api.utils.MetadataTypeUtils)2 MetadataTypeVisitor (org.mule.metadata.api.visitor.MetadataTypeVisitor)2 JavaTypeUtils.getType (org.mule.metadata.java.api.utils.JavaTypeUtils.getType)2 NullSafe (org.mule.runtime.extension.api.annotation.param.NullSafe)2 ExtensionMetadataTypeUtils.isFlattenedParameterGroup (org.mule.runtime.extension.api.util.ExtensionMetadataTypeUtils.isFlattenedParameterGroup)2 ExtensionMetadataTypeUtils.isMap (org.mule.runtime.extension.api.util.ExtensionMetadataTypeUtils.isMap)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 InputStream (java.io.InputStream)1 Field (java.lang.reflect.Field)1 Collections.emptyList (java.util.Collections.emptyList)1 Map (java.util.Map)1