[CAMEL-15370] CxfRsProducer: All but last value of query parameter with multiple values are lost (#4076)
* [CAMEL-15370] CxfRsProducer: All but last value of query parameter with
Multiple Values are lost
* [CAMEL-15370] CxfRsProducer: All but last value of query parameter with
multiple values are lost
* fixed tabs/spaces
* tabs/spaces
Co-authored-by: k5 <me@k-5.de>
diff --git a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducer.java b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducer.java
index 4c4d227..6467f7b 100644
--- a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducer.java
+++ b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducer.java
@@ -237,25 +237,27 @@
CxfRsEndpoint cxfRsEndpoint = (CxfRsEndpoint) getEndpoint();
// check if there is a query map in the message header
Map<String, String> maps = inMessage.getHeader(CxfConstants.CAMEL_CXF_RS_QUERY_MAP, Map.class);
- if (maps == null) {
- // Get the map from HTTP_QUERY header
+ if (maps != null) {
+ insertQueryParametersFromMap(client, maps);
+ } else {
String queryString = inMessage.getHeader(Exchange.HTTP_QUERY, String.class);
if (queryString != null) {
- maps = getQueryParametersFromQueryString(queryString,
- ExchangeHelper.getCharsetName(exchange));
+ // Insert QueryParameters from HTTP_QUERY header
+ insertQueryParametersFromQueryString(client, queryString, ExchangeHelper.getCharsetName(exchange));
+ } else {
+ insertQueryParametersFromMap(client, cxfRsEndpoint.getParameters());
}
}
- if (maps == null) {
- maps = cxfRsEndpoint.getParameters();
- }
+
+ setupClientHeaders(client, exchange);
+ }
+
+ private void insertQueryParametersFromMap(WebClient client, Map<String, String> maps) {
if (maps != null) {
for (Map.Entry<String, String> entry : maps.entrySet()) {
client.query(entry.getKey(), entry.getValue());
}
}
-
- setupClientHeaders(client, exchange);
-
}
protected void setupClientMatrix(WebClient client, Exchange exchange) throws Exception {
@@ -482,19 +484,17 @@
return clientFactoryBeanCache;
}
- private Map<String, String> getQueryParametersFromQueryString(String queryString, String charset) throws UnsupportedEncodingException {
- Map<String, String> answer = new LinkedHashMap<>();
+ private void insertQueryParametersFromQueryString(WebClient client, String queryString, String charset) throws UnsupportedEncodingException {
for (String param : queryString.split("&")) {
String[] pair = param.split("=", 2);
if (pair.length == 2) {
String name = URLDecoder.decode(pair[0], charset);
String value = URLDecoder.decode(pair[1], charset);
- answer.put(name, value);
+ client.query(name, value);
} else {
throw new IllegalArgumentException("Invalid parameter, expected to be a pair but was " + param);
}
}
- return answer;
}
private Method findRightMethod(List<Class<?>> resourceClasses, String methodName,
diff --git a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducerTest.java b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducerTest.java
index 3818f6d..16e8042 100644
--- a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducerTest.java
+++ b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducerTest.java
@@ -410,7 +410,47 @@
assertEquals("q1=new&q2=world", response, "The response value is wrong");
}
-
+ @Test
+ public void testProducerWithQueryParametersMultipleValues() {
+ Exchange exchange = template.send("cxfrs://http://localhost:" + getPort2() + "/" + getClass().getSimpleName() + "/testQuery?httpClientAPI=true&synchronous=true", new Processor() {
+ public void process(Exchange exchange) throws Exception {
+ exchange.setPattern(ExchangePattern.InOut);
+ Message inMessage = exchange.getIn();
+ // set the Http method
+ inMessage.setHeader(Exchange.HTTP_METHOD, "GET");
+ inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_RESPONSE_CLASS, InputStream.class);
+ inMessage.setHeader(Exchange.HTTP_QUERY, "id=1&id=2");
+ inMessage.setBody(null);
+ }
+
+ });
+
+ // get the response message
+ String response = exchange.getOut().getBody(String.class);
+ assertNotNull(response, "The response should not be null");
+ assertEquals("id=1&id=2", response, "The response value is wrong");
+ }
+
+ @Test
+ public void testProducerWithQueryParametersEscapeAmpersand() {
+ Exchange exchange = template.send("cxfrs://http://localhost:" + getPort2() + "/" + getClass().getSimpleName() + "/testQuery?httpClientAPI=true&synchronous=true", new Processor() {
+ public void process(Exchange exchange) throws Exception {
+ exchange.setPattern(ExchangePattern.InOut);
+ Message inMessage = exchange.getIn();
+ // set the Http method
+ inMessage.setHeader(Exchange.HTTP_METHOD, "GET");
+ inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_RESPONSE_CLASS, InputStream.class);
+ inMessage.setHeader(Exchange.HTTP_QUERY, "id=1%262");
+ inMessage.setBody(null);
+ }
+
+ });
+
+ // get the response message
+ String response = exchange.getOut().getBody(String.class);
+ assertNotNull(response, "The response should not be null");
+ assertEquals("id=1%262", response, "The response value is wrong");
+ }
@Test
public void testRestServerDirectlyGetCustomer() {