Example usage for io.netty.handler.codec.http.cors CorsConfigBuilder build

List of usage examples for io.netty.handler.codec.http.cors CorsConfigBuilder build

Introduction

In this page you can find the example usage for io.netty.handler.codec.http.cors CorsConfigBuilder build.

Prototype

public CorsConfig build() 

Source Link

Document

Builds a CorsConfig with settings specified by previous method calls.

Usage

From source file:org.infinispan.server.endpoint.subsystem.RestSubsystemAdd.java

License:Open Source License

private List<CorsConfig> getCorsConfig(ModelNode modelNode) {
    List<CorsConfig> corsConfigList = new ArrayList<>();
    if (modelNode.hasDefined(ModelKeys.CORS_RULE)) {
        List<ModelNode> rules = modelNode.get(ModelKeys.CORS_RULE).asList();
        for (ModelNode rule : rules) {
            ModelNode ruleDefinition = rule.get(rule.keys().iterator().next());
            Integer maxAgeSeconds = extractInt(ruleDefinition, ModelKeys.MAX_AGE_SECONDS);
            Boolean allowCredentials = extractBool(ruleDefinition, ModelKeys.ALLOW_CREDENTIALS);
            String[] origins = asArray(ruleDefinition, ModelKeys.ALLOWED_ORIGINS);
            String[] methods = asArray(ruleDefinition, ModelKeys.ALLOWED_METHODS);
            String[] headers = asArray(ruleDefinition, ModelKeys.ALLOWED_HEADERS);
            String[] exposes = asArray(ruleDefinition, ModelKeys.EXPOSE_HEADERS);

            HttpMethod[] httpMethods = Arrays.stream(methods).map(HttpMethod::valueOf)
                    .toArray(HttpMethod[]::new);

            CorsConfigBuilder builder;
            if (Arrays.stream(origins).anyMatch(s -> s.equals("*"))) {
                builder = CorsConfigBuilder.forAnyOrigin();
            } else {
                builder = CorsConfigBuilder.forOrigins(origins);
            }// ww w.  j a  v a  2s  .  c o  m
            builder.allowedRequestMethods(httpMethods);
            if (headers.length > 0)
                builder.allowedRequestHeaders(headers);
            if (exposes.length > 0)
                builder.exposeHeaders(exposes);
            if (maxAgeSeconds != null)
                builder.maxAge(maxAgeSeconds);
            if (allowCredentials)
                builder.allowCredentials();
            corsConfigList.add(builder.build());
        }
    }
    return corsConfigList;
}