use of org.mule.runtime.api.metadata.DataType in project mule by mulesoft.
the class ExtendedTransformationService method applyAllTransformers.
private Message applyAllTransformers(final Message message, final CoreEvent event, final List<? extends Transformer> transformers) throws MuleException {
Message result = message;
if (!transformers.isEmpty()) {
for (int index = 0; index < transformers.size(); index++) {
Transformer transformer = transformers.get(index);
Class<?> srcCls = result.getPayload().getDataType().getType();
DataType originalSourceType = DataType.fromType(srcCls);
if (transformer.isSourceDataTypeSupported(originalSourceType)) {
if (logger.isDebugEnabled()) {
logger.debug("Using " + transformer + " to transform payload.");
}
result = transformMessage(result, event, transformer);
} else {
if (logger.isDebugEnabled()) {
logger.debug("Transformer " + transformer + " doesn't support the source payload: " + srcCls);
}
if (canSkipTransformer(result, transformers, index)) {
continue;
}
// Resolves implicit conversion if possible
Transformer implicitTransformer = muleContext.getDataTypeConverterResolver().resolve(originalSourceType, transformer.getSourceDataTypes());
if (implicitTransformer != null) {
if (logger.isDebugEnabled()) {
logger.debug("Performing implicit transformation with: " + transformer);
}
result = transformMessage(result, event, implicitTransformer);
result = transformMessage(result, event, transformer);
} else {
throw new IllegalArgumentException("Cannot apply transformer " + transformer + " on source payload: " + srcCls);
}
}
}
Transformer lastTransformer = transformers.get(transformers.size() - 1);
DataType returnDataType = lastTransformer.getReturnDataType();
checkResultDataType(message, returnDataType, result.getPayload().getValue());
}
return result;
}
use of org.mule.runtime.api.metadata.DataType in project mule by mulesoft.
the class TransformerChain method generateTransformerName.
@Override
protected String generateTransformerName() {
String name = transformers.get(0).getClass().getSimpleName();
int i = name.indexOf("To");
DataType dt = transformers.get(transformers.size() - 1).getReturnDataType();
if (i > 0 && dt != null) {
String target = dt.getType().getSimpleName();
if (target.equals("byte[]")) {
target = "byteArray";
}
name = name.substring(0, i + 2) + capitalize(target);
}
return name;
}
use of org.mule.runtime.api.metadata.DataType 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.DataType in project mule by mulesoft.
the class ReflectiveExpressionFunctionExecutor method getWrapper.
private Function<Object, Object> getWrapper(Parameter parameter) {
Type[] generics = parameter.getType().getGenericInterfaces();
Class<?> type;
if (generics.length == 0) {
type = Object.class;
} else {
type = model.getAllParameterModels().stream().filter(p -> p.getModelProperty(ImplementingParameterModelProperty.class).map(mp -> mp.getParameter().getName().equals(parameter.getName())).orElse(false)).map(p -> getType(p.getType()).orElse(Object.class)).findFirst().orElseThrow(() -> new IllegalStateException(format("Missing parameter with name [%s]", parameter.getName())));
}
DataType expected = DataType.fromType(type);
return value -> {
if (value == null) {
return null;
}
if (value instanceof TypedValue) {
if (((TypedValue) value).getDataType().equals(DataType.TYPED_VALUE)) {
// DW will wrap anything of type TypedValue in a new TypedValue with DataType.TYPED_VALUE
value = ((TypedValue) value).getValue();
}
TypedValue typedValue = (TypedValue) value;
// We have to check for transformations of the value because weave won't be able to validate types
return type.isInstance(typedValue.getValue()) ? value : new TypedValue<>(transformationService.transform(typedValue.getValue(), typedValue.getDataType(), expected), typedValue.getDataType());
} else {
return new TypedValue<>(value, fromObject(value));
}
};
}
use of org.mule.runtime.api.metadata.DataType in project mule by mulesoft.
the class ExpressionTypedValueValueResolver method resolve.
@Override
public TypedValue<T> resolve(ValueResolvingContext context) throws MuleException {
initEvaluator();
TypedValue typedValue = evaluator.resolveTypedValue(context.getEvent());
if (!isInstance(expectedClass, typedValue.getValue())) {
DataType expectedDataType = DataType.builder().type(expectedClass).mediaType(typedValue.getDataType().getMediaType()).build();
return new TypedValue<>(typeSafeTransformer.<T>transform(typedValue.getValue(), typedValue.getDataType(), expectedDataType), expectedDataType);
}
return typedValue;
}
Aggregations