Search in sources :

Example 1 with CorsFilter

use of org.jboss.resteasy.plugins.interceptors.CorsFilter in project openremote by openremote.

the class AbstractHttpServerProtocol method createDeployment.

protected ResteasyDeployment createDeployment(Application application, AssetAttribute protocolConfiguration) {
    ResteasyDeployment resteasyDeployment = new ResteasyDeployment();
    resteasyDeployment.setApplication(application);
    List<String> allowedOrigins;
    if (devMode) {
        allowedOrigins = Collections.singletonList("*");
    } else {
        Optional<MetaItem> allowedOriginsMeta = protocolConfiguration.getMetaItem(META_PROTOCOL_ALLOWED_ORIGINS);
        allowedOrigins = allowedOriginsMeta.flatMap(AbstractValueHolder::getValueAsString).map(Collections::singletonList).orElseGet(() -> allowedOriginsMeta.flatMap(AbstractValueHolder::getValueAsArray).flatMap(arrayValue -> Values.getArrayElements(arrayValue, StringValue.class, true, false, StringValue::getString)).orElse(null));
    }
    if (allowedOrigins != null) {
        String allowedMethods = protocolConfiguration.getMetaItem(META_PROTOCOL_ALLOWED_METHODS).flatMap(AbstractValueHolder::getValueAsString).orElse(DEFAULT_ALLOWED_METHODS);
        if (TextUtil.isNullOrEmpty(allowedMethods)) {
            throw new IllegalArgumentException("Allowed methods meta item must be a non empty string: " + META_PROTOCOL_ALLOWED_METHODS);
        }
        CorsFilter corsFilter = new CorsFilter();
        corsFilter.getAllowedOrigins().addAll(allowedOrigins);
        corsFilter.setAllowedMethods(allowedMethods);
        resteasyDeployment.getProviders().add(corsFilter);
    }
    return resteasyDeployment;
}
Also used : java.util(java.util) ServletException(javax.servlet.ServletException) ProtocolConfiguration(org.openremote.model.asset.agent.ProtocolConfiguration) AttributeRef(org.openremote.model.attribute.AttributeRef) ArrayValue(org.openremote.model.value.ArrayValue) Application(javax.ws.rs.core.Application) ServletInfo(io.undertow.servlet.api.ServletInfo) HttpMethod(javax.ws.rs.HttpMethod) Level(java.util.logging.Level) Servlets(io.undertow.servlet.Servlets) Container(org.openremote.container.Container) Lists(com.google.common.collect.Lists) ResteasyDeployment(org.jboss.resteasy.spi.ResteasyDeployment) TextUtil(org.openremote.model.util.TextUtil) PROTOCOL_NAMESPACE(org.openremote.model.Constants.PROTOCOL_NAMESPACE) AbstractValueHolder(org.openremote.model.AbstractValueHolder) StringValue(org.openremote.model.value.StringValue) org.openremote.container.web(org.openremote.container.web) Pair(org.openremote.model.util.Pair) ModelValueMessageBodyConverter(org.openremote.container.json.ModelValueMessageBodyConverter) Logger(java.util.logging.Logger) DeploymentManager(io.undertow.servlet.api.DeploymentManager) HttpServlet30Dispatcher(org.jboss.resteasy.plugins.server.servlet.HttpServlet30Dispatcher) AbstractProtocol(org.openremote.agent.protocol.AbstractProtocol) HttpHandler(io.undertow.server.HttpHandler) MetaItem(org.openremote.model.attribute.MetaItem) IdentityService(org.openremote.container.security.IdentityService) CorsFilter(org.jboss.resteasy.plugins.interceptors.CorsFilter) RoleBasedSecurityFeature(org.jboss.resteasy.plugins.interceptors.RoleBasedSecurityFeature) JacksonConfig(org.openremote.container.json.JacksonConfig) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) Values(org.openremote.model.value.Values) Pattern(java.util.regex.Pattern) ValueHolder(org.openremote.model.ValueHolder) AssetAttribute(org.openremote.model.asset.AssetAttribute) CorsFilter(org.jboss.resteasy.plugins.interceptors.CorsFilter) ResteasyDeployment(org.jboss.resteasy.spi.ResteasyDeployment) MetaItem(org.openremote.model.attribute.MetaItem) AbstractValueHolder(org.openremote.model.AbstractValueHolder) StringValue(org.openremote.model.value.StringValue)

Aggregations

Lists (com.google.common.collect.Lists)1 HttpHandler (io.undertow.server.HttpHandler)1 Servlets (io.undertow.servlet.Servlets)1 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)1 DeploymentManager (io.undertow.servlet.api.DeploymentManager)1 ServletInfo (io.undertow.servlet.api.ServletInfo)1 java.util (java.util)1 Level (java.util.logging.Level)1 Logger (java.util.logging.Logger)1 Pattern (java.util.regex.Pattern)1 ServletException (javax.servlet.ServletException)1 HttpMethod (javax.ws.rs.HttpMethod)1 Application (javax.ws.rs.core.Application)1 CorsFilter (org.jboss.resteasy.plugins.interceptors.CorsFilter)1 RoleBasedSecurityFeature (org.jboss.resteasy.plugins.interceptors.RoleBasedSecurityFeature)1 HttpServlet30Dispatcher (org.jboss.resteasy.plugins.server.servlet.HttpServlet30Dispatcher)1 ResteasyDeployment (org.jboss.resteasy.spi.ResteasyDeployment)1 AbstractProtocol (org.openremote.agent.protocol.AbstractProtocol)1 Container (org.openremote.container.Container)1 JacksonConfig (org.openremote.container.json.JacksonConfig)1