Search in sources :

Example 1 with AbstractTransformer

use of org.mule.runtime.core.api.transformer.AbstractTransformer 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 2 with AbstractTransformer

use of org.mule.runtime.core.api.transformer.AbstractTransformer 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)

Aggregations

DataTypeParamsBuilder (org.mule.runtime.api.metadata.DataTypeParamsBuilder)2 AbstractTransformer (org.mule.runtime.core.api.transformer.AbstractTransformer)2 DataHandler (javax.activation.DataHandler)1 DataSource (javax.activation.DataSource)1 DataType (org.mule.runtime.api.metadata.DataType)1 TransformerException (org.mule.runtime.core.api.transformer.TransformerException)1