Skip to content

Commit

Permalink
Fixed resource and view name inference from request path.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Apr 17, 2016
1 parent 00d42f0 commit 00ba765
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 18 deletions.
7 changes: 5 additions & 2 deletions rapidoid-commons/src/main/java/org/rapidoid/commons/Str.java
Expand Up @@ -82,9 +82,12 @@ public static String replace(String s, String[][] repls) {
}

public static String replace(String s, String regex, Mapper<String[], String> replacer) {
return replace(s, Pattern.compile(regex), replacer);
}

public static String replace(String s, Pattern regex, Mapper<String[], String> replacer) {
StringBuffer output = new StringBuffer();
Pattern p = Pattern.compile(regex);
Matcher matcher = p.matcher(s);
Matcher matcher = regex.matcher(s);

while (matcher.find()) {
int len = matcher.groupCount() + 1;
Expand Down
23 changes: 17 additions & 6 deletions rapidoid-http-fast/src/main/java/org/rapidoid/http/HttpUtils.java
Expand Up @@ -28,6 +28,7 @@
import org.rapidoid.crypto.Crypto;
import org.rapidoid.http.customize.JsonResponseRenderer;
import org.rapidoid.io.Res;
import org.rapidoid.lambda.Mapper;
import org.rapidoid.serialize.Serialize;
import org.rapidoid.u.U;
import org.rapidoid.util.ErrCodeAndMsg;
Expand All @@ -51,6 +52,13 @@ public class HttpUtils implements HttpMetadata {

public static final String _USER = "_USER";

private static final Mapper<String[], String> PATH_PARAM_EXTRACTOR = new Mapper<String[], String>() {
@Override
public String map(String[] src) throws Exception {
return src[1].split(":", 2)[0];
}
};

public static String[] pathSegments(Req req) {
return Str.triml(req.path(), "/").split("/");
}
Expand Down Expand Up @@ -104,17 +112,20 @@ public static boolean isPostReq(Req req) {
}

public static String resName(String path) {
String resourceName = path.substring(1);

if (resourceName.isEmpty()) {
resourceName = "index";
String res = Str.replace(path, PathPattern.PATH_PARAM_REGEX, PATH_PARAM_EXTRACTOR);

res = Str.triml(res, "/");

if (res.isEmpty()) {
res = "index";
} else {
if (resourceName.endsWith(".html")) {
resourceName = Str.sub(resourceName, 0, -5);
if (res.endsWith(".html")) {
res = Str.sub(res, 0, -5);
}
}

return resourceName;
return res;
}

public static String verbAndResourceName(Req req) {
Expand Down
20 changes: 10 additions & 10 deletions rapidoid-http-fast/src/main/java/org/rapidoid/http/PathPattern.java
@@ -1,5 +1,12 @@
package org.rapidoid.http;

import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.cls.Cls;
import org.rapidoid.commons.Str;
import org.rapidoid.lambda.Mapper;
import org.rapidoid.u.U;

import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
Expand All @@ -15,9 +22,9 @@
* Licensed 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.
Expand All @@ -26,18 +33,11 @@
* #L%
*/

import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.cls.Cls;
import org.rapidoid.commons.Str;
import org.rapidoid.lambda.Mapper;
import org.rapidoid.u.U;

@Authors("Nikolche Mihajlovski")
@Since("5.1.0")
public class PathPattern {

private static final String PATH_PARAM_REGEX = "\\{([^\\}]+)\\}";
static final Pattern PATH_PARAM_REGEX = Pattern.compile("\\{([^\\}]+)\\}");

private static final Pattern PATH_PARAM_PARTS = Pattern.compile("(\\w+)(?::(.+))?");

Expand Down
@@ -0,0 +1,43 @@
package org.rapidoid.http;

/*
* #%L
* rapidoid-http-fast
* %%
* Copyright (C) 2014 - 2016 Nikolche Mihajlovski and contributors
* %%
* Licensed 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.
* #L%
*/

import org.junit.Test;
import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.test.TestCommons;

@Authors("Nikolche Mihajlovski")
@Since("5.1.0")
public class HttpUtilsTest extends TestCommons {

@Test
public void testView() {
eq(HttpUtils.defaultView("/"), "index");
eq(HttpUtils.defaultView("/abc"), "abc");
eq(HttpUtils.defaultView("/x/y/z"), "x/y/z");

eq(HttpUtils.defaultView("/books/{x}"), "books/x");
eq(HttpUtils.defaultView("/books/{id:\\d+}"), "books/id");
eq(HttpUtils.defaultView("/books/{a:.*}-{b}/view"), "books/a-b/view");
}

}

0 comments on commit 00ba765

Please sign in to comment.