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<?>> {
|
||||
|
||||
}
|
||||
@@ -26,14 +26,20 @@ import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.integration.Message;
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.integration.core.MessageBuilder;
|
||||
import org.springframework.web.client.RestClientException;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
@@ -42,14 +48,26 @@ public class DefaultOutboundRequestMapperTests {
|
||||
|
||||
@Test
|
||||
public void simpleStringValueFormData() throws Exception {
|
||||
DefaultOutboundRequestMapper mapper = new DefaultOutboundRequestMapper();
|
||||
mapper.setHttpMethod(HttpMethod.POST);
|
||||
HttpRequestExecutingMessageHandler handler = new HttpRequestExecutingMessageHandler("http://www.springsource.org/spring-integration");
|
||||
MockRestTemplate template = new MockRestTemplate();
|
||||
new DirectFieldAccessor(handler).setPropertyValue("restTemplate", template);
|
||||
handler.setHttpMethod(HttpMethod.POST);
|
||||
Map<String, String> form = new LinkedHashMap<String, String>();
|
||||
form.put("a", "1");
|
||||
form.put("b", "2");
|
||||
form.put("c", "3");
|
||||
Message<?> message = MessageBuilder.withPayload(form).build();
|
||||
HttpEntity<?> request = mapper.fromMessage(message);
|
||||
QueueChannel replyChannel = new QueueChannel();
|
||||
handler.setOutputChannel(replyChannel);
|
||||
Exception exception = null;
|
||||
try {
|
||||
handler.handleMessage(message);
|
||||
}
|
||||
catch (Exception e) {
|
||||
exception = e;
|
||||
}
|
||||
assertEquals("intentional", exception.getCause().getMessage());
|
||||
HttpEntity<?> request = template.lastRequestEntity.get();
|
||||
Object body = request.getBody();
|
||||
assertTrue(body instanceof Map<?, ?>);
|
||||
Map<?, ?> map = (Map <?, ?>) body;
|
||||
@@ -60,17 +78,26 @@ public class DefaultOutboundRequestMapperTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void stringArrayValueFormData() throws Exception {
|
||||
DefaultOutboundRequestMapper mapper = new DefaultOutboundRequestMapper();
|
||||
mapper.setHttpMethod(HttpMethod.POST);
|
||||
Map form = new LinkedHashMap();
|
||||
HttpRequestExecutingMessageHandler handler = new HttpRequestExecutingMessageHandler("http://www.springsource.org/spring-integration");
|
||||
MockRestTemplate template = new MockRestTemplate();
|
||||
new DirectFieldAccessor(handler).setPropertyValue("restTemplate", template);
|
||||
handler.setHttpMethod(HttpMethod.POST);
|
||||
Map<String, Object> form = new LinkedHashMap<String, Object>();
|
||||
form.put("a", new String[] { "1", "2", "3" });
|
||||
form.put("b", "4");
|
||||
form.put("c", new String[] { "5" });
|
||||
form.put("d", "6");
|
||||
Message<?> message = MessageBuilder.withPayload(form).build();
|
||||
HttpEntity<?> request = mapper.fromMessage(message);
|
||||
Exception exception = null;
|
||||
try {
|
||||
handler.handleMessage(message);
|
||||
}
|
||||
catch (Exception e) {
|
||||
exception = e;
|
||||
}
|
||||
assertEquals("intentional", exception.getCause().getMessage());
|
||||
HttpEntity<?> request = template.lastRequestEntity.get();
|
||||
Object body = request.getBody();
|
||||
assertTrue(body instanceof Map<?, ?>);
|
||||
Map<?, ?> map = (Map <?, ?>) body;
|
||||
@@ -96,11 +123,12 @@ public class DefaultOutboundRequestMapperTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void listValueFormData() throws Exception {
|
||||
DefaultOutboundRequestMapper mapper = new DefaultOutboundRequestMapper();
|
||||
mapper.setHttpMethod(HttpMethod.POST);
|
||||
Map form = new LinkedHashMap();
|
||||
HttpRequestExecutingMessageHandler handler = new HttpRequestExecutingMessageHandler("http://www.springsource.org/spring-integration");
|
||||
MockRestTemplate template = new MockRestTemplate();
|
||||
new DirectFieldAccessor(handler).setPropertyValue("restTemplate", template);
|
||||
handler.setHttpMethod(HttpMethod.POST);
|
||||
Map<String, Object> form = new LinkedHashMap<String, Object>();
|
||||
List<String> listA = new ArrayList<String>();
|
||||
listA.add("1");
|
||||
listA.add("2");
|
||||
@@ -108,7 +136,15 @@ public class DefaultOutboundRequestMapperTests {
|
||||
form.put("b", Collections.EMPTY_LIST);
|
||||
form.put("c", Collections.singletonList("3"));
|
||||
Message<?> message = MessageBuilder.withPayload(form).build();
|
||||
HttpEntity<?> request = mapper.fromMessage(message);
|
||||
Exception exception = null;
|
||||
try {
|
||||
handler.handleMessage(message);
|
||||
}
|
||||
catch (Exception e) {
|
||||
exception = e;
|
||||
}
|
||||
assertEquals("intentional", exception.getCause().getMessage());
|
||||
HttpEntity<?> request = template.lastRequestEntity.get();
|
||||
Object body = request.getBody();
|
||||
assertTrue(body instanceof Map<?, ?>);
|
||||
Map<?, ?> map = (Map <?, ?>) body;
|
||||
@@ -131,16 +167,25 @@ public class DefaultOutboundRequestMapperTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void nameOnlyWithNullValues() throws Exception {
|
||||
DefaultOutboundRequestMapper mapper = new DefaultOutboundRequestMapper();
|
||||
mapper.setHttpMethod(HttpMethod.POST);
|
||||
Map form = new LinkedHashMap();
|
||||
HttpRequestExecutingMessageHandler handler = new HttpRequestExecutingMessageHandler("http://www.springsource.org/spring-integration");
|
||||
MockRestTemplate template = new MockRestTemplate();
|
||||
new DirectFieldAccessor(handler).setPropertyValue("restTemplate", template);
|
||||
handler.setHttpMethod(HttpMethod.POST);
|
||||
Map<String, Object> form = new LinkedHashMap<String, Object>();
|
||||
form.put("a", null);
|
||||
form.put("b", "foo");
|
||||
form.put("c", null);
|
||||
Message<?> message = MessageBuilder.withPayload(form).build();
|
||||
HttpEntity<?> request = mapper.fromMessage(message);
|
||||
Exception exception = null;
|
||||
try {
|
||||
handler.handleMessage(message);
|
||||
}
|
||||
catch (Exception e) {
|
||||
exception = e;
|
||||
}
|
||||
assertEquals("intentional", exception.getCause().getMessage());
|
||||
HttpEntity<?> request = template.lastRequestEntity.get();
|
||||
Object body = request.getBody();
|
||||
assertTrue(body instanceof Map<?, ?>);
|
||||
Map<?, ?> map = (Map<?, ?>) body;
|
||||
@@ -155,13 +200,23 @@ public class DefaultOutboundRequestMapperTests {
|
||||
|
||||
@Test
|
||||
public void nonFormDataInMap() throws Exception {
|
||||
DefaultOutboundRequestMapper mapper = new DefaultOutboundRequestMapper();
|
||||
mapper.setHttpMethod(HttpMethod.POST);
|
||||
HttpRequestExecutingMessageHandler handler = new HttpRequestExecutingMessageHandler("http://www.springsource.org/spring-integration");
|
||||
MockRestTemplate template = new MockRestTemplate();
|
||||
new DirectFieldAccessor(handler).setPropertyValue("restTemplate", template);
|
||||
handler.setHttpMethod(HttpMethod.POST);
|
||||
Map<String, TestBean> form = new LinkedHashMap<String, TestBean>();
|
||||
form.put("A", new TestBean());
|
||||
form.put("B", new TestBean());
|
||||
Message<?> message = MessageBuilder.withPayload(form).build();
|
||||
HttpEntity<?> request = mapper.fromMessage(message);
|
||||
Exception exception = null;
|
||||
try {
|
||||
handler.handleMessage(message);
|
||||
}
|
||||
catch (Exception e) {
|
||||
exception = e;
|
||||
}
|
||||
assertEquals("intentional", exception.getCause().getMessage());
|
||||
HttpEntity<?> request = template.lastRequestEntity.get();
|
||||
Map<?, ?> map = (Map<?, ?>) request.getBody();
|
||||
assertEquals(2, map.size());
|
||||
assertEquals(TestBean.class, map.get("A").getClass());
|
||||
@@ -173,4 +228,17 @@ public class DefaultOutboundRequestMapperTests {
|
||||
private static class TestBean implements Serializable {
|
||||
}
|
||||
|
||||
|
||||
private static class MockRestTemplate extends RestTemplate {
|
||||
|
||||
private final AtomicReference<HttpEntity<?>> lastRequestEntity = new AtomicReference<HttpEntity<?>>();
|
||||
|
||||
@Override
|
||||
public <T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity,
|
||||
Class<T> responseType, Map<String, ?> uriVariables) throws RestClientException {
|
||||
this.lastRequestEntity.set(requestEntity);
|
||||
throw new RuntimeException("intentional");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -34,9 +34,7 @@ import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.client.ClientHttpRequestFactory;
|
||||
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
||||
import org.springframework.integration.endpoint.AbstractEndpoint;
|
||||
import org.springframework.integration.http.DefaultOutboundRequestMapper;
|
||||
import org.springframework.integration.http.HttpRequestExecutingMessageHandler;
|
||||
import org.springframework.integration.http.OutboundRequestMapper;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@@ -65,17 +63,14 @@ public class HttpOutboundChannelAdapterParserTests {
|
||||
assertEquals(false, handlerAccessor.getPropertyValue("expectReply"));
|
||||
assertEquals(this.applicationContext.getBean("requests"), endpointAccessor.getPropertyValue("inputChannel"));
|
||||
assertNull(handlerAccessor.getPropertyValue("outputChannel"));
|
||||
OutboundRequestMapper mapper = (OutboundRequestMapper) handlerAccessor.getPropertyValue("requestMapper");
|
||||
DirectFieldAccessor templateAccessor = new DirectFieldAccessor(handlerAccessor.getPropertyValue("restTemplate"));
|
||||
ClientHttpRequestFactory requestFactory = (ClientHttpRequestFactory)
|
||||
templateAccessor.getPropertyValue("requestFactory");
|
||||
assertTrue(mapper instanceof DefaultOutboundRequestMapper);
|
||||
assertTrue(requestFactory instanceof SimpleClientHttpRequestFactory);
|
||||
DirectFieldAccessor mapperAccessor = new DirectFieldAccessor(mapper);
|
||||
assertEquals("http://localhost/test1", handlerAccessor.getPropertyValue("uri"));
|
||||
assertEquals(HttpMethod.POST, handlerAccessor.getPropertyValue("httpMethod"));
|
||||
assertEquals("UTF-8", mapperAccessor.getPropertyValue("charset"));
|
||||
assertEquals(true, mapperAccessor.getPropertyValue("extractPayload"));
|
||||
assertEquals("UTF-8", handlerAccessor.getPropertyValue("charset"));
|
||||
assertEquals(true, handlerAccessor.getPropertyValue("extractPayload"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -89,22 +84,19 @@ public class HttpOutboundChannelAdapterParserTests {
|
||||
assertNull(handlerAccessor.getPropertyValue("outputChannel"));
|
||||
assertEquals(77, handlerAccessor.getPropertyValue("order"));
|
||||
assertEquals(Boolean.FALSE, endpointAccessor.getPropertyValue("autoStartup"));
|
||||
OutboundRequestMapper mapper = (OutboundRequestMapper) handlerAccessor.getPropertyValue("requestMapper");
|
||||
DirectFieldAccessor templateAccessor = new DirectFieldAccessor(handlerAccessor.getPropertyValue("restTemplate"));
|
||||
ClientHttpRequestFactory requestFactory = (ClientHttpRequestFactory)
|
||||
templateAccessor.getPropertyValue("requestFactory");
|
||||
assertEquals(Boolean.class, handlerAccessor.getPropertyValue("expectedResponseType"));
|
||||
assertTrue(mapper instanceof DefaultOutboundRequestMapper);
|
||||
assertTrue(requestFactory instanceof SimpleClientHttpRequestFactory);
|
||||
Object converterListBean = this.applicationContext.getBean("converterList");
|
||||
assertEquals(converterListBean, templateAccessor.getPropertyValue("messageConverters"));
|
||||
Object requestFactoryBean = this.applicationContext.getBean("testRequestFactory");
|
||||
assertEquals(requestFactoryBean, requestFactory);
|
||||
DirectFieldAccessor mapperAccessor = new DirectFieldAccessor(mapper);
|
||||
assertEquals("http://localhost/test2/{foo}", handlerAccessor.getPropertyValue("uri"));
|
||||
assertEquals(HttpMethod.GET, handlerAccessor.getPropertyValue("httpMethod"));
|
||||
assertEquals("UTF-8", mapperAccessor.getPropertyValue("charset"));
|
||||
assertEquals(false, mapperAccessor.getPropertyValue("extractPayload"));
|
||||
assertEquals("UTF-8", handlerAccessor.getPropertyValue("charset"));
|
||||
assertEquals(false, handlerAccessor.getPropertyValue("extractPayload"));
|
||||
Map<String, Expression> uriVariableExpressions =
|
||||
(Map<String, Expression>) handlerAccessor.getPropertyValue("uriVariableExpressions");
|
||||
assertEquals(1, uriVariableExpressions.size());
|
||||
|
||||
@@ -36,9 +36,7 @@ import org.springframework.http.client.ClientHttpRequestFactory;
|
||||
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
||||
import org.springframework.integration.core.MessageChannel;
|
||||
import org.springframework.integration.endpoint.AbstractEndpoint;
|
||||
import org.springframework.integration.http.DefaultOutboundRequestMapper;
|
||||
import org.springframework.integration.http.HttpRequestExecutingMessageHandler;
|
||||
import org.springframework.integration.http.OutboundRequestMapper;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@@ -69,17 +67,14 @@ public class HttpOutboundGatewayParserTests {
|
||||
DirectFieldAccessor handlerAccessor = new DirectFieldAccessor(handler);
|
||||
Object replyChannel = handlerAccessor.getPropertyValue("outputChannel");
|
||||
assertNull(replyChannel);
|
||||
OutboundRequestMapper mapper = (OutboundRequestMapper) handlerAccessor.getPropertyValue("requestMapper");
|
||||
DirectFieldAccessor templateAccessor = new DirectFieldAccessor(handlerAccessor.getPropertyValue("restTemplate"));
|
||||
ClientHttpRequestFactory requestFactory = (ClientHttpRequestFactory)
|
||||
templateAccessor.getPropertyValue("requestFactory");
|
||||
assertTrue(mapper instanceof DefaultOutboundRequestMapper);
|
||||
assertTrue(requestFactory instanceof SimpleClientHttpRequestFactory);
|
||||
DirectFieldAccessor mapperAccessor = new DirectFieldAccessor(mapper);
|
||||
assertEquals("http://localhost/test1", handlerAccessor.getPropertyValue("uri"));
|
||||
assertEquals(HttpMethod.POST, handlerAccessor.getPropertyValue("httpMethod"));
|
||||
assertEquals("UTF-8", mapperAccessor.getPropertyValue("charset"));
|
||||
assertEquals(true, mapperAccessor.getPropertyValue("extractPayload"));
|
||||
assertEquals("UTF-8", handlerAccessor.getPropertyValue("charset"));
|
||||
assertEquals(true, handlerAccessor.getPropertyValue("extractPayload"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -96,20 +91,17 @@ public class HttpOutboundGatewayParserTests {
|
||||
Object replyChannel = handlerAccessor.getPropertyValue("outputChannel");
|
||||
assertNotNull(replyChannel);
|
||||
assertEquals(this.applicationContext.getBean("replies"), replyChannel);
|
||||
OutboundRequestMapper mapper = (OutboundRequestMapper) handlerAccessor.getPropertyValue("requestMapper");
|
||||
DirectFieldAccessor templateAccessor = new DirectFieldAccessor(handlerAccessor.getPropertyValue("restTemplate"));
|
||||
ClientHttpRequestFactory requestFactory = (ClientHttpRequestFactory)
|
||||
templateAccessor.getPropertyValue("requestFactory");
|
||||
assertTrue(mapper instanceof DefaultOutboundRequestMapper);
|
||||
assertTrue(requestFactory instanceof SimpleClientHttpRequestFactory);
|
||||
Object converterListBean = this.applicationContext.getBean("converterList");
|
||||
assertEquals(converterListBean, templateAccessor.getPropertyValue("messageConverters"));
|
||||
assertEquals(String.class, handlerAccessor.getPropertyValue("expectedResponseType"));
|
||||
DirectFieldAccessor mapperAccessor = new DirectFieldAccessor(mapper);
|
||||
assertEquals("http://localhost/test2", handlerAccessor.getPropertyValue("uri"));
|
||||
assertEquals(HttpMethod.PUT, handlerAccessor.getPropertyValue("httpMethod"));
|
||||
assertEquals("UTF-8", mapperAccessor.getPropertyValue("charset"));
|
||||
assertEquals(false, mapperAccessor.getPropertyValue("extractPayload"));
|
||||
assertEquals("UTF-8", handlerAccessor.getPropertyValue("charset"));
|
||||
assertEquals(false, handlerAccessor.getPropertyValue("extractPayload"));
|
||||
Object requestFactoryBean = this.applicationContext.getBean("testRequestFactory");
|
||||
assertEquals(requestFactoryBean, requestFactory);
|
||||
Object sendTimeout = new DirectFieldAccessor(
|
||||
|
||||
Reference in New Issue
Block a user