INT-3813: HTTP module improvements

JIRA: https://jira.spring.io/browse/INT-3813
This commit is contained in:
Artem Bilan
2015-08-30 08:50:04 -04:00
parent f3a8f04f68
commit c86141cff7
6 changed files with 29 additions and 28 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2015 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.
@@ -62,7 +62,7 @@ public class HttpOutboundChannelAdapterParser extends AbstractOutboundChannelAda
String mappedRequestHeaders = element.getAttribute("mapped-request-headers");
if (StringUtils.hasText(headerMapper)) {
if (StringUtils.hasText(mappedRequestHeaders)) {
parserContext.getReaderContext().error("The 'mappped-request-headers' attribute is not " +
parserContext.getReaderContext().error("The 'mapped-request-headers' attribute is not " +
"allowed when a 'header-mapper' has been specified.", parserContext.extractSource(element));
return null;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2015 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.
@@ -66,7 +66,7 @@ public class HttpOutboundGatewayParser extends AbstractConsumerEndpointParser {
String mappedResponseHeaders = element.getAttribute("mapped-response-headers");
if (StringUtils.hasText(headerMapper)) {
if (StringUtils.hasText(mappedRequestHeaders) || StringUtils.hasText(mappedResponseHeaders)) {
parserContext.getReaderContext().error("Neither 'mappped-request-headers' or 'mapped-response-headers' " +
parserContext.getReaderContext().error("Neither 'mapped-request-headers' or 'mapped-response-headers' " +
"attributes are allowed when a 'header-mapper' has been specified.", parserContext.extractSource(element));
return null;
}

View File

@@ -116,7 +116,7 @@ public abstract class HttpRequestHandlingEndpointSupport extends MessagingGatewa
private static final boolean jaxb2Present = ClassUtils.isPresent("javax.xml.bind.Binder",
HttpRequestHandlingEndpointSupport.class.getClassLoader());
private static boolean romeToolsPresent = ClassUtils.isPresent("com.rometools.rome.feed.atom.Feed",
private static final boolean romeToolsPresent = ClassUtils.isPresent("com.rometools.rome.feed.atom.Feed",
HttpRequestHandlingEndpointSupport.class.getClassLoader());
private static final List<HttpMethod> nonReadableBodyHttpMethods =
@@ -580,8 +580,7 @@ public abstract class HttpRequestHandlingEndpointSupport extends MessagingGatewa
this.evaluationContext = createEvaluationContext();
}
Object value = this.statusCodeExpression.getValue(this.evaluationContext);
HttpStatus httpStatus = buildHttpStatus(value);
return httpStatus;
return buildHttpStatus(value);
}
/**

View File

@@ -103,7 +103,7 @@ public class HttpRequestExecutingMessageHandler extends AbstractReplyProducingMe
private volatile boolean extractPayloadExplicitlySet = false;
private volatile String charset = "UTF-8";
private volatile Charset charset = Charset.forName("UTF-8");
private volatile boolean transferCookies = false;
@@ -219,7 +219,7 @@ public class HttpRequestExecutingMessageHandler extends AbstractReplyProducingMe
*/
public void setCharset(String charset) {
Assert.isTrue(Charset.isSupported(charset), "unsupported charset '" + charset + "'");
this.charset = charset;
this.charset = Charset.forName(charset);
}
/**
@@ -245,17 +245,15 @@ public class HttpRequestExecutingMessageHandler extends AbstractReplyProducingMe
*/
public void setExpectedResponseType(Class<?> expectedResponseType) {
Assert.notNull(expectedResponseType, "'expectedResponseType' must not be null");
this.expectedResponseTypeExpression = new LiteralExpression(expectedResponseType.getName());
this.expectedResponseTypeExpression = new ValueExpression<Class<?>>(expectedResponseType);
}
/**
* Specify the {@link Expression} to determine the type for the expected response
* The returned value of the expression could be an instance of {@link Class} or
* {@link String} representing a fully qualified class name
*
* @param expectedResponseTypeExpression The expected response type expression.
*
* Also see {@link #setExpectedResponseTypeExpression(Expression)}
* Also see {@link #setExpectedResponseType}
*/
public void setExpectedResponseTypeExpression(Expression expectedResponseTypeExpression) {
this.expectedResponseTypeExpression = expectedResponseTypeExpression;
@@ -452,8 +450,9 @@ public class HttpRequestExecutingMessageHandler extends AbstractReplyProducingMe
}
// otherwise, we are creating a request with a body and need to deal with the content-type header as well
if (httpHeaders.getContentType() == null) {
MediaType contentType = (payload instanceof String) ? this.resolveContentType((String) payload, this.charset)
: this.resolveContentType(payload);
MediaType contentType = (payload instanceof String)
? resolveContentType((String) payload, this.charset)
: resolveContentType(payload);
httpHeaders.setContentType(contentType);
}
if (MediaType.APPLICATION_FORM_URLENCODED.equals(httpHeaders.getContentType()) ||
@@ -511,8 +510,8 @@ public class HttpRequestExecutingMessageHandler extends AbstractReplyProducingMe
return !HttpMethod.GET.equals(httpMethod);
}
private MediaType resolveContentType(String content, String charset) {
return new MediaType("text", "plain", Charset.forName(charset));
private MediaType resolveContentType(String content, Charset charset) {
return new MediaType("text", "plain", charset);
}
private MultiValueMap<Object, Object> convertToMultiValueMap(Map<?, ?> simpleMap) {
@@ -602,7 +601,8 @@ public class HttpRequestExecutingMessageHandler extends AbstractReplyProducingMe
"'expectedResponseType' can be an instance of 'Class<?>', 'String' or 'ParameterizedTypeReference<?>'; "
+ "evaluation resulted in a" + expectedResponseType.getClass() + ".");
if (expectedResponseType instanceof String && StringUtils.hasText((String) expectedResponseType)){
expectedResponseType = ClassUtils.forName((String) expectedResponseType, ClassUtils.getDefaultClassLoader());
expectedResponseType = ClassUtils.forName((String) expectedResponseType,
getApplicationContext().getClassLoader());
}
}
return expectedResponseType;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 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.
@@ -25,6 +25,7 @@ import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Map;
import org.hamcrest.Matchers;
@@ -118,7 +119,7 @@ public class HttpOutboundChannelAdapterParserTests {
Expression uriExpression = (Expression) handlerAccessor.getPropertyValue("uriExpression");
assertEquals("http://localhost/test1", uriExpression.getValue());
assertEquals(HttpMethod.POST.name(), TestUtils.getPropertyValue(handler, "httpMethodExpression", Expression.class).getExpressionString());
assertEquals("UTF-8", handlerAccessor.getPropertyValue("charset"));
assertEquals(Charset.forName("UTF-8"), handlerAccessor.getPropertyValue("charset"));
assertEquals(true, handlerAccessor.getPropertyValue("extractPayload"));
}
@@ -147,7 +148,7 @@ public class HttpOutboundChannelAdapterParserTests {
Expression uriExpression = (Expression) handlerAccessor.getPropertyValue("uriExpression");
assertEquals("http://localhost/test2/{foo}", uriExpression.getValue());
assertEquals(HttpMethod.GET.name(), TestUtils.getPropertyValue(handler, "httpMethodExpression", Expression.class).getExpressionString());
assertEquals("UTF-8", handlerAccessor.getPropertyValue("charset"));
assertEquals(Charset.forName("UTF-8"), handlerAccessor.getPropertyValue("charset"));
assertEquals(false, handlerAccessor.getPropertyValue("extractPayload"));
Map<String, Expression> uriVariableExpressions =
(Map<String, Expression>) handlerAccessor.getPropertyValue("uriVariableExpressions");
@@ -192,7 +193,7 @@ public class HttpOutboundChannelAdapterParserTests {
Expression uriExpression = (Expression) handlerAccessor.getPropertyValue("uriExpression");
assertEquals("http://localhost/test1", uriExpression.getValue());
assertEquals(HttpMethod.POST.name(), TestUtils.getPropertyValue(handler, "httpMethodExpression", Expression.class).getExpressionString());
assertEquals("UTF-8", handlerAccessor.getPropertyValue("charset"));
assertEquals(Charset.forName("UTF-8"), handlerAccessor.getPropertyValue("charset"));
assertEquals(true, handlerAccessor.getPropertyValue("extractPayload"));
//INT-3055
@@ -223,7 +224,7 @@ public class HttpOutboundChannelAdapterParserTests {
assertNotNull(expression);
assertEquals("'http://localhost/test1'", expression.getExpressionString());
assertEquals(HttpMethod.POST.name(), TestUtils.getPropertyValue(handler, "httpMethodExpression", Expression.class).getExpressionString());
assertEquals("UTF-8", handlerAccessor.getPropertyValue("charset"));
assertEquals(Charset.forName("UTF-8"), handlerAccessor.getPropertyValue("charset"));
assertEquals(true, handlerAccessor.getPropertyValue("extractPayload"));
}
@@ -253,7 +254,7 @@ public class HttpOutboundChannelAdapterParserTests {
assertNotNull(expression);
assertEquals("'http://localhost/test1'", expression.getExpressionString());
assertEquals(HttpMethod.POST.name(), TestUtils.getPropertyValue(handler, "httpMethodExpression", Expression.class).getExpressionString());
assertEquals("UTF-8", handlerAccessor.getPropertyValue("charset"));
assertEquals(Charset.forName("UTF-8"), handlerAccessor.getPropertyValue("charset"));
assertEquals(true, handlerAccessor.getPropertyValue("extractPayload"));
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 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.
@@ -24,6 +24,7 @@ import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Map;
import org.hamcrest.Matchers;
@@ -102,7 +103,7 @@ public class HttpOutboundGatewayParserTests {
Expression uriExpression = (Expression) handlerAccessor.getPropertyValue("uriExpression");
assertEquals("http://localhost/test1", uriExpression.getValue());
assertEquals(HttpMethod.POST.name(), TestUtils.getPropertyValue(handler, "httpMethodExpression", Expression.class).getExpressionString());
assertEquals("UTF-8", handlerAccessor.getPropertyValue("charset"));
assertEquals(Charset.forName("UTF-8"), handlerAccessor.getPropertyValue("charset"));
assertEquals(true, handlerAccessor.getPropertyValue("extractPayload"));
assertEquals(false, handlerAccessor.getPropertyValue("transferCookies"));
}
@@ -132,7 +133,7 @@ public class HttpOutboundGatewayParserTests {
Expression uriExpression = (Expression) handlerAccessor.getPropertyValue("uriExpression");
assertEquals("http://localhost/test2", uriExpression.getValue());
assertEquals(HttpMethod.PUT.name(), TestUtils.getPropertyValue(handler, "httpMethodExpression", Expression.class).getExpressionString());
assertEquals("UTF-8", handlerAccessor.getPropertyValue("charset"));
assertEquals(Charset.forName("UTF-8"), handlerAccessor.getPropertyValue("charset"));
assertEquals(false, handlerAccessor.getPropertyValue("extractPayload"));
Object requestFactoryBean = this.applicationContext.getBean("testRequestFactory");
assertEquals(requestFactoryBean, requestFactory);
@@ -174,7 +175,7 @@ public class HttpOutboundGatewayParserTests {
assertNotNull(expression);
assertEquals("'http://localhost/test1'", expression.getExpressionString());
assertEquals(HttpMethod.POST.name(), TestUtils.getPropertyValue(handler, "httpMethodExpression", Expression.class).getExpressionString());
assertEquals("UTF-8", handlerAccessor.getPropertyValue("charset"));
assertEquals(Charset.forName("UTF-8"), handlerAccessor.getPropertyValue("charset"));
assertEquals(true, handlerAccessor.getPropertyValue("extractPayload"));
assertEquals(false, handlerAccessor.getPropertyValue("transferCookies"));