|
| 1 | +/* |
| 2 | + * Copyright 2020-present the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.graphql.server.support; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.function.BiConsumer; |
| 24 | + |
| 25 | +import org.jspecify.annotations.Nullable; |
| 26 | +import reactor.core.publisher.Mono; |
| 27 | + |
| 28 | +import org.springframework.graphql.server.WebGraphQlInterceptor; |
| 29 | +import org.springframework.graphql.server.WebGraphQlRequest; |
| 30 | +import org.springframework.graphql.server.WebGraphQlResponse; |
| 31 | +import org.springframework.http.HttpHeaders; |
| 32 | +import org.springframework.util.ObjectUtils; |
| 33 | + |
| 34 | +/** |
| 35 | + * Interceptor that copies HTTP request headers to the GraphQL context to make |
| 36 | + * them available to data fetchers such as annotated controllers, which can use |
| 37 | + * {@link org.springframework.graphql.data.method.annotation.ContextValue @ContextValue} |
| 38 | + * method parameters to access the headers as context values. |
| 39 | + * |
| 40 | + * <p>User {@link #builder()} to build an instance, and specify headers of |
| 41 | + * interest that should be copied to the GraphQL context. |
| 42 | + * |
| 43 | + * @author Rossen Stoyanchev |
| 44 | + * @since 2.0 |
| 45 | + */ |
| 46 | +public final class HttpRequestHeaderInterceptor implements WebGraphQlInterceptor { |
| 47 | + |
| 48 | + private final List<BiConsumer<HttpHeaders, Map<String, Object>>> mappers; |
| 49 | + |
| 50 | + |
| 51 | + private HttpRequestHeaderInterceptor(List<BiConsumer<HttpHeaders, Map<String, Object>>> mappers) { |
| 52 | + this.mappers = new ArrayList<>(mappers); |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + @Override |
| 57 | + public Mono<WebGraphQlResponse> intercept(WebGraphQlRequest request, Chain chain) { |
| 58 | + request.configureExecutionInput((executionInput, builder) -> { |
| 59 | + HttpHeaders headers = request.getHeaders(); |
| 60 | + Map<String, Object> target = new HashMap<>(this.mappers.size()); |
| 61 | + this.mappers.forEach((mapper) -> mapper.accept(headers, target)); |
| 62 | + builder.graphQLContext(target); |
| 63 | + return builder.build(); |
| 64 | + }); |
| 65 | + return chain.next(request); |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + /** |
| 70 | + * Return a builder to create an {@link HttpRequestHeaderInterceptor}. |
| 71 | + */ |
| 72 | + public static Builder builder() { |
| 73 | + return new DefaultBuilder(); |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + /** |
| 78 | + * Builder for {@link HttpRequestHeaderInterceptor}. |
| 79 | + */ |
| 80 | + public interface Builder { |
| 81 | + |
| 82 | + /** |
| 83 | + * Add names of HTTP headers to copy to the GraphQL context, using keys |
| 84 | + * identical to the header names. Only the first value is copied. |
| 85 | + * @param headerName the name(s) of header(s) to copy |
| 86 | + */ |
| 87 | + Builder mapHeader(String... headerName); |
| 88 | + |
| 89 | + /** |
| 90 | + * Add a mapping between an HTTP header name and the key under which it |
| 91 | + * should appear in the GraphQL context. Only the first value is copied. |
| 92 | + * @param headerName the name of a header to copy |
| 93 | + * @param contextKey the key to map to in the GraphQL context |
| 94 | + */ |
| 95 | + Builder mapHeaderToKey(String headerName, String contextKey); |
| 96 | + |
| 97 | + /** |
| 98 | + * Add names of HTTP headers to copy to the GraphQL context, using keys |
| 99 | + * identical to the header names. All values are copied as a List. |
| 100 | + * @param headerName the name(s) of header(s) to copy |
| 101 | + */ |
| 102 | + Builder mapMultiValueHeader(String... headerName); |
| 103 | + |
| 104 | + /** |
| 105 | + * Add a mapping between an HTTP header name and the key under which it |
| 106 | + * should appear in the GraphQL context. All values are copied as a List. |
| 107 | + * @param headerName the name of a header to copy |
| 108 | + * @param contextKey the key to map to in the GraphQL context |
| 109 | + */ |
| 110 | + Builder mapMultiValueHeaderToKey(String headerName, String contextKey); |
| 111 | + |
| 112 | + /** |
| 113 | + * Create the interceptor instance. |
| 114 | + */ |
| 115 | + HttpRequestHeaderInterceptor build(); |
| 116 | + } |
| 117 | + |
| 118 | + |
| 119 | + /** |
| 120 | + * Default implementation of {@link Builder}. |
| 121 | + */ |
| 122 | + private static final class DefaultBuilder implements Builder { |
| 123 | + |
| 124 | + private final List<BiConsumer<HttpHeaders, Map<String, Object>>> mappers = new ArrayList<>(); |
| 125 | + |
| 126 | + @Override |
| 127 | + public DefaultBuilder mapHeader(String... headers) { |
| 128 | + for (String header : headers) { |
| 129 | + initMapper(header, null); |
| 130 | + } |
| 131 | + return this; |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public DefaultBuilder mapHeaderToKey(String header, String contextKey) { |
| 136 | + initMapper(header, contextKey); |
| 137 | + return this; |
| 138 | + } |
| 139 | + |
| 140 | + private void initMapper(String header, @Nullable String key) { |
| 141 | + this.mappers.add((headers, target) -> { |
| 142 | + Object value = headers.getFirst(header); |
| 143 | + if (value != null) { |
| 144 | + target.put((key != null) ? key : header, value); |
| 145 | + } |
| 146 | + }); |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public DefaultBuilder mapMultiValueHeader(String... headers) { |
| 151 | + for (String header : headers) { |
| 152 | + initMultiValueMapper(header, null); |
| 153 | + } |
| 154 | + return this; |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public DefaultBuilder mapMultiValueHeaderToKey(String header, String contextKey) { |
| 159 | + initMultiValueMapper(header, contextKey); |
| 160 | + return this; |
| 161 | + } |
| 162 | + |
| 163 | + private void initMultiValueMapper(String header, @Nullable String key) { |
| 164 | + this.mappers.add((headers, target) -> { |
| 165 | + List<?> list = headers.getValuesAsList(header); |
| 166 | + if (!ObjectUtils.isEmpty(list)) { |
| 167 | + target.put((key != null) ? key : header, list); |
| 168 | + } |
| 169 | + }); |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public HttpRequestHeaderInterceptor build() { |
| 174 | + return new HttpRequestHeaderInterceptor(this.mappers); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | +} |
0 commit comments