Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
  • 2 commits
  • 6 files changed
  • 0 commit comments
  • 1 contributor
View
8 README.ja.md
@@ -185,6 +185,10 @@ web.xmlにて、次のようにroutingfilterを設定されているところを
<param-name>checkInterval</param-name>
<param-value>-1</param-value>
</init-param>
+ <init-param>
+ <param-name>contextSensitive</param-name>
+ <param-value>false</param-value>
+ </init-param>
</filter>
追加されているパラメータについて説明します。
@@ -196,6 +200,10 @@ checkInterval は、ルート定義ファイルの更新をチェックしにい
避けるようにしてください。このパラメータを設定しない、またはマイナスの値を設定すると更新チェック自体がおこなわれず、
アプリケーションを再起動しない限りルート定義はリロードされません。
+contextSensitive は、コンテキストパスを意識したマッチング/パス生成するかどうかのフラグです。これがtrueの場合は、
+コンテキストパスより後ろのパスを使ってルートのマッチングをし、UrlRewriterを使ってパスを生成する際に自動的に
+コンテキストパスが補われるようになります。デフォルトはfalseです。
+
## License
SAStruts Advanced Routes はApache License 2.0 の元に配布されます。
View
18 src/main/java/net/unit8/sastruts/AdvancedRoutingFilter.java
@@ -49,9 +49,14 @@
*/
protected Long checkInterval;
+ /**
+ * If contextSensitive is true, recognize the path after context path and generate the path with context path.
+ */
+ protected boolean contextSensitive = false;
+
public void init(FilterConfig config) throws ServletException {
String access = config.getInitParameter("jspDirectAccess");
- if (!StringUtil.isBlank(access)) {
+ if (StringUtil.isNotBlank(access)) {
jspDirectAccess = Boolean.valueOf(access);
}
@@ -69,6 +74,14 @@ public void init(FilterConfig config) throws ServletException {
if (checkInterval == null || checkInterval < 0) {
checkInterval = -1L;
}
+
+ String contextSensitiveParam = config.getInitParameter("contextSensitive");
+ if (StringUtil.isNotBlank(contextSensitiveParam)) {
+ contextSensitive = Boolean.valueOf(contextSensitiveParam);
+ }
+ if (contextSensitive) {
+ UrlRewriter.contextPath = config.getServletContext().getContextPath();
+ }
}
public void destroy() {
@@ -118,7 +131,8 @@ public void doFilter(ServletRequest request, ServletResponse response,
if (path.indexOf('.') < 0) {
// If the request pass via reverse proxy, the original path must be gotten from HTTP header.
- path = RequestUtil.getRequest().getRequestURI();
+ if (!contextSensitive)
+ path = req.getRequestURI();
Options options = Routes.recognizePath(path);
String controller = options.getString("controller");
String action = options.getString("action");
View
62 src/main/java/net/unit8/sastruts/UrlRewriter.java
@@ -1,7 +1,6 @@
package net.unit8.sastruts;
import java.util.ArrayList;
-import java.util.Collection;
import java.util.List;
import java.util.Map;
@@ -15,24 +14,58 @@
import org.seasar.framework.util.StringUtil;
import org.seasar.struts.config.S2ExecuteConfig;
import org.seasar.struts.util.S2ExecuteConfigUtil;
+import org.seasar.struts.util.URLEncoderUtil;
public class UrlRewriter {
+ public static String contextPath = null;
+
public static String urlFor(Map<String, Object> opts) {
Options options = new Options(opts);
- StringBuilder url = new StringBuilder();
- url.append(Routes.generate(options));
- return url.toString();
+ return urlFor(options);
}
public static String urlFor(String optionString) {
Options options = parseOptionString(optionString);
+ return urlFor(options);
+ }
+
+ public static String urlFor(Options options) {
StringBuilder url = new StringBuilder();
- url.append(Routes.generate(options));
+
+ boolean trailingSlash = false;
+ if (options.containsKey("trailing_slash")) {
+ trailingSlash = options.getBoolean("trailing_slash");
+ options.remove("trailing_slash");
+ }
+
+ String anchor = null;
+ if (options.containsKey("anchor")) {
+ anchor = "#" + URLEncoderUtil.encode(options.getString("anchor"));
+ options.remove("anchor");
+ }
+ if (StringUtils.isNotEmpty(contextPath))
+ url.append(contextPath);
+ String generated = Routes.generate(options);
+ url.append(trailingSlash ? trailingSlash(generated) : generated);
+ if (!StringUtils.isEmpty(anchor))
+ url.append(anchor);
return url.toString();
}
+ private static String trailingSlash(String url) {
+ int queryStringIdx = StringUtils.indexOf(url, '?');
+ if (queryStringIdx < 0 || StringUtils.length(url) < 1)
+ return url + "/";
+ if (queryStringIdx != 0 && url.charAt(queryStringIdx - 1) == '/') {
+ return url;
+ } else {
+ return StringUtils.substring(url, 0, queryStringIdx) + "/" + StringUtils.substring(url, queryStringIdx);
+ }
+ }
+
+ @SuppressWarnings("unchecked")
public static Options parseOptionString(String optionString) {
- String[] urlTokens = StringUtils.split(optionString, "?", 2);
+ String[] urlTokens = StringUtils.split(optionString, "?", 2);
String[] actionTokens = StringUtils.split(urlTokens[0], "#", 2);
Options options = new Options();
@@ -40,11 +73,12 @@ public static Options parseOptionString(String optionString) {
options.$("action", actionTokens[0]);
options.$("controller", currentController());
} else {
- options.$("controller", actionTokens[0]).$("action", actionTokens[1]);
+ options.$("controller", actionTokens[0]).$("action",
+ actionTokens[1]);
}
if (urlTokens.length == 2 && StringUtil.isNotEmpty(urlTokens[1])) {
String[] paramToken = StringUtils.split(urlTokens[1], "&");
- for(String keyValuePair: paramToken) {
+ for (String keyValuePair : paramToken) {
String[] pair = StringUtils.split(keyValuePair, "=", 2);
if (pair.length == 1) {
options.$(pair[0], null);
@@ -53,7 +87,7 @@ public static Options parseOptionString(String optionString) {
if (value == null) {
options.$(pair[0], pair[1]);
} else if (value instanceof ArrayList) {
- ((ArrayList<String>)value).add(pair[1]);
+ ((ArrayList<String>) value).add(pair[1]);
} else {
List<String> values = new ArrayList<String>();
values.add(StringConversionUtil.toString(value));
@@ -68,8 +102,12 @@ public static Options parseOptionString(String optionString) {
public static String currentController() {
S2ExecuteConfig executeConfig = S2ExecuteConfigUtil.getExecuteConfig();
Class<?> clazz = executeConfig.getMethod().getDeclaringClass();
- NamingConvention namingConvention = SingletonS2Container.getComponent(NamingConvention.class);
- String componentName = namingConvention.fromClassNameToComponentName(clazz.getName());
- return StringUtil.trimSuffix(namingConvention.fromComponentNameToPartOfClassName(componentName), namingConvention.getActionSuffix());
+ NamingConvention namingConvention = SingletonS2Container
+ .getComponent(NamingConvention.class);
+ String componentName = namingConvention
+ .fromClassNameToComponentName(clazz.getName());
+ return StringUtil.trimSuffix(namingConvention
+ .fromComponentNameToPartOfClassName(componentName),
+ namingConvention.getActionSuffix());
}
}
View
9 src/main/java/net/unit8/sastruts/routing/Route.java
@@ -10,6 +10,8 @@
import javax.servlet.http.HttpServletRequest;
+import net.unit8.sastruts.routing.segment.DividerSegment;
+
import org.apache.commons.lang.StringUtils;
import org.seasar.framework.util.StringUtil;
import org.seasar.struts.util.RequestUtil;
@@ -151,7 +153,12 @@ public boolean matchesControllerAndAction(String controller, String action) {
public String generate(Options options, Options hash) {
String path = null;
if (generationRequirements(options, hash)) {
- path = segments.get(segments.size() - 1).stringStructure(segments.subList(0, segments.size() - 1), hash);
+ int lastIndex = segments.size() - 1;
+ Segment last = segments.get(lastIndex);
+ path = last.stringStructure(segments.subList(0, lastIndex), hash);
+ if (last instanceof DividerSegment && StringUtils.equals(last.getValue(), "/")) {
+ path = path + "/";
+ }
}
return appendQueryString(path, hash, extraKeys(options));
}
View
3 src/main/java/net/unit8/sastruts/routing/RouteBuilder.java
@@ -171,9 +171,6 @@ public Route build(String path, Options options) {
if (path.charAt(0) != '/')
path = "/" + path;
- if (path.charAt(path.length() - 1) != '/')
- path = path + "/";
-
String prefix = options.getString("pathPrefix").replace("^/", "");
if (StringUtil.isNotBlank(prefix))
path = "/" + prefix + path;
View
11 src/test/java/net/unit8/sastruts/UrlRewriterTest.java
@@ -4,6 +4,7 @@
import static org.junit.Assert.*;
import net.unit8.sastruts.routing.Options;
+import net.unit8.sastruts.routing.Routes;
import net.unit8.sastruts.testapp.action.UserAction;
import net.unit8.sastruts.testapp.action.admin.ProofAction;
@@ -11,6 +12,7 @@
import org.junit.runner.RunWith;
import org.seasar.framework.unit.Seasar2;
import org.seasar.framework.unit.annotation.RegisterNamingConvention;
+import org.seasar.framework.util.ResourceUtil;
import org.seasar.framework.util.tiger.ReflectionUtil;
import org.seasar.struts.config.S2ExecuteConfig;
import org.seasar.struts.util.S2ExecuteConfigUtil;
@@ -49,4 +51,13 @@ public void testSubPackage() {
assertThat(options.getString("id"), is("1"));
}
+ @Test
+ public void testGenerateOption() {
+ Routes.load(ResourceUtil.getResourceAsFile("routes/testutil.xml"));
+ assertThat(UrlRewriter.urlFor("my#account"), is("/my/account"));
+ assertThat(UrlRewriter.urlFor("my#account?trailing_slash=true"), is("/my/account/"));
+ assertThat(UrlRewriter.urlFor("my#account?trailing_slash=false"), is("/my/account"));
+ assertThat(UrlRewriter.urlFor("my#account?anchor=p3"), is("/my/account#p3"));
+ }
+
}

No commit comments for this range

Something went wrong with that request. Please try again.