Repository: camel Updated Branches: refs/heads/master 222817838 -> c0f89d60f
CAMEL-11050 Support for optional query paramete... ...rs in REST component This adds support for optional query parameters in REST component. Optional query parameters will not be added to the dispatched query parameters if not present in the incoming message headers. To make a query parameter optional one needs to denote it by adding `?` to the header name, for instance: `param={param?}`. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/35638b28 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/35638b28 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/35638b28 Branch: refs/heads/master Commit: 35638b280be7ca43031ea4c5d937590f30495e70 Parents: 2228178 Author: Zoran Regvart <zregv...@apache.org> Authored: Wed Mar 22 19:36:09 2017 +0100 Committer: Zoran Regvart <zregv...@apache.org> Committed: Wed Mar 22 22:44:17 2017 +0100 ---------------------------------------------------------------------- .../camel/component/rest/RestProducer.java | 61 +++++++++------- .../camel/component/rest/RestProducerTest.java | 75 ++++++++++++++++++++ 2 files changed, 112 insertions(+), 24 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/35638b28/camel-core/src/main/java/org/apache/camel/component/rest/RestProducer.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/component/rest/RestProducer.java b/camel-core/src/main/java/org/apache/camel/component/rest/RestProducer.java index 3fbf279..a69b1f6 100644 --- a/camel-core/src/main/java/org/apache/camel/component/rest/RestProducer.java +++ b/camel-core/src/main/java/org/apache/camel/component/rest/RestProducer.java @@ -16,8 +16,11 @@ */ package org.apache.camel.component.rest; +import java.io.UnsupportedEncodingException; +import java.net.URISyntaxException; import java.net.URLDecoder; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.Map; import javax.xml.bind.JAXBContext; @@ -169,30 +172,7 @@ public class RestProducer extends DefaultAsyncProducer { } // resolve uri parameters - String query = getEndpoint().getQueryParameters(); - if (query != null) { - Map<String, Object> params = URISupport.parseQuery(query); - for (Map.Entry<String, Object> entry : params.entrySet()) { - Object v = entry.getValue(); - if (v != null) { - String a = v.toString(); - // decode the key as { may be decoded to %NN - a = URLDecoder.decode(a, "UTF-8"); - if (a.startsWith("{") && a.endsWith("}")) { - String key = a.substring(1, a.length() - 1); - String value = inMessage.getHeader(key, String.class); - if (value != null) { - params.put(key, value); - } else { - params.put(entry.getKey(), entry.getValue()); - } - } else { - params.put(entry.getKey(), entry.getValue()); - } - } - } - query = URISupport.createQueryString(params); - } + String query = createQueryParameters(getEndpoint().getQueryParameters(), inMessage); if (query != null) { // the query parameters for the rest call to be used @@ -390,4 +370,37 @@ public class RestProducer extends DefaultAsyncProducer { return key.startsWith("json.in.") || key.startsWith("json.out.") || key.startsWith("xml.in.") || key.startsWith("xml.out."); } + static String createQueryParameters(String query, Message inMessage) throws URISyntaxException, UnsupportedEncodingException { + if (query != null) { + final Map<String, Object> givenParams = URISupport.parseQuery(query); + final Map<String, Object> params = new LinkedHashMap<>(givenParams.size()); + for (Map.Entry<String, Object> entry : givenParams.entrySet()) { + Object v = entry.getValue(); + if (v != null) { + String a = v.toString(); + // decode the key as { may be decoded to %NN + a = URLDecoder.decode(a, "UTF-8"); + if (a.startsWith("{") && a.endsWith("}")) { + String key = a.substring(1, a.length() - 1); + boolean optional = false; + if (key.endsWith("?")) { + key = key.substring(0, key.length() - 1); + optional = true; + } + String value = inMessage.getHeader(key, String.class); + if (value != null) { + params.put(key, value); + } else if (!optional) { + // value is null and parameter is not optional + params.put(entry.getKey(), entry.getValue()); + } + } else { + params.put(entry.getKey(), entry.getValue()); + } + } + } + query = URISupport.createQueryString(params); + } + return query; + } } http://git-wip-us.apache.org/repos/asf/camel/blob/35638b28/camel-core/src/test/java/org/apache/camel/component/rest/RestProducerTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/component/rest/RestProducerTest.java b/camel-core/src/test/java/org/apache/camel/component/rest/RestProducerTest.java new file mode 100644 index 0000000..2ca0981 --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/component/rest/RestProducerTest.java @@ -0,0 +1,75 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.camel.component.rest; + +import java.io.UnsupportedEncodingException; +import java.net.URISyntaxException; + +import org.apache.camel.impl.DefaultMessage; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +public class RestProducerTest { + + @Test + public void shouldCreateDefinedQueryParameters() throws UnsupportedEncodingException, URISyntaxException { + assertEquals("param=value", RestProducer.createQueryParameters("param=value", null)); + } + + @Test + public void shouldCreateOptionalPlaceholderQueryParametersForPresentValues() + throws UnsupportedEncodingException, URISyntaxException { + final DefaultMessage message = new DefaultMessage(); + message.setHeader("param", "header"); + + assertEquals("param=header", RestProducer.createQueryParameters("param={param?}", message)); + } + + @Test + public void shouldCreatePlaceholderQueryParameters() throws UnsupportedEncodingException, URISyntaxException { + final DefaultMessage message = new DefaultMessage(); + message.setHeader("param", "header"); + + assertEquals("param=header", RestProducer.createQueryParameters("param={param}", message)); + } + + @Test + public void shouldCreateQueryNoParameters() throws UnsupportedEncodingException, URISyntaxException { + assertNull(RestProducer.createQueryParameters(null, null)); + } + + @Test + public void shouldNotCreateOptionalPlaceholderQueryParametersForMissingValues() + throws UnsupportedEncodingException, URISyntaxException { + final DefaultMessage message = new DefaultMessage(); + + assertEquals("", RestProducer.createQueryParameters("param={param?}", message)); + } + + @Test + public void shouldSupportAllCombinations() throws UnsupportedEncodingException, URISyntaxException { + final DefaultMessage message = new DefaultMessage(); + message.setHeader("required", "header_required"); + message.setHeader("optional_present", "header_optional_present"); + + assertEquals("given=value&required=header_required&optional_present=header_optional_present", + RestProducer.createQueryParameters( + "given=value&required={required}&optional={optional?}&optional_present={optional_present?}", message)); + } +}