1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.forgerock.api.models;
18
19 import static org.forgerock.api.util.ValidationUtil.containsWhitespace;
20 import static org.forgerock.api.util.ValidationUtil.isEmpty;
21
22 import java.lang.reflect.Method;
23 import java.util.Objects;
24
25 import com.fasterxml.jackson.annotation.JsonInclude;
26 import com.fasterxml.jackson.annotation.JsonProperty;
27 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
28 import org.wrensecurity.guava.common.base.Strings;
29
30 import org.forgerock.api.ApiValidationException;
31
32
33
34
35 @JsonDeserialize(builder = Action.Builder.class)
36 @JsonInclude(JsonInclude.Include.NON_NULL)
37 public final class Action extends Operation implements Comparable<Action> {
38
39 private final String name;
40 private final Schema request;
41 private final Schema response;
42
43
44
45
46
47
48 private Action(Builder builder) {
49 super(builder);
50 this.name = builder.name;
51 this.request = builder.request;
52 this.response = builder.response;
53
54 if (isEmpty(name)) {
55 throw new ApiValidationException("name is required");
56 }
57 if (containsWhitespace(name)) {
58 throw new ApiValidationException("name contains whitespace");
59 }
60 }
61
62
63
64
65
66
67 public String getName() {
68 return name;
69 }
70
71
72
73
74
75
76 public Schema getRequest() {
77 return request;
78 }
79
80
81
82
83
84
85 public Schema getResponse() {
86 return response;
87 }
88
89 @Override
90 public boolean equals(Object o) {
91 if (this == o) {
92 return true;
93 }
94 if (o == null || getClass() != o.getClass()) {
95 return false;
96 }
97 if (!super.equals(o)) {
98 return false;
99 }
100 Action action = (Action) o;
101 return super.equals(o)
102 && Objects.equals(name, action.name)
103 && Objects.equals(request, action.request)
104 && Objects.equals(response, action.response);
105 }
106
107 @Override
108 public int hashCode() {
109 return Objects.hash(super.hashCode(), name, request, response);
110 }
111
112
113
114
115
116
117 public static final Builder action() {
118 return new Builder();
119 }
120
121
122
123
124
125
126 @Override
127 protected void allocateToResource(Resource.Builder resourceBuilder) {
128 resourceBuilder.action(this);
129 }
130
131
132
133
134
135
136
137
138
139 public static Action fromAnnotation(org.forgerock.api.annotations.Action action, Method annotated,
140 ApiDescription descriptor, Class<?> relativeType) {
141 Builder builder = action();
142 String specifiedName = action.name();
143 if (Strings.isNullOrEmpty(specifiedName)) {
144 if (annotated == null) {
145 throw new IllegalArgumentException("Action does not have a name: " + action);
146 }
147 specifiedName = annotated.getName();
148 }
149 return builder.name(specifiedName)
150 .request(Schema.fromAnnotation(action.request(), descriptor, relativeType))
151 .response(Schema.fromAnnotation(action.response(), descriptor, relativeType))
152 .detailsFromAnnotation(action.operationDescription(), descriptor, relativeType)
153 .build();
154 }
155
156
157
158
159
160
161
162
163
164
165 @Override
166 public int compareTo(Action action) {
167 return this.name.compareTo(action.getName());
168 }
169
170
171
172
173 public static final class Builder extends Operation.Builder<Builder> {
174
175 private String name;
176 private Schema request;
177 private Schema response;
178
179 @Override
180 protected Builder self() {
181 return this;
182 }
183
184
185
186
187
188
189
190 @JsonProperty("name")
191 public Builder name(String name) {
192 this.name = name;
193 return this;
194 }
195
196
197
198
199
200
201
202 @JsonProperty("request")
203 public Builder request(Schema request) {
204 this.request = request;
205 return this;
206 }
207
208
209
210
211
212
213
214 @JsonProperty("response")
215 public Builder response(Schema response) {
216 this.response = response;
217 return this;
218 }
219
220
221
222
223
224
225 public Action build() {
226 return new Action(this);
227 }
228 }
229
230 }