-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathGrailsActionContext.java
127 lines (110 loc) · 3.82 KB
/
GrailsActionContext.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
package springfox.documentation.grails;
import com.fasterxml.classmate.TypeResolver;
import com.google.common.collect.ImmutableSet;
import grails.core.GrailsControllerClass;
import grails.web.mapping.UrlMapping;
import org.grails.datastore.mapping.model.PersistentEntity;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.method.HandlerMethod;
import springfox.documentation.service.ResolvedMethodParameter;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import static com.google.common.collect.Lists.*;
import static springfox.documentation.grails.Actions.*;
import static springfox.documentation.grails.UrlMappings.*;
class GrailsActionContext {
private final GrailsControllerClass controller;
private final PersistentEntity domainClass;
private final GrailsActionAttributes urlProvider;
private final String action;
private final TypeResolver resolver;
private final Optional<UrlMapping> urlMapping;
private final boolean isRestfulController;
private final Set<MediaType> mediaTypes;
private final Collection<RequestMethod> requestMethods;
private final boolean missingMapping;
public GrailsActionContext(
GrailsControllerClass controller,
PersistentEntity domainClass,
GrailsActionAttributes urlProvider,
String action,
TypeResolver resolver) {
this.controller = controller;
this.domainClass = domainClass;
this.urlProvider = urlProvider;
this.action = action;
this.resolver = resolver;
this.isRestfulController = isRestfulController(controller, action);
this.requestMethods = urlProvider.httpMethods(this);
String httpMethod = requestMethods
.stream()
.findFirst()
.map(Enum::toString)
.orElse(null);
this.missingMapping = requestMethods.isEmpty();
this.urlMapping = urlProvider.urlMapping(
selector(
controller.getLogicalPropertyName(),
action,
httpMethod));
this.mediaTypes = mediaTypeOverrides(this);
maybeAddDefaultMediaType(mediaTypes);
}
private void maybeAddDefaultMediaType(Set<MediaType> mediaTypes) {
if (mediaTypes.isEmpty()) {
mediaTypes.add(MediaType.APPLICATION_JSON);
}
}
public GrailsControllerClass getController() {
return controller;
}
public PersistentEntity getDomainClass() {
return domainClass;
}
public String getAction() {
return action;
}
public String path() {
return urlMapping
.map(mapping -> urlProvider.actionUrl(this, mapping))
.orElse(String.format("/%s/%s", controller.getLogicalPropertyName(), action))
.toLowerCase();
}
public List<ResolvedMethodParameter> pathParameters() {
if (domainClass == null) {
return newArrayList();
}
return urlMapping
.map(u -> resolvedPathParameters(resolver, u, domainClass))
.orElse(newArrayList());
}
public Collection<RequestMethod> getRequestMethods() {
return requestMethods;
}
public HandlerMethod handlerMethod() {
Map<String, HandlerMethod> actions = actionsToHandler(controller.getClazz());
return actions.get(action);
}
public Set<MediaType> supportedMediaTypes() {
return ImmutableSet.copyOf(mediaTypes);
}
public boolean isRestfulController() {
return isRestfulController;
}
public boolean isMissingMapping() {
return missingMapping;
}
private boolean isRestfulController(GrailsControllerClass controller, String action) {
try {
Class<?> restfulController = Class.forName("grails.rest.RestfulController");
return restfulController.isAssignableFrom(controller.getClazz())
&& urlProvider.isRestfulAction(action);
} catch (ClassNotFoundException e) {
return false;
}
}
}