@@ -67,8 +67,8 @@ public class CorsFilter extends Filter {
67
67
private boolean allowedCredentials = false ;
68
68
69
69
/**
70
- * The value of 'Access-Control-Allow-Headers' response header. Used only if
71
- * {@link #allowAllRequestedHeaders} is false.
70
+ * The value of 'Access-Control-Allow-Headers' response header. Used only if {@link #allowAllRequestedHeaders} is
71
+ * false.
72
72
*/
73
73
private Set <String > allowedHeaders = null ;
74
74
@@ -78,17 +78,23 @@ public class CorsFilter extends Filter {
78
78
/** Helper for generating CORS response. */
79
79
private CorsResponseHelper corsResponseHelper ;
80
80
81
- /** The set of methods allowed by default, used when {@link #skippingResourceForCorsOptions} is turned on. By default: GET, PUT, POST, DELETE, PATCH. */
81
+ /**
82
+ * The set of methods allowed by default, used when {@link #skippingResourceForCorsOptions} is turned on. By
83
+ * default: GET, PUT, POST, DELETE, PATCH.
84
+ */
82
85
private Set <Method > defaultAllowedMethods = new HashSet <>(Arrays .asList (Method .GET , Method .POST , Method .PUT ,
83
86
Method .DELETE , Method .PATCH ));
84
87
85
88
/** The value of 'Access-Control-Expose-Headers' response header. */
86
89
private Set <String > exposedHeaders = null ;
87
90
91
+ /** The value of 'Access-Control-Max-Age' response header. Default is that the header is not set. */
92
+ private int maxAge = -1 ;
93
+
88
94
/**
89
95
* If true, the filter does not call the server resource for OPTIONS method
90
- * of CORS request and set Access-Control-Allow-Methods header with
91
- * {@link #defaultAllowedMethods}. Default is false.
96
+ * of CORS request and set Access-Control-Allow-Methods header with {@link #defaultAllowedMethods}. Default is
97
+ * false.
92
98
*/
93
99
private boolean skippingResourceForCorsOptions = false ;
94
100
@@ -122,9 +128,22 @@ public CorsFilter(Context context, Restlet next) {
122
128
}
123
129
124
130
/**
125
- * Skip the call to the server resource if the {@link #skippingResourceForCorsOptions}
126
- * is true and if the current request use the OPTIONS method and is a CORS request.
127
- *
131
+ * Add CORS headers to response
132
+ *
133
+ * @param request
134
+ * The request to handle.
135
+ * @param response
136
+ * The response
137
+ */
138
+ @ Override
139
+ protected void afterHandle (Request request , Response response ) {
140
+ getCorsResponseHelper ().addCorsResponseHeaders (request , response );
141
+ }
142
+
143
+ /**
144
+ * Skip the call to the server resource if the {@link #skippingResourceForCorsOptions} is true and if the current
145
+ * request use the OPTIONS method and is a CORS request.
146
+ *
128
147
* @param request
129
148
* The request to handle.
130
149
* @param response
@@ -142,19 +161,6 @@ && getCorsResponseHelper().isCorsRequest(request)) {
142
161
}
143
162
}
144
163
145
- /**
146
- * Add CORS headers to response
147
- *
148
- * @param request
149
- * The request to handle.
150
- * @param response
151
- * The response
152
- */
153
- @ Override
154
- protected void afterHandle (Request request , Response response ) {
155
- getCorsResponseHelper ().addCorsResponseHeaders (request , response );
156
- }
157
-
158
164
/**
159
165
* Returns the modifiable set of headers allowed by the actual request on
160
166
* the current resource.<br>
@@ -181,8 +187,7 @@ public Set<String> getAllowedOrigins() {
181
187
}
182
188
183
189
/**
184
- * Returns a lazy-initialized instance of
185
- * {@link org.restlet.engine.application.CorsResponseHelper}.
190
+ * Returns a lazy-initialized instance of {@link org.restlet.engine.application.CorsResponseHelper}.
186
191
*/
187
192
protected CorsResponseHelper getCorsResponseHelper () {
188
193
if (corsResponseHelper == null ) {
@@ -193,12 +198,14 @@ protected CorsResponseHelper getCorsResponseHelper() {
193
198
.setAllowAllRequestedHeaders (allowAllRequestedHeaders );
194
199
corsResponseHelper .setAllowedHeaders (allowedHeaders );
195
200
corsResponseHelper .setExposedHeaders (exposedHeaders );
201
+ corsResponseHelper .setMaxAge (maxAge );
196
202
}
197
203
return corsResponseHelper ;
198
204
}
199
205
200
206
/**
201
207
* Returns the list of methods allowed by default, used when {@link #skippingResourceForCorsOptions} is turned on.
208
+ *
202
209
* @return The list of methods allowed by default, used when {@link #skippingResourceForCorsOptions} is turned on.
203
210
*/
204
211
public Set <Method > getDefaultAllowedMethods () {
@@ -218,6 +225,17 @@ public Set<String> getExposedHeaders() {
218
225
return exposedHeaders ;
219
226
}
220
227
228
+ /**
229
+ * Indicates how long (in seconds) the results of a preflight request can be cached in a preflight result cache.<br>
230
+ * In case of a negative value, the results of a preflight request is not meant to be cached.<br>
231
+ * Note that when used with HTTP connectors, this property maps to the "Access-Control-Max-Age" header.
232
+ *
233
+ * @return Indicates how long the results of a preflight request can be cached in a preflight result cache.
234
+ */
235
+ public int getMaxAge () {
236
+ return maxAge ;
237
+ }
238
+
221
239
/**
222
240
* If true, indicates that the value of 'Access-Control-Request-Headers'
223
241
* request header will be copied into the 'Access-Control-Allow-Headers'
@@ -239,11 +257,11 @@ public boolean isAllowedCredentials() {
239
257
240
258
/**
241
259
* If true, the filter does not call the server resource for OPTIONS method
242
- * of CORS request and set Access-Control-Allow-Methods header with
243
- * {@link #defaultAllowedMethods}. Default is false.
244
- *
260
+ * of CORS request and set Access-Control-Allow-Methods header with {@link #defaultAllowedMethods}. Default is
261
+ * false.
262
+ *
245
263
* @return True if the filter does not call the server resource for
246
- * OPTIONS method of CORS request.
264
+ * OPTIONS method of CORS request.
247
265
*/
248
266
public boolean isSkippingResourceForCorsOptions () {
249
267
return skippingResourceForCorsOptions ;
@@ -305,7 +323,10 @@ public CorsFilter setAllowingAllRequestedHeaders(
305
323
306
324
/**
307
325
* Sets the list of methods allowed by default, used when {@link #skippingResourceForCorsOptions} is turned on.
308
- * @param defaultAllowedMethods The list of methods allowed by default, used when {@link #skippingResourceForCorsOptions} is turned on.
326
+ *
327
+ * @param defaultAllowedMethods
328
+ * The list of methods allowed by default, used when {@link #skippingResourceForCorsOptions} is turned
329
+ * on.
309
330
*/
310
331
public CorsFilter setDefaultAllowedMethods (Set <Method > defaultAllowedMethods ) {
311
332
this .defaultAllowedMethods = defaultAllowedMethods ;
@@ -324,12 +345,24 @@ public CorsFilter setExposedHeaders(Set<String> exposedHeaders) {
324
345
return this ;
325
346
}
326
347
348
+ /**
349
+ * Sets the value of 'Access-Control-Max-Age' response header.<br>
350
+ * In case of negative value, the header is not set.
351
+ *
352
+ * @param maxAge
353
+ * The value of 'Access-Control-Max-Age' response header.
354
+ */
355
+ public CorsFilter setMaxAge (int maxAge ) {
356
+ this .maxAge = maxAge ;
357
+ return this ;
358
+ }
359
+
327
360
/**
328
361
* Sets the value of skipResourceForCorsOptions field.
329
- *
362
+ *
330
363
* @param skipResourceForCorsOptions
331
- * True if the filter does not call the server resource for
332
- * OPTIONS method of CORS request.
364
+ * True if the filter does not call the server resource for
365
+ * OPTIONS method of CORS request.
333
366
* @return Itself for chaining methods calls.
334
367
*/
335
368
public CorsFilter setSkippingResourceForCorsOptions (boolean skipResourceForCorsOptions ) {
0 commit comments