Search in sources :

Example 1 with MethodInvokingTransformer

use of org.springframework.integration.transformer.MethodInvokingTransformer in project spring-integration by spring-projects.

the class HeaderAnnotationTransformerTests method headerAnnotationWithPrefixedHeader.

// INT-1082
@Test
public void headerAnnotationWithPrefixedHeader() {
    Object target = new TestTransformer();
    MethodInvokingTransformer transformer = new MethodInvokingTransformer(target, "appendCorrelationId");
    MessageTransformingHandler handler = new MessageTransformingHandler(transformer);
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.afterPropertiesSet();
    QueueChannel outputChannel = new QueueChannel();
    handler.setOutputChannel(outputChannel);
    handler.handleMessage(MessageBuilder.withPayload("test").setCorrelationId("abc").build());
    Message<?> result = outputChannel.receive(0);
    assertNotNull(result);
    assertEquals("testabc", result.getPayload());
    assertEquals("abc", new IntegrationMessageHeaderAccessor(result).getCorrelationId());
}
Also used : IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) MessageTransformingHandler(org.springframework.integration.transformer.MessageTransformingHandler) QueueChannel(org.springframework.integration.channel.QueueChannel) BeanFactory(org.springframework.beans.factory.BeanFactory) MethodInvokingTransformer(org.springframework.integration.transformer.MethodInvokingTransformer) Test(org.junit.Test)

Example 2 with MethodInvokingTransformer

use of org.springframework.integration.transformer.MethodInvokingTransformer in project spring-integration by spring-projects.

the class HeaderAnnotationTransformerTests method headerAnnotationWithUnprefixedHeader.

@Test
public void headerAnnotationWithUnprefixedHeader() {
    Object target = new TestTransformer();
    MethodInvokingTransformer transformer = new MethodInvokingTransformer(target, "appendFoo");
    MessageTransformingHandler handler = new MessageTransformingHandler(transformer);
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.afterPropertiesSet();
    QueueChannel outputChannel = new QueueChannel();
    handler.setOutputChannel(outputChannel);
    handler.handleMessage(MessageBuilder.withPayload("test").setHeader("foo", "bar").build());
    Message<?> result = outputChannel.receive(0);
    assertNotNull(result);
    assertEquals("testbar", result.getPayload());
    assertEquals("bar", result.getHeaders().get("foo"));
}
Also used : MessageTransformingHandler(org.springframework.integration.transformer.MessageTransformingHandler) QueueChannel(org.springframework.integration.channel.QueueChannel) BeanFactory(org.springframework.beans.factory.BeanFactory) MethodInvokingTransformer(org.springframework.integration.transformer.MethodInvokingTransformer) Test(org.junit.Test)

Example 3 with MethodInvokingTransformer

use of org.springframework.integration.transformer.MethodInvokingTransformer in project spring-integration by spring-projects.

the class HeaderAnnotationTransformerTests method headerAnnotationWithUnprefixedHeaderAndRelativeExpression.

@Test
public void headerAnnotationWithUnprefixedHeaderAndRelativeExpression() {
    Object target = new TestTransformer();
    MethodInvokingTransformer transformer = new MethodInvokingTransformer(target, "evalFoo");
    MessageTransformingHandler handler = new MessageTransformingHandler(transformer);
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.afterPropertiesSet();
    QueueChannel outputChannel = new QueueChannel();
    handler.setOutputChannel(outputChannel);
    handler.handleMessage(MessageBuilder.withPayload("test").setHeader("foo", "bar").build());
    Message<?> result = outputChannel.receive(0);
    assertNotNull(result);
    assertEquals("BAR", result.getPayload());
    assertEquals("bar", result.getHeaders().get("foo"));
}
Also used : MessageTransformingHandler(org.springframework.integration.transformer.MessageTransformingHandler) QueueChannel(org.springframework.integration.channel.QueueChannel) BeanFactory(org.springframework.beans.factory.BeanFactory) MethodInvokingTransformer(org.springframework.integration.transformer.MethodInvokingTransformer) Test(org.junit.Test)

Example 4 with MethodInvokingTransformer

use of org.springframework.integration.transformer.MethodInvokingTransformer in project spring-integration by spring-projects.

the class TransformerFactoryBean method createMethodInvokingHandler.

@Override
protected MessageHandler createMethodInvokingHandler(Object targetObject, String targetMethodName) {
    Assert.notNull(targetObject, "targetObject must not be null");
    Transformer transformer = null;
    if (targetObject instanceof Transformer) {
        transformer = (Transformer) targetObject;
    } else {
        this.checkForIllegalTarget(targetObject, targetMethodName);
        if (StringUtils.hasText(targetMethodName)) {
            transformer = new MethodInvokingTransformer(targetObject, targetMethodName);
        } else {
            transformer = new MethodInvokingTransformer(targetObject);
        }
    }
    return this.createHandler(transformer);
}
Also used : ExpressionEvaluatingTransformer(org.springframework.integration.transformer.ExpressionEvaluatingTransformer) MethodInvokingTransformer(org.springframework.integration.transformer.MethodInvokingTransformer) Transformer(org.springframework.integration.transformer.Transformer) MethodInvokingTransformer(org.springframework.integration.transformer.MethodInvokingTransformer)

Example 5 with MethodInvokingTransformer

use of org.springframework.integration.transformer.MethodInvokingTransformer in project spring-integration by spring-projects.

the class IntegrationFlowDefinition method transform.

/**
 * Populate the {@link MessageTransformingHandler} instance for the
 * {@link org.springframework.integration.handler.MessageProcessor} from provided {@link MessageProcessorSpec}.
 * In addition accept options for the integration endpoint using {@link GenericEndpointSpec}.
 * <pre class="code">
 * {@code
 *  .transform(Scripts.script("classpath:myScript.py").variable("foo", bar()),
 *           e -> e.autoStartup(false))
 * }
 * </pre>
 * @param messageProcessorSpec the {@link MessageProcessorSpec} to use.
 * @param endpointConfigurer the {@link Consumer} to provide integration endpoint options.
 * @return the current {@link IntegrationFlowDefinition}.
 * @see MethodInvokingTransformer
 */
public B transform(MessageProcessorSpec<?> messageProcessorSpec, Consumer<GenericEndpointSpec<MessageTransformingHandler>> endpointConfigurer) {
    Assert.notNull(messageProcessorSpec, "'messageProcessorSpec' must not be null");
    MessageProcessor<?> processor = messageProcessorSpec.get();
    return addComponent(processor).transform(new MethodInvokingTransformer(processor), endpointConfigurer);
}
Also used : MethodInvokingTransformer(org.springframework.integration.transformer.MethodInvokingTransformer)

Aggregations

MethodInvokingTransformer (org.springframework.integration.transformer.MethodInvokingTransformer)9 MessageTransformingHandler (org.springframework.integration.transformer.MessageTransformingHandler)7 Test (org.junit.Test)4 BeanFactory (org.springframework.beans.factory.BeanFactory)4 QueueChannel (org.springframework.integration.channel.QueueChannel)4 Bean (org.springframework.context.annotation.Bean)2 IntegrationMessageHeaderAccessor (org.springframework.integration.IntegrationMessageHeaderAccessor)2 Transformer (org.springframework.integration.annotation.Transformer)2 ExpressionEvaluatingTransformer (org.springframework.integration.transformer.ExpressionEvaluatingTransformer)2 Transformer (org.springframework.integration.transformer.Transformer)2 AbstractReplyProducingMessageHandler (org.springframework.integration.handler.AbstractReplyProducingMessageHandler)1 LambdaMessageProcessor (org.springframework.integration.handler.LambdaMessageProcessor)1 ClaimCheckInTransformer (org.springframework.integration.transformer.ClaimCheckInTransformer)1 ClaimCheckOutTransformer (org.springframework.integration.transformer.ClaimCheckOutTransformer)1 GenericTransformer (org.springframework.integration.transformer.GenericTransformer)1 MessageHandler (org.springframework.messaging.MessageHandler)1