Search in sources :

Example 1 with TransformerChain

use of org.mule.runtime.core.privileged.transformer.TransformerChain in project mule by mulesoft.

the class TransformerChainingTestCase method testIgnoreBadInputBreaksChainWithTransformationOrderValidInvalid.

@Test
public void testIgnoreBadInputBreaksChainWithTransformationOrderValidInvalid() throws Exception {
    AbstractTransformer invalidTransformer = (AbstractTransformer) this.getInvalidTransformer();
    assertNotNull(invalidTransformer);
    invalidTransformer.setIgnoreBadInput(false);
    AbstractTransformer validTransformer = (AbstractTransformer) this.getIncreaseByOneTransformer();
    assertNotNull(validTransformer);
    Message message = of(new Integer(0));
    Transformer messageTransformer = new TransformerChain(validTransformer, invalidTransformer);
    try {
        transformationService.applyTransformers(message, eventBuilder(muleContext).message(of(0)).build(), messageTransformer);
        fail("Transformer chain is expected to fail because of invalid transformer within chain.");
    } catch (MuleException tfe) {
        assertNotNull(tfe);
    }
}
Also used : Message(org.mule.runtime.api.message.Message) TransformerChain(org.mule.runtime.core.privileged.transformer.TransformerChain) MuleException(org.mule.runtime.api.exception.MuleException) Test(org.junit.Test)

Example 2 with TransformerChain

use of org.mule.runtime.core.privileged.transformer.TransformerChain in project mule by mulesoft.

the class TypeBasedTransformerResolver method resolve.

public Transformer resolve(DataType source, DataType result) throws ResolverException {
    Transformer transformer = exactTransformerCache.get(source.toString() + result.toString());
    if (transformer != null) {
        return transformer;
    }
    MuleRegistry registry = ((MuleContextWithRegistries) muleContext).getRegistry();
    List<Transformer> trans = registry.lookupTransformers(source, result);
    Transformer compositeTransformer = graphTransformerResolver.resolve(source, result);
    if (compositeTransformer != null) {
        // Needs to create a new list because the lookup returns a cached instance
        trans = new LinkedList<>(trans);
        trans.add(compositeTransformer);
    }
    transformer = getNearestTransformerMatch(trans, source.getType(), result.getType());
    // If an exact mach is not found, we have a 'second pass' transformer that can be used to converting to String or
    // byte[]
    Transformer secondPass;
    if (transformer == null) {
        // using Object.class and then convert to String or byte[] using the second pass transformer
        if (String.class.equals(result.getType())) {
            secondPass = objectToString;
        } else if (byte[].class.equals(result.getType())) {
            secondPass = objectToByteArray;
        } else {
            return null;
        }
        // Perform a more general search
        trans = registry.lookupTransformers(source, DataType.OBJECT);
        transformer = getNearestTransformerMatch(trans, source.getType(), result.getType());
        if (transformer != null) {
            transformer = new TransformerChain(transformer, secondPass);
            try {
                registry.registerTransformer(transformer);
            } catch (MuleException e) {
                throw new ResolverException(e.getI18nMessage(), e);
            }
        }
    }
    if (transformer != null) {
        exactTransformerCache.put(source.toString() + result.toString(), transformer);
    }
    return transformer;
}
Also used : Transformer(org.mule.runtime.core.api.transformer.Transformer) ResolverException(org.mule.runtime.core.internal.transformer.ResolverException) MuleContextWithRegistries(org.mule.runtime.core.internal.context.MuleContextWithRegistries) TransformerChain(org.mule.runtime.core.privileged.transformer.TransformerChain) MuleException(org.mule.runtime.api.exception.MuleException)

Example 3 with TransformerChain

use of org.mule.runtime.core.privileged.transformer.TransformerChain in project mule by mulesoft.

the class TransformerChainingTestCase method testSingleChainedTransformer.

@Test
public void testSingleChainedTransformer() throws Exception {
    AbstractTransformer validTransformer = (AbstractTransformer) this.getIncreaseByOneTransformer();
    assertNotNull(validTransformer);
    Message message = of(new Integer(0));
    Transformer messageTransformer = new TransformerChain(validTransformer);
    message = transformationService.applyTransformers(message, eventBuilder(muleContext).message(of(0)).build(), messageTransformer);
    Object transformedMessage = message.getPayload().getValue();
    assertNotNull(transformedMessage);
    assertEquals(new Integer(1), transformedMessage);
}
Also used : Message(org.mule.runtime.api.message.Message) TransformerChain(org.mule.runtime.core.privileged.transformer.TransformerChain) Test(org.junit.Test)

Example 4 with TransformerChain

use of org.mule.runtime.core.privileged.transformer.TransformerChain in project mule by mulesoft.

the class TransformerChainingTestCase method testThreeChainedTransformers.

@Test
public void testThreeChainedTransformers() throws Exception {
    AbstractTransformer validTransformer = (AbstractTransformer) this.getIncreaseByOneTransformer();
    assertNotNull(validTransformer);
    Message message = of(new Integer(0));
    Transformer messageTransformer = new TransformerChain(validTransformer, validTransformer, validTransformer);
    message = transformationService.applyTransformers(message, eventBuilder(muleContext).message(of(0)).build(), messageTransformer);
    Object transformedMessage = message.getPayload().getValue();
    assertNotNull(transformedMessage);
    assertEquals(new Integer(3), transformedMessage);
}
Also used : Message(org.mule.runtime.api.message.Message) TransformerChain(org.mule.runtime.core.privileged.transformer.TransformerChain) Test(org.junit.Test)

Example 5 with TransformerChain

use of org.mule.runtime.core.privileged.transformer.TransformerChain in project mule by mulesoft.

the class TransformerChainingTestCase method testIgnoreBadInputBreaksWithTransformationOrderInvalidValidWhenEnforcedOn.

@Test(expected = MessageTransformerException.class)
public void testIgnoreBadInputBreaksWithTransformationOrderInvalidValidWhenEnforcedOn() throws Exception {
    AbstractTransformer invalidTransformer = (AbstractTransformer) this.getInvalidTransformer();
    assertNotNull(invalidTransformer);
    invalidTransformer.setIgnoreBadInput(true);
    AbstractTransformer validTransformer = (AbstractTransformer) this.getIncreaseByOneTransformer();
    assertNotNull(validTransformer);
    Message message = of(new Integer(0));
    Transformer messageTransformer = new TransformerChain(invalidTransformer, validTransformer);
    transformationService.applyTransformers(message, eventBuilder(muleContext).message(of(0)).build(), messageTransformer);
}
Also used : Message(org.mule.runtime.api.message.Message) TransformerChain(org.mule.runtime.core.privileged.transformer.TransformerChain) Test(org.junit.Test)

Aggregations

TransformerChain (org.mule.runtime.core.privileged.transformer.TransformerChain)7 Test (org.junit.Test)6 Message (org.mule.runtime.api.message.Message)6 MuleException (org.mule.runtime.api.exception.MuleException)3 Transformer (org.mule.runtime.core.api.transformer.Transformer)1 MuleContextWithRegistries (org.mule.runtime.core.internal.context.MuleContextWithRegistries)1 ResolverException (org.mule.runtime.core.internal.transformer.ResolverException)1