diff options
author | Morten Tokle <mortent@verizonmedia.com> | 2021-04-22 14:52:26 +0200 |
---|---|---|
committer | Morten Tokle <mortent@verizonmedia.com> | 2021-04-22 14:52:26 +0200 |
commit | cd91e6cd80ab56a12d99a0940d38f8ee8286fd7b (patch) | |
tree | 377e2882c682a4b886bd09f533740c7301679ced /container-core/src/main/java/com/yahoo/container/jdisc/HttpMethodAclMapping.java | |
parent | 4c46d92474745467a53eb53336fd4c5c162b2375 (diff) |
Provide standard mapping
Diffstat (limited to 'container-core/src/main/java/com/yahoo/container/jdisc/HttpMethodAclMapping.java')
-rw-r--r-- | container-core/src/main/java/com/yahoo/container/jdisc/HttpMethodAclMapping.java | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/container-core/src/main/java/com/yahoo/container/jdisc/HttpMethodAclMapping.java b/container-core/src/main/java/com/yahoo/container/jdisc/HttpMethodAclMapping.java new file mode 100644 index 00000000000..c40c4eb96a2 --- /dev/null +++ b/container-core/src/main/java/com/yahoo/container/jdisc/HttpMethodAclMapping.java @@ -0,0 +1,71 @@ +// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +package com.yahoo.container.jdisc; + +import com.yahoo.jdisc.http.HttpRequest; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +import static com.yahoo.jdisc.http.HttpRequest.Method.CONNECT; +import static com.yahoo.jdisc.http.HttpRequest.Method.DELETE; +import static com.yahoo.jdisc.http.HttpRequest.Method.GET; +import static com.yahoo.jdisc.http.HttpRequest.Method.HEAD; +import static com.yahoo.jdisc.http.HttpRequest.Method.OPTIONS; +import static com.yahoo.jdisc.http.HttpRequest.Method.PATCH; +import static com.yahoo.jdisc.http.HttpRequest.Method.POST; +import static com.yahoo.jdisc.http.HttpRequest.Method.PUT; +import static com.yahoo.jdisc.http.HttpRequest.Method.TRACE; + +/** + * Acl Mapping based on http method. + * Defaults to: + * {GET, HEAD, OPTIONS} -> READ + * {POST, DELETE, PUT, PATCH, CONNECT, TRACE} -> WRITE + * @author mortent + */ +public class HttpMethodAclMapping implements AclMapping { + + private final Map<HttpRequest.Method, Action> mappings; + + private HttpMethodAclMapping(Map<HttpRequest.Method, Action> overrides) { + HashMap<HttpRequest.Method, Action> tmp = new HashMap<>(defaultMappings()); + tmp.putAll(overrides); + mappings = Map.copyOf(tmp); + } + + private static Map<HttpRequest.Method, Action> defaultMappings() { + return Map.of(GET, Action.READ, + HEAD, Action.READ, + OPTIONS, Action.READ, + POST, Action.WRITE, + DELETE, Action.WRITE, + PUT, Action.WRITE, + PATCH, Action.WRITE, + CONNECT, Action.WRITE, + TRACE, Action.WRITE); + } + + @Override + public Action get(RequestView requestView) { + return Optional.ofNullable(mappings.get(requestView.method())) + .orElseThrow(() -> new IllegalArgumentException("Illegal request method: " + requestView.method())); + } + + public static HttpMethodAclMapping.Builder standard() { + return new HttpMethodAclMapping.Builder(); + } + + public static class Builder { + private final Map<com.yahoo.jdisc.http.HttpRequest.Method, Action> overrides = new HashMap<>(); + public HttpMethodAclMapping.Builder override(HttpRequest.Method method, Action action) { + overrides.put(method, action); + return this; + } + public HttpMethodAclMapping build() { + return new HttpMethodAclMapping(overrides); + } + } +} |