Search in sources :

Example 1 with DataTypeParamsBuilder

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

the class SetPayloadMessageProcessor method process.

@Override
public CoreEvent process(CoreEvent event) throws MuleException {
    final Message.Builder builder = Message.builder(event.getMessage());
    final CoreEvent.Builder eventBuilder = CoreEvent.builder(event);
    if (dataType == null) {
        final TypedValue typedValue = resolveTypedValue(event);
        builder.value(typedValue.getValue()).mediaType(typedValue.getDataType().getMediaType());
    } else {
        Object value = resolveValue(event);
        final DataTypeParamsBuilder dataTypeBuilder = DataType.builder(dataType).type(value == null ? Object.class : value.getClass());
        builder.value(value).mediaType(dataTypeBuilder.build().getMediaType());
    }
    return eventBuilder.message(builder.build()).build();
}
Also used : DataTypeParamsBuilder(org.mule.runtime.api.metadata.DataTypeParamsBuilder) Message(org.mule.runtime.api.message.Message) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) TypedValue(org.mule.runtime.api.metadata.TypedValue)

Example 2 with DataTypeParamsBuilder

use of org.mule.runtime.api.metadata.DataTypeParamsBuilder 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 3 with DataTypeParamsBuilder

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

the class SpringRegistryBootstrap method doRegisterTransformer.

@Override
protected void doRegisterTransformer(TransformerBootstrapProperty bootstrapProperty, Class<?> returnClass, Class<? extends Transformer> transformerClass) throws Exception {
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(transformerClass);
    DataType returnType = null;
    if (returnClass != null) {
        DataTypeParamsBuilder dataTypeBuilder = DataType.builder().type(returnClass);
        if (isNotEmpty(bootstrapProperty.getMimeType())) {
            dataTypeBuilder = dataTypeBuilder.mediaType(bootstrapProperty.getMimeType());
        }
        builder.addPropertyValue("returnDataType", dataTypeBuilder.build());
    }
    String name = bootstrapProperty.getName();
    if (name == null) {
        // Prefixes the generated default name to ensure there is less chance of conflict if the user registers
        // the transformer with the same name
        name = "_" + TransformerUtils.generateTransformerName(transformerClass, returnType);
    }
    builder.addPropertyValue("name", name);
    notifyIfOptional(name, bootstrapProperty.getOptional());
    doRegisterObject(name, builder);
}
Also used : DataTypeParamsBuilder(org.mule.runtime.api.metadata.DataTypeParamsBuilder) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) DataType(org.mule.runtime.api.metadata.DataType)

Example 4 with DataTypeParamsBuilder

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

the class TransformerObjectFactory method doGetObject.

@Override
public final Transformer doGetObject() throws Exception {
    AbstractTransformer transformerInstance = createInstance();
    if (returnClass != null || mimeType != null) {
        DataTypeParamsBuilder builder = DataType.builder().type(getReturnType());
        if (isNotEmpty(mimeType)) {
            builder.mediaType(mimeType);
        }
        transformerInstance.setReturnDataType(builder.charset(encoding).build());
    }
    transformerInstance.setIgnoreBadInput(ignoreBadInput);
    transformerInstance.setName(name);
    postProcessInstance(transformerInstance);
    return transformerInstance;
}
Also used : AbstractTransformer(org.mule.runtime.core.api.transformer.AbstractTransformer) DataTypeParamsBuilder(org.mule.runtime.api.metadata.DataTypeParamsBuilder)

Example 5 with DataTypeParamsBuilder

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

the class DataTypeBuilderTestCase method recycleBuilder.

@Test
public void recycleBuilder() {
    final DataTypeParamsBuilder builder = DataType.builder().type(String.class);
    builder.build();
    expected.expect(IllegalStateException.class);
    builder.build();
}
Also used : DataTypeParamsBuilder(org.mule.runtime.api.metadata.DataTypeParamsBuilder) Test(org.junit.Test)

Aggregations

DataTypeParamsBuilder (org.mule.runtime.api.metadata.DataTypeParamsBuilder)9 Test (org.junit.Test)2 DataType (org.mule.runtime.api.metadata.DataType)2 AbstractTransformer (org.mule.runtime.core.api.transformer.AbstractTransformer)2 DataHandler (javax.activation.DataHandler)1 DataSource (javax.activation.DataSource)1 Message (org.mule.runtime.api.message.Message)1 TypedValue (org.mule.runtime.api.metadata.TypedValue)1 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)1 DiscoverableTransformer (org.mule.runtime.core.api.transformer.DiscoverableTransformer)1 Transformer (org.mule.runtime.core.api.transformer.Transformer)1 TransformerException (org.mule.runtime.core.api.transformer.TransformerException)1 RegistrationException (org.mule.runtime.core.privileged.registry.RegistrationException)1 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)1