Search in sources :

Example 56 with Transformer

use of org.mule.runtime.core.api.transformer.Transformer in project mule by mulesoft.

the class ExtendedTransformationService method canSkipTransformer.

private boolean canSkipTransformer(Message message, List<? extends Transformer> transformers, int index) {
    Transformer transformer = transformers.get(index);
    boolean skipConverter = false;
    if (transformer instanceof Converter) {
        if (index == transformers.size() - 1) {
            try {
                checkTransformerReturnClass(transformer, message.getPayload().getValue());
                skipConverter = true;
            } catch (TransformerException e) {
            // Converter cannot be skipped
            }
        } else {
            skipConverter = true;
        }
    }
    if (skipConverter) {
        logger.debug("Skipping converter: " + transformer);
    }
    return skipConverter;
}
Also used : Transformer(org.mule.runtime.core.api.transformer.Transformer) MessageTransformer(org.mule.runtime.core.api.transformer.MessageTransformer) Converter(org.mule.runtime.core.api.transformer.Converter) MessageTransformerException(org.mule.runtime.core.api.transformer.MessageTransformerException) TransformerException(org.mule.runtime.core.api.transformer.TransformerException)

Example 57 with Transformer

use of org.mule.runtime.core.api.transformer.Transformer in project mule by mulesoft.

the class TransformerUtils method getTransformers.

/**
 * Builds a list of Transformers.
 *
 * @param names - a list of transformers separated by commands
 * @param muleContext the current muleContext. This is used to look up transformers in the registry
 * @return a list (possibly empty) of transformers or
 * @throws MuleException if any of the transformers cannot be found
 */
public static List<Transformer> getTransformers(String names, MuleContext muleContext) throws MuleException {
    if (null != names) {
        List<Transformer> transformers = new LinkedList<>();
        StringTokenizer st = new StringTokenizer(names, COMMA);
        while (st.hasMoreTokens()) {
            String key = st.nextToken().trim();
            Transformer transformer = ((MuleContextWithRegistries) muleContext).getRegistry().lookupTransformer(key);
            if (transformer == null) {
                throw new DefaultMuleException(objectNotRegistered("Transformer", key));
            }
            transformers.add(transformer);
        }
        return transformers;
    } else {
        return null;
    }
}
Also used : DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) StringTokenizer(java.util.StringTokenizer) Transformer(org.mule.runtime.core.api.transformer.Transformer) AbstractTransformer(org.mule.runtime.core.api.transformer.AbstractTransformer) LinkedList(java.util.LinkedList)

Example 58 with Transformer

use of org.mule.runtime.core.api.transformer.Transformer in project mule by mulesoft.

the class AbstractTransformerTestCase method testRoundTrip.

@Test
public void testRoundTrip() throws Exception {
    if (this.getRoundTripTransformer() != null) {
        Transformer trans = this.getTransformer();
        Transformer trans2 = this.getRoundTripTransformer();
        Message message = of(getTestData());
        message = ((ExtendedTransformationService) muleContext.getTransformationService()).applyTransformers(message, null, asList(trans, trans2));
        Object result = message.getPayload().getValue();
        this.compareRoundtripResults(this.getTestData(), result);
    }
}
Also used : Transformer(org.mule.runtime.core.api.transformer.Transformer) Message(org.mule.runtime.api.message.Message) Test(org.junit.Test)

Example 59 with Transformer

use of org.mule.runtime.core.api.transformer.Transformer in project mule by mulesoft.

the class AbstractTransformerTestCase method testTransform.

@Test
public void testTransform() throws Exception {
    Transformer trans = this.getTransformer();
    Object result = trans.transform(getTestData());
    Object expectedResult = this.getResultData();
    final boolean match = this.compareResults(expectedResult, result);
    if (!match) {
        fail(format("Transformation result does not match expected result. Expected '%s', but got '%s'", expectedResult, result));
    }
}
Also used : Transformer(org.mule.runtime.core.api.transformer.Transformer) Test(org.junit.Test)

Example 60 with Transformer

use of org.mule.runtime.core.api.transformer.Transformer in project mule by mulesoft.

the class CoreComponentBuildingDefinitionProvider method getTransformersBuildingDefinitions.

private List<ComponentBuildingDefinition> getTransformersBuildingDefinitions() {
    List<ComponentBuildingDefinition> transformerComponentBuildingDefinitions = new ArrayList<>();
    transformerComponentBuildingDefinitions.add(getCoreTransformerBaseBuilder(XmlEntityEncoder.class).withIdentifier("xml-entity-encoder-transformer").build());
    transformerComponentBuildingDefinitions.add(getCoreTransformerBaseBuilder(XmlEntityDecoder.class).withIdentifier("xml-entity-decoder-transformer").build());
    transformerComponentBuildingDefinitions.add(getCoreTransformerBaseBuilder(GZipCompressTransformer.class).withIdentifier("gzip-compress-transformer").build());
    transformerComponentBuildingDefinitions.add(getCoreTransformerBaseBuilder(GZipUncompressTransformer.class).withIdentifier("gzip-uncompress-transformer").build());
    KeyAttributeDefinitionPair strategyParameterDefinition = newBuilder().withKey("strategy").withAttributeDefinition(fromSimpleReferenceParameter("strategy-ref").build()).build();
    transformerComponentBuildingDefinitions.add(getTransformerBaseBuilder(getEncryptionTransformerConfigurationFactory(EncryptionTransformer.class), EncryptionTransformer.class, strategyParameterDefinition).withIdentifier("encrypt-transformer").withNamespace(CORE_PREFIX).build());
    transformerComponentBuildingDefinitions.add(getTransformerBaseBuilder(getEncryptionTransformerConfigurationFactory(DecryptionTransformer.class), DecryptionTransformer.class, strategyParameterDefinition).withIdentifier("decrypt-transformer").withNamespace(CORE_PREFIX).build());
    transformerComponentBuildingDefinitions.add(getCoreTransformerBaseBuilder(ByteArrayToHexString.class).withIdentifier("byte-array-to-hex-string-transformer").build());
    transformerComponentBuildingDefinitions.add(getCoreTransformerBaseBuilder(HexStringToByteArray.class).withIdentifier("hex-string-to-byte-array-transformer").build());
    transformerComponentBuildingDefinitions.add(getCoreTransformerBaseBuilder(ByteArrayToObject.class).withIdentifier("byte-array-to-object-transformer").build());
    transformerComponentBuildingDefinitions.add(getCoreTransformerBaseBuilder(ObjectToByteArray.class).withIdentifier("object-to-byte-array-transformer").build());
    transformerComponentBuildingDefinitions.add(getCoreTransformerBaseBuilder(ObjectToString.class).withIdentifier("object-to-string-transformer").build());
    transformerComponentBuildingDefinitions.add(getCoreTransformerBaseBuilder(ByteArrayToSerializable.class).withIdentifier("byte-array-to-serializable-transformer").build());
    transformerComponentBuildingDefinitions.add(getCoreTransformerBaseBuilder(SerializableToByteArray.class).withIdentifier("serializable-to-byte-array-transformer").build());
    transformerComponentBuildingDefinitions.add(getCoreTransformerBaseBuilder(ObjectToString.class).withIdentifier("byte-array-to-string-transformer").build());
    transformerComponentBuildingDefinitions.add(getCoreTransformerBaseBuilder(ObjectToByteArray.class).withIdentifier("string-to-byte-array-transformer").build());
    transformerComponentBuildingDefinitions.add(baseDefinition.withIdentifier("parse-template").withTypeDefinition(fromType(ParseTemplateProcessor.class)).withSetterParameterDefinition("content", fromChildConfiguration(String.class).withIdentifier("content").build()).withSetterParameterDefinition("target", fromSimpleParameter("target").build()).withSetterParameterDefinition("location", fromSimpleParameter("location").build()).withSetterParameterDefinition("targetValue", fromSimpleParameter("targetValue").build()).build());
    transformerComponentBuildingDefinitions.add(getCoreMuleMessageTransformerBaseBuilder().withIdentifier("content").withTypeDefinition(fromType(String.class)).build());
    transformerComponentBuildingDefinitions.add(getCoreTransformerBaseBuilder(AutoTransformer.class).withIdentifier("auto-transformer").build());
    transformerComponentBuildingDefinitions.add(getCoreMuleMessageTransformerBaseBuilder().withIdentifier("append-string-transformer").withTypeDefinition(fromType(StringAppendTransformer.class)).withSetterParameterDefinition("message", fromSimpleParameter("message").build()).build());
    transformerComponentBuildingDefinitions.add(getTransformerBaseBuilder(getCustomTransformerConfigurationFactory(), Transformer.class, newBuilder().withKey("class").withAttributeDefinition(fromSimpleParameter("class").build()).build()).withTypeDefinition(fromConfigurationAttribute("class")).withIdentifier("custom-transformer").withNamespace(CORE_PREFIX).build());
    transformerComponentBuildingDefinitions.add(getTransformerBaseBuilder(getExpressionTransformerConfigurationFactory(), ExpressionTransformer.class, newBuilder().withKey("returnSourceIfNull").withAttributeDefinition(fromSimpleParameter("returnSourceIfNull").build()).build(), newBuilder().withKey("expression").withAttributeDefinition(fromSimpleParameter("expression").build()).build(), newBuilder().withKey("arguments").withAttributeDefinition(fromChildCollectionConfiguration(ExpressionArgument.class).build()).build()).withIdentifier("expression-transformer").withNamespace(CORE_PREFIX).withTypeDefinition(fromType(ExpressionTransformer.class)).build());
    transformerComponentBuildingDefinitions.add(baseDefinition.withObjectFactoryType(ConfigurableObjectFactory.class).withIdentifier("return-argument").withTypeDefinition(fromType(ExpressionArgument.class)).withSetterParameterDefinition("factory", fromFixedValue(getExpressionArgumentConfigurationFactory()).build()).withSetterParameterDefinition("parameters", fromMultipleDefinitions(newBuilder().withKey("optional").withAttributeDefinition(fromSimpleParameter("optional").build()).build(), newBuilder().withKey("expression").withAttributeDefinition(fromSimpleParameter("expression").build()).build(), newBuilder().withKey("muleContext").withAttributeDefinition(fromReferenceObject(MuleContext.class).build()).build()).build()).build());
    transformerComponentBuildingDefinitions.add(baseDefinition.withObjectFactoryType(ConfigurableObjectFactory.class).withIdentifier("bean-property").withTypeDefinition(fromType(ExpressionArgument.class)).withSetterParameterDefinition("factory", fromFixedValue(getExpressionArgumentConfigurationFactory()).build()).withSetterParameterDefinition("parameters", fromMultipleDefinitions(newBuilder().withKey("optional").withAttributeDefinition(fromSimpleParameter("optional").build()).build(), newBuilder().withKey("expression").withAttributeDefinition(fromSimpleParameter("expression").build()).build(), newBuilder().withKey("muleContext").withAttributeDefinition(fromReferenceObject(MuleContext.class).build()).build(), newBuilder().withKey("propertyName").withAttributeDefinition(fromSimpleParameter("property-name").build()).build()).build()).build());
    return transformerComponentBuildingDefinitions;
}
Also used : XmlEntityEncoder(org.mule.runtime.core.internal.transformer.codec.XmlEntityEncoder) ByteArrayToObject(org.mule.runtime.core.privileged.transformer.simple.ByteArrayToObject) HexStringToByteArray(org.mule.runtime.core.internal.transformer.simple.HexStringToByteArray) StringAppendTransformer(org.mule.runtime.core.internal.transformer.simple.StringAppendTransformer) Transformer(org.mule.runtime.core.api.transformer.Transformer) EncryptionTransformer(org.mule.runtime.core.internal.transformer.encryption.EncryptionTransformer) DecryptionTransformer(org.mule.runtime.core.internal.transformer.encryption.DecryptionTransformer) GZipCompressTransformer(org.mule.runtime.core.internal.transformer.compression.GZipCompressTransformer) AbstractTransformer(org.mule.runtime.core.api.transformer.AbstractTransformer) AutoTransformer(org.mule.runtime.core.internal.transformer.simple.AutoTransformer) AbstractEncryptionTransformer(org.mule.runtime.core.internal.transformer.encryption.AbstractEncryptionTransformer) GZipUncompressTransformer(org.mule.runtime.core.internal.transformer.compression.GZipUncompressTransformer) AbstractExpressionTransformer(org.mule.runtime.core.internal.transformer.expression.AbstractExpressionTransformer) ExpressionTransformer(org.mule.runtime.core.internal.transformer.expression.ExpressionTransformer) ObjectToByteArray(org.mule.runtime.core.internal.transformer.simple.ObjectToByteArray) SerializableToByteArray(org.mule.runtime.core.privileged.transformer.simple.SerializableToByteArray) GZipUncompressTransformer(org.mule.runtime.core.internal.transformer.compression.GZipUncompressTransformer) ArrayList(java.util.ArrayList) XmlEntityDecoder(org.mule.runtime.core.internal.transformer.codec.XmlEntityDecoder) AbstractExpressionTransformer(org.mule.runtime.core.internal.transformer.expression.AbstractExpressionTransformer) ExpressionTransformer(org.mule.runtime.core.internal.transformer.expression.ExpressionTransformer) EncryptionTransformer(org.mule.runtime.core.internal.transformer.encryption.EncryptionTransformer) AbstractEncryptionTransformer(org.mule.runtime.core.internal.transformer.encryption.AbstractEncryptionTransformer) ByteArrayToHexString(org.mule.runtime.core.internal.transformer.simple.ByteArrayToHexString) ObjectToString(org.mule.runtime.core.internal.transformer.simple.ObjectToString) ParseTemplateProcessor(org.mule.runtime.core.internal.processor.simple.ParseTemplateProcessor) DecryptionTransformer(org.mule.runtime.core.internal.transformer.encryption.DecryptionTransformer) ObjectToString(org.mule.runtime.core.internal.transformer.simple.ObjectToString) KeyAttributeDefinitionPair(org.mule.runtime.dsl.api.component.KeyAttributeDefinitionPair) ByteArrayToSerializable(org.mule.runtime.core.privileged.transformer.simple.ByteArrayToSerializable) AutoTransformer(org.mule.runtime.core.internal.transformer.simple.AutoTransformer) GZipCompressTransformer(org.mule.runtime.core.internal.transformer.compression.GZipCompressTransformer) ComponentBuildingDefinition(org.mule.runtime.dsl.api.component.ComponentBuildingDefinition) ExpressionArgument(org.mule.runtime.core.internal.transformer.expression.ExpressionArgument) ByteArrayToHexString(org.mule.runtime.core.internal.transformer.simple.ByteArrayToHexString)

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