Search in sources :

Example 36 with Transformer

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();
    }
}
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 37 with Transformer

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;
    }
}
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 38 with Transformer

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

Example 39 with Transformer

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);
}
Also used : Transformer(org.mule.runtime.core.api.transformer.Transformer) MockConverterBuilder(org.mule.runtime.core.internal.transformer.builder.MockConverterBuilder) ArrayList(java.util.ArrayList) DataType(org.mule.runtime.api.metadata.DataType) Test(org.junit.Test) SmallTest(org.mule.tck.size.SmallTest)

Example 40 with Transformer

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");
    }
}
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

Transformer (org.mule.runtime.core.api.transformer.Transformer)87 Test (org.junit.Test)65 SmallTest (org.mule.tck.size.SmallTest)54 MockConverterBuilder (org.mule.runtime.core.internal.transformer.builder.MockConverterBuilder)37 Message (org.mule.runtime.api.message.Message)35 MockTransformerBuilder (org.mule.runtime.core.internal.transformer.builder.MockTransformerBuilder)19 DataType (org.mule.runtime.api.metadata.DataType)14 Converter (org.mule.runtime.core.api.transformer.Converter)12 ArrayList (java.util.ArrayList)10 TransformerException (org.mule.runtime.core.api.transformer.TransformerException)10 AbstractTransformer (org.mule.runtime.core.api.transformer.AbstractTransformer)9 CompositeConverter (org.mule.runtime.core.privileged.transformer.CompositeConverter)9 Serializable (java.io.Serializable)6 MuleContextWithRegistries (org.mule.runtime.core.internal.context.MuleContextWithRegistries)6 TransformerWeighting (org.mule.runtime.core.internal.registry.TransformerWeighting)6 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)4 MessageTransformerException (org.mule.runtime.core.api.transformer.MessageTransformerException)4 IOException (java.io.IOException)3 LinkedList (java.util.LinkedList)3 List (java.util.List)3