Search in sources :

Example 86 with DataType

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

the class TransformerUtils method transformToAny.

public static <T> Object transformToAny(T input, MuleContext muleContext, DataType... supportedTypes) {
    final DataType sourceType = DataType.fromType(input.getClass());
    Object transformedData = null;
    for (DataType supportedType : supportedTypes) {
        transformedData = attemptTransformation(sourceType, input, supportedType, muleContext);
        if (transformedData != null) {
            break;
        }
    }
    return transformedData;
}
Also used : DataType(org.mule.runtime.api.metadata.DataType)

Example 87 with DataType

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

the class ContentTypeHandlingTestCase method setsContentTypeOnXml.

@Test
public void setsContentTypeOnXml() throws Exception {
    CoreEvent response = runFlow("setsContentTypeOnXml");
    DataType dataType = response.getMessage().getPayload().getDataType();
    assertCustomEncoding(dataType);
    assertThat(dataType.getMediaType().getPrimaryType(), is(MediaType.TEXT.getPrimaryType()));
    assertThat(dataType.getMediaType().getSubType(), is(MediaType.TEXT.getSubType()));
}
Also used : CoreEvent(org.mule.runtime.core.api.event.CoreEvent) DataType(org.mule.runtime.api.metadata.DataType) Test(org.junit.Test)

Example 88 with DataType

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

the class ContentTypeHandlingTestCase method maintainsContentType.

@Test
public void maintainsContentType() throws Exception {
    CoreEvent response = flowRunner("defaultContentType").withPayload("").run();
    final DataType responseDataType = response.getMessage().getPayload().getDataType();
    assertDefaultEncoding(responseDataType);
    assertDefaultMimeType(responseDataType);
}
Also used : CoreEvent(org.mule.runtime.core.api.event.CoreEvent) DataType(org.mule.runtime.api.metadata.DataType) Test(org.junit.Test)

Example 89 with DataType

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

the class ContentTypeHandlingTestCase method onlySetMimeTypeOnXml.

@Test
public void onlySetMimeTypeOnXml() throws Exception {
    CoreEvent response = runFlow("onlySetMimeTypeOnXml");
    DataType dataType = response.getMessage().getPayload().getDataType();
    assertDefaultEncoding(dataType);
    assertCustomMimeType(dataType);
}
Also used : CoreEvent(org.mule.runtime.core.api.event.CoreEvent) DataType(org.mule.runtime.api.metadata.DataType) Test(org.junit.Test)

Example 90 with DataType

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

the class ReflectiveFunctionExecutorFactory method createExecutor.

@Override
public FunctionExecutor createExecutor(FunctionModel functionModel, FunctionParameterDefaultValueResolverFactory defaultResolverFactory) {
    DataType returnType = fromType(getType(functionModel.getOutput().getType()).orElseThrow(() -> new MuleRuntimeException(createStaticMessage(format("Failed to obtain the return type for function [%s]", functionModel.getName())))));
    List<FunctionParameter> functionParameters = functionModel.getAllParameterModels().stream().map(p -> {
        MetadataType paramType = p.getType();
        DataType type = isTypedValue(paramType) ? fromType(TypedValue.class) : toDataType(paramType);
        if (p.isRequired()) {
            return new FunctionParameter(p.getName(), type);
        }
        Object defaultValue = p.getDefaultValue();
        if (defaultValue == null) {
            return new FunctionParameter(p.getName(), type, context -> Defaults.defaultValue(type.getType()));
        }
        return new FunctionParameter(p.getName(), type, defaultResolverFactory.create(defaultValue, type));
    }).collect(toList());
    return new ReflectiveExpressionFunctionExecutor(functionModel, returnType, functionParameters, method, getDelegateInstance());
}
Also used : FunctionModel(org.mule.runtime.api.meta.model.function.FunctionModel) DataType(org.mule.runtime.api.metadata.DataType) Defaults(com.google.common.base.Defaults) I18nMessageFactory.createStaticMessage(org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage) ReflectiveMethodOperationExecutor(org.mule.runtime.module.extension.internal.runtime.operation.ReflectiveMethodOperationExecutor) Preconditions.checkArgument(org.mule.runtime.api.util.Preconditions.checkArgument) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) String.format(java.lang.String.format) TypedValue(org.mule.runtime.api.metadata.TypedValue) Collectors.toList(java.util.stream.Collectors.toList) ExtensionMetadataTypeUtils.getType(org.mule.runtime.extension.api.util.ExtensionMetadataTypeUtils.getType) List(java.util.List) DataType.fromType(org.mule.runtime.api.metadata.DataType.fromType) IntrospectionUtils.toDataType(org.mule.runtime.module.extension.internal.util.IntrospectionUtils.toDataType) MetadataType(org.mule.metadata.api.model.MetadataType) FunctionParameter(org.mule.runtime.api.metadata.FunctionParameter) Method(java.lang.reflect.Method) ExtensionMetadataTypeUtils.isTypedValue(org.mule.runtime.extension.api.util.ExtensionMetadataTypeUtils.isTypedValue) ComponentExecutorFactory(org.mule.runtime.extension.api.runtime.operation.ComponentExecutorFactory) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) MetadataType(org.mule.metadata.api.model.MetadataType) DataType(org.mule.runtime.api.metadata.DataType) IntrospectionUtils.toDataType(org.mule.runtime.module.extension.internal.util.IntrospectionUtils.toDataType) FunctionParameter(org.mule.runtime.api.metadata.FunctionParameter)

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