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()));
}
}
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;
}
Aggregations