Skip to content

Commit

Permalink
Fix #182
Browse files Browse the repository at this point in the history
Signed-off-by: Clement Escoffier <clement.escoffier@gmail.com>
  • Loading branch information
cescoffier committed Apr 22, 2014
1 parent af5cb75 commit 7b44eb2
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions core/router/src/main/java/org/wisdom/router/RequestRouter.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import org.wisdom.api.router.RoutingException;

import javax.validation.Validator;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;

/**
Expand Down Expand Up @@ -177,15 +179,13 @@ private String computeUrlForRoute(Route route, Map<String, Object> params) {
String originalRegexEscaped = String.format("\\{%s\\}", entry.getKey());

// The value that will be added into the regex => myId for instance...
String resultingRegexReplacement = entry.getValue().toString();
String resultingRegexReplacement = pathEncode(entry.getValue().toString());

// If regex is in the url as placeholder we replace the placeholder
if (urlWithReplacedPlaceholders.contains(originalRegex)) {

urlWithReplacedPlaceholders = urlWithReplacedPlaceholders.replaceAll(
originalRegexEscaped,
resultingRegexReplacement);

// If the parameter is not there as placeholder we add it as queryParameter
} else {
queryParameterMap.put(entry.getKey(), entry.getValue());
Expand All @@ -204,7 +204,8 @@ private String computeUrlForRoute(Route route, Map<String, Object> params) {
Map.Entry<String, Object> queryParameterEntry = iterator.next();
queryParameterStringBuffer.append(queryParameterEntry.getKey());
queryParameterStringBuffer.append("=");
queryParameterStringBuffer.append(queryParameterEntry.getValue());
// Don't forget to encode the value.
queryParameterStringBuffer.append(encode(queryParameterEntry.getValue().toString()));

if (iterator.hasNext()) {
queryParameterStringBuffer.append("&");
Expand All @@ -221,6 +222,21 @@ private String computeUrlForRoute(Route route, Map<String, Object> params) {
return urlWithReplacedPlaceholders;
}

private String pathEncode(String s) {
// TODO Implement the RFC 3986, 3.3. Path.
return s
.replace(" ", "%20")
.replace("/", "%2F");
}

private String encode(String v) {
try {
return URLEncoder.encode(v, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException("UTF-8 not supported", e);
}
}

public Validator getValidator() {
return validator;
}
Expand Down

0 comments on commit 7b44eb2

Please sign in to comment.