Search in sources :

Example 6 with MessageChannel

use of org.springframework.messaging.MessageChannel in project camel by apache.

the class SpringIntegrationConsumer method handleMessage.

public void handleMessage(org.springframework.messaging.Message<?> siInMessage) {
    // we received a message from spring integration
    // wrap that in a Camel Exchange and process it
    Exchange exchange = getEndpoint().createExchange(getEndpoint().isInOut() ? ExchangePattern.InOut : ExchangePattern.InOnly);
    exchange.setIn(new SpringIntegrationMessage(siInMessage));
    // process the exchange
    try {
        getProcessor().process(exchange);
    } catch (Exception e) {
        getExceptionHandler().handleException("Error processing exchange", exchange, e);
        return;
    }
    // reply logic
    if (getEndpoint().isInOut()) {
        MessageChannel reply = null;
        // get the output channel from message header
        Object returnAddress = siInMessage.getHeaders().getReplyChannel();
        if (returnAddress != null) {
            if (returnAddress instanceof String) {
                reply = context.getApplicationContext().getBean((String) returnAddress, MessageChannel.class);
            } else if (returnAddress instanceof MessageChannel) {
                reply = (MessageChannel) returnAddress;
            }
        } else {
            reply = outputChannel;
            // we want to do in-out so the inputChannel is mandatory (used to receive reply from spring integration)
            if (reply == null) {
                throw new IllegalArgumentException("OutputChannel has not been configured on " + getEndpoint());
            }
        }
        if (reply == null) {
            throw new IllegalArgumentException("Cannot resolve ReplyChannel from message: " + siInMessage);
        }
        // put the message back the outputChannel if we need
        org.springframework.messaging.Message<?> siOutMessage = SpringIntegrationBinding.storeToSpringIntegrationMessage(exchange.getOut());
        // send the message to spring integration
        log.debug("Sending {} to ReplyChannel: {}", siOutMessage, reply);
        reply.send(siOutMessage);
    }
}
Also used : Exchange(org.apache.camel.Exchange) MessageChannel(org.springframework.messaging.MessageChannel)

Example 7 with MessageChannel

use of org.springframework.messaging.MessageChannel in project camel by apache.

the class CamelTargetAdapter method send.

public boolean send(Message<?> message) throws Exception {
    boolean result = false;
    ExchangePattern pattern;
    if (isExpectReply()) {
        pattern = ExchangePattern.InOut;
    } else {
        pattern = ExchangePattern.InOnly;
    }
    Exchange inExchange = new DefaultExchange(getCamelContext(), pattern);
    SpringIntegrationBinding.storeToCamelMessage(message, inExchange.getIn());
    Exchange outExchange = getCamelTemplate().send(getCamelEndpointUri(), inExchange);
    org.apache.camel.Message camelMsg = outExchange.hasOut() ? outExchange.getOut() : outExchange.getIn();
    if (camelMsg.isFault()) {
        result = true;
    }
    Message<?> response;
    if (isExpectReply()) {
        //Check the message header for the return address
        response = SpringIntegrationBinding.storeToSpringIntegrationMessage(outExchange.getOut());
        if (replyChannel == null) {
            MessageChannel messageReplyChannel = (MessageChannel) message.getHeaders().get(MessageHeaders.REPLY_CHANNEL);
            if (messageReplyChannel != null) {
                result = messageReplyChannel.send(response);
            } else {
                throw new MessageDeliveryException(response, "Cannot resolve ReplyChannel from message: " + message);
            }
        } else {
            result = replyChannel.send(response);
        }
    }
    return result;
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) MessageChannel(org.springframework.messaging.MessageChannel) ExchangePattern(org.apache.camel.ExchangePattern) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException)

Example 8 with MessageChannel

use of org.springframework.messaging.MessageChannel in project spring-security by spring-projects.

the class AbstractSecurityWebSocketMessageBrokerConfigurerTests method csrfProtectionForConnect.

@Test
public void csrfProtectionForConnect() throws InterruptedException {
    loadConfig(SockJsSecurityConfig.class);
    SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(SimpMessageType.CONNECT);
    Message<?> message = message(headers, "/authentication");
    MessageChannel messageChannel = clientInboundChannel();
    try {
        messageChannel.send(message);
        fail("Expected Exception");
    } catch (MessageDeliveryException success) {
        assertThat(success.getCause()).isInstanceOf(MissingCsrfTokenException.class);
    }
}
Also used : MessageChannel(org.springframework.messaging.MessageChannel) MissingCsrfTokenException(org.springframework.security.web.csrf.MissingCsrfTokenException) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) SimpMessageHeaderAccessor(org.springframework.messaging.simp.SimpMessageHeaderAccessor) Test(org.junit.Test)

Example 9 with MessageChannel

use of org.springframework.messaging.MessageChannel in project spring-security by spring-projects.

the class AbstractSecurityWebSocketMessageBrokerConfigurerTests method addsAuthenticationPrincipalResolverWhenNoAuthorization.

@Test
public void addsAuthenticationPrincipalResolverWhenNoAuthorization() throws InterruptedException {
    loadConfig(NoInboundSecurityConfig.class);
    MessageChannel messageChannel = clientInboundChannel();
    Message<String> message = message("/permitAll/authentication");
    messageChannel.send(message);
    assertThat(context.getBean(MyController.class).authenticationPrincipal).isEqualTo((String) messageUser.getPrincipal());
}
Also used : MessageChannel(org.springframework.messaging.MessageChannel) Test(org.junit.Test)

Example 10 with MessageChannel

use of org.springframework.messaging.MessageChannel in project camel by apache.

the class SpringIntegrationOneWayConsumerTest method testSendingOneWayMessage.

@Test
public void testSendingOneWayMessage() throws Exception {
    MockEndpoint resultEndpoint = getMockEndpoint("mock:result");
    resultEndpoint.expectedBodiesReceived(MESSAGE_BODY);
    MessageChannel outputChannel = getMandatoryBean(MessageChannel.class, "outputChannel");
    outputChannel.send(new GenericMessage<Object>(MESSAGE_BODY));
    assertMockEndpointsSatisfied();
}
Also used : MessageChannel(org.springframework.messaging.MessageChannel) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Test(org.junit.Test)

Aggregations

MessageChannel (org.springframework.messaging.MessageChannel)20 Test (org.junit.Test)18 MessageHandler (org.springframework.messaging.MessageHandler)5 SimpMessageHeaderAccessor (org.springframework.messaging.simp.SimpMessageHeaderAccessor)5 GenericMessage (org.springframework.messaging.support.GenericMessage)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 Message (org.springframework.messaging.Message)4 MessageDeliveryException (org.springframework.messaging.MessageDeliveryException)4 MessagingException (org.springframework.messaging.MessagingException)4 ExecutorSubscribableChannel (org.springframework.messaging.support.ExecutorSubscribableChannel)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 DirectChannel (org.springframework.integration.channel.DirectChannel)3 StubMessageChannel (org.springframework.messaging.StubMessageChannel)3 SubscribableChannel (org.springframework.messaging.SubscribableChannel)3 BinaryMessage (org.springframework.web.socket.BinaryMessage)3 TextMessage (org.springframework.web.socket.TextMessage)3 WebSocketMessage (org.springframework.web.socket.WebSocketMessage)3 HashMap (java.util.HashMap)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Exchange (org.apache.camel.Exchange)2