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

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

Introduction

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

Prototype

public CorsConfigBuilder allowedRequestMethods(final HttpMethod... methods) 

Source Link

Document

Specifies the allowed set of HTTP Request Methods that should be returned in the CORS 'Access-Control-Request-Method' 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);
            }/*ww  w . j  av  a 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;
}