Search in sources :

Example 6 with TransformerException

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();
    }
}
Also used : Transformer(org.mule.runtime.core.api.transformer.Transformer) AbstractMessageTransformer(org.mule.runtime.core.api.transformer.AbstractMessageTransformer) Message(org.mule.runtime.api.message.Message) MessageTransformerException(org.mule.runtime.core.api.transformer.MessageTransformerException) MessageTransformerException(org.mule.runtime.core.api.transformer.MessageTransformerException) TransformerException(org.mule.runtime.core.api.transformer.TransformerException)

Example 7 with TransformerException

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()));
    }
}
Also used : AbstractTransformer(org.mule.runtime.core.api.transformer.AbstractTransformer) DataTypeParamsBuilder(org.mule.runtime.api.metadata.DataTypeParamsBuilder) DataType(org.mule.runtime.api.metadata.DataType) DataHandler(javax.activation.DataHandler) TransformerException(org.mule.runtime.core.api.transformer.TransformerException) DataSource(javax.activation.DataSource)

Example 8 with TransformerException

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;
    }
}
Also used : Transformer(org.mule.runtime.core.api.transformer.Transformer) AbstractTransformer(org.mule.runtime.core.api.transformer.AbstractTransformer) MuleContextWithRegistries(org.mule.runtime.core.internal.context.MuleContextWithRegistries) TransformerException(org.mule.runtime.core.api.transformer.TransformerException)

Example 9 with TransformerException

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"));
}
Also used : Message(org.mule.runtime.api.message.Message) I18nMessageFactory.createStaticMessage(org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) TransformerException(org.mule.runtime.core.api.transformer.TransformerException) Ignore(org.junit.Ignore) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 10 with TransformerException

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");
    }
}
Also used : Transformer(org.mule.runtime.core.api.transformer.Transformer) TransformerException(org.mule.runtime.core.api.transformer.TransformerException) Test(org.junit.Test) SmallTest(org.mule.tck.size.SmallTest)

Aggregations

TransformerException (org.mule.runtime.core.api.transformer.TransformerException)19 Transformer (org.mule.runtime.core.api.transformer.Transformer)10 Test (org.junit.Test)5 IOException (java.io.IOException)4 DataType (org.mule.runtime.api.metadata.DataType)4 MessageTransformerException (org.mule.runtime.core.api.transformer.MessageTransformerException)4 InputStream (java.io.InputStream)3 MuleContextWithRegistries (org.mule.runtime.core.internal.context.MuleContextWithRegistries)3 Message (org.mule.runtime.api.message.Message)2 AbstractTransformer (org.mule.runtime.core.api.transformer.AbstractTransformer)2 MessagingException (org.mule.runtime.core.internal.exception.MessagingException)2 SmallTest (org.mule.tck.size.SmallTest)2 Apple (org.mule.tck.testmodels.fruit.Apple)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 PushbackInputStream (java.io.PushbackInputStream)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Lock (java.util.concurrent.locks.Lock)1