Adds support for wildcards
Project: http://git-wip-us.apache.org/repos/asf/struts/repo Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/3b31c428 Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/3b31c428 Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/3b31c428 Branch: refs/heads/master Commit: 3b31c428856766389ad6df4ba1edc3d60ecf5e24 Parents: c1928ad Author: Lukasz Lenart <lukaszlen...@apache.org> Authored: Mon Aug 31 18:36:29 2015 +0200 Committer: Lukasz Lenart <lukaszlen...@apache.org> Committed: Mon Aug 31 18:36:29 2015 +0200 ---------------------------------------------------------------------- .../xwork2/config/entities/ActionConfig.java | 22 +-- .../xwork2/config/entities/AllowedMethods.java | 152 +++++++++++++++++++ 2 files changed, 165 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/struts/blob/3b31c428/core/src/main/java/com/opensymphony/xwork2/config/entities/ActionConfig.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/com/opensymphony/xwork2/config/entities/ActionConfig.java b/core/src/main/java/com/opensymphony/xwork2/config/entities/ActionConfig.java index fd61ad9..5303e83 100644 --- a/core/src/main/java/com/opensymphony/xwork2/config/entities/ActionConfig.java +++ b/core/src/main/java/com/opensymphony/xwork2/config/entities/ActionConfig.java @@ -52,7 +52,7 @@ public class ActionConfig extends Located implements Serializable { protected String methodName; protected String packageName; protected String name; - protected Set<String> allowedMethods; + protected AllowedMethods allowedMethods; protected ActionConfig(String packageName, String name, String className) { this.packageName = packageName; @@ -62,7 +62,7 @@ public class ActionConfig extends Located implements Serializable { results = new LinkedHashMap<>(); interceptors = new ArrayList<>(); exceptionMappings = new ArrayList<>(); - allowedMethods = new HashSet<>(Collections.singletonList(DEFAULT_METHOD)); + allowedMethods = new AllowedMethods(new HashSet<>(Collections.singletonList(DEFAULT_METHOD))); } /** @@ -79,7 +79,7 @@ public class ActionConfig extends Located implements Serializable { this.interceptors = new ArrayList<>(orig.interceptors); this.results = new LinkedHashMap<>(orig.results); this.exceptionMappings = new ArrayList<>(orig.exceptionMappings); - this.allowedMethods = new HashSet<>(orig.allowedMethods); + this.allowedMethods = new AllowedMethods(orig.allowedMethods.list()); this.location = orig.location; } @@ -100,7 +100,7 @@ public class ActionConfig extends Located implements Serializable { } public Set<String> getAllowedMethods() { - return allowedMethods; + return allowedMethods.list(); } /** @@ -128,7 +128,7 @@ public class ActionConfig extends Located implements Serializable { } public boolean isAllowedMethod(String method) { - return method.equals(methodName != null ? methodName : DEFAULT_METHOD) || allowedMethods.contains(method); + return method.equals(methodName != null ? methodName : DEFAULT_METHOD) || allowedMethods.isAllowed(method); } @Override public boolean equals(Object o) { @@ -210,14 +210,16 @@ public class ActionConfig extends Located implements Serializable { public static class Builder implements InterceptorListHolder{ protected ActionConfig target; + protected Set<String> allowedMethods; public Builder(ActionConfig toClone) { target = new ActionConfig(toClone); - addAllowedMethod(toClone.getAllowedMethods()); + allowedMethods = toClone.getAllowedMethods(); } public Builder(String packageName, String name, String className) { target = new ActionConfig(packageName, name, className); + allowedMethods = new HashSet<>(); } public Builder packageName(String name) { @@ -308,12 +310,14 @@ public class ActionConfig extends Located implements Serializable { } public Builder addAllowedMethod(String methodName) { - target.allowedMethods.add(methodName); + if (methodName != null) { + allowedMethods.add(methodName); + } return this; } public Builder addAllowedMethod(Collection<String> methods) { - target.allowedMethods.addAll(methods); + allowedMethods.addAll(methods); return this; } @@ -327,7 +331,7 @@ public class ActionConfig extends Located implements Serializable { target.results = Collections.unmodifiableMap(target.results); target.interceptors = Collections.unmodifiableList(target.interceptors); target.exceptionMappings = Collections.unmodifiableList(target.exceptionMappings); - target.allowedMethods = Collections.unmodifiableSet(target.allowedMethods); + target.allowedMethods = new AllowedMethods(allowedMethods); ActionConfig result = target; target = new ActionConfig(target); http://git-wip-us.apache.org/repos/asf/struts/blob/3b31c428/core/src/main/java/com/opensymphony/xwork2/config/entities/AllowedMethods.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/com/opensymphony/xwork2/config/entities/AllowedMethods.java b/core/src/main/java/com/opensymphony/xwork2/config/entities/AllowedMethods.java new file mode 100644 index 0000000..7a4fec1 --- /dev/null +++ b/core/src/main/java/com/opensymphony/xwork2/config/entities/AllowedMethods.java @@ -0,0 +1,152 @@ +package com.opensymphony.xwork2.config.entities; + +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; +import java.util.regex.Pattern; + +public class AllowedMethods { + + private Set<AllowedMethod> allowedMethods; + + public AllowedMethods(Set<String> methods) { + Set<AllowedMethod> allowedMethods = new HashSet<>(); + for (String method : methods) { + allowedMethods.add(build(method)); + } + this.allowedMethods = Collections.unmodifiableSet(allowedMethods); + } + + private AllowedMethod build(String method) { + boolean isPattern = false; + int len = method.length(); + StringBuilder ret = new StringBuilder(); + char c; + for (int x = 0; x < len; x++) { + c = method.charAt(x); + if (x < len - 2 && c == '{' && '}' == method.charAt(x + 2)) { + ret.append("(.*)"); + isPattern = true; + x += 2; + } else { + ret.append(c); + } + } + if (isPattern) { + return new PatternAllowedMethod(ret.toString(), method); + } else { + return new LiteralAllowedMethod(ret.toString()); + } + } + + public boolean isAllowed(String method) { + for (AllowedMethod allowedMethod : allowedMethods) { + if (allowedMethod.isAllowed(method)) { + return true; + } + } + return false; + } + + public Set<String> list() { + Set<String> result = new HashSet<>(); + for (AllowedMethod allowedMethod : allowedMethods) { + result.add(allowedMethod.original()); + } + return result; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + AllowedMethods that = (AllowedMethods) o; + + return allowedMethods.equals(that.allowedMethods); + } + + @Override + public int hashCode() { + return allowedMethods.hashCode(); + } + + private interface AllowedMethod { + boolean isAllowed(String methodName); + + String original(); + } + + private static class PatternAllowedMethod implements AllowedMethod { + + private final Pattern allowedMethodPattern; + private String original; + + public PatternAllowedMethod(String pattern, String original) { + this.original = original; + allowedMethodPattern = Pattern.compile(pattern); + } + + @Override + public boolean isAllowed(String methodName) { + return allowedMethodPattern.matcher(methodName).matches(); + } + + @Override + public String original() { + return original; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + PatternAllowedMethod that = (PatternAllowedMethod) o; + + return original.equals(that.original); + + } + + @Override + public int hashCode() { + return original.hashCode(); + } + } + + private static class LiteralAllowedMethod implements AllowedMethod { + + private String allowedMethod; + + public LiteralAllowedMethod(String allowedMethod) { + this.allowedMethod = allowedMethod; + } + + @Override + public boolean isAllowed(String methodName) { + return methodName.equals(allowedMethod); + } + + @Override + public String original() { + return allowedMethod; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + LiteralAllowedMethod that = (LiteralAllowedMethod) o; + + return allowedMethod.equals(that.allowedMethod); + + } + + @Override + public int hashCode() { + return allowedMethod.hashCode(); + } + } + +}