Provided setters for 'requestMapper' and 'requestExecutor' on HttpOutboundEndpoint.
This commit is contained in:
@@ -120,9 +120,9 @@ public class HttpInboundEndpoint extends SimpleMessagingGateway implements HttpR
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify a {@link InboundRequestMapper} implementation to map from the
|
||||
* inbound HTTP request to a Message. The default implementation
|
||||
* is {@link DefaultInboundRequestMapper}.
|
||||
* Specify an {@link InboundRequestMapper} implementation to map from the
|
||||
* inbound {@link HttpServletRequest} instances to Messages at runtime.
|
||||
* The default implementation is {@link DefaultInboundRequestMapper}.
|
||||
*/
|
||||
public void setRequestMapper(InboundRequestMapper requestMapper) {
|
||||
Assert.notNull(requestMapper, "requestMapper must not be null");
|
||||
|
||||
@@ -24,36 +24,59 @@ import org.springframework.integration.core.Message;
|
||||
import org.springframework.integration.handler.AbstractReplyProducingMessageHandler;
|
||||
import org.springframework.integration.handler.ReplyMessageHolder;
|
||||
import org.springframework.integration.message.MessageHandlingException;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
|
||||
/**
|
||||
* An outbound endpoint for executing an HTTP request and mapping the response
|
||||
* to a reply Message.
|
||||
* An outbound endpoint that maps a request Message to an {@link HttpRequest},
|
||||
* executes that request, and then maps the response to a reply Message.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
* @since 1.0.2
|
||||
*/
|
||||
public class HttpOutboundEndpoint extends AbstractReplyProducingMessageHandler {
|
||||
|
||||
private final OutboundRequestMapper requestMapper;
|
||||
private volatile OutboundRequestMapper requestMapper;
|
||||
|
||||
private volatile HttpRequestExecutor requestExecutor;
|
||||
|
||||
|
||||
/**
|
||||
* Create an HttpOutboundEndpoint for sending requests to the provided URL.
|
||||
*/
|
||||
public HttpOutboundEndpoint(URL url) {
|
||||
this.requestMapper = new DefaultOutboundRequestMapper(url);
|
||||
this.requestExecutor = new SimpleHttpRequestExecutor();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Specify an {@link OutboundRequestMapper} implementation to map from
|
||||
* Messages to outbound {@link HttpRequest} objects. The default
|
||||
* implementation is {@link DefaultOutboundRequestMapper}.
|
||||
*/
|
||||
public void setRequestMapper(OutboundRequestMapper requestMapper) {
|
||||
Assert.notNull(requestMapper, "requestMapper must not be null");
|
||||
this.requestMapper = requestMapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify the {@link HttpRequestExecutor} to use for executing the
|
||||
* {@link HttpRequest} instances at runtime. The default implementation
|
||||
* is {@link SimpleHttpRequestExecutor}.
|
||||
*/
|
||||
public void setRequestExecutor(HttpRequestExecutor requestExecutor) {
|
||||
Assert.notNull(requestExecutor, "requestExecutor must not be null");
|
||||
this.requestExecutor = requestExecutor;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handleRequestMessage(Message<?> requestMessage, ReplyMessageHolder replyMessageHolder) {
|
||||
try {
|
||||
HttpRequest request = this.requestMapper.fromMessage(requestMessage);
|
||||
InputStream responseBody = this.requestExecutor.executeRequest(request);
|
||||
ByteArrayOutputStream responseByteStream = new ByteArrayOutputStream();
|
||||
FileCopyUtils.copy(responseBody, responseByteStream);
|
||||
replyMessageHolder.set(responseByteStream.toByteArray());
|
||||
Object replyPayload = this.createReplyPayloadFromResponse(responseBody);
|
||||
replyMessageHolder.set(replyPayload);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new MessageHandlingException(requestMessage,
|
||||
@@ -61,4 +84,10 @@ public class HttpOutboundEndpoint extends AbstractReplyProducingMessageHandler {
|
||||
}
|
||||
}
|
||||
|
||||
private Object createReplyPayloadFromResponse(InputStream responseBody) throws Exception {
|
||||
ByteArrayOutputStream responseByteStream = new ByteArrayOutputStream();
|
||||
FileCopyUtils.copy(responseBody, responseByteStream);
|
||||
return responseByteStream.toByteArray();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user