Search in sources :

Example 51 with MessageHandlingException

use of org.springframework.messaging.MessageHandlingException in project spring-integration by spring-projects.

the class HttpRequestExecutingMessageHandler method exchange.

@Override
protected Object exchange(Supplier<URI> uriSupplier, HttpMethod httpMethod, HttpEntity<?> httpRequest, Object expectedResponseType, Message<?> requestMessage) {
    URI uri = uriSupplier.get();
    ResponseEntity<?> httpResponse;
    try {
        if (expectedResponseType instanceof ParameterizedTypeReference<?>) {
            httpResponse = this.restTemplate.exchange(uri, httpMethod, httpRequest, (ParameterizedTypeReference<?>) expectedResponseType);
        } else {
            httpResponse = this.restTemplate.exchange(uri, httpMethod, httpRequest, (Class<?>) expectedResponseType);
        }
        return getReply(httpResponse);
    } catch (RestClientException e) {
        throw new MessageHandlingException(requestMessage, "HTTP request execution failed for URI [" + uri + "]", e);
    }
}
Also used : ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) RestClientException(org.springframework.web.client.RestClientException) URI(java.net.URI) MessageHandlingException(org.springframework.messaging.MessageHandlingException)

Example 52 with MessageHandlingException

use of org.springframework.messaging.MessageHandlingException in project spring-integration by spring-projects.

the class IdempotentReceiverIntegrationTests method testIdempotentReceiverOnBeanMessageHandler.

@Test
public void testIdempotentReceiverOnBeanMessageHandler() {
    PollableChannel replyChannel = new QueueChannel();
    Message<String> message = MessageBuilder.withPayload("bar").setReplyChannel(replyChannel).build();
    this.annotatedBeanMessageHandlerChannel.send(message);
    Message<?> receive = replyChannel.receive(10000);
    assertNotNull(receive);
    assertFalse(receive.getHeaders().containsKey(IntegrationMessageHeaderAccessor.DUPLICATE_MESSAGE));
    this.annotatedBeanMessageHandlerChannel.send(message);
    receive = replyChannel.receive(10000);
    assertNotNull(receive);
    assertTrue(receive.getHeaders().containsKey(IntegrationMessageHeaderAccessor.DUPLICATE_MESSAGE));
    assertTrue(receive.getHeaders().get(IntegrationMessageHeaderAccessor.DUPLICATE_MESSAGE, Boolean.class));
    this.annotatedBeanMessageHandlerChannel2.send(new GenericMessage<String>("baz"));
    try {
        this.annotatedBeanMessageHandlerChannel2.send(new GenericMessage<String>("baz"));
        fail("MessageHandlingException expected");
    } catch (Exception e) {
        assertThat(e.getMessage(), containsString("duplicate message has been received"));
    }
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) PollableChannel(org.springframework.messaging.PollableChannel) Matchers.containsString(org.hamcrest.Matchers.containsString) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MessageHandlingException(org.springframework.messaging.MessageHandlingException) MessageRejectedException(org.springframework.integration.MessageRejectedException) Test(org.junit.Test)

Example 53 with MessageHandlingException

use of org.springframework.messaging.MessageHandlingException in project spring-integration by spring-projects.

the class MailSendingMessageHandler method convertMessageToMailMessage.

@SuppressWarnings("unchecked")
private MailMessage convertMessageToMailMessage(Message<?> message) {
    MailMessage mailMessage = null;
    Object payload = message.getPayload();
    if (payload instanceof MimeMessage) {
        mailMessage = new MimeMailMessage((MimeMessage) payload);
    } else if (payload instanceof MailMessage) {
        mailMessage = (MailMessage) payload;
    } else if (payload instanceof byte[]) {
        mailMessage = this.createMailMessageFromByteArrayMessage((Message<byte[]>) message);
    } else if (payload instanceof String) {
        String contentType = (String) message.getHeaders().get(MailHeaders.CONTENT_TYPE);
        if (StringUtils.hasText(contentType)) {
            mailMessage = this.createMailMessageWithContentType((Message<String>) message, contentType);
        } else {
            mailMessage = new SimpleMailMessage();
            mailMessage.setText((String) payload);
        }
    } else {
        throw new MessageHandlingException(message, "Unable to create MailMessage from payload type [" + message.getPayload().getClass().getName() + "], " + "expected MimeMessage, MailMessage, byte array or String.");
    }
    this.applyHeadersToMailMessage(mailMessage, message.getHeaders());
    return mailMessage;
}
Also used : MimeMailMessage(org.springframework.mail.javamail.MimeMailMessage) MailMessage(org.springframework.mail.MailMessage) SimpleMailMessage(org.springframework.mail.SimpleMailMessage) MimeMailMessage(org.springframework.mail.javamail.MimeMailMessage) MimeMailMessage(org.springframework.mail.javamail.MimeMailMessage) MailMessage(org.springframework.mail.MailMessage) SimpleMailMessage(org.springframework.mail.SimpleMailMessage) MimeMessage(javax.mail.internet.MimeMessage) Message(org.springframework.messaging.Message) SimpleMailMessage(org.springframework.mail.SimpleMailMessage) MimeMessage(javax.mail.internet.MimeMessage) MessageHandlingException(org.springframework.messaging.MessageHandlingException)

Example 54 with MessageHandlingException

use of org.springframework.messaging.MessageHandlingException in project spring-integration by spring-projects.

the class ChannelSecurityInterceptorSecuredChannelAnnotationTests method testSecurityContextPropagationPublishSubscribeChannel.

@Test
public void testSecurityContextPropagationPublishSubscribeChannel() {
    login("bob", "bobspassword", "ROLE_ADMIN", "ROLE_PRESIDENT");
    this.publishSubscribeChannel.send(new GenericMessage<String>("test"));
    Message<?> receive = this.securedChannelQueue.receive(10000);
    assertNotNull(receive);
    IntegrationMessageHeaderAccessor headerAccessor = new IntegrationMessageHeaderAccessor(receive);
    assertEquals(0, headerAccessor.getSequenceNumber());
    receive = this.securedChannelQueue2.receive(10000);
    assertNotNull(receive);
    headerAccessor = new IntegrationMessageHeaderAccessor(receive);
    assertEquals(0, headerAccessor.getSequenceNumber());
    this.publishSubscribeChannel.setApplySequence(true);
    this.publishSubscribeChannel.send(new GenericMessage<String>("test"));
    receive = this.securedChannelQueue.receive(10000);
    assertNotNull(receive);
    headerAccessor = new IntegrationMessageHeaderAccessor(receive);
    assertEquals(1, headerAccessor.getSequenceNumber());
    receive = this.securedChannelQueue2.receive(10000);
    assertNotNull(receive);
    headerAccessor = new IntegrationMessageHeaderAccessor(receive);
    assertEquals(2, headerAccessor.getSequenceNumber());
    this.publishSubscribeChannel.setApplySequence(false);
    SecurityContextHolder.clearContext();
    this.publishSubscribeChannel.send(new GenericMessage<String>("test"));
    Message<?> errorMessage = this.errorChannel.receive(10000);
    assertNotNull(errorMessage);
    Object payload = errorMessage.getPayload();
    assertThat(payload, instanceOf(MessageHandlingException.class));
    assertThat(((MessageHandlingException) payload).getCause(), instanceOf(AuthenticationCredentialsNotFoundException.class));
}
Also used : IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) AuthenticationCredentialsNotFoundException(org.springframework.security.authentication.AuthenticationCredentialsNotFoundException) MessageHandlingException(org.springframework.messaging.MessageHandlingException) Test(org.junit.Test)

Example 55 with MessageHandlingException

use of org.springframework.messaging.MessageHandlingException in project spring-integration by spring-projects.

the class SimpleWebServiceOutboundGatewayTests method soapActionAndCustomCallback.

// INT-1051
@Test
public void soapActionAndCustomCallback() {
    String uri = "http://www.example.org";
    SimpleWebServiceOutboundGateway gateway = new SimpleWebServiceOutboundGateway(new TestDestinationProvider(uri));
    final AtomicReference<String> soapActionFromCallback = new AtomicReference<String>();
    gateway.setRequestCallback(message -> {
        SoapMessage soapMessage = (SoapMessage) message;
        soapActionFromCallback.set(soapMessage.getSoapAction());
    });
    gateway.setBeanFactory(mock(BeanFactory.class));
    gateway.afterPropertiesSet();
    String soapActionHeaderValue = "testAction";
    String request = "<test>foo</test>";
    try {
        gateway.handleMessage(MessageBuilder.withPayload(request).setHeader(WebServiceHeaders.SOAP_ACTION, soapActionHeaderValue).build());
        fail("Expected MessageHandlingException");
    } catch (MessageHandlingException e) {
    // expected
    }
    assertNotNull(soapActionFromCallback.get());
    assertEquals("\"" + soapActionHeaderValue + "\"", soapActionFromCallback.get());
}
Also used : BeanFactory(org.springframework.beans.factory.BeanFactory) AtomicReference(java.util.concurrent.atomic.AtomicReference) MessageHandlingException(org.springframework.messaging.MessageHandlingException) SoapMessage(org.springframework.ws.soap.SoapMessage) Test(org.junit.Test)

Aggregations

MessageHandlingException (org.springframework.messaging.MessageHandlingException)65 Test (org.junit.Test)34 ErrorMessage (org.springframework.messaging.support.ErrorMessage)14 IOException (java.io.IOException)12 GenericMessage (org.springframework.messaging.support.GenericMessage)12 Message (org.springframework.messaging.Message)10 File (java.io.File)9 MessagingException (org.springframework.messaging.MessagingException)9 Matchers.containsString (org.hamcrest.Matchers.containsString)6 BeanFactory (org.springframework.beans.factory.BeanFactory)6 FileNotFoundException (java.io.FileNotFoundException)4 ArrayList (java.util.ArrayList)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)4 MessageRejectedException (org.springframework.integration.MessageRejectedException)4 QueueChannel (org.springframework.integration.channel.QueueChannel)4 AbstractReplyProducingMessageHandler (org.springframework.integration.handler.AbstractReplyProducingMessageHandler)4 FileOutputStream (java.io.FileOutputStream)3 OutputStream (java.io.OutputStream)3 IntegrationMessageHeaderAccessor (org.springframework.integration.IntegrationMessageHeaderAccessor)3