/** * Jooby https://jooby.io * Apache License Version 2.0 https://jooby.io/LICENSE.txt * Copyright 2014 Edgar Espina */ package io.jooby; import com.typesafe.config.Config; import javax.annotation.Nonnull; import java.time.Duration; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.concurrent.TimeUnit; import java.util.function.Predicate; import java.util.regex.Pattern; import java.util.stream.Collectors; import static java.util.Objects.requireNonNull; /** *
* Cross-origin resource sharing (CORS) is a mechanism that allows restricted resources (e.g. fonts, * JavaScript, etc.) on a web page to be requested from another domain outside the domain from which * the resource originated. *
* ** This class represent the available options for configure CORS in Jooby. *
* *
* {
* decorator(new CorsHandler());
* }
*
*
* * Previous example, adds a cors filter using the default cors options. *
* * @author edgar * @since 2.0.4 */ public class Cors { private static class Matcher* origin: "*" * credentials: true * allowedMethods: [GET, POST] * allowedHeaders: [X-Requested-With, Content-Type, Accept, Origin] * maxAge: 30m * exposedHeaders: [] **/ public Cors() { setOrigin("*"); setUseCredentials(true); setMethods("GET", "POST"); setHeaders("X-Requested-With", "Content-Type", "Accept", "Origin"); setMaxAge(Duration.ofMinutes(_30)); } /** * If true, set the
Access-Control-Allow-Credentials header.
*
* @return If the Access-Control-Allow-Credentials header must be set.
*/
public boolean getUseCredentials() {
return this.credentials;
}
/**
* If true, set the Access-Control-Allow-Credentials header.
*
* @param credentials Credentials.
* @return This cors.
*/
public Cors setUseCredentials(boolean credentials) {
this.credentials = credentials;
return this;
}
/**
* @return True if any origin is accepted.
*/
public boolean anyOrigin() {
return origin.wild;
}
/**
* An origin must be a "*" (any origin), a domain name (like, http://foo.com) and/or a regex
* (like, http://*.domain.com).
*
* @return List of valid origins: Default is: *
*/
public List*.
*/
public boolean anyHeader() {
return headers.wild;
}
/**
* True if all the headers are allowed.
*
* @param headers Headers to test.
* @return True if all the headers are allowed.
*/
public boolean allowHeader(final String... headers) {
return allowHeaders(Arrays.asList(headers));
}
/**
* True if all the headers are allowed.
*
* @param headers Headers to test.
* @return True if all the headers are allowed.
*/
public boolean allowHeaders(final ListX-Requested-With,
* Content-Type, Accept and Origin.
*/
public List* if any
* header is allowed.
*
* @param headers Headers to set.
* @return This cors.
*/
public Cors setHeaders(final String... headers) {
return setHeaders(Arrays.asList(headers));
}
/**
* Set one or more allowed headers. Possible values are a header name or * if any
* header is allowed.
*
* @param headers Headers to set.
* @return This cors.
*/
public Cors setHeaders(final List-1 to turn this off.
* @return This cors.
*/
public Cors setMaxAge(final Duration preflightMaxAge) {
this.maxAge = preflightMaxAge;
return this;
}
/**
* Get cors options from application configuration file.
*
* {@code
* cors {
* origin: *
* methods: [GET, POST]
* headers: [Custom-Header]
* maxAge: 30m
* exposesHeaders: [Header]
* }
* }
*
* @param conf Configuration.
* @return Cors options.
*/
public static @Nonnull Cors from(@Nonnull Config conf) {
Config cors = conf.hasPath("cors") ? conf.getConfig("cors") : conf;
Cors options = new Cors();
if (cors.hasPath("origin")) {
options.setOrigin(list(cors.getAnyRef("origin")));
}
if (cors.hasPath("credentials")) {
options.setUseCredentials(cors.getBoolean("credentials"));
}
if (cors.hasPath("methods")) {
options.setMethods(list(cors.getAnyRef("methods")));
}
if (cors.hasPath("headers")) {
options.setHeaders(list(cors.getAnyRef("headers")));
}
if (cors.hasPath("maxAge")) {
options.setMaxAge(Duration.ofSeconds(cors.getDuration("maxAge", TimeUnit.SECONDS)));
}
if (cors.hasPath("exposedHeaders")) {
options.setExposedHeaders(list(cors.getAnyRef("exposedHeaders")));
}
return options;
}
@SuppressWarnings({"unchecked", "rawtypes"})
private static List