Skip to content

Commit

Permalink
CAMEL-8417: Fixed RAW on endpoints to support multiple querty paramet…
Browse files Browse the repository at this point in the history
…ers of the same key.
  • Loading branch information
davsclaus committed Mar 2, 2015
1 parent 8db720c commit 39947ce
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 4 deletions.
27 changes: 23 additions & 4 deletions camel-core/src/main/java/org/apache/camel/util/URISupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -295,13 +296,31 @@ public static Map<String, Object> parseParameters(URI uri) throws URISyntaxExcep
* @see #RAW_TOKEN_START
* @see #RAW_TOKEN_END
*/
@SuppressWarnings("unchecked")
public static void resolveRawParameterValues(Map<String, Object> parameters) {
for (Map.Entry<String, Object> entry : parameters.entrySet()) {
if (entry.getValue() != null) {
String value = entry.getValue().toString();
if (value.startsWith(RAW_TOKEN_START) && value.endsWith(RAW_TOKEN_END)) {
value = value.substring(4, value.length() - 1);
entry.setValue(value);
// if the value is a list then we need to iterate
Object value = entry.getValue();
if (value instanceof List) {
List list = (List) value;
for (int i = 0; i < list.size(); i++) {
Object obj = list.get(i);
if (obj != null) {
String str = obj.toString();
if (str.startsWith(RAW_TOKEN_START) && str.endsWith(RAW_TOKEN_END)) {
str = str.substring(4, str.length() - 1);
// update the string in the list
list.set(i, str);
}
}
}
} else {
String str = entry.getValue().toString();
if (str.startsWith(RAW_TOKEN_START) && str.endsWith(RAW_TOKEN_END)) {
str = str.substring(4, str.length() - 1);
entry.setValue(str);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.camel.issues;

import java.util.List;
import java.util.Map;

import org.apache.camel.Component;
Expand Down Expand Up @@ -46,6 +47,7 @@ public final class MyEndpoint extends DefaultEndpoint {

private String username;
private String password;
private List<String> lines;

public MyEndpoint(String endpointUri, Component component) {
super(endpointUri, component);
Expand All @@ -58,6 +60,7 @@ public Producer createProducer() throws Exception {
public void process(Exchange exchange) throws Exception {
exchange.getIn().setHeader("username", getUsername());
exchange.getIn().setHeader("password", getPassword());
exchange.getIn().setHeader("lines", getLines());
}
};
}
Expand Down Expand Up @@ -87,6 +90,14 @@ public String getPassword() {
public void setPassword(String password) {
this.password = password;
}

public List<String> getLines() {
return lines;
}

public void setLines(List<String> lines) {
this.lines = lines;
}
}

public void testRawUriParameter() throws Exception {
Expand All @@ -97,7 +108,32 @@ public void testRawUriParameter() throws Exception {
template.sendBody("direct:start", "Hello World");

assertMockEndpointsSatisfied();
}

public void testUriParameterLines() throws Exception {
getMockEndpoint("mock:result").expectedMessageCount(1);

template.sendBody("direct:lines", "Hello World");

assertMockEndpointsSatisfied();

List<String> lines = (List<String>) getMockEndpoint("mock:result").getReceivedExchanges().get(0).getIn().getHeader("lines");
assertEquals(2, lines.size());
assertEquals("abc", lines.get(0));
assertEquals("def", lines.get(1));
}

public void testRawUriParameterLines() throws Exception {
getMockEndpoint("mock:result").expectedMessageCount(1);

template.sendBody("direct:rawlines", "Hello World");

assertMockEndpointsSatisfied();

List<String> lines = (List<String>) getMockEndpoint("mock:result").getReceivedExchanges().get(0).getIn().getHeader("lines");
assertEquals(2, lines.size());
assertEquals("++abc++", lines.get(0));
assertEquals("++def++", lines.get(1));
}

@Override
Expand All @@ -110,6 +146,14 @@ public void configure() throws Exception {
from("direct:start")
.to("mycomponent:foo?username=scott&password=RAW(++%%w?rd))")
.to("mock:result");

from("direct:lines")
.to("mycomponent:foo?lines=abc&lines=def")
.to("mock:result");

from("direct:rawlines")
.to("mycomponent:foo?lines=RAW(++abc++)&lines=RAW(++def++)")
.to("mock:result");
}
};
}
Expand Down

0 comments on commit 39947ce

Please sign in to comment.