From 1a0fa63616b1732add888b3941834dea5afd64d3 Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Thu, 19 Mar 2009 23:39:01 +0000 Subject: [PATCH] Provided setters for 'requestMapper' and 'requestExecutor' on HttpOutboundEndpoint. --- .../integration/http/HttpInboundEndpoint.java | 6 +-- .../http/HttpOutboundEndpoint.java | 41 ++++++++++++++++--- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/org.springframework.integration.http/src/main/java/org/springframework/integration/http/HttpInboundEndpoint.java b/org.springframework.integration.http/src/main/java/org/springframework/integration/http/HttpInboundEndpoint.java index b25edfd3b5..385049e7cb 100644 --- a/org.springframework.integration.http/src/main/java/org/springframework/integration/http/HttpInboundEndpoint.java +++ b/org.springframework.integration.http/src/main/java/org/springframework/integration/http/HttpInboundEndpoint.java @@ -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"); diff --git a/org.springframework.integration.http/src/main/java/org/springframework/integration/http/HttpOutboundEndpoint.java b/org.springframework.integration.http/src/main/java/org/springframework/integration/http/HttpOutboundEndpoint.java index cb8d53b00a..65073682ce 100644 --- a/org.springframework.integration.http/src/main/java/org/springframework/integration/http/HttpOutboundEndpoint.java +++ b/org.springframework.integration.http/src/main/java/org/springframework/integration/http/HttpOutboundEndpoint.java @@ -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(); + } + }