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

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

Introduction

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

Prototype

long maxAge

To view the source code for io.netty.handler.codec.http.cors CorsConfigBuilder maxAge.

Click Source Link

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);
            }/*  w  ww.ja va 2 s. 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;
}