Search in sources :

Example 1 with MetadataFailure

use of org.mule.runtime.api.metadata.resolving.MetadataFailure in project mule by mulesoft.

the class MetadataOutputDelegate method getOutputMetadataDescriptor.

/**
 * Creates an {@link OutputMetadataDescriptor} representing the Component's output metadata using the
 * {@link OutputTypeResolver}, if one is available to resolve the output {@link MetadataType}.
 *
 * @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 OutputMetadataDescriptor} representing the Component's output metadata, resolved using the
 * {@link OutputTypeResolver} if one is available to resolve its {@link MetadataType}. Failure if the dynamic
 * resolution fails for any reason.
 */
MetadataResult<OutputMetadataDescriptor> getOutputMetadataDescriptor(MetadataContext context, Object key) {
    if (!(component instanceof HasOutputModel)) {
        return failure(MetadataFailure.Builder.newFailure().withMessage("The given component has not output definition to be described").onComponent());
    }
    MetadataResult<MetadataType> output = getOutputMetadata(context, key);
    MetadataResult<MetadataType> attributes = getOutputAttributesMetadata(context, key);
    HasOutputModel componentWithOutput = (HasOutputModel) this.component;
    MetadataResult<TypeMetadataDescriptor> outputDescriptor = toMetadataDescriptorResult(componentWithOutput.getOutput().getType(), componentWithOutput.getOutput().hasDynamicType(), output);
    MetadataResult<TypeMetadataDescriptor> attributesDescriptor = toMetadataDescriptorResult(componentWithOutput.getOutputAttributes().getType(), false, attributes);
    OutputMetadataDescriptor descriptor = OutputMetadataDescriptor.builder().withReturnType(outputDescriptor.get()).withAttributesType(attributesDescriptor.get()).build();
    if (!output.isSuccess() || !attributes.isSuccess()) {
        List<MetadataFailure> failures = ImmutableList.<MetadataFailure>builder().addAll(output.getFailures()).addAll(attributes.getFailures()).build();
        return failure(descriptor, failures);
    }
    return success(descriptor);
}
Also used : TypeMetadataDescriptor(org.mule.runtime.api.metadata.descriptor.TypeMetadataDescriptor) OutputMetadataDescriptor(org.mule.runtime.api.metadata.descriptor.OutputMetadataDescriptor) HasOutputModel(org.mule.runtime.api.meta.model.HasOutputModel) MessageMetadataType(org.mule.metadata.message.api.MessageMetadataType) MetadataType(org.mule.metadata.api.model.MetadataType) MetadataFailure(org.mule.runtime.api.metadata.resolving.MetadataFailure)

Example 2 with MetadataFailure

use of org.mule.runtime.api.metadata.resolving.MetadataFailure in project mule by mulesoft.

the class MetadataOutputDelegate method getOutputMetadata.

/**
 * Given a {@link MetadataKey} of a type and a {@link MetadataContext}, resolves the {@link MetadataType} of the Components's
 * output using the {@link OutputTypeResolver} associated to the current component.
 *
 * @param context {@link MetadataContext} of the Metadata resolution
 * @param key     {@link MetadataKey} of the type which's structure has to be resolved
 * @return a {@link MetadataResult} with the {@link MetadataType} of the component's output
 */
private MetadataResult<MetadataType> getOutputMetadata(final MetadataContext context, final Object key) {
    OutputModel output = ((HasOutputModel) component).getOutput();
    if (isVoid(output.getType()) || !output.hasDynamicType()) {
        return success(output.getType());
    }
    try {
        MetadataType metadata = resolverFactory.getOutputResolver().getOutputType(context, key);
        if (isMetadataResolvedCorrectly(metadata, true)) {
            return success(adaptToListIfNecessary(metadata, key, context));
        }
        MetadataFailure failure = newFailure().withMessage("Error resolving Output Payload metadata").withFailureCode(NO_DYNAMIC_TYPE_AVAILABLE).withReason(NULL_TYPE_ERROR).onOutputPayload();
        return failure(output.getType(), failure);
    } catch (Exception e) {
        return failure(output.getType(), newFailure(e).onOutputAttributes());
    }
}
Also used : HasOutputModel(org.mule.runtime.api.meta.model.HasOutputModel) MessageMetadataType(org.mule.metadata.message.api.MessageMetadataType) MetadataType(org.mule.metadata.api.model.MetadataType) MetadataFailure(org.mule.runtime.api.metadata.resolving.MetadataFailure) OutputModel(org.mule.runtime.api.meta.model.OutputModel) HasOutputModel(org.mule.runtime.api.meta.model.HasOutputModel) MetadataResolvingException(org.mule.runtime.api.metadata.MetadataResolvingException)

Example 3 with MetadataFailure

use of org.mule.runtime.api.metadata.resolving.MetadataFailure in project mule by mulesoft.

the class MetadataInputDelegate method getInputMetadataDescriptors.

/**
 * For each of the Component's {@link ParameterModel} creates the corresponding {@link TypeMetadataDescriptor} using only its
 * static {@link MetadataType} and ignoring if any parameter has a dynamic type.
 *
 * @return A {@link List} containing a {@link MetadataResult} of {@link TypeMetadataDescriptor} for each input parameter using
 * only its static {@link MetadataType} and ignoring if any parameter has a dynamic type.
 */
MetadataResult<InputMetadataDescriptor> getInputMetadataDescriptors(MetadataContext context, Object key) {
    InputMetadataDescriptor.InputMetadataDescriptorBuilder input = InputMetadataDescriptor.builder();
    List<MetadataResult<ParameterMetadataDescriptor>> results = new LinkedList<>();
    for (ParameterModel parameter : component.getAllParameterModels()) {
        MetadataResult<ParameterMetadataDescriptor> result = getParameterMetadataDescriptor(parameter, context, key);
        input.withParameter(parameter.getName(), result.get());
        results.add(result);
    }
    List<MetadataFailure> failures = results.stream().flatMap(e -> e.getFailures().stream()).collect(toList());
    return failures.isEmpty() ? success(input.build()) : failure(input.build(), failures);
}
Also used : InputMetadataDescriptor(org.mule.runtime.api.metadata.descriptor.InputMetadataDescriptor) ParameterMetadataDescriptor(org.mule.runtime.api.metadata.descriptor.ParameterMetadataDescriptor) MetadataResult.success(org.mule.runtime.api.metadata.resolving.MetadataResult.success) MetadataResult(org.mule.runtime.api.metadata.resolving.MetadataResult) ParameterModel(org.mule.runtime.api.meta.model.parameter.ParameterModel) InputMetadataDescriptor(org.mule.runtime.api.metadata.descriptor.InputMetadataDescriptor) ComponentModel(org.mule.runtime.api.meta.model.ComponentModel) MetadataTypeUtils.isNullType(org.mule.metadata.api.utils.MetadataTypeUtils.isNullType) TypeMetadataDescriptor(org.mule.runtime.api.metadata.descriptor.TypeMetadataDescriptor) MetadataKey(org.mule.runtime.api.metadata.MetadataKey) NO_DYNAMIC_TYPE_AVAILABLE(org.mule.runtime.api.metadata.resolving.FailureCode.NO_DYNAMIC_TYPE_AVAILABLE) LinkedList(java.util.LinkedList) InputTypeResolver(org.mule.runtime.api.metadata.resolving.InputTypeResolver) MetadataTypeUtils.isCollection(org.mule.metadata.api.utils.MetadataTypeUtils.isCollection) ClassInformationAnnotation(org.mule.metadata.java.api.annotation.ClassInformationAnnotation) String.format(java.lang.String.format) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) MetadataContext(org.mule.runtime.api.metadata.MetadataContext) Builder.newFailure(org.mule.runtime.api.metadata.resolving.MetadataFailure.Builder.newFailure) MetadataType(org.mule.metadata.api.model.MetadataType) Optional(java.util.Optional) MetadataResolvingException(org.mule.runtime.api.metadata.MetadataResolvingException) ParameterMetadataDescriptorBuilder(org.mule.runtime.api.metadata.descriptor.ParameterMetadataDescriptor.ParameterMetadataDescriptorBuilder) NamedTypeResolver(org.mule.runtime.api.metadata.resolving.NamedTypeResolver) MetadataFailure(org.mule.runtime.api.metadata.resolving.MetadataFailure) JavaTypeUtils.getType(org.mule.metadata.java.api.utils.JavaTypeUtils.getType) MetadataResult.failure(org.mule.runtime.api.metadata.resolving.MetadataResult.failure) ParameterMetadataDescriptor(org.mule.runtime.api.metadata.descriptor.ParameterMetadataDescriptor) ParameterModel(org.mule.runtime.api.meta.model.parameter.ParameterModel) MetadataFailure(org.mule.runtime.api.metadata.resolving.MetadataFailure) LinkedList(java.util.LinkedList) MetadataResult(org.mule.runtime.api.metadata.resolving.MetadataResult)

Example 4 with MetadataFailure

use of org.mule.runtime.api.metadata.resolving.MetadataFailure in project mule by mulesoft.

the class MetadataInputDelegate method getParameterMetadata.

/**
 * Given a {@link MetadataKey} of a type and a {@link MetadataContext}, resolves the {@link MetadataType} of the
 * {@code parameter} using the {@link InputTypeResolver} associated to the current component.
 *
 * @param context {@link MetadataContext} of the MetaData resolution
 * @param key     {@link MetadataKey} of the type which's structure has to be resolved
 * @return a {@link MetadataResult} with the {@link MetadataType} of the {@code parameter}.
 */
private MetadataResult<MetadataType> getParameterMetadata(ParameterModel parameter, MetadataContext context, Object key) {
    try {
        boolean allowsNullType = !parameter.isRequired() && (parameter.getDefaultValue() == null);
        MetadataType metadata = resolverFactory.getInputResolver(parameter.getName()).getInputMetadata(context, key);
        if (isMetadataResolvedCorrectly(metadata, allowsNullType)) {
            return success(adaptToListIfNecessary(metadata, parameter, context));
        }
        MetadataFailure failure = newFailure().withMessage(format("Error resolving metadata for the [%s] input parameter", parameter.getName())).withFailureCode(NO_DYNAMIC_TYPE_AVAILABLE).withReason(NULL_TYPE_ERROR).onParameter(parameter.getName());
        return failure(parameter.getType(), failure);
    } catch (Exception e) {
        return failure(parameter.getType(), newFailure(e).onParameter(parameter.getName()));
    }
}
Also used : MetadataType(org.mule.metadata.api.model.MetadataType) MetadataFailure(org.mule.runtime.api.metadata.resolving.MetadataFailure) MetadataResolvingException(org.mule.runtime.api.metadata.MetadataResolvingException)

Example 5 with MetadataFailure

use of org.mule.runtime.api.metadata.resolving.MetadataFailure in project mule by mulesoft.

the class MetadataMediator method getMetadata.

/**
 * Resolves the {@link ComponentMetadataDescriptor} for the associated {@code context} using static and dynamic resolving of the
 * Component parameters, attributes and output.
 *
 * @param context current {@link MetadataContext} that will be used by the {@link InputTypeResolver} and
 *        {@link OutputTypeResolver}
 * @param keyValue {@link Object} resolved {@link MetadataKey} object
 * @return Successful {@link MetadataResult} if the MetadataTypes are resolved without errors Failure {@link MetadataResult}
 *         when the Metadata retrieval of any element fails for any reason
 */
private MetadataResult<ComponentMetadataDescriptor<T>> getMetadata(MetadataContext context, Object keyValue, MetadataAttributes.MetadataAttributesBuilder attributesBuilder) {
    MetadataResult<OutputMetadataDescriptor> output = outputDelegate.getOutputMetadataDescriptor(context, keyValue);
    MetadataResult<InputMetadataDescriptor> input = inputDelegate.getInputMetadataDescriptors(context, keyValue);
    if (output.isSuccess() && input.isSuccess()) {
        MetadataAttributes metadataAttributes = getMetadataAttributes(attributesBuilder, outputDelegate, input.get());
        T model = getTypedModel(input.get(), output.get());
        return success(ComponentMetadataDescriptor.builder(model).withAttributes(metadataAttributes).build());
    }
    List<MetadataFailure> failures = ImmutableList.<MetadataFailure>builder().addAll(output.getFailures()).addAll(input.getFailures()).build();
    return failure(ComponentMetadataDescriptor.builder(component).build(), failures);
}
Also used : InputMetadataDescriptor(org.mule.runtime.api.metadata.descriptor.InputMetadataDescriptor) OutputMetadataDescriptor(org.mule.runtime.api.metadata.descriptor.OutputMetadataDescriptor) MetadataAttributes(org.mule.runtime.api.metadata.MetadataAttributes) MetadataFailure(org.mule.runtime.api.metadata.resolving.MetadataFailure)

Aggregations

MetadataFailure (org.mule.runtime.api.metadata.resolving.MetadataFailure)6 MetadataType (org.mule.metadata.api.model.MetadataType)5 MetadataResolvingException (org.mule.runtime.api.metadata.MetadataResolvingException)4 MessageMetadataType (org.mule.metadata.message.api.MessageMetadataType)3 HasOutputModel (org.mule.runtime.api.meta.model.HasOutputModel)2 InputMetadataDescriptor (org.mule.runtime.api.metadata.descriptor.InputMetadataDescriptor)2 OutputMetadataDescriptor (org.mule.runtime.api.metadata.descriptor.OutputMetadataDescriptor)2 TypeMetadataDescriptor (org.mule.runtime.api.metadata.descriptor.TypeMetadataDescriptor)2 String.format (java.lang.String.format)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Optional (java.util.Optional)1 Collectors.toList (java.util.stream.Collectors.toList)1 MetadataTypeUtils.isCollection (org.mule.metadata.api.utils.MetadataTypeUtils.isCollection)1 MetadataTypeUtils.isNullType (org.mule.metadata.api.utils.MetadataTypeUtils.isNullType)1 ClassInformationAnnotation (org.mule.metadata.java.api.annotation.ClassInformationAnnotation)1 JavaTypeUtils.getType (org.mule.metadata.java.api.utils.JavaTypeUtils.getType)1 ComponentModel (org.mule.runtime.api.meta.model.ComponentModel)1 OutputModel (org.mule.runtime.api.meta.model.OutputModel)1 ParameterModel (org.mule.runtime.api.meta.model.parameter.ParameterModel)1