com.jetdrone.vertx.yoke.middleware.Cors Maven / Gradle / Ivy
package com.jetdrone.vertx.yoke.middleware;
import static io.netty.handler.codec.http.HttpHeaders.Names.ACCESS_CONTROL_ALLOW_CREDENTIALS;
import static io.netty.handler.codec.http.HttpHeaders.Names.ACCESS_CONTROL_ALLOW_HEADERS;
import static io.netty.handler.codec.http.HttpHeaders.Names.ACCESS_CONTROL_ALLOW_METHODS;
import static io.netty.handler.codec.http.HttpHeaders.Names.ACCESS_CONTROL_ALLOW_ORIGIN;
import static io.netty.handler.codec.http.HttpHeaders.Names.ACCESS_CONTROL_EXPOSE_HEADERS;
import static io.netty.handler.codec.http.HttpHeaders.Names.ACCESS_CONTROL_REQUEST_HEADERS;
import static io.netty.handler.codec.http.HttpHeaders.Names.ACCESS_CONTROL_REQUEST_METHOD;
import static io.netty.handler.codec.http.HttpHeaders.Names.ORIGIN;
import static io.netty.handler.codec.http.HttpMethod.OPTIONS;
import java.util.Collection;
import java.util.Set;
import java.util.regex.Pattern;
import org.jetbrains.annotations.NotNull;
import org.vertx.java.core.Handler;
import org.vertx.java.core.http.HttpServerResponse;
import com.jetdrone.vertx.yoke.Middleware;
/**
* Basic CORS support.
*/
public class Cors extends Middleware
{
private final Pattern allowedOriginPattern;
private final Set allowedMethods;
private final Set allowedHeaders;
private final Set exposedHeaders;
private final boolean allowCredentials;
/**
* @param allowedOriginPattern if null, '*' will be used.
*/
public Cors(final Pattern allowedOriginPattern,
final Set allowedMethods,
final Set allowedHeaders,
final Set exposedHeaders,
final boolean allowCredentials)
{
if (allowCredentials && allowedOriginPattern == null)
{
throw new IllegalArgumentException("Resource that supports credentials can't accept all origins.");
}
this.allowedOriginPattern = allowedOriginPattern;
this.allowedMethods = allowedMethods;
this.allowedHeaders = allowedHeaders;
this.exposedHeaders = exposedHeaders;
this.allowCredentials = allowCredentials;
}
@Override
public void handle(@NotNull final YokeRequest request, @NotNull final Handler