use of org.mule.runtime.core.api.transformer.Transformer in project mule by mulesoft.
the class TransformerChain method transformMessage.
@Override
public Object transformMessage(CoreEvent event, Charset outputEncoding) throws MessageTransformerException {
Message result = event.getMessage();
Object temp = event.getMessage();
Transformer lastTransformer = null;
for (Object element : transformers) {
lastTransformer = (Transformer) element;
try {
temp = lastTransformer.transform(temp);
} catch (TransformerException e) {
throw new MessageTransformerException(lastTransformer, e, event.getMessage());
}
if (temp instanceof Message) {
result = (Message) temp;
} else {
result = Message.builder(event.getMessage()).value(temp).build();
event = CoreEvent.builder(event).message(result).build();
}
}
if (lastTransformer != null && Message.class.isAssignableFrom(lastTransformer.getReturnDataType().getType())) {
return result;
} else {
return result.getPayload().getValue();
}
}
use of org.mule.runtime.core.api.transformer.Transformer in project mule by mulesoft.
the class TransformerUtils method attemptTransformation.
private static <S, R> R attemptTransformation(DataType sourceDataType, S source, DataType resultDataType, MuleContext muleContext) {
Transformer transformer;
try {
transformer = ((MuleContextWithRegistries) muleContext).getRegistry().lookupTransformer(sourceDataType, resultDataType);
} catch (TransformerException e) {
LOGGER.debug("Could not find a transformer from type {} to {}", sourceDataType.getType().getName(), resultDataType.getType().getName());
return null;
}
LOGGER.debug("Located transformer {} from type {} to type {}. Attempting transformation...", transformer.getName(), sourceDataType.getType().getName(), resultDataType.getType().getName());
try {
return (R) transformer.transform(source);
} catch (TransformerException e) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(format("Transformer %s threw exception while trying to transform an object of type %s into a %s", transformer.getName(), sourceDataType.getType().getName(), resultDataType.getType().getName()), e);
}
return null;
}
}
use of org.mule.runtime.core.api.transformer.Transformer in project mule by mulesoft.
the class AbstractTransformerTestCase method testRoundtripTransform.
@Test
public void testRoundtripTransform() throws Exception {
Transformer roundTripTransformer = this.getRoundTripTransformer();
// If null this is just a one way test
if (roundTripTransformer != null) {
Object result = roundTripTransformer.transform(this.getResultData());
assertNotNull("The result of the roundtrip transform shouldn't be null", result);
final boolean match = this.compareRoundtripResults(this.getTestData(), result);
if (!match) {
fail(format("The result of the roundtrip transform does not match expected result. Expected '%s', but got '%s'", this.getTestData(), result));
}
}
}
use of org.mule.runtime.core.api.transformer.Transformer in project mule by mulesoft.
the class DynamicDataTypeConverterResolverTestCase method findsExpectedConverter.
@Test
public void findsExpectedConverter() throws TransformerException {
Transformer expectedConverter = new MockConverterBuilder().from(DataType.BYTE_ARRAY).to(DataType.STRING).build();
when(muleContext.getRegistry()).thenReturn(muleRegistry);
when(muleRegistry.lookupTransformer(DataType.BYTE_ARRAY, DataType.STRING)).thenReturn(expectedConverter);
DynamicDataTypeConversionResolver resolver = new DynamicDataTypeConversionResolver(muleContext);
List<DataType> targetValues = new ArrayList<>();
targetValues.add(DataType.INPUT_STREAM);
targetValues.add(DataType.STRING);
Transformer resolvedConverter = resolver.resolve(DataType.BYTE_ARRAY, targetValues);
assertEquals(expectedConverter, resolvedConverter);
}
use of org.mule.runtime.core.api.transformer.Transformer in project mule by mulesoft.
the class TransformerResolutionTestCase method resolvesMultipleApplicableTransformers.
@Test
public void resolvesMultipleApplicableTransformers() throws MuleException {
((MuleContextWithRegistries) muleContext).getRegistry().registerTransformer(new StringToOrange());
((MuleContextWithRegistries) muleContext).getRegistry().registerTransformer(new StringToApple());
((MuleContextWithRegistries) muleContext).getRegistry().registerTransformer(new StringToFruit());
try {
Transformer transformer = ((MuleContextWithRegistries) muleContext).getRegistry().lookupTransformer(DataType.STRING, FRUIT_DATA_TYPE);
assertTrue(String.format("Expected a %s transformer but got %s", StringToFruit.class.getName(), transformer.getClass().getName()), transformer instanceof StringToFruit);
} catch (TransformerException e) {
fail("Unable to properly resolve transformer");
}
}
Aggregations