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

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

Introduction

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

Prototype

public CorsConfigBuilder allowedRequestHeaders(final CharSequence... headers) 

Source Link

Document

Specifies the if headers that should be returned in the CORS 'Access-Control-Allow-Headers' response header.

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);
            }//from w  w w .j  a  v a2  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;
}