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