moved the DefaultOutboundRequestMapper code into the HttpRequestExecutingMessageHandler since we no longer expose the OutboundRequestMapper as a configurable strategy
This commit is contained in:
@@ -1,156 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2010 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.Serializable;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.xml.transform.Source;
|
||||
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.integration.Message;
|
||||
import org.springframework.integration.MessageHeaders;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Default implementation of {@link OutboundRequestMapper}.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
* @since 1.0.2
|
||||
*/
|
||||
public class DefaultOutboundRequestMapper implements OutboundRequestMapper {
|
||||
|
||||
private volatile HttpMethod httpMethod = HttpMethod.POST;
|
||||
|
||||
private volatile boolean extractPayload = true;
|
||||
|
||||
private volatile ContentTypeResolver contentTypeResolver = new DefaultContentTypeResolver();
|
||||
|
||||
private volatile String charset = "UTF-8";
|
||||
|
||||
|
||||
/**
|
||||
* Specify the {@link HttpMethod} that will be used when executing requests.
|
||||
*/
|
||||
public void setHttpMethod(HttpMethod httpMethod) {
|
||||
this.httpMethod = httpMethod;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify whether the outbound message's payload should be extracted
|
||||
* when preparing the request body. Otherwise the Message instance itself
|
||||
* will be serialized. The default value is <code>true</code>.
|
||||
*/
|
||||
public void setExtractPayload(boolean extractPayload) {
|
||||
this.extractPayload = extractPayload;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify the charset name to use for converting String-typed payloads to
|
||||
* bytes. The default is 'UTF-8'.
|
||||
*/
|
||||
public void setCharset(String charset) {
|
||||
Assert.isTrue(Charset.isSupported(charset), "unsupported charset '" + charset + "'");
|
||||
this.charset = charset;
|
||||
}
|
||||
|
||||
public HttpEntity<?> fromMessage(Message<?> message) throws Exception {
|
||||
Assert.notNull(message, "message must not be null");
|
||||
return (this.extractPayload) ? this.createHttpEntityWithPayloadAsBody(message)
|
||||
: this.createHttpEntityWithMessageAsBody(message);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private HttpEntity<?> createHttpEntityWithPayloadAsBody(Message<?> requestMessage) {
|
||||
if (requestMessage.getPayload() instanceof HttpEntity<?>) {
|
||||
return (HttpEntity<?>) requestMessage.getPayload();
|
||||
}
|
||||
HttpHeaders httpHeaders = new HttpHeaders();
|
||||
this.mapMessageHeadersToHttpHeaders(requestMessage.getHeaders(), httpHeaders);
|
||||
Object payload = requestMessage.getPayload();
|
||||
MediaType contentType = (payload instanceof String) ? this.contentTypeResolver.resolveContentType((String) payload, this.charset)
|
||||
: this.contentTypeResolver.resolveContentType(payload);
|
||||
httpHeaders.setContentType(contentType);
|
||||
if (HttpMethod.POST.equals(this.httpMethod) || HttpMethod.PUT.equals(this.httpMethod)) {
|
||||
return new HttpEntity(requestMessage.getPayload(), httpHeaders);
|
||||
}
|
||||
return new HttpEntity(httpHeaders);
|
||||
}
|
||||
|
||||
private HttpEntity<Object> createHttpEntityWithMessageAsBody(Message<?> requestMessage) {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(new MediaType("application", "x-java-serialized-object"));
|
||||
return new HttpEntity<Object>(requestMessage, headers);
|
||||
}
|
||||
|
||||
private void mapMessageHeadersToHttpHeaders(MessageHeaders messageHeaders, HttpHeaders httpHeaders) {
|
||||
for (String headerName : messageHeaders.keySet()) {
|
||||
Object value = messageHeaders.get(headerName);
|
||||
if (value instanceof String && !headerName.startsWith(MessageHeaders.PREFIX)) {
|
||||
httpHeaders.add(headerName, (String) value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static class DefaultContentTypeResolver implements ContentTypeResolver {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public MediaType resolveContentType(Object content) {
|
||||
MediaType contentType = null;
|
||||
if (content instanceof byte[]) {
|
||||
contentType = MediaType.APPLICATION_OCTET_STREAM;
|
||||
}
|
||||
else if (content instanceof Source) {
|
||||
contentType = MediaType.TEXT_XML;
|
||||
}
|
||||
else {
|
||||
if (content instanceof Map && isFormData((Map) content)) {
|
||||
contentType = MediaType.APPLICATION_FORM_URLENCODED;
|
||||
}
|
||||
if (contentType == null && content instanceof Serializable) {
|
||||
contentType = new MediaType("application", "x-java-serialized-object");
|
||||
}
|
||||
}
|
||||
if (contentType == null) {
|
||||
throw new IllegalArgumentException("payload must be a byte array, " +
|
||||
"String, Map, Source, or Serializable object, received: " + content.getClass());
|
||||
}
|
||||
return contentType;
|
||||
}
|
||||
|
||||
public MediaType resolveContentType(String content, String charset) {
|
||||
return new MediaType("text", "plain", Charset.forName(charset));
|
||||
}
|
||||
|
||||
/**
|
||||
* If all keys are Strings, we'll consider the Map to be form data.
|
||||
*/
|
||||
private boolean isFormData(Map<?, ?> map) {
|
||||
for (Object key : map.keySet()) {
|
||||
if (!(key instanceof String)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,11 +16,15 @@
|
||||
|
||||
package org.springframework.integration.http;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.net.URI;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.xml.transform.Source;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.expression.Expression;
|
||||
@@ -31,6 +35,7 @@ import org.springframework.expression.spel.support.StandardTypeConverter;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.client.ClientHttpRequestFactory;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
@@ -63,18 +68,20 @@ public class HttpRequestExecutingMessageHandler extends AbstractReplyProducingMe
|
||||
|
||||
private volatile HttpMethod httpMethod = HttpMethod.POST;
|
||||
|
||||
private boolean expectReply = true;
|
||||
private volatile boolean expectReply = true;
|
||||
|
||||
private volatile Class<?> expectedResponseType;
|
||||
|
||||
private final RestTemplate restTemplate = new RestTemplate();
|
||||
private volatile boolean extractPayload = true;
|
||||
|
||||
private final DefaultOutboundRequestMapper requestMapper = new DefaultOutboundRequestMapper();
|
||||
private volatile String charset = "UTF-8";
|
||||
|
||||
private volatile HeaderMapper<HttpHeaders> headerMapper = new DefaultHttpHeaderMapper();
|
||||
|
||||
private final Map<String, Expression> uriVariableExpressions = new HashMap<String, Expression>();
|
||||
|
||||
private final RestTemplate restTemplate = new RestTemplate();
|
||||
|
||||
private final StandardEvaluationContext evaluationContext = new StandardEvaluationContext();
|
||||
|
||||
|
||||
@@ -99,7 +106,6 @@ public class HttpRequestExecutingMessageHandler extends AbstractReplyProducingMe
|
||||
* Specify the {@link HttpMethod} for requests. The default method will be POST.
|
||||
*/
|
||||
public void setHttpMethod(HttpMethod httpMethod) {
|
||||
this.requestMapper.setHttpMethod(httpMethod);
|
||||
this.httpMethod = httpMethod;
|
||||
}
|
||||
|
||||
@@ -109,7 +115,7 @@ public class HttpRequestExecutingMessageHandler extends AbstractReplyProducingMe
|
||||
* will be serialized. The default value is <code>true</code>.
|
||||
*/
|
||||
public void setExtractPayload(boolean extractPayload) {
|
||||
this.requestMapper.setExtractPayload(extractPayload);
|
||||
this.extractPayload = extractPayload;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -117,7 +123,8 @@ public class HttpRequestExecutingMessageHandler extends AbstractReplyProducingMe
|
||||
* bytes. The default is 'UTF-8'.
|
||||
*/
|
||||
public void setCharset(String charset) {
|
||||
this.requestMapper.setCharset(charset);
|
||||
Assert.isTrue(Charset.isSupported(charset), "unsupported charset '" + charset + "'");
|
||||
this.charset = charset;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -207,7 +214,7 @@ public class HttpRequestExecutingMessageHandler extends AbstractReplyProducingMe
|
||||
Object value = entry.getValue().getValue(this.evaluationContext, requestMessage, String.class);
|
||||
uriVariables.put(entry.getKey(), value);
|
||||
}
|
||||
HttpEntity<?> httpRequest = this.requestMapper.fromMessage(requestMessage);
|
||||
HttpEntity<?> httpRequest = this.generateHttpRequest(requestMessage);
|
||||
ResponseEntity<?> httpResponse = this.restTemplate.exchange(this.uri, this.httpMethod, httpRequest, this.expectedResponseType, uriVariables);
|
||||
if (this.expectReply) {
|
||||
Map<String, ?> headers = this.headerMapper.toHeaders(httpResponse.getHeaders());
|
||||
@@ -231,4 +238,71 @@ public class HttpRequestExecutingMessageHandler extends AbstractReplyProducingMe
|
||||
}
|
||||
}
|
||||
|
||||
private HttpEntity<?> generateHttpRequest(Message<?> message) throws Exception {
|
||||
Assert.notNull(message, "message must not be null");
|
||||
return (this.extractPayload) ? this.createHttpEntityWithPayloadAsBody(message)
|
||||
: this.createHttpEntityWithMessageAsBody(message);
|
||||
}
|
||||
|
||||
private HttpEntity<?> createHttpEntityWithPayloadAsBody(Message<?> requestMessage) {
|
||||
if (requestMessage.getPayload() instanceof HttpEntity<?>) {
|
||||
return (HttpEntity<?>) requestMessage.getPayload();
|
||||
}
|
||||
HttpHeaders httpHeaders = new HttpHeaders();
|
||||
this.headerMapper.fromHeaders(requestMessage.getHeaders(), httpHeaders);
|
||||
Object payload = requestMessage.getPayload();
|
||||
MediaType contentType = (payload instanceof String) ? this.resolveContentType((String) payload, this.charset)
|
||||
: this.resolveContentType(payload);
|
||||
httpHeaders.setContentType(contentType);
|
||||
if (HttpMethod.POST.equals(this.httpMethod) || HttpMethod.PUT.equals(this.httpMethod)) {
|
||||
return new HttpEntity<Object>(requestMessage.getPayload(), httpHeaders);
|
||||
}
|
||||
return new HttpEntity<Object>(httpHeaders);
|
||||
}
|
||||
|
||||
private HttpEntity<Object> createHttpEntityWithMessageAsBody(Message<?> requestMessage) {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(new MediaType("application", "x-java-serialized-object"));
|
||||
return new HttpEntity<Object>(requestMessage, headers);
|
||||
}
|
||||
|
||||
private MediaType resolveContentType(Object content) {
|
||||
MediaType contentType = null;
|
||||
if (content instanceof byte[]) {
|
||||
contentType = MediaType.APPLICATION_OCTET_STREAM;
|
||||
}
|
||||
else if (content instanceof Source) {
|
||||
contentType = MediaType.TEXT_XML;
|
||||
}
|
||||
else {
|
||||
if (content instanceof Map && isFormData((Map<?, ?>) content)) {
|
||||
contentType = MediaType.APPLICATION_FORM_URLENCODED;
|
||||
}
|
||||
if (contentType == null && content instanceof Serializable) {
|
||||
contentType = new MediaType("application", "x-java-serialized-object");
|
||||
}
|
||||
}
|
||||
if (contentType == null) {
|
||||
throw new IllegalArgumentException("payload must be a byte array, " +
|
||||
"String, Map, Source, or Serializable object, received: " + content.getClass());
|
||||
}
|
||||
return contentType;
|
||||
}
|
||||
|
||||
private MediaType resolveContentType(String content, String charset) {
|
||||
return new MediaType("text", "plain", Charset.forName(charset));
|
||||
}
|
||||
|
||||
/**
|
||||
* If all keys are Strings, we'll consider the Map to be form data.
|
||||
*/
|
||||
private boolean isFormData(Map<?, ?> map) {
|
||||
for (Object key : map.keySet()) {
|
||||
if (!(key instanceof String)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2010 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 org.springframework.http.HttpEntity;
|
||||
import org.springframework.integration.mapping.OutboundMessageMapper;
|
||||
|
||||
/**
|
||||
* Strategy for mapping to an {@link HttpEntity} from a message.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
* @since 1.0.2
|
||||
*/
|
||||
public interface OutboundRequestMapper extends OutboundMessageMapper<HttpEntity<?>> {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user