Search in sources :

Example 46 with DataType

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

the class ExtendedTransformationService method applyAllTransformers.

private Message applyAllTransformers(final Message message, final CoreEvent event, final List<? extends Transformer> transformers) throws MuleException {
    Message result = message;
    if (!transformers.isEmpty()) {
        for (int index = 0; index < transformers.size(); index++) {
            Transformer transformer = transformers.get(index);
            Class<?> srcCls = result.getPayload().getDataType().getType();
            DataType originalSourceType = DataType.fromType(srcCls);
            if (transformer.isSourceDataTypeSupported(originalSourceType)) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Using " + transformer + " to transform payload.");
                }
                result = transformMessage(result, event, transformer);
            } else {
                if (logger.isDebugEnabled()) {
                    logger.debug("Transformer " + transformer + " doesn't support the source payload: " + srcCls);
                }
                if (canSkipTransformer(result, transformers, index)) {
                    continue;
                }
                // Resolves implicit conversion if possible
                Transformer implicitTransformer = muleContext.getDataTypeConverterResolver().resolve(originalSourceType, transformer.getSourceDataTypes());
                if (implicitTransformer != null) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Performing implicit transformation with: " + transformer);
                    }
                    result = transformMessage(result, event, implicitTransformer);
                    result = transformMessage(result, event, transformer);
                } else {
                    throw new IllegalArgumentException("Cannot apply transformer " + transformer + " on source payload: " + srcCls);
                }
            }
        }
        Transformer lastTransformer = transformers.get(transformers.size() - 1);
        DataType returnDataType = lastTransformer.getReturnDataType();
        checkResultDataType(message, returnDataType, result.getPayload().getValue());
    }
    return result;
}
Also used : Transformer(org.mule.runtime.core.api.transformer.Transformer) MessageTransformer(org.mule.runtime.core.api.transformer.MessageTransformer) Message(org.mule.runtime.api.message.Message) DataType(org.mule.runtime.api.metadata.DataType)

Example 47 with DataType

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

the class TransformerChain method generateTransformerName.

@Override
protected String generateTransformerName() {
    String name = transformers.get(0).getClass().getSimpleName();
    int i = name.indexOf("To");
    DataType dt = transformers.get(transformers.size() - 1).getReturnDataType();
    if (i > 0 && dt != null) {
        String target = dt.getType().getSimpleName();
        if (target.equals("byte[]")) {
            target = "byteArray";
        }
        name = name.substring(0, i + 2) + capitalize(target);
    }
    return name;
}
Also used : DataType(org.mule.runtime.api.metadata.DataType)

Example 48 with DataType

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

the class TransformerUtils method checkTransformerReturnClass.

/**
 * Checks whether a given value is a valid output for a transformer.
 *
 * @param transformer the transformer used to validate
 * @param value the output value
 * @throws TransformerException if the output value is of a unexpected type.
 */
public static void checkTransformerReturnClass(Transformer transformer, Object value) throws TransformerException {
    if (value == null && (transformer instanceof AbstractTransformer && ((AbstractTransformer) transformer).isAllowNullReturn())) {
        return;
    }
    if (transformer.getReturnDataType() != null) {
        DataTypeParamsBuilder dtBuilder = DataType.builder().fromObject(value);
        if (!(value instanceof DataHandler) && !(value instanceof DataSource)) {
            // To avoid getting an error because the DataType was constructed with a default mediaType
            dtBuilder = dtBuilder.mediaType(transformer.getReturnDataType().getMediaType());
        }
        DataType dt = dtBuilder.build();
        if (!transformer.getReturnDataType().isCompatibleWith(dt)) {
            throw new TransformerException(transformUnexpectedType(dt, transformer.getReturnDataType()), transformer);
        }
    }
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("The transformed value is of expected type. Type is: " + ClassUtils.getSimpleName(value.getClass()));
    }
}
Also used : AbstractTransformer(org.mule.runtime.core.api.transformer.AbstractTransformer) DataTypeParamsBuilder(org.mule.runtime.api.metadata.DataTypeParamsBuilder) DataType(org.mule.runtime.api.metadata.DataType) DataHandler(javax.activation.DataHandler) TransformerException(org.mule.runtime.core.api.transformer.TransformerException) DataSource(javax.activation.DataSource)

Example 49 with DataType

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

the class ReflectiveExpressionFunctionExecutor method getWrapper.

private Function<Object, Object> getWrapper(Parameter parameter) {
    Type[] generics = parameter.getType().getGenericInterfaces();
    Class<?> type;
    if (generics.length == 0) {
        type = Object.class;
    } else {
        type = model.getAllParameterModels().stream().filter(p -> p.getModelProperty(ImplementingParameterModelProperty.class).map(mp -> mp.getParameter().getName().equals(parameter.getName())).orElse(false)).map(p -> getType(p.getType()).orElse(Object.class)).findFirst().orElseThrow(() -> new IllegalStateException(format("Missing parameter with name [%s]", parameter.getName())));
    }
    DataType expected = DataType.fromType(type);
    return value -> {
        if (value == null) {
            return null;
        }
        if (value instanceof TypedValue) {
            if (((TypedValue) value).getDataType().equals(DataType.TYPED_VALUE)) {
                // DW will wrap anything of type TypedValue in a new TypedValue with DataType.TYPED_VALUE
                value = ((TypedValue) value).getValue();
            }
            TypedValue typedValue = (TypedValue) value;
            // We have to check for transformations of the value because weave won't be able to validate types
            return type.isInstance(typedValue.getValue()) ? value : new TypedValue<>(transformationService.transform(typedValue.getValue(), typedValue.getDataType(), expected), typedValue.getDataType());
        } else {
            return new TypedValue<>(value, fromObject(value));
        }
    };
}
Also used : FunctionModel(org.mule.runtime.api.meta.model.function.FunctionModel) BindingContext(org.mule.runtime.api.el.BindingContext) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) ImplementingParameterModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.ImplementingParameterModelProperty) DataType.fromObject(org.mule.runtime.api.metadata.DataType.fromObject) LifecycleUtils.initialiseIfNeeded(org.mule.runtime.core.api.lifecycle.LifecycleUtils.initialiseIfNeeded) Function(java.util.function.Function) ReflectionUtils.invokeMethod(org.springframework.util.ReflectionUtils.invokeMethod) ClassUtils(org.mule.runtime.core.api.util.ClassUtils) Inject(javax.inject.Inject) MuleContext(org.mule.runtime.core.api.MuleContext) MuleException(org.mule.runtime.api.exception.MuleException) Parameter(java.lang.reflect.Parameter) FunctionParameter(org.mule.runtime.api.metadata.FunctionParameter) LifecycleUtils.stopIfNeeded(org.mule.runtime.core.api.lifecycle.LifecycleUtils.stopIfNeeded) ClassUtils.withContextClassLoader(org.mule.runtime.core.api.util.ClassUtils.withContextClassLoader) Method(java.lang.reflect.Method) TransformationService(org.mule.runtime.api.transformation.TransformationService) ExecutionContext(org.mule.runtime.extension.api.runtime.operation.ExecutionContext) Logger(org.slf4j.Logger) DataType(org.mule.runtime.api.metadata.DataType) Optional.ofNullable(java.util.Optional.ofNullable) LifecycleUtils.startIfNeeded(org.mule.runtime.core.api.lifecycle.LifecycleUtils.startIfNeeded) String.format(java.lang.String.format) LifecycleUtils.disposeIfNeeded(org.mule.runtime.core.api.lifecycle.LifecycleUtils.disposeIfNeeded) TypedValue(org.mule.runtime.api.metadata.TypedValue) ExtensionMetadataTypeUtils.getType(org.mule.runtime.extension.api.util.ExtensionMetadataTypeUtils.getType) Lifecycle(org.mule.runtime.api.lifecycle.Lifecycle) List(java.util.List) Type(java.lang.reflect.Type) DataType.fromType(org.mule.runtime.api.metadata.DataType.fromType) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) Optional(java.util.Optional) DataType(org.mule.runtime.api.metadata.DataType) ExtensionMetadataTypeUtils.getType(org.mule.runtime.extension.api.util.ExtensionMetadataTypeUtils.getType) Type(java.lang.reflect.Type) DataType.fromType(org.mule.runtime.api.metadata.DataType.fromType) DataType(org.mule.runtime.api.metadata.DataType) ImplementingParameterModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.ImplementingParameterModelProperty) TypedValue(org.mule.runtime.api.metadata.TypedValue)

Example 50 with DataType

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

the class ExpressionTypedValueValueResolver method resolve.

@Override
public TypedValue<T> resolve(ValueResolvingContext context) throws MuleException {
    initEvaluator();
    TypedValue typedValue = evaluator.resolveTypedValue(context.getEvent());
    if (!isInstance(expectedClass, typedValue.getValue())) {
        DataType expectedDataType = DataType.builder().type(expectedClass).mediaType(typedValue.getDataType().getMediaType()).build();
        return new TypedValue<>(typeSafeTransformer.<T>transform(typedValue.getValue(), typedValue.getDataType(), expectedDataType), expectedDataType);
    }
    return typedValue;
}
Also used : DataType(org.mule.runtime.api.metadata.DataType) TypedValue(org.mule.runtime.api.metadata.TypedValue)

Aggregations

DataType (org.mule.runtime.api.metadata.DataType)102 Test (org.junit.Test)67 SmallTest (org.mule.tck.size.SmallTest)29 DefaultCollectionDataType (org.mule.runtime.core.internal.metadata.DefaultCollectionDataType)24 FunctionDataType (org.mule.runtime.api.metadata.FunctionDataType)20 DefaultFunctionDataType (org.mule.runtime.core.internal.metadata.DefaultFunctionDataType)20 DefaultMapDataType (org.mule.runtime.core.internal.metadata.DefaultMapDataType)20 SimpleDataType (org.mule.runtime.core.internal.metadata.SimpleDataType)20 Message (org.mule.runtime.api.message.Message)18 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)18 TypedValue (org.mule.runtime.api.metadata.TypedValue)12 PrivilegedEvent (org.mule.runtime.core.privileged.event.PrivilegedEvent)12 CompiledExpression (org.mule.mvel2.compiler.CompiledExpression)10 CollectionDataType (org.mule.runtime.api.metadata.CollectionDataType)10 Transformer (org.mule.runtime.core.api.transformer.Transformer)10 ArrayList (java.util.ArrayList)9 List (java.util.List)9 ParserContext (org.mule.mvel2.ParserContext)7 MapDataType (org.mule.runtime.api.metadata.MapDataType)6 MVELExpressionLanguage (org.mule.runtime.core.internal.el.mvel.MVELExpressionLanguage)6