mirror of
https://github.com/apache/struts.git
synced 2026-08-11 01:27:14 +00:00
Uses factory method pattern
This commit is contained in:
@@ -63,7 +63,7 @@ public class ActionConfig extends Located implements Serializable {
|
||||
results = new LinkedHashMap<>();
|
||||
interceptors = new ArrayList<>();
|
||||
exceptionMappings = new ArrayList<>();
|
||||
allowedMethods = new AllowedMethods(new HashSet<>(Collections.singletonList(DEFAULT_METHOD)));
|
||||
allowedMethods = AllowedMethods.build(new HashSet<>(Collections.singletonList(DEFAULT_METHOD)));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -80,7 +80,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 AllowedMethods(orig.allowedMethods.list());
|
||||
this.allowedMethods = AllowedMethods.build(orig.allowedMethods.list());
|
||||
this.location = orig.location;
|
||||
}
|
||||
|
||||
@@ -332,7 +332,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 = new AllowedMethods(allowedMethods);
|
||||
target.allowedMethods = AllowedMethods.build(allowedMethods);
|
||||
|
||||
ActionConfig result = target;
|
||||
target = new ActionConfig(target);
|
||||
|
||||
@@ -9,37 +9,39 @@ public class AllowedMethods {
|
||||
|
||||
private Set<AllowedMethod> allowedMethods;
|
||||
|
||||
public AllowedMethods(Set<String> methods) {
|
||||
public static AllowedMethods build(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;
|
||||
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 && !method.startsWith("regex:")) {
|
||||
allowedMethods.add(new PatternAllowedMethod(ret.toString(), method));
|
||||
} else if (method.startsWith("regex:")) {
|
||||
String pattern = method.substring(method.indexOf(":") + 1);
|
||||
allowedMethods.add(new PatternAllowedMethod(pattern, method));
|
||||
} else {
|
||||
ret.append(c);
|
||||
allowedMethods.add(new LiteralAllowedMethod(ret.toString()));
|
||||
}
|
||||
}
|
||||
if (isPattern && !method.startsWith("regex:")) {
|
||||
return new PatternAllowedMethod(ret.toString(), method);
|
||||
} else if (method.startsWith("regex:")) {
|
||||
String pattern = method.substring(method.indexOf(":") + 1);
|
||||
return new PatternAllowedMethod(pattern, method);
|
||||
} else {
|
||||
return new LiteralAllowedMethod(ret.toString());
|
||||
}
|
||||
|
||||
return new AllowedMethods(allowedMethods);
|
||||
}
|
||||
|
||||
private AllowedMethods(Set<AllowedMethod> methods) {
|
||||
this.allowedMethods = Collections.unmodifiableSet(methods);
|
||||
}
|
||||
|
||||
public boolean isAllowed(String method) {
|
||||
|
||||
Reference in New Issue
Block a user