Package org.restlet.engine.application
Class CorsResponseHelper
java.lang.Object
org.restlet.engine.application.CorsResponseHelper
Helps to generate response CORS headers.
The CORS specification defines a subset of methods qualified as simple HEAD, GET and POST. Any other methods should send a preflight request with the method OPTIONS.
The CORS specification defines a subset of methods qualified as simple HEAD, GET and POST. Any other methods should send a preflight request with the method OPTIONS.
- Author:
- Manuel Boillod
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionbooleanIf true, copies the value of 'Access-Control-Request-Headers' request header into the 'Access-Control-Allow-Headers' response header.booleanIf true, add 'Access-Control-Allow-Credentials' header.The value of 'Access-Control-Allow-Headers' response header.The value of 'Access-Control-Allow-Origin' header.The value of 'Access-Control-Expose-Headers' response header.intThe value of 'Access-Control-Max-Age' response header. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddCorsResponseHeaders(Request request, Response response) Adds CORS headers to the given response.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.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.booleanisCorsRequest(Request request) Returns true if the request is a CORS request.voidsetAllowAllRequestedHeaders(boolean allowAllRequestedHeaders) If true, copies the value of 'Access-Control-Request-Headers' request header into the 'Access-Control-Allow-Headers' response header.voidsetAllowedCredentials(boolean allowedCredentials) If true, adds 'Access-Control-Allow-Credentials' header.voidsetAllowedHeaders(Set<String> allowedHeaders) Sets the value of the 'Access-Control-Allow-Headers' response header.voidsetAllowedOrigins(Set<String> allowedOrigins) Sets the value of 'Access-Control-Allow-Origin' header.voidsetExposedHeaders(Set<String> exposedHeaders) Sets the value of 'Access-Control-Expose-Headers' response header.voidsetMaxAge(int maxAge) Sets the value of 'Access-Control-Max-Age' response header.
In case of negative value, the header is not set.
-
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. -
allowedCredentials
public boolean allowedCredentialsIf true, add 'Access-Control-Allow-Credentials' header. Default is false. -
allowedHeaders
The value of 'Access-Control-Allow-Headers' response header. Used only ifallowAllRequestedHeadersis false. -
allowedOrigins
The value of 'Access-Control-Allow-Origin' header. Default is '*'. -
exposedHeaders
The value of 'Access-Control-Expose-Headers' response header. -
maxAge
public int maxAgeThe value of 'Access-Control-Max-Age' response header. Default is that the header is not set.
-
-
Constructor Details
-
CorsResponseHelper
public CorsResponseHelper()
-
-
Method Details
-
addCorsResponseHeaders
Adds CORS headers to the given response.- Parameters:
request- The current request.response- The response.
-
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.
-
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.
-
isCorsRequest
Returns true if the request is a CORS request.- Parameters:
request- The current request.- Returns:
- true if the request is a CORS request.
-
setAllowAllRequestedHeaders
public void setAllowAllRequestedHeaders(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.- Parameters:
allowAllRequestedHeaders- True to copy the value of 'Access-Control-Request-Headers' request header into the 'Access-Control-Allow-Headers' response header. If false, useallowedHeaders.
-
setAllowedCredentials
public void setAllowedCredentials(boolean allowedCredentials) If true, adds 'Access-Control-Allow-Credentials' header.- Parameters:
allowedCredentials- True to add the 'Access-Control-Allow-Credentials' header.
-
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.
-
setAllowedOrigins
Sets the value of 'Access-Control-Allow-Origin' header.- Parameters:
allowedOrigins- The value of 'Access-Control-Allow-Origin' header.
-
setExposedHeaders
Sets the value of 'Access-Control-Expose-Headers' response header.- Parameters:
exposedHeaders- The value of 'Access-Control-Expose-Headers' response header.
-
setMaxAge
public void 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.
-