Class ChainedCorsPolicyBuilder
- java.lang.Object
-
- com.linecorp.armeria.server.cors.ChainedCorsPolicyBuilder
-
public class ChainedCorsPolicyBuilder extends Object
Builds a newCorsPolicy
.This class can only be created through the
CorsServiceBuilder.andForOrigins(String...)
orCorsServiceBuilder.andForOrigin(String)
method of theCorsServiceBuilder
.Calling
and()
method will return the control toCorsServiceBuilder
.
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description B
allowCredentials()
Enables cookies to be added to CORS requests.B
allowNullOrigin()
Enables a successful CORS response with a"null"
value for the CORS response header"Access-Control-Allow-Origin"
.B
allowRequestHeaders(CharSequence... headers)
Specifies the headers that should be returned in the CORS"Access-Control-Allow-Headers"
response header.B
allowRequestMethods(HttpMethod... methods)
Specifies the allowed set of HTTP request methods that should be returned in the CORS"Access-Control-Allow-Methods"
response header.CorsServiceBuilder
and()
Returns the parentCorsServiceBuilder
.ChainedCorsPolicyBuilder
andForOrigin(String origin)
Creates a new instance ofChainedCorsPolicyBuilder
added to the parentCorsServiceBuilder
.ChainedCorsPolicyBuilder
andForOrigins(String... origins)
Creates a new instance ofChainedCorsPolicyBuilder
added to the parentCorsServiceBuilder
.B
disablePreflightResponseHeaders()
Specifies that no preflight response headers should be added to a preflight response.B
exposeHeaders(CharSequence... headers)
Specifies the headers to be exposed to calling clients.B
maxAge(long maxAge)
Sets the CORS"Access-Control-Max-Age"
response header and enables the caching of the preflight response for the specified time.B
preflightResponseHeader(CharSequence name, Iterable<?> values)
Specifies HTTP response headers that should be added to a CORS preflight response.B
preflightResponseHeader(CharSequence name, Object... values)
Specifies HTTP response headers that should be added to a CORS preflight response.B
preflightResponseHeader(CharSequence name, Supplier<?> valueSupplier)
Specifies HTTP response headers that should be added to a CORS preflight response.B
route(String pathPattern)
Adds a path pattern that this policy is supposed to be applied to.String
toString()
-
-
-
Method Detail
-
and
public CorsServiceBuilder and()
Returns the parentCorsServiceBuilder
.
-
andForOrigins
public ChainedCorsPolicyBuilder andForOrigins(String... origins)
Creates a new instance ofChainedCorsPolicyBuilder
added to the parentCorsServiceBuilder
.- Returns:
- the created instance.
-
andForOrigin
public ChainedCorsPolicyBuilder andForOrigin(String origin)
Creates a new instance ofChainedCorsPolicyBuilder
added to the parentCorsServiceBuilder
.- Returns:
- the created instance.
-
route
public B route(String pathPattern)
Adds a path pattern that this policy is supposed to be applied to.- Parameters:
pathPattern
- the path pattern that this policy is supposed to be applied to- Returns:
this
to support method chaining.- Throws:
IllegalArgumentException
- if the path pattern is not valid
-
allowNullOrigin
public B allowNullOrigin()
Enables a successful CORS response with a"null"
value for the CORS response header"Access-Control-Allow-Origin"
. Web browsers may set the"Origin"
request header to"null"
if a resource is loaded from the local file system.- Returns:
this
to support method chaining.
-
allowCredentials
public B allowCredentials()
Enables cookies to be added to CORS requests. Calling this method will set the CORS"Access-Control-Allow-Credentials"
response header totrue
. By default, cookies are not included in CORS requests.Please note that cookie support needs to be enabled on the client side as well. The client needs to opt-in to send cookies by calling:
xhr.withCredentials = true;
The default value for
'withCredentials'
isfalse
in which case no cookies are sent. Setting this totrue
will include cookies in cross origin requests.- Returns:
this
to support method chaining.
-
maxAge
public B maxAge(long maxAge)
Sets the CORS"Access-Control-Max-Age"
response header and enables the caching of the preflight response for the specified time. During this time no preflight request will be made.- Parameters:
maxAge
- the maximum time, in seconds, that the preflight response may be cached.- Returns:
this
to support method chaining.
-
exposeHeaders
public B exposeHeaders(CharSequence... headers)
Specifies the headers to be exposed to calling clients.During a simple CORS request, only certain response headers are made available by the browser, for example using:
xhr.getResponseHeader("Content-Type");
The headers that are available by default are:
Cahce-Control
Content-Language
Content-Type
Expires
Last-Modified
Pragma
To expose other headers they need to be specified which is what this method enables by adding the headers to the CORS
"Access-Control-Expose-Headers"
response header.- Parameters:
headers
- the values to be added to the"Access-Control-Expose-Headers"
response header- Returns:
this
to support method chaining.
-
allowRequestMethods
public B allowRequestMethods(HttpMethod... methods)
Specifies the allowed set of HTTP request methods that should be returned in the CORS"Access-Control-Allow-Methods"
response header.- Parameters:
methods
- theHttpMethod
s that should be allowed.- Returns:
this
to support method chaining.
-
allowRequestHeaders
public B allowRequestHeaders(CharSequence... headers)
Specifies the headers that should be returned in the CORS"Access-Control-Allow-Headers"
response header.If a client specifies headers on the request, for example by calling:
The server will receive the above header name in thexhr.setRequestHeader('My-Custom-Header', 'SomeValue');
"Access-Control-Request-Headers"
of the preflight request. The server will then decide if it allows this header to be sent for the real request (remember that a preflight is not the real request but a request asking the server if it allows a request).- Parameters:
headers
- the headers to be added to the preflight"Access-Control-Allow-Headers"
response header.- Returns:
this
to support method chaining.
-
preflightResponseHeader
public B preflightResponseHeader(CharSequence name, Object... values)
Specifies HTTP response headers that should be added to a CORS preflight response.An intermediary like a load balancer might require that a CORS preflight request have certain headers set. This enables such headers to be added.
- Parameters:
name
- the name of the HTTP header.values
- the values for the HTTP header.- Returns:
this
to support method chaining.
-
preflightResponseHeader
public B preflightResponseHeader(CharSequence name, Iterable<?> values)
Specifies HTTP response headers that should be added to a CORS preflight response.An intermediary like a load balancer might require that a CORS preflight request have certain headers set. This enables such headers to be added.
- Parameters:
name
- the name of the HTTP header.values
- the values for the HTTP header.- Returns:
this
to support method chaining.
-
preflightResponseHeader
public B preflightResponseHeader(CharSequence name, Supplier<?> valueSupplier)
Specifies HTTP response headers that should be added to a CORS preflight response.An intermediary like a load balancer might require that a CORS preflight request have certain headers set. This enables such headers to be added.
Some values must be dynamically created when the HTTP response is created, for example the
"Date"
response header. This can be accomplished by using aSupplier
which will have itsSupplier.get()
method invoked when the HTTP response is created.- Parameters:
name
- the name of the HTTP header.valueSupplier
- aSupplier
which will be invoked at HTTP response creation.- Returns:
this
to support method chaining.
-
disablePreflightResponseHeaders
public B disablePreflightResponseHeaders()
Specifies that no preflight response headers should be added to a preflight response.- Returns:
this
to support method chaining.
-
-