forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCORS.java
More file actions
324 lines (286 loc) · 9.52 KB
/
CORS.java
File metadata and controls
324 lines (286 loc) · 9.52 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package act.security;
/*-
* #%L
* ACT Framework
* %%
* Copyright (C) 2014 - 2017 ActFramework
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import static org.osgl.http.H.Header.Names.*;
import act.Act;
import act.app.ActionContext;
import org.osgl.$;
import org.osgl.http.H;
import org.osgl.inject.BeanSpec;
import org.osgl.util.C;
import org.osgl.util.E;
import org.osgl.util.S;
import java.lang.annotation.*;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Collection;
/**
* Provice CORS header manipulation methods
*/
public class CORS {
/**
* Mark a controller class or action handler method that
* must not add any CORS headers irregarding to the
* global CORS setting
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface Disable {
}
/**
* Mark a controller class or action handler method that
* needs to add `Access-Control-Allow-Origin` header
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface AllowOrigin {
/**
* The value set to the `Access-Control-Allow-Origin` header
* @return the value
*/
String value() default "*";
}
/**
* Mark a controller class or action handler method that
* needs to add `Access-Control-Allow-Headers` header
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface AllowHeaders {
/**
* The value set to the `Access-Control-Allow-Headers` header
* @return the value
*/
String value() default "*";
}
/**
* Mark a controller class or action handler method that
* needs to add `Access-Control-Expose-Headers` header
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface ExposeHeaders {
/**
* The value set to the `Access-Control-Expose-Headers` header
* @return the value
*/
String value() default "*";
}
/**
* Mark a controller class or action handler method that
* needs to add `Access-Control-Allow-Credentials` header
* and set the value to `false`
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface DisallowCredentials {
}
/**
* Mark a controller class or action handler method that
* needs to add `Access-Control-Allow-Credentials` header
* and set the value to `true`
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface AllowCredentials {
}
/**
* Mark a controller class or action handler method that
* needs to add `Access-Control-Max-Age` header
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface MaxAge {
/**
* The value set to the `Access-Control-Max-Age` header
* @return the value
*/
int value() default 30 * 60;
}
public static Spec spec(Collection<H.Method> methods) {
return new Spec(methods);
}
public static Spec spec(Class controller) {
return spec(BeanSpec.of(controller, Act.injector()));
}
public static Spec spec(Method action) {
Type type = Method.class;
Annotation[] annotations = action.getDeclaredAnnotations();
return spec(BeanSpec.of(type, annotations, Act.injector()));
}
private static Spec spec(BeanSpec beanSpec) {
return new Spec()
.with(beanSpec.getAnnotation(Disable.class))
.with(beanSpec.getAnnotation(AllowOrigin.class))
.with(beanSpec.getAnnotation(ExposeHeaders.class))
.with(beanSpec.getAnnotation(AllowCredentials.class))
.with(beanSpec.getAnnotation(DisallowCredentials.class))
.with(beanSpec.getAnnotation(AllowHeaders.class))
.with(beanSpec.getAnnotation(MaxAge.class));
}
public static class Spec extends $.Visitor<ActionContext> {
public static final Spec DUMB = new Spec() {
@Override
public void visit(ActionContext context) throws $.Break {
// do nothing implementation
}
@Override
public void applyTo(ActionContext context) throws $.Break {
// do nothing implementation
}
};
private boolean disableCORS;
private String origin;
private String methods;
private String exposeHeaders;
private String allowHeaders;
private Boolean allowCredentials;
private int maxAge = -1;
private boolean effective = false;
private Spec(Collection<H.Method> methodSet) {
E.illegalArgumentIf(methodSet.isEmpty());
methods = S.join(", ", C.list(methodSet).map($.F.<H.Method>asString()));
effective = true;
}
private Spec() {}
public boolean effective() {
return effective;
}
public boolean disabled() {
return disableCORS;
}
public Spec with(Disable disableCORS) {
if (null != disableCORS) {
this.effective = true;
this.disableCORS = true;
}
return this;
}
public Spec with(AllowOrigin allowOrigin) {
if (null != allowOrigin) {
this.effective = true;
origin = allowOrigin.value();
}
return this;
}
public Spec with(AllowHeaders allowHeaders) {
if (null != allowHeaders) {
this.effective = true;
this.allowHeaders = allowHeaders.value();
}
return this;
}
public Spec with(ExposeHeaders exposeHeaders) {
if (null != exposeHeaders) {
this.effective = true;
this.exposeHeaders = exposeHeaders.value();
}
return this;
}
public Spec with(AllowCredentials allowCredentials) {
if (null != allowCredentials) {
this.effective = true;
this.allowCredentials = true;
}
return this;
}
public Spec with(DisallowCredentials disallowCredentials) {
if (null != disallowCredentials) {
this.effective = true;
this.allowCredentials = false;
}
return this;
}
public Spec with(MaxAge maxAge) {
if (null != maxAge) {
this.effective = true;
this.maxAge = maxAge.value();
}
return this;
}
@Override
public void visit(ActionContext context) throws $.Break {
applyTo(context);
}
public void applyTo(ActionContext context) throws $.Break {
if (!effective) {
return;
}
if (disableCORS) {
context.disableCORS();
return;
}
H.Response r = context.resp();
if (null != origin) {
r.addHeaderIfNotAdded(ACCESS_CONTROL_ALLOW_ORIGIN, origin);
}
if (context.isOptionsMethod()) {
if (null != methods) {
r.addHeaderIfNotAdded(ACCESS_CONTROL_ALLOW_METHODS, methods);
}
if (null != exposeHeaders) {
r.addHeaderIfNotAdded(ACCESS_CONTROL_EXPOSE_HEADERS, exposeHeaders);
}
if (null != allowCredentials) {
r.addHeaderIfNotAdded(ACCESS_CONTROL_ALLOW_CREDENTIALS, S.string(allowCredentials));
}
if (null != allowHeaders) {
r.addHeaderIfNotAdded(ACCESS_CONTROL_ALLOW_HEADERS, allowHeaders);
}
if (-1 < maxAge) {
r.addHeaderIfNotAdded(ACCESS_CONTROL_MAX_AGE, S.string(maxAge));
}
}
}
public Spec chain(final Spec next) {
if (!next.effective()) {
return this;
}
if (!effective()) {
return next;
}
if (next.disabled()) {
return next;
}
if (disabled()) {
return this;
}
final Spec me = this;
return new Spec() {
@Override
public boolean effective() {
return true;
}
@Override
public void applyTo(ActionContext context) throws $.Break {
me.visit(context);
next.visit(context);
}
};
}
}
}