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