restx.security.CORS Maven / Gradle / Ivy
package restx.security;
import com.google.common.base.Optional;
import restx.RestxRequest;
import java.util.Collection;
import java.util.Collections;
/**
* User: xavierhanin
* Date: 4/1/13
* Time: 10:29 PM
*/
public abstract class CORS {
static CORS check(Iterable authorizers, RestxRequest request, String origin, String method, String restxPath) {
for (CORSAuthorizer authorizer : authorizers) {
Optional cors = authorizer.checkCORS(request, origin, method, restxPath);
if (cors.isPresent()) {
return cors.get();
}
}
return CORS.reject();
}
public static CORS reject() {
return REJECTED_CORS;
}
public static CORS accept(String origin, Collection methods) {
return new AcceptedCORS(origin, methods, Collections.emptyList(), Optional.absent(), 1728000);
}
public static CORS accept(String origin, Collection methods, int maxAge) {
return new AcceptedCORS(origin, methods, Collections.emptyList(), Optional.absent(), maxAge);
}
public static CORS accept(String origin, Collection methods, Collection headers, int maxAge) {
return new AcceptedCORS(origin, methods, headers, Optional.absent(), maxAge);
}
public static CORS accept(String origin, Collection methods, Collection headers, Optional credentials, int maxAge) {
return new AcceptedCORS(origin, methods, headers, credentials, maxAge);
}
public abstract boolean isAccepted();
private static final RejectedCORS REJECTED_CORS = new RejectedCORS();
private final static class RejectedCORS extends CORS {
public boolean isAccepted() {
return false;
}
}
}