
io.muserver.handlers.CORSHandlerBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mu-server Show documentation
Show all versions of mu-server Show documentation
A simple but powerful web server framework
The newest version!
package io.muserver.handlers;
import io.muserver.Method;
import io.muserver.MuHandlerBuilder;
import io.muserver.Mutils;
import io.muserver.rest.CORSConfig;
import io.muserver.rest.CORSConfigBuilder;
import java.util.HashSet;
import java.util.Set;
import static java.util.Arrays.asList;
/**
* Used to create a handler that puts appropriate CORS headers on requests.
* Simply add this handler before any other handlers that you want CORS headers added to.
*/
public class CORSHandlerBuilder implements MuHandlerBuilder {
private CORSConfig corsConfig;
private Set allowedMethods;
/**
* Sets the CORS configuration for the handler
* @param corsConfig The config to use
* @return This builder
*/
public CORSHandlerBuilder withCORSConfig(CORSConfig corsConfig) {
Mutils.notNull("corsConfig", corsConfig);
this.corsConfig = corsConfig;
return this;
}
/**
* Sets the CORS configuration for the handler
* @param corsConfig The config to use
* @return This builder
*/
public CORSHandlerBuilder withCORSConfig(CORSConfigBuilder corsConfig) {
return withCORSConfig(corsConfig.build());
}
/**
* Specifies the headers allowed for CORS requests. Defaults to all methods except Trace and Connect.
* @param methods The methods to allow, or null to allow all except Trace and Connect.
* @return This builder.
*/
public CORSHandlerBuilder withAllowedMethods(Method... methods) {
if (methods == null) {
allowedMethods = null;
} else {
allowedMethods = new HashSet<>(asList(methods));
}
return this;
}
/**
* A helper for creating a config object to pass to {@link #withCORSConfig(CORSConfigBuilder)}
* @return a CORS configuration object
*/
public static CORSConfigBuilder config() {
return CORSConfigBuilder.corsConfig();
}
/**
* A helper for creating a CORS handler
* @return A new builder.
*/
public static CORSHandlerBuilder corsHandler() {
return new CORSHandlerBuilder();
}
@Override
public CORSHandler build() {
Set methods = this.allowedMethods;
if (methods == null || methods.isEmpty()) {
methods = new HashSet<>(asList(Method.values()));
methods.remove(Method.TRACE);
methods.remove(Method.CONNECT);
}
if (corsConfig == null) {
throw new IllegalStateException("You must specify the CORS config");
}
return new CORSHandler(corsConfig, methods);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy