Added HttpResponse abstraction to encapsulate response body and headers.
This commit is contained in:
@@ -18,6 +18,9 @@ package org.springframework.integration.http;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
@@ -70,11 +73,11 @@ public abstract class AbstractHttpRequestExecutor implements HttpRequestExecutor
|
||||
/**
|
||||
* Execute a request to send its content to its target URL.
|
||||
* @param request the request to execute
|
||||
* @return the InputStream result
|
||||
* @return the HttpResponse result
|
||||
* @throws IOException if thrown by I/O operations
|
||||
* @throws Exception in case of general errors
|
||||
*/
|
||||
public final InputStream executeRequest(HttpRequest request) throws Exception {
|
||||
public final HttpResponse executeRequest(HttpRequest request) throws Exception {
|
||||
if (logger.isDebugEnabled()) {
|
||||
StringBuilder sb = new StringBuilder("Sending HTTP request to [" + request.getTargetUrl() + "]");
|
||||
Integer contentLength = request.getContentLength();
|
||||
@@ -89,6 +92,41 @@ public abstract class AbstractHttpRequestExecutor implements HttpRequestExecutor
|
||||
/**
|
||||
* Subclasses must implement this method to execute the request.
|
||||
*/
|
||||
protected abstract InputStream doExecuteRequest(HttpRequest request) throws Exception;
|
||||
protected abstract HttpResponse doExecuteRequest(HttpRequest request) throws Exception;
|
||||
|
||||
|
||||
/**
|
||||
* Default implementation of {@link HttpResponse}.
|
||||
*/
|
||||
class DefaultHttpResponse implements HttpResponse {
|
||||
|
||||
private final InputStream body;
|
||||
|
||||
private final Map<String, List<String>> headers;
|
||||
|
||||
|
||||
public DefaultHttpResponse(InputStream body, Map<String, List<String>> headers) {
|
||||
this.body = body;
|
||||
this.headers = (headers != null) ? headers : Collections.<String, List<String>>emptyMap();
|
||||
}
|
||||
|
||||
|
||||
public InputStream getBody() {
|
||||
return this.body;
|
||||
}
|
||||
|
||||
public String getFirstHeader(String key) {
|
||||
List<String> values = this.headers.get(key);
|
||||
return (values != null && values.size() > 0) ? values.get(0) : null;
|
||||
}
|
||||
|
||||
public Map<String, List<String>> getHeaders() {
|
||||
return this.headers;
|
||||
}
|
||||
|
||||
public List<String> getHeaders(String key) {
|
||||
return this.headers.get(key);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -20,6 +20,11 @@ import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
|
||||
import org.apache.commons.httpclient.Header;
|
||||
@@ -39,8 +44,6 @@ import org.apache.commons.httpclient.methods.TraceMethod;
|
||||
|
||||
import org.springframework.context.i18n.LocaleContext;
|
||||
import org.springframework.context.i18n.LocaleContextHolder;
|
||||
import org.springframework.integration.http.AbstractHttpRequestExecutor;
|
||||
import org.springframework.integration.http.HttpRequest;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
@@ -115,7 +118,7 @@ public class CommonsHttpRequestExecutor extends AbstractHttpRequestExecutor {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected InputStream doExecuteRequest(HttpRequest request) throws Exception {
|
||||
protected HttpResponse doExecuteRequest(HttpRequest request) throws Exception {
|
||||
HttpMethod httpMethod = createHttpMethod(request);
|
||||
try {
|
||||
if (httpMethod instanceof EntityEnclosingMethod) {
|
||||
@@ -123,7 +126,7 @@ public class CommonsHttpRequestExecutor extends AbstractHttpRequestExecutor {
|
||||
}
|
||||
executeHttpMethod(getHttpClient(), httpMethod);
|
||||
validateResponse(httpMethod);
|
||||
return readResponseBody(httpMethod);
|
||||
return new DefaultHttpResponse(readResponseBody(httpMethod), getResponseHeaders(httpMethod));
|
||||
}
|
||||
finally {
|
||||
// Need to explicitly release because it might be pooled.
|
||||
@@ -257,6 +260,21 @@ public class CommonsHttpRequestExecutor extends AbstractHttpRequestExecutor {
|
||||
return responseStream;
|
||||
}
|
||||
|
||||
private Map<String, List<String>> getResponseHeaders(HttpMethod httpMethod) {
|
||||
Map<String, List<String>> headers = new HashMap<String, List<String>>();
|
||||
for (Header header : httpMethod.getResponseHeaders()) {
|
||||
String name = header.getName();
|
||||
String value = header.getValue();
|
||||
List<String> values = headers.get(name);
|
||||
if (values == null) {
|
||||
values = new ArrayList<String>();
|
||||
}
|
||||
values.add(value);
|
||||
headers.put(name, values);
|
||||
}
|
||||
return Collections.unmodifiableMap(headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the given response indicates a GZIP response.
|
||||
* <p>This implementation checks whether the HTTP "Content-Encoding"
|
||||
|
||||
@@ -79,8 +79,8 @@ public class HttpOutboundEndpoint extends AbstractReplyProducingMessageHandler {
|
||||
protected void handleRequestMessage(Message<?> requestMessage, ReplyMessageHolder replyMessageHolder) {
|
||||
try {
|
||||
HttpRequest request = this.requestMapper.fromMessage(requestMessage);
|
||||
InputStream responseBody = this.requestExecutor.executeRequest(request);
|
||||
Object replyPayload = this.createReplyPayloadFromResponse(responseBody);
|
||||
HttpResponse response = this.requestExecutor.executeRequest(request);
|
||||
Object replyPayload = this.createReplyPayloadFromResponse(response);
|
||||
replyMessageHolder.set(replyPayload);
|
||||
}
|
||||
catch (Exception e) {
|
||||
@@ -89,8 +89,10 @@ public class HttpOutboundEndpoint extends AbstractReplyProducingMessageHandler {
|
||||
}
|
||||
}
|
||||
|
||||
private Object createReplyPayloadFromResponse(InputStream responseBody) throws Exception {
|
||||
private Object createReplyPayloadFromResponse(HttpResponse response) throws Exception {
|
||||
ByteArrayOutputStream responseByteStream = new ByteArrayOutputStream();
|
||||
InputStream responseBody = response.getBody();
|
||||
Assert.notNull(responseBody, "received null response body");
|
||||
FileCopyUtils.copy(responseBody, responseByteStream);
|
||||
return responseByteStream.toByteArray();
|
||||
}
|
||||
|
||||
@@ -16,11 +16,8 @@
|
||||
|
||||
package org.springframework.integration.http;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* Strategy that will allow a http request response exchange with a remote
|
||||
* server.
|
||||
* Strategy for executing an http request response exchange with a remote server.
|
||||
*
|
||||
* @author Iwein Fuld
|
||||
* @author Mark Fisher
|
||||
@@ -28,6 +25,6 @@ import java.io.InputStream;
|
||||
*/
|
||||
public interface HttpRequestExecutor {
|
||||
|
||||
InputStream executeRequest(HttpRequest request) throws Exception;
|
||||
HttpResponse executeRequest(HttpRequest request) throws Exception;
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.http;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Representation of an HTTP response as returned by an implementation of
|
||||
* the {@link HttpRequestExecutor} strategy.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
* @since 1.0.2
|
||||
*/
|
||||
public interface HttpResponse {
|
||||
|
||||
/**
|
||||
* Return all response headers as a map. There may be multiple values per
|
||||
* key in the map. Hence, the value type is a List of Strings.
|
||||
*/
|
||||
Map<String, List<String>> getHeaders();
|
||||
|
||||
/**
|
||||
* Return all header values for a given key, or null if it has no values.
|
||||
*/
|
||||
List<String> getHeaders(String key);
|
||||
|
||||
/**
|
||||
* Return the first header value for a given key, or null if it has no values.
|
||||
*/
|
||||
String getFirstHeader(String key);
|
||||
|
||||
/**
|
||||
* Return the body of the response as an InputStream.
|
||||
*/
|
||||
InputStream getBody();
|
||||
|
||||
}
|
||||
@@ -22,6 +22,8 @@ import java.io.InputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
|
||||
import org.springframework.context.i18n.LocaleContext;
|
||||
@@ -42,12 +44,13 @@ import org.springframework.util.StringUtils;
|
||||
public class SimpleHttpRequestExecutor extends AbstractHttpRequestExecutor {
|
||||
|
||||
@Override
|
||||
protected InputStream doExecuteRequest(HttpRequest request) throws Exception {
|
||||
protected HttpResponse doExecuteRequest(HttpRequest request) throws Exception {
|
||||
HttpURLConnection connection = this.openConnection(request.getTargetUrl());
|
||||
this.prepareConnection(connection, request);
|
||||
this.writeRequestBody(connection, request.getBody());
|
||||
this.validateResponse(connection);
|
||||
return this.readResponseBody(connection);
|
||||
InputStream responseBody = this.readResponseBody(connection);
|
||||
return new DefaultHttpResponse(responseBody, this.getResponseHeaders(connection));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -146,6 +149,10 @@ public class SimpleHttpRequestExecutor extends AbstractHttpRequestExecutor {
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, List<String>> getResponseHeaders(HttpURLConnection connection) {
|
||||
return connection.getHeaderFields();
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the given response is a GZIP response.
|
||||
* <p>
|
||||
|
||||
Reference in New Issue
Block a user