|
| 1 | +/* |
| 2 | + * Copyright 2020-2024 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.webflux; |
| 18 | + |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +import org.apache.commons.logging.Log; |
| 24 | +import org.apache.commons.logging.LogFactory; |
| 25 | + |
| 26 | +import org.springframework.http.HttpHeaders; |
| 27 | +import org.springframework.http.HttpMethod; |
| 28 | +import org.springframework.http.MediaType; |
| 29 | +import org.springframework.http.server.PathContainer; |
| 30 | +import org.springframework.lang.Nullable; |
| 31 | +import org.springframework.util.Assert; |
| 32 | +import org.springframework.util.MimeTypeUtils; |
| 33 | +import org.springframework.web.cors.reactive.CorsUtils; |
| 34 | +import org.springframework.web.reactive.function.server.RequestPredicate; |
| 35 | +import org.springframework.web.reactive.function.server.ServerRequest; |
| 36 | +import org.springframework.web.util.pattern.PathPattern; |
| 37 | +import org.springframework.web.util.pattern.PathPatternParser; |
| 38 | + |
| 39 | +/** |
| 40 | + * {@link RequestPredicate} implementations tailored for GraphQL reactive endpoints. |
| 41 | + * |
| 42 | + * @author Brian Clozel |
| 43 | + * @since 1.3.0 |
| 44 | + */ |
| 45 | +public class GraphQlRequestPredicates { |
| 46 | + |
| 47 | + private static final Log logger = LogFactory.getLog(GraphQlRequestPredicates.class); |
| 48 | + |
| 49 | + /** |
| 50 | + * Create a {@link RequestPredicate predicate} that matches GraphQL HTTP requests for the configured path. |
| 51 | + * |
| 52 | + * @param path the path on which the GraphQL HTTP endpoint is mapped |
| 53 | + * @see GraphQlHttpHandler |
| 54 | + */ |
| 55 | + public static RequestPredicate graphQlHttp(String path) { |
| 56 | + return new GraphQlHttpRequestPredicate(path, MediaType.APPLICATION_JSON, MediaType.APPLICATION_GRAPHQL_RESPONSE); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Create a {@link RequestPredicate predicate} that matches GraphQL SSE over HTTP requests for the configured path. |
| 61 | + * |
| 62 | + * @param path the path on which the GraphQL SSE endpoint is mapped |
| 63 | + * @see GraphQlSseHandler |
| 64 | + */ |
| 65 | + public static RequestPredicate graphQlSse(String path) { |
| 66 | + return new GraphQlHttpRequestPredicate(path, MediaType.TEXT_EVENT_STREAM); |
| 67 | + } |
| 68 | + |
| 69 | + private static class GraphQlHttpRequestPredicate implements RequestPredicate { |
| 70 | + |
| 71 | + private final PathPattern pattern; |
| 72 | + |
| 73 | + private final List<MediaType> acceptedMediaTypes; |
| 74 | + |
| 75 | + |
| 76 | + GraphQlHttpRequestPredicate(String path, MediaType... accepted) { |
| 77 | + Assert.notNull(path, "'path' must not be null"); |
| 78 | + Assert.notEmpty(accepted, "'accepted' must not be empty"); |
| 79 | + PathPatternParser parser = PathPatternParser.defaultInstance; |
| 80 | + path = parser.initFullPathPattern(path); |
| 81 | + this.pattern = parser.parse(path); |
| 82 | + this.acceptedMediaTypes = Arrays.asList(accepted); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public boolean test(ServerRequest request) { |
| 87 | + return methodMatch(request, HttpMethod.POST) |
| 88 | + && contentTypeMatch(request, MediaType.APPLICATION_JSON) |
| 89 | + && acceptMatch(request, this.acceptedMediaTypes) |
| 90 | + && pathMatch(request, this.pattern); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private static boolean methodMatch(ServerRequest request, HttpMethod expected) { |
| 95 | + HttpMethod actual = resolveMethod(request); |
| 96 | + boolean methodMatch = expected.equals(actual); |
| 97 | + traceMatch("Method", expected, actual, methodMatch); |
| 98 | + return methodMatch; |
| 99 | + } |
| 100 | + |
| 101 | + private static HttpMethod resolveMethod(ServerRequest request) { |
| 102 | + if (CorsUtils.isPreFlightRequest(request.exchange().getRequest())) { |
| 103 | + String accessControlRequestMethod = |
| 104 | + request.headers().firstHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD); |
| 105 | + if (accessControlRequestMethod != null) { |
| 106 | + return HttpMethod.valueOf(accessControlRequestMethod); |
| 107 | + } |
| 108 | + } |
| 109 | + return request.method(); |
| 110 | + } |
| 111 | + |
| 112 | + private static boolean contentTypeMatch(ServerRequest request, MediaType expected) { |
| 113 | + if (CorsUtils.isPreFlightRequest(request.exchange().getRequest())) { |
| 114 | + return true; |
| 115 | + } |
| 116 | + ServerRequest.Headers headers = request.headers(); |
| 117 | + MediaType actual = headers.contentType().orElse(MediaType.APPLICATION_OCTET_STREAM); |
| 118 | + boolean contentTypeMatch = expected.includes(actual); |
| 119 | + traceMatch("Content-Type", expected, actual, contentTypeMatch); |
| 120 | + return contentTypeMatch; |
| 121 | + } |
| 122 | + |
| 123 | + private static boolean acceptMatch(ServerRequest request, List<MediaType> expected) { |
| 124 | + if (CorsUtils.isPreFlightRequest(request.exchange().getRequest())) { |
| 125 | + return true; |
| 126 | + } |
| 127 | + ServerRequest.Headers headers = request.headers(); |
| 128 | + List<MediaType> acceptedMediaTypes = acceptedMediaTypes(headers); |
| 129 | + boolean match = false; |
| 130 | + outer: |
| 131 | + for (MediaType acceptedMediaType : acceptedMediaTypes) { |
| 132 | + for (MediaType mediaType : expected) { |
| 133 | + if (acceptedMediaType.isCompatibleWith(mediaType)) { |
| 134 | + match = true; |
| 135 | + break outer; |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + traceMatch("Accept", expected, acceptedMediaTypes, match); |
| 140 | + return match; |
| 141 | + } |
| 142 | + |
| 143 | + private static List<MediaType> acceptedMediaTypes(ServerRequest.Headers headers) { |
| 144 | + List<MediaType> acceptedMediaTypes = headers.accept(); |
| 145 | + if (acceptedMediaTypes.isEmpty()) { |
| 146 | + acceptedMediaTypes = Collections.singletonList(MediaType.ALL); |
| 147 | + } else { |
| 148 | + MimeTypeUtils.sortBySpecificity(acceptedMediaTypes); |
| 149 | + } |
| 150 | + return acceptedMediaTypes; |
| 151 | + } |
| 152 | + |
| 153 | + private static boolean pathMatch(ServerRequest request, PathPattern pattern) { |
| 154 | + PathContainer pathContainer = request.requestPath().pathWithinApplication(); |
| 155 | + boolean pathMatch = pattern.matches(pathContainer); |
| 156 | + traceMatch("Pattern", pattern.getPatternString(), request.path(), pathMatch); |
| 157 | + return pathMatch; |
| 158 | + } |
| 159 | + |
| 160 | + private static void traceMatch(String prefix, Object desired, @Nullable Object actual, boolean match) { |
| 161 | + if (logger.isTraceEnabled()) { |
| 162 | + logger.trace(String.format("%s \"%s\" %s against value \"%s\"", |
| 163 | + prefix, desired, match ? "matches" : "does not match", actual)); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | +} |
0 commit comments