Skip to content

Commit ebac25e

Browse files
committed
[breaking] params - url-decoding restx path by default.
if you want to keep old behaviour (no decoded path), simply @provide 'restx.http.decode.url.path.params' key with false value
1 parent 46e2ff7 commit ebac25e

File tree

6 files changed

+27
-4
lines changed

6 files changed

+27
-4
lines changed

restx-core/src/main/java/restx/AbstractRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* Time: 18:38
1919
*/
2020
public abstract class AbstractRequest implements RestxRequest {
21-
private final HttpSettings httpSettings;
21+
protected final HttpSettings httpSettings;
2222

2323
protected AbstractRequest(HttpSettings httpSettings) {
2424
this.httpSettings = httpSettings;

restx-core/src/main/java/restx/HttpSettings.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,8 @@ public interface HttpSettings {
2121

2222
@SettingsKey(key = "restx.http.gzip.paths", defaultValue = "/{s:.+}")
2323
Collection<String> gzipPaths();
24+
25+
@SettingsKey(key = "restx.http.decode.url.path.params", defaultValue = "true",
26+
doc="Will issue a URLDecoder.decode() on every PATH parameters if true")
27+
boolean decodeURLPathParams();
2428
}

restx-core/src/main/java/restx/HttpSettingsConfig.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,9 @@ public Collection<String> gzipPaths() {
3939
return Splitter.on(",").trimResults().splitToList(
4040
config.getString("restx.http.gzip.paths").or("/{s:.+}"));
4141
}
42+
43+
@Override
44+
public boolean decodeURLPathParams() {
45+
return config.getBoolean("restx.http.decode.url.path.params").or(Boolean.TRUE).booleanValue();
46+
}
4247
}

restx-core/src/main/resources/restx/httpConfig.properties

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,7 @@ restx.http.scheme=
1515

1616
# the collection of RESTX paths on which gzip filter should be applied
1717
# to enable it on all resources, use `/{s:.+}`
18-
restx.http.gzip.paths=/{s:.+}
18+
restx.http.gzip.paths=/{s:.+}
19+
20+
# Will issue a URLDecoder.decode() on restx path resolution
21+
restx.http.decode.url.path.params=true

restx-samplest/src/test/java/samplest/core/UrlParametersResourceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class UrlParametersResourceTest {
2727
@Parameterized.Parameters(name="{0}")
2828
public static Iterable<Object[]> data(){
2929
return Arrays.asList(new Object[][]{
30-
{"/v1/v2/35v4/v5", "a=v1 b=v2 c=35 d=v4 e=v5"}
30+
{"/v1/hello%20world/35v4/v5", "a=v1 b=hello world c=35 d=v4 e=v5"},
3131
});
3232
}
3333

restx-servlet/src/main/java/restx/servlet/HttpServletRestxRequest.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package restx.servlet;
22

33
import com.google.common.base.Optional;
4+
import com.google.common.base.Throwables;
45
import com.google.common.collect.*;
56
import restx.AbstractRequest;
67
import restx.HttpSettings;
@@ -11,6 +12,8 @@
1112
import java.io.BufferedInputStream;
1213
import java.io.IOException;
1314
import java.io.InputStream;
15+
import java.io.UnsupportedEncodingException;
16+
import java.net.URLDecoder;
1417
import java.util.Collections;
1518
import java.util.List;
1619
import java.util.Locale;
@@ -48,7 +51,15 @@ protected String getLocalScheme() {
4851

4952
@Override
5053
public String getRestxPath() {
51-
return request.getRequestURI().substring((getBaseApiPath()).length());
54+
String restxPath = request.getRequestURI().substring((getBaseApiPath()).length());
55+
if(this.httpSettings.decodeURLPathParams()) {
56+
try {
57+
restxPath = URLDecoder.decode(restxPath, "UTF-8");
58+
} catch (UnsupportedEncodingException e) {
59+
throw Throwables.propagate(e);
60+
}
61+
}
62+
return restxPath;
5263
}
5364

5465
@Override

0 commit comments

Comments
 (0)