-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathUrlMappings.java
135 lines (115 loc) · 4.98 KB
/
UrlMappings.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package springfox.documentation.grails;
import com.fasterxml.classmate.ResolvedType;
import com.fasterxml.classmate.TypeResolver;
import com.google.common.base.Strings;
import grails.gorm.validation.Constrained;
import grails.gorm.validation.ConstrainedProperty;
import grails.web.mapping.UrlMapping;
import org.grails.datastore.mapping.model.PersistentEntity;
import springfox.documentation.service.ResolvedMethodParameter;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.IntPredicate;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import static com.google.common.collect.Lists.*;
import static springfox.documentation.grails.Parameters.*;
class UrlMappings {
private UrlMappings() {
throw new UnsupportedOperationException();
}
public static Predicate<UrlMapping> selector(
String logicalControllerName,
String action,
String httpMethod) {
return u -> httpMethodMatches(httpMethod, u)
&& controllerMatches(u, logicalControllerName)
&& actionMatches(action, u);
}
public static Map<String, String> pathParameters(UrlMapping mapping) {
Constrained[] constraints = mapping.getConstraints();
return IntStream.range(0, constraints.length)
.filter(indicesToUse(mapping))
.mapToObj(i -> constraints[i])
.filter(c -> c instanceof ConstrainedProperty)
.map(ConstrainedProperty.class::cast)
.collect(Collectors.toMap(
ConstrainedProperty::getPropertyName,
c -> String.format("{%s}", c.getPropertyName())));
}
public static List<ResolvedMethodParameter> resolvedPathParameters(
TypeResolver resolver,
UrlMapping mapping,
PersistentEntity domainClass) {
Constrained[] constraints = mapping.getConstraints();
List<ConstrainedProperty> pathProperties = IntStream.range(0, constraints.length)
.filter(indicesToUse(mapping))
.mapToObj(i -> constraints[i])
.filter(c -> c instanceof ConstrainedProperty)
.map(ConstrainedProperty.class::cast)
.collect(Collectors.toList());
List<ResolvedMethodParameter> resolved = newArrayList();
for (int index = 0; index < pathProperties.size(); index++) {
resolved.add(
pathParameter(
index,
pathProperties.get(index).getPropertyName(),
resolvedPropertyType(resolver, domainClass, pathProperties.get(index))));
}
return resolved;
}
private static IntPredicate indicesToUse(UrlMapping mapping) {
return index -> {
Constrained property = mapping.getConstraints()[index];
return property instanceof ConstrainedProperty
&& !((ConstrainedProperty)property).getPropertyName().equals("controller")
&& !((ConstrainedProperty)property).getPropertyName().equals("action")
&& !property.isNullable();
};
}
private static ResolvedType resolvedPropertyType(
TypeResolver resolver,
PersistentEntity domainClass,
ConstrainedProperty property) {
if (domainClass.hasProperty(property.getPropertyName(), domainClass.getJavaClass())) {
return resolver.resolve(domainClass.getPropertyByName(property.getPropertyName()).getType());
}
return resolver.resolve(String.class);
}
private static boolean httpMethodMatches(String httpMethod, UrlMapping urlMapping) {
return anyMethod(httpMethod) || Objects.equals(urlMapping.getHttpMethod(), httpMethod);
}
private static boolean anyMethod(String methodName) {
return Strings.isNullOrEmpty(methodName);
}
private static boolean actionMatches(String context, UrlMapping urlMapping) {
return isWildcardAction(urlMapping) || explicitAction(context, urlMapping);
}
private static boolean explicitAction(String action, UrlMapping urlMapping) {
return Objects.equals(urlMapping.getActionName(), action);
}
private static boolean controllerMatches(UrlMapping urlMapping, String logicalControllerName) {
return isWildcardController(urlMapping) || explicitController(urlMapping, logicalControllerName);
}
private static boolean explicitController(UrlMapping urlMapping, String logicalControllerName) {
return Objects.equals(urlMapping.getControllerName(), logicalControllerName);
}
private static boolean isWildcardController(UrlMapping urlMapping) {
return urlMapping.getControllerName() == null
&& hasControllerConstraint(urlMapping, "controller");
}
private static boolean isWildcardAction(UrlMapping urlMapping) {
return urlMapping.getControllerName() == null
&& hasControllerConstraint(urlMapping, "action");
}
private static boolean hasControllerConstraint(UrlMapping urlMapping, String name) {
return !Arrays.stream(urlMapping.getConstraints())
.filter(c -> c instanceof ConstrainedProperty)
.map(ConstrainedProperty.class::cast)
.filter(c -> c.getPropertyName().equals(name))
.collect(Collectors.toList()).isEmpty();
}
}