HttpOutboundEndpoint now returns reply payload types based on the response's content-type header. A value of "application/x-java-serialized-object" will trigger deserialization to some Object, a value that begins with "text" will be mapped to a String, and everything else will currently return a byte array.

This commit is contained in:
Mark Fisher
2009-03-20 20:51:21 +00:00
parent a8408dd216
commit 980488b0c0

View File

@@ -17,7 +17,10 @@
package org.springframework.integration.http;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectStreamException;
import java.net.URL;
import org.springframework.integration.core.Message;
@@ -90,11 +93,52 @@ public class HttpOutboundEndpoint extends AbstractReplyProducingMessageHandler {
}
private Object createReplyPayloadFromResponse(HttpResponse response) throws Exception {
ByteArrayOutputStream responseByteStream = new ByteArrayOutputStream();
InputStream responseBody = response.getBody();
Assert.notNull(responseBody, "received null response body");
String contentType = response.getFirstHeader("Content-Type");
if (contentType != null && contentType.startsWith("application/x-java-serialized-object")) {
return this.deserializePayload(responseBody);
}
ByteArrayOutputStream responseByteStream = new ByteArrayOutputStream();
FileCopyUtils.copy(responseBody, responseByteStream);
if (contentType != null && contentType.startsWith("text")) {
String charsetName = this.getCharsetName(response);
if (charsetName == null) {
charsetName = "ISO-8859-1";
}
return responseByteStream.toString(charsetName);
}
return responseByteStream.toByteArray();
}
private String getCharsetName(HttpResponse httpResponse) {
String contentType = httpResponse.getFirstHeader("Content-Type");
if (contentType != null) {
int beginIndex = contentType.indexOf("charset=");
if (beginIndex != -1) {
return contentType.substring(beginIndex + "charset=".length()).trim();
}
}
return null;
}
private Object deserializePayload(InputStream responseBody) throws IOException, ClassNotFoundException {
ObjectInputStream objectStream = null;
try {
objectStream = new ObjectInputStream(responseBody);
return objectStream.readObject();
}
catch (ObjectStreamException e) {
throw new IllegalArgumentException("failed to deserialize response", e);
}
finally {
try {
objectStream.close();
}
catch (Exception e) {
// ignore
}
}
}
}