INT-3054 URI Variable Mapping Improvements

Permit missing URI vars in HTTP outbound gateway

Do not attempt to evaluate uri variables that are not in the URL

The http gateway might not know what uri variables it might obtain in the URI.
This patch makes sure that only the uri variables defined in the uri are parsed (the rest are ignored)
This commit is contained in:
Wallace Wadge
2013-06-10 11:19:08 +02:00
committed by Gary Russell
parent 0301f262e0
commit 3369d3f9b1
2 changed files with 47 additions and 4 deletions

View File

@@ -63,6 +63,7 @@ import org.springframework.web.client.ResponseErrorHandler;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
import org.springframework.web.util.UriTemplate;
/**
* A {@link MessageHandler} implementation that executes HTTP requests by delegating
@@ -79,6 +80,7 @@ import org.springframework.web.util.UriComponentsBuilder;
* @author Gary Russell
* @author Gunnar Hillert
* @author Artem Bilan
* @author Wallace Wadge
* @since 2.0
*/
public class HttpRequestExecutingMessageHandler extends AbstractReplyProducingMessageHandler {
@@ -337,11 +339,19 @@ public class HttpRequestExecutingMessageHandler extends AbstractReplyProducingMe
protected Object handleRequestMessage(Message<?> requestMessage) {
String uri = this.uriExpression.getValue(this.evaluationContext, requestMessage, String.class);
Assert.notNull(uri, "URI Expression evaluation cannot result in null");
List<String> uriVariableNames = new UriTemplate(uri).getVariableNames();
if (logger.isWarnEnabled() && this.uriVariableExpressions.size() != uriVariableNames.size()){
logger.warn("The number of URI variables expecting resolution in the provided uri do not match those in the provided variables");
}
try {
Map<String, Object> uriVariables = new HashMap<String, Object>();
for (Map.Entry<String, Expression> entry : this.uriVariableExpressions.entrySet()) {
Object value = entry.getValue().getValue(this.evaluationContext, requestMessage, String.class);
uriVariables.put(entry.getKey(), value);
for (String var : uriVariableNames) {
Expression exp = this.uriVariableExpressions.get(var);
if (exp != null){
Object value = exp.getValue(this.evaluationContext, requestMessage, String.class);
uriVariables.put(var, value);
}
}
HttpMethod httpMethod = this.determineHttpMethod(requestMessage);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2013 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.
@@ -21,10 +21,13 @@ import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.Test;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpRequest;
@@ -35,6 +38,7 @@ import org.springframework.integration.message.GenericMessage;
/**
* @author Dave Syer
* @author Mark Fisher
* @author Wallace Wadge
* @since 2.0
*/
public class UriVariableExpressionTests {
@@ -65,4 +69,33 @@ public class UriVariableExpressionTests {
assertEquals("http://test/bar", uriHolder.get().toString());
}
/** Test for INT-3054: Do not break if there are extra uri variables defined in the http outbound gateway. */
@Test
public void testFromMessageWithSuperfluousExpressionsInt3054() throws Exception {
final AtomicReference<URI> uriHolder = new AtomicReference<URI>();
HttpRequestExecutingMessageHandler handler = new HttpRequestExecutingMessageHandler("http://test/{foo}");
SpelExpressionParser parser = new SpelExpressionParser();
Map<String, Expression> multipleExpressions = new HashMap<String, Expression>();
multipleExpressions.put("foo", parser.parseExpression("payload"));
multipleExpressions.put("extra-to-be-ignored", parser.parseExpression("headers.extra"));
handler.setUriVariableExpressions(multipleExpressions);
handler.setRequestFactory(new SimpleClientHttpRequestFactory() {
@Override
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
uriHolder.set(uri);
throw new RuntimeException("intentional");
}
});
Message<?> message = new GenericMessage<Object>("bar");
Exception exception = null;
try {
handler.handleMessage(message);
}
catch (Exception e) {
exception = e;
}
assertEquals("intentional", exception.getCause().getMessage());
assertEquals("http://test/bar", uriHolder.get().toString());
}
}