Package org.restlet.engine.application
Class CorsFilter
java.lang.Object
org.restlet.Restlet
org.restlet.routing.Filter
org.restlet.engine.application.CorsFilter
- All Implemented Interfaces:
Uniform
Filter that helps support CORS requests. This filter lets the target
resources specify the allowed methods.
Example:
Router router = new Router(getContext());
CorsFilter corsFilter = new CorsFilter(getContext(), router);
corsFilter.setAllowedOrigins(new HashSet(Arrays.asList("http://server.com")));
corsFilter.setAllowedCredentials(true);
- Author:
- Manuel Boillod
-
Field Summary
FieldsModifier and TypeFieldDescriptionbooleanIf true, copies the value of 'Access-Control-Request-Headers' request header into the 'Access-Control-Allow-Headers' response header. -
Constructor Summary
ConstructorsConstructorDescriptionConstructor.CorsFilter(Context context) Constructor.CorsFilter(Context context, Restlet next) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidafterHandle(Request request, Response response) Add CORS headers to responseprotected intbeforeHandle(Request request, Response response) Skip the call to the server resource if theskippingResourceForCorsOptionsis true and if the current request use the OPTIONS method and is a CORS request.Returns the modifiable set of headers allowed by the actual request on the current resource.
Note that when used with HTTP connectors, this property maps to the "Access-Control-Allow-Headers" header.Returns the URI an origin server allows for the requested resource.protected CorsResponseHelperReturns a lazy-initialized instance ofCorsResponseHelper.Returns the list of methods allowed by default, used whenskippingResourceForCorsOptionsis turned on.Returns a modifiable whitelist of headers an origin server allows for the requested resource.
Note that when used with HTTP connectors, this property maps to the "Access-Control-Expose-Headers" header.intIndicates how long (in seconds) the results of a preflight request can be cached in a preflight result cache.
In case of a negative value, the results of a preflight request is not meant to be cached.
Note that when used with HTTP connectors, this property maps to the "Access-Control-Max-Age" header.booleanIf true, indicates that the value of 'Access-Control-Request-Headers' request header will be copied into the 'Access-Control-Allow-Headers' response header.booleanIf true, adds 'Access-Control-Allow-Credentials' header.booleanIf true, the filter does not call the server resource for OPTIONS method of CORS request and set Access-Control-Allow-Methods header withdefaultAllowedMethods.setAllowedCredentials(boolean allowedCredentials) If true, adds 'Access-Control-Allow-Credentials' header.setAllowedHeaders(Set<String> allowedHeaders) Sets the value of the 'Access-Control-Allow-Headers' response header.setAllowedOrigins(Set<String> allowedOrigins) Sets the value of 'Access-Control-Allow-Origin' header.setAllowingAllRequestedHeaders(boolean allowingAllRequestedHeaders) If true, copies the value of 'Access-Control-Request-Headers' request header into the 'Access-Control-Allow-Headers' response header.setDefaultAllowedMethods(Set<Method> defaultAllowedMethods) Sets the list of methods allowed by default, used whenskippingResourceForCorsOptionsis turned on.setExposedHeaders(Set<String> exposedHeaders) Sets the value of 'Access-Control-Expose-Headers' response header.setMaxAge(int maxAge) Sets the value of 'Access-Control-Max-Age' response header.
In case of negative value, the header is not set.setSkippingResourceForCorsOptions(boolean skipResourceForCorsOptions) Sets the value of skipResourceForCorsOptions field.Methods inherited from class org.restlet.routing.Filter
doHandle, getNext, handle, hasNext, setNext, setNext, start, stopMethods inherited from class org.restlet.Restlet
createFinder, finalize, getApplication, getAuthor, getContext, getDescription, getFinderClass, getLogger, getName, getOwner, handle, handle, handle, isStarted, isStopped, setAuthor, setContext, setDescription, setFinderClass, setName, setOwner
-
Field Details
-
allowAllRequestedHeaders
public boolean allowAllRequestedHeadersIf true, copies the value of 'Access-Control-Request-Headers' request header into the 'Access-Control-Allow-Headers' response header. If false, useallowedHeaders. Default is true.
-
-
Constructor Details
-
CorsFilter
public CorsFilter()Constructor. -
CorsFilter
Constructor.- Parameters:
context- The context.
-
CorsFilter
Constructor.- Parameters:
context- The context.next- The next Restlet.
-
-
Method Details
-
afterHandle
Add CORS headers to response- Overrides:
afterHandlein classFilter- Parameters:
request- The request to handle.response- The response
-
beforeHandle
Skip the call to the server resource if theskippingResourceForCorsOptionsis true and if the current request use the OPTIONS method and is a CORS request.- Overrides:
beforeHandlein classFilter- Parameters:
request- The request to handle.response- The response to update.- Returns:
- The continuation status. Either
Filter.CONTINUEorFilter.SKIPorFilter.STOP.
-
getAllowedHeaders
Returns the modifiable set of headers allowed by the actual request on the current resource.
Note that when used with HTTP connectors, this property maps to the "Access-Control-Allow-Headers" header.- Returns:
- The set of headers allowed by the actual request on the current resource.
-
getAllowedOrigins
Returns the URI an origin server allows for the requested resource. Use "*" as a wildcard character.
Note that when used with HTTP connectors, this property maps to the "Access-Control-Allow-Origin" header.- Returns:
- The origin allowed by the requested resource.
-
getCorsResponseHelper
Returns a lazy-initialized instance ofCorsResponseHelper. -
getDefaultAllowedMethods
Returns the list of methods allowed by default, used whenskippingResourceForCorsOptionsis turned on.- Returns:
- The list of methods allowed by default, used when
skippingResourceForCorsOptionsis turned on.
-
getExposedHeaders
Returns a modifiable whitelist of headers an origin server allows for the requested resource.
Note that when used with HTTP connectors, this property maps to the "Access-Control-Expose-Headers" header.- Returns:
- The set of headers an origin server allows for the requested resource.
-
getMaxAge
public int getMaxAge()Indicates how long (in seconds) the results of a preflight request can be cached in a preflight result cache.
In case of a negative value, the results of a preflight request is not meant to be cached.
Note that when used with HTTP connectors, this property maps to the "Access-Control-Max-Age" header.- Returns:
- Indicates how long the results of a preflight request can be cached in a preflight result cache.
-
isAllowAllRequestedHeaders
public boolean isAllowAllRequestedHeaders()If true, indicates that the value of 'Access-Control-Request-Headers' request header will be copied into the 'Access-Control-Allow-Headers' response header. If false, useallowedHeaders. -
isAllowedCredentials
public boolean isAllowedCredentials()If true, adds 'Access-Control-Allow-Credentials' header.- Returns:
- True, if the 'Access-Control-Allow-Credentials' header will be added.
-
isSkippingResourceForCorsOptions
public boolean isSkippingResourceForCorsOptions()If true, the filter does not call the server resource for OPTIONS method of CORS request and set Access-Control-Allow-Methods header withdefaultAllowedMethods. Default is false.- Returns:
- True if the filter does not call the server resource for OPTIONS method of CORS request.
-
setAllowedCredentials
If true, adds 'Access-Control-Allow-Credentials' header.- Parameters:
allowedCredentials- True to add the 'Access-Control-Allow-Credentials' header.- Returns:
- Itself for chaining methods calls.
-
setAllowedHeaders
Sets the value of the 'Access-Control-Allow-Headers' response header. Used only ifallowAllRequestedHeadersis false.- Parameters:
allowedHeaders- The value of 'Access-Control-Allow-Headers' response header.- Returns:
- Itself for chaining methods calls.
-
setAllowedOrigins
Sets the value of 'Access-Control-Allow-Origin' header.- Parameters:
allowedOrigins- The value of 'Access-Control-Allow-Origin' header.- Returns:
- Itself for chaining methods calls.
-
setAllowingAllRequestedHeaders
If true, copies the value of 'Access-Control-Request-Headers' request header into the 'Access-Control-Allow-Headers' response header. If false, useallowedHeaders.- Parameters:
allowingAllRequestedHeaders- True to copy the value of 'Access-Control-Request-Headers' request header into the 'Access-Control-Allow-Headers' response header. If false, useallowedHeaders.- Returns:
- Itself for chaining methods calls.
-
setDefaultAllowedMethods
Sets the list of methods allowed by default, used whenskippingResourceForCorsOptionsis turned on.- Parameters:
defaultAllowedMethods- The list of methods allowed by default, used whenskippingResourceForCorsOptionsis turned on.- Returns:
- Itself for chaining methods calls.
-
setExposedHeaders
Sets the value of 'Access-Control-Expose-Headers' response header.- Parameters:
exposedHeaders- The value of 'Access-Control-Expose-Headers' response header.- Returns:
- Itself for chaining methods calls.
-
setMaxAge
Sets the value of 'Access-Control-Max-Age' response header.
In case of negative value, the header is not set.- Parameters:
maxAge- The value of 'Access-Control-Max-Age' response header.
-
setSkippingResourceForCorsOptions
Sets the value of skipResourceForCorsOptions field.- Parameters:
skipResourceForCorsOptions- True if the filter does not call the server resource for OPTIONS method of CORS request.- Returns:
- Itself for chaining methods calls.
-