INT-831, INT-861 added 'expectReply' property to HttpRequestExecutingMessageHandler (default is true)

This commit is contained in:
Mark Fisher
2010-06-24 19:16:52 +00:00
parent 242370550c
commit c022a8d559

View File

@@ -25,6 +25,7 @@ import java.util.Map;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.integration.core.Message;
@@ -52,6 +53,8 @@ public class HttpRequestExecutingMessageHandler extends AbstractReplyProducingMe
private volatile OutboundRequestMapper requestMapper = new DefaultOutboundRequestMapper();
private boolean expectReply = true;
private volatile Class<?> expectedResponseType = Object.class;
private final RestTemplate restTemplate = new RestTemplate();
@@ -94,6 +97,14 @@ public class HttpRequestExecutingMessageHandler extends AbstractReplyProducingMe
this.defaultHttpMethod = defaultHttpMethod;
}
/**
* Specify whether a reply Message is expected. If not, this handler will simply return null for a
* successful response or throw an Exception for a non-successful response. The default is true.
*/
public void setExpectReply(boolean expectReply) {
this.expectReply = expectReply;
}
/**
* Specify the expected response type for the REST request.
*/
@@ -149,11 +160,14 @@ public class HttpRequestExecutingMessageHandler extends AbstractReplyProducingMe
if (!isWritableRequestMethod(httpMethod) && httpRequest.getBody() != null) {
httpRequest = new HttpEntity<Object>(null, httpRequest.getHeaders());
}
HttpEntity<?> httpResponse = this.restTemplate.exchange(uri, httpMethod, httpRequest, this.expectedResponseType, uriVariables);
Object responseBody = httpResponse.getBody();
MessageBuilder<?> replyBuilder = (responseBody instanceof Message<?>) ?
MessageBuilder.fromMessage((Message<?>) responseBody) : MessageBuilder.withPayload(responseBody);
return replyBuilder.copyHeaders(httpResponse.getHeaders().toSingleValueMap()).build();
ResponseEntity<?> httpResponse = this.restTemplate.exchange(uri, httpMethod, httpRequest, this.expectedResponseType, uriVariables);
if (this.expectReply) {
Object responseBody = httpResponse.getBody();
MessageBuilder<?> replyBuilder = (responseBody instanceof Message<?>) ?
MessageBuilder.fromMessage((Message<?>) responseBody) : MessageBuilder.withPayload(responseBody);
return replyBuilder.copyHeaders(httpResponse.getHeaders().toSingleValueMap()).build();
}
return null;
}
catch (MessagingException e) {
throw e;