use of org.mule.runtime.core.api.transformer.TransformerException 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.TransformerException 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.TransformerException 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.TransformerException in project mule by mulesoft.
the class MessagingExceptionTestCase method payloadInfoException.
@Test
@Ignore("MULE-10266 review how the transformationService is obtained when building an exception.")
public void payloadInfoException() throws Exception {
MuleException.verboseExceptions = true;
CoreEvent testEvent = mock(CoreEvent.class);
Object payload = mock(Object.class);
// This has to be done this way since mockito doesn't allow to verify toString()
when(payload.toString()).then(new FailAnswer("toString() expected not to be called."));
Message muleMessage = of(payload);
when(transformationService.transform(muleMessage, DataType.STRING)).thenThrow(new TransformerException(createStaticMessage("exception thrown")));
when(testEvent.getMessage()).thenReturn(muleMessage);
MessagingException e = new MessagingException(createStaticMessage(message), testEvent);
assertThat(e.getInfo().get(PAYLOAD_INFO_KEY), is(TransformerException.class.getName() + " while getting payload: exception thrown"));
}
use of org.mule.runtime.core.api.transformer.TransformerException 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