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
public class CorsFilter extends Filter
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);
-
-
Field Summary
Fields Modifier and Type Field Description booleanallowAllRequestedHeadersIf true, copies the value of 'Access-Control-Request-Headers' request header into the 'Access-Control-Allow-Headers' response header.private booleanallowedCredentialsIf true, add 'Access-Control-Allow-Credentials' header.private java.util.Set<java.lang.String>allowedHeadersThe value of 'Access-Control-Allow-Headers' response header.private java.util.Set<java.lang.String>allowedOriginsThe value of 'Access-Control-Allow-Origin' header.private CorsResponseHelpercorsResponseHelperHelper for generating CORS response.private java.util.Set<Method>defaultAllowedMethodsThe set of methods allowed by default, used whenskippingResourceForCorsOptionsis turned on.private java.util.Set<java.lang.String>exposedHeadersThe value of 'Access-Control-Expose-Headers' response header.private intmaxAgeThe value of 'Access-Control-Max-Age' response header.private booleanskippingResourceForCorsOptionsIf true, the filter does not call the server resource for OPTIONS method of CORS request and set Access-Control-Allow-Methods header withdefaultAllowedMethods.
-
Constructor Summary
Constructors Constructor Description CorsFilter()Constructor.CorsFilter(Context context)Constructor.CorsFilter(Context context, Restlet next)Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected 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.java.util.Set<java.lang.String>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.java.util.Set<java.lang.String>getAllowedOrigins()Returns the URI an origin server allows for the requested resource.protected CorsResponseHelpergetCorsResponseHelper()Returns a lazy-initialized instance ofCorsResponseHelper.java.util.Set<Method>getDefaultAllowedMethods()Returns the list of methods allowed by default, used whenskippingResourceForCorsOptionsis turned on.java.util.Set<java.lang.String>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.intgetMaxAge()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.booleanisAllowAllRequestedHeaders()If true, indicates that the value of 'Access-Control-Request-Headers' request header will be copied into the 'Access-Control-Allow-Headers' response header.booleanisAllowedCredentials()If true, adds 'Access-Control-Allow-Credentials' header.booleanisSkippingResourceForCorsOptions()If true, the filter does not call the server resource for OPTIONS method of CORS request and set Access-Control-Allow-Methods header withdefaultAllowedMethods.CorsFiltersetAllowedCredentials(boolean allowedCredentials)If true, adds 'Access-Control-Allow-Credentials' header.CorsFiltersetAllowedHeaders(java.util.Set<java.lang.String> allowedHeaders)Sets the value of the 'Access-Control-Allow-Headers' response header.CorsFiltersetAllowedOrigins(java.util.Set<java.lang.String> allowedOrigins)Sets the value of 'Access-Control-Allow-Origin' header.CorsFiltersetAllowingAllRequestedHeaders(boolean allowingAllRequestedHeaders)If true, copies the value of 'Access-Control-Request-Headers' request header into the 'Access-Control-Allow-Headers' response header.CorsFiltersetDefaultAllowedMethods(java.util.Set<Method> defaultAllowedMethods)Sets the list of methods allowed by default, used whenskippingResourceForCorsOptionsis turned on.CorsFiltersetExposedHeaders(java.util.Set<java.lang.String> exposedHeaders)Sets the value of 'Access-Control-Expose-Headers' response header.CorsFiltersetMaxAge(int maxAge)Sets the value of 'Access-Control-Max-Age' response header.
In case of negative value, the header is not set.CorsFiltersetSkippingResourceForCorsOptions(boolean skipResourceForCorsOptions)Sets the value of skipResourceForCorsOptions field.-
Methods inherited from class org.restlet.routing.Filter
doHandle, getNext, handle, hasNext, setNext, setNext, start, stop
-
Methods 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 Detail
-
allowAllRequestedHeaders
public boolean allowAllRequestedHeaders
If 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.
-
allowedCredentials
private boolean allowedCredentials
If true, add 'Access-Control-Allow-Credentials' header. Default is false.
-
allowedHeaders
private java.util.Set<java.lang.String> allowedHeaders
The value of 'Access-Control-Allow-Headers' response header. Used only ifallowAllRequestedHeadersis false.
-
allowedOrigins
private java.util.Set<java.lang.String> allowedOrigins
The value of 'Access-Control-Allow-Origin' header. Default is '*'.
-
corsResponseHelper
private CorsResponseHelper corsResponseHelper
Helper for generating CORS response.
-
defaultAllowedMethods
private java.util.Set<Method> defaultAllowedMethods
The set of methods allowed by default, used whenskippingResourceForCorsOptionsis turned on. By default: GET, PUT, POST, DELETE, PATCH.
-
exposedHeaders
private java.util.Set<java.lang.String> exposedHeaders
The value of 'Access-Control-Expose-Headers' response header.
-
maxAge
private int maxAge
The value of 'Access-Control-Max-Age' response header. Default is that the header is not set.
-
skippingResourceForCorsOptions
private boolean skippingResourceForCorsOptions
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.
-
-
Method Detail
-
afterHandle
protected void afterHandle(Request request, Response response)
Add CORS headers to response- Overrides:
afterHandlein classFilter- Parameters:
request- The request to handle.response- The response
-
beforeHandle
protected int beforeHandle(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.- Overrides:
beforeHandlein classFilter- Parameters:
request- The request to handle.response- The response to update.- Returns:
- The continuation status. Either
Filter.CONTINUEorFilter.SKIPorFilter.STOP.
-
getAllowedHeaders
public java.util.Set<java.lang.String> 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
public java.util.Set<java.lang.String> 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
protected CorsResponseHelper getCorsResponseHelper()
Returns a lazy-initialized instance ofCorsResponseHelper.
-
getDefaultAllowedMethods
public java.util.Set<Method> 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
public java.util.Set<java.lang.String> 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
public CorsFilter setAllowedCredentials(boolean allowedCredentials)
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
public CorsFilter setAllowedHeaders(java.util.Set<java.lang.String> allowedHeaders)
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
public CorsFilter setAllowedOrigins(java.util.Set<java.lang.String> allowedOrigins)
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
public CorsFilter setAllowingAllRequestedHeaders(boolean allowingAllRequestedHeaders)
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
public CorsFilter setDefaultAllowedMethods(java.util.Set<Method> defaultAllowedMethods)
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.
-
setExposedHeaders
public CorsFilter setExposedHeaders(java.util.Set<java.lang.String> exposedHeaders)
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
public CorsFilter setMaxAge(int maxAge)
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
public CorsFilter setSkippingResourceForCorsOptions(boolean skipResourceForCorsOptions)
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.
-
-