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