@@ -67,8 +67,8 @@ public class CorsFilter extends Filter {
6767 private boolean allowedCredentials = false ;
6868
6969 /**
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.
7272 */
7373 private Set <String > allowedHeaders = null ;
7474
@@ -78,17 +78,23 @@ public class CorsFilter extends Filter {
7878 /** Helper for generating CORS response. */
7979 private CorsResponseHelper corsResponseHelper ;
8080
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+ */
8285 private Set <Method > defaultAllowedMethods = new HashSet <>(Arrays .asList (Method .GET , Method .POST , Method .PUT ,
8386 Method .DELETE , Method .PATCH ));
8487
8588 /** The value of 'Access-Control-Expose-Headers' response header. */
8689 private Set <String > exposedHeaders = null ;
8790
91+ /** The value of 'Access-Control-Max-Age' response header. Default is that the header is not set. */
92+ private int maxAge = -1 ;
93+
8894 /**
8995 * 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.
9298 */
9399 private boolean skippingResourceForCorsOptions = false ;
94100
@@ -122,9 +128,22 @@ public CorsFilter(Context context, Restlet next) {
122128 }
123129
124130 /**
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+ *
128147 * @param request
129148 * The request to handle.
130149 * @param response
@@ -142,19 +161,6 @@ && getCorsResponseHelper().isCorsRequest(request)) {
142161 }
143162 }
144163
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-
158164 /**
159165 * Returns the modifiable set of headers allowed by the actual request on
160166 * the current resource.<br>
@@ -181,8 +187,7 @@ public Set<String> getAllowedOrigins() {
181187 }
182188
183189 /**
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}.
186191 */
187192 protected CorsResponseHelper getCorsResponseHelper () {
188193 if (corsResponseHelper == null ) {
@@ -193,12 +198,14 @@ protected CorsResponseHelper getCorsResponseHelper() {
193198 .setAllowAllRequestedHeaders (allowAllRequestedHeaders );
194199 corsResponseHelper .setAllowedHeaders (allowedHeaders );
195200 corsResponseHelper .setExposedHeaders (exposedHeaders );
201+ corsResponseHelper .setMaxAge (maxAge );
196202 }
197203 return corsResponseHelper ;
198204 }
199205
200206 /**
201207 * Returns the list of methods allowed by default, used when {@link #skippingResourceForCorsOptions} is turned on.
208+ *
202209 * @return The list of methods allowed by default, used when {@link #skippingResourceForCorsOptions} is turned on.
203210 */
204211 public Set <Method > getDefaultAllowedMethods () {
@@ -218,6 +225,17 @@ public Set<String> getExposedHeaders() {
218225 return exposedHeaders ;
219226 }
220227
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+
221239 /**
222240 * If true, indicates that the value of 'Access-Control-Request-Headers'
223241 * request header will be copied into the 'Access-Control-Allow-Headers'
@@ -239,11 +257,11 @@ public boolean isAllowedCredentials() {
239257
240258 /**
241259 * 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+ *
245263 * @return True if the filter does not call the server resource for
246- * OPTIONS method of CORS request.
264+ * OPTIONS method of CORS request.
247265 */
248266 public boolean isSkippingResourceForCorsOptions () {
249267 return skippingResourceForCorsOptions ;
@@ -305,7 +323,10 @@ public CorsFilter setAllowingAllRequestedHeaders(
305323
306324 /**
307325 * 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.
309330 */
310331 public CorsFilter setDefaultAllowedMethods (Set <Method > defaultAllowedMethods ) {
311332 this .defaultAllowedMethods = defaultAllowedMethods ;
@@ -324,12 +345,24 @@ public CorsFilter setExposedHeaders(Set<String> exposedHeaders) {
324345 return this ;
325346 }
326347
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+
327360 /**
328361 * Sets the value of skipResourceForCorsOptions field.
329- *
362+ *
330363 * @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.
333366 * @return Itself for chaining methods calls.
334367 */
335368 public CorsFilter setSkippingResourceForCorsOptions (boolean skipResourceForCorsOptions ) {
0 commit comments