Search in sources :

Example 16 with MetadataType

use of org.mule.metadata.api.model.MetadataType in project mule by mulesoft.

the class OperationModelLoaderDelegate method processBlockingOperation.

private void processBlockingOperation(boolean supportsConfig, MethodElement operationMethod, OperationDeclarer operation) {
    operation.blocking(true);
    operation.withOutputAttributes().ofType(operationMethod.getAttributesMetadataType());
    final MetadataType outputType = operationMethod.getReturnMetadataType();
    if (isAutoPaging(operationMethod)) {
        operation.supportsStreaming(true).withOutput().ofType(outputType);
        addPagedOperationModelProperty(operationMethod, operation, supportsConfig);
        processPagingTx(operation, operationMethod);
    } else {
        operation.withOutput().ofType(outputType);
        handleByteStreaming(operationMethod, operation, outputType);
    }
}
Also used : MetadataType(org.mule.metadata.api.model.MetadataType)

Example 17 with MetadataType

use of org.mule.metadata.api.model.MetadataType in project mule by mulesoft.

the class ParameterModelsLoaderDelegate method parseNullSafe.

private void parseNullSafe(ExtensionParameter extensionParameter, ParameterDeclarer parameter) {
    if (extensionParameter.isAnnotatedWith(NullSafe.class)) {
        if (extensionParameter.isAnnotatedWith(ConfigOverride.class)) {
            throw new IllegalParameterModelDefinitionException(format("Parameter '%s' is annotated with '@%s' and also marked as a config override, which is redundant. " + "The default value for this parameter will come from the configuration parameter", extensionParameter.getName(), NullSafe.class.getSimpleName()));
        }
        if (extensionParameter.isRequired() && !extensionParameter.isAnnotatedWith(ParameterGroup.class)) {
            throw new IllegalParameterModelDefinitionException(format("Parameter '%s' is required but annotated with '@%s', which is redundant", extensionParameter.getName(), NullSafe.class.getSimpleName()));
        }
        Type nullSafeAnnotationType = extensionParameter.getValueFromAnnotation(NullSafe.class).get().getClassValue(NullSafe::defaultImplementingType);
        final boolean hasDefaultOverride = !nullSafeAnnotationType.isSameType(Object.class);
        MetadataType nullSafeType = hasDefaultOverride ? nullSafeAnnotationType.asMetadataType() : parameter.getDeclaration().getType();
        boolean isInstantiable = hasDefaultOverride ? nullSafeAnnotationType.isInstantiable() : extensionParameter.getType().isInstantiable();
        parameter.getDeclaration().getType().accept(new BasicTypeMetadataVisitor() {

            @Override
            protected void visitBasicType(MetadataType metadataType) {
                throw new IllegalParameterModelDefinitionException(format("Parameter '%s' is annotated with '@%s' but is of type '%s'. That annotation can only be " + "used with complex types (Pojos, Lists, Maps)", extensionParameter.getName(), NullSafe.class.getSimpleName(), extensionParameter.getType().getName()));
            }

            @Override
            public void visitArrayType(ArrayType arrayType) {
                if (hasDefaultOverride) {
                    throw new IllegalParameterModelDefinitionException(format("Parameter '%s' is annotated with '@%s' is of type '%s'" + " but a 'defaultImplementingType' was provided." + " Type override is not allowed for Collections", extensionParameter.getName(), NullSafe.class.getSimpleName(), extensionParameter.getType().getName()));
                }
            }

            @Override
            public void visitObject(ObjectType objectType) {
                if (hasDefaultOverride && isMap(objectType)) {
                    throw new IllegalParameterModelDefinitionException(format("Parameter '%s' is annotated with '@%s' is of type '%s'" + " but a 'defaultImplementingType' was provided." + " Type override is not allowed for Maps", extensionParameter.getName(), NullSafe.class.getSimpleName(), extensionParameter.getType().getName()));
                }
                if (hasDefaultOverride && extensionParameter.getType().isInstantiable()) {
                    throw new IllegalParameterModelDefinitionException(format("Parameter '%s' is annotated with '@%s' is of concrete type '%s'," + " but a 'defaultImplementingType' was provided." + " Type override is not allowed for concrete types", extensionParameter.getName(), NullSafe.class.getSimpleName(), extensionParameter.getType().getName()));
                }
                if (!isInstantiable && !isMap(nullSafeType)) {
                    throw new IllegalParameterModelDefinitionException(format("Parameter '%s' is annotated with '@%s' but is of type '%s'. That annotation can only be " + "used with complex instantiable types (Pojos, Lists, Maps)", extensionParameter.getName(), NullSafe.class.getSimpleName(), extensionParameter.getType().getName()));
                }
                if (hasDefaultOverride && !extensionParameter.getType().isAssignableFrom(nullSafeAnnotationType)) {
                    throw new IllegalParameterModelDefinitionException(format("Parameter '%s' is annotated with '@%s' of type '%s', but provided type '%s" + " is not a subtype of the parameter's type", extensionParameter.getName(), NullSafe.class.getSimpleName(), extensionParameter.getType().getName(), getType(nullSafeType).getName()));
                }
            }
        });
        parameter.withModelProperty(new NullSafeModelProperty(nullSafeType));
        if (hasDefaultOverride) {
            parameter.withModelProperty(new DefaultImplementingTypeModelProperty(nullSafeType));
        }
    }
}
Also used : BasicTypeMetadataVisitor(org.mule.metadata.api.visitor.BasicTypeMetadataVisitor) ArrayType(org.mule.metadata.api.model.ArrayType) ObjectType(org.mule.metadata.api.model.ObjectType) Type(org.mule.runtime.module.extension.api.loader.java.type.Type) ArrayType(org.mule.metadata.api.model.ArrayType) ObjectType(org.mule.metadata.api.model.ObjectType) MetadataType(org.mule.metadata.api.model.MetadataType) JavaTypeUtils.getType(org.mule.metadata.java.api.utils.JavaTypeUtils.getType) NullSafeModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.NullSafeModelProperty) MetadataType(org.mule.metadata.api.model.MetadataType) DefaultImplementingTypeModelProperty(org.mule.runtime.extension.api.property.DefaultImplementingTypeModelProperty) NullSafe(org.mule.runtime.extension.api.annotation.param.NullSafe) ConfigOverride(org.mule.runtime.extension.api.annotation.param.ConfigOverride) IllegalParameterModelDefinitionException(org.mule.runtime.extension.api.exception.IllegalParameterModelDefinitionException)

Example 18 with MetadataType

use of org.mule.metadata.api.model.MetadataType in project mule by mulesoft.

the class MetadataInputDelegate method getParameterMetadataDescriptor.

/**
 * Creates a {@link TypeMetadataDescriptor} representing the Component's Content metadata using the
 * {@link InputTypeResolver}, if one is available to resolve the {@link MetadataType}. If no the Component has no Content
 * parameter, then {@link Optional#empty()} is returned.
 *
 * @param context current {@link MetadataContext} that will be used by the {@link InputTypeResolver}
 * @param key     {@link MetadataKey} of the type which's structure has to be resolved
 * @return Success with an {@link Optional} {@link TypeMetadataDescriptor} representing the Component's Content metadata,
 * resolved using the {@link InputTypeResolver} if one is available to resolve its {@link MetadataType}, returning
 * {@link Optional#empty()} if no Content parameter is present Failure if the dynamic resolution fails for any reason.
 */
private MetadataResult<ParameterMetadataDescriptor> getParameterMetadataDescriptor(ParameterModel parameter, MetadataContext context, Object key) {
    ParameterMetadataDescriptorBuilder descriptorBuilder = ParameterMetadataDescriptor.builder(parameter.getName());
    if (!parameter.hasDynamicType()) {
        return success(descriptorBuilder.withType(parameter.getType()).build());
    }
    descriptorBuilder.dynamic(true);
    MetadataResult<MetadataType> inputMetadataResult = getParameterMetadata(parameter, context, key);
    MetadataType type = inputMetadataResult.get() == null ? parameter.getType() : inputMetadataResult.get();
    ParameterMetadataDescriptor descriptor = descriptorBuilder.withType(type).build();
    return inputMetadataResult.isSuccess() ? success(descriptor) : failure(descriptor, inputMetadataResult.getFailures());
}
Also used : ParameterMetadataDescriptor(org.mule.runtime.api.metadata.descriptor.ParameterMetadataDescriptor) ParameterMetadataDescriptorBuilder(org.mule.runtime.api.metadata.descriptor.ParameterMetadataDescriptor.ParameterMetadataDescriptorBuilder) MetadataType(org.mule.metadata.api.model.MetadataType)

Example 19 with MetadataType

use of org.mule.metadata.api.model.MetadataType in project mule by mulesoft.

the class MetadataOutputDelegate method adaptToListIfNecessary.

private MetadataType adaptToListIfNecessary(MetadataType resolvedType, Object key, MetadataContext metadataContext) throws MetadataResolvingException {
    MetadataType componentOutputType = ((HasOutputModel) component).getOutput().getType();
    if (!isCollection(componentOutputType) || isVoid(resolvedType) || isNullType(resolvedType)) {
        return resolvedType;
    }
    MetadataType collectionValueType = ((ArrayType) componentOutputType).getType();
    Class<?> collectionType = getCollectionType(collectionValueType);
    if (Message.class.equals(collectionType)) {
        MessageMetadataType message = (MessageMetadataType) collectionValueType;
        resolvedType = wrapInMessageType(resolvedType, key, metadataContext, message.getAttributesType());
    }
    return metadataContext.getTypeBuilder().arrayType().with(new ClassInformationAnnotation(getCollectionType(componentOutputType))).of(resolvedType).build();
}
Also used : ArrayType(org.mule.metadata.api.model.ArrayType) MessageMetadataType(org.mule.metadata.message.api.MessageMetadataType) MessageMetadataType(org.mule.metadata.message.api.MessageMetadataType) MetadataType(org.mule.metadata.api.model.MetadataType) ClassInformationAnnotation(org.mule.metadata.java.api.annotation.ClassInformationAnnotation)

Example 20 with MetadataType

use of org.mule.metadata.api.model.MetadataType in project mule by mulesoft.

the class MetadataOutputDelegate method toMetadataDescriptorResult.

private MetadataResult<TypeMetadataDescriptor> toMetadataDescriptorResult(MetadataType type, boolean isDynamic, MetadataResult<MetadataType> result) {
    MetadataType resultingType = result.get() == null ? type : result.get();
    TypeMetadataDescriptor descriptor = TypeMetadataDescriptor.builder().withType(resultingType).dynamic(isDynamic).build();
    return result.isSuccess() ? success(descriptor) : failure(descriptor, result.getFailures());
}
Also used : TypeMetadataDescriptor(org.mule.runtime.api.metadata.descriptor.TypeMetadataDescriptor) MessageMetadataType(org.mule.metadata.message.api.MessageMetadataType) MetadataType(org.mule.metadata.api.model.MetadataType)

Aggregations

MetadataType (org.mule.metadata.api.model.MetadataType)99 ObjectType (org.mule.metadata.api.model.ObjectType)36 ArrayType (org.mule.metadata.api.model.ArrayType)28 Test (org.junit.Test)26 MetadataTypeVisitor (org.mule.metadata.api.visitor.MetadataTypeVisitor)21 Optional (java.util.Optional)20 MessageMetadataType (org.mule.metadata.message.api.MessageMetadataType)18 String.format (java.lang.String.format)17 List (java.util.List)17 ObjectFieldType (org.mule.metadata.api.model.ObjectFieldType)17 StringType (org.mule.metadata.api.model.StringType)15 Map (java.util.Map)14 Collectors.toList (java.util.stream.Collectors.toList)14 ExtensionModel (org.mule.runtime.api.meta.model.ExtensionModel)14 ParameterModel (org.mule.runtime.api.meta.model.parameter.ParameterModel)14 Reference (org.mule.runtime.api.util.Reference)14 DslElementSyntax (org.mule.runtime.extension.api.dsl.syntax.DslElementSyntax)14 ParameterGroupModel (org.mule.runtime.api.meta.model.parameter.ParameterGroupModel)13 Set (java.util.Set)11 ParameterizedModel (org.mule.runtime.api.meta.model.parameter.ParameterizedModel)11