|
| 1 | +/* |
| 2 | + * Copyright 2002-2022 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 | +package org.springframework.graphql.server.webflux; |
| 17 | + |
| 18 | +import java.util.Map; |
| 19 | + |
| 20 | +import org.reactivestreams.Publisher; |
| 21 | +import reactor.core.publisher.Mono; |
| 22 | + |
| 23 | +import org.springframework.core.ResolvableType; |
| 24 | +import org.springframework.core.codec.Decoder; |
| 25 | +import org.springframework.core.codec.Encoder; |
| 26 | +import org.springframework.core.io.buffer.DataBuffer; |
| 27 | +import org.springframework.core.io.buffer.DefaultDataBufferFactory; |
| 28 | +import org.springframework.graphql.GraphQlResponse; |
| 29 | +import org.springframework.graphql.server.support.SerializableGraphQlRequest; |
| 30 | +import org.springframework.http.MediaType; |
| 31 | +import org.springframework.http.codec.CodecConfigurer; |
| 32 | +import org.springframework.http.codec.DecoderHttpMessageReader; |
| 33 | +import org.springframework.http.codec.EncoderHttpMessageWriter; |
| 34 | +import org.springframework.util.Assert; |
| 35 | +import org.springframework.util.MimeTypeUtils; |
| 36 | + |
| 37 | +/** |
| 38 | + * Helper class for encoding and decoding GraphQL messages in HTTP transport. |
| 39 | + * |
| 40 | + * @author Rossen Stoyanchev |
| 41 | + * @author Brian Clozel |
| 42 | + * @since 1.3.0 |
| 43 | + */ |
| 44 | +final class HttpCodecDelegate { |
| 45 | + |
| 46 | + private static final ResolvableType REQUEST_TYPE = ResolvableType.forClass(SerializableGraphQlRequest.class); |
| 47 | + |
| 48 | + private static final ResolvableType RESPONSE_TYPE = ResolvableType.forClassWithGenerics(Map.class, String.class, Object.class); |
| 49 | + |
| 50 | + |
| 51 | + private final Decoder<?> decoder; |
| 52 | + |
| 53 | + private final Encoder<?> encoder; |
| 54 | + |
| 55 | + |
| 56 | + HttpCodecDelegate(CodecConfigurer codecConfigurer) { |
| 57 | + Assert.notNull(codecConfigurer, "CodecConfigurer is required"); |
| 58 | + this.decoder = findJsonDecoder(codecConfigurer); |
| 59 | + this.encoder = findJsonEncoder(codecConfigurer); |
| 60 | + } |
| 61 | + |
| 62 | + private static Decoder<?> findJsonDecoder(CodecConfigurer configurer) { |
| 63 | + return configurer.getReaders().stream() |
| 64 | + .filter((reader) -> reader.canRead(REQUEST_TYPE, MediaType.APPLICATION_JSON)) |
| 65 | + .map((reader) -> ((DecoderHttpMessageReader<?>) reader).getDecoder()) |
| 66 | + .findFirst() |
| 67 | + .orElseThrow(() -> new IllegalArgumentException("No JSON Decoder")); |
| 68 | + } |
| 69 | + |
| 70 | + private static Encoder<?> findJsonEncoder(CodecConfigurer configurer) { |
| 71 | + return configurer.getWriters().stream() |
| 72 | + .filter((writer) -> writer.canWrite(RESPONSE_TYPE, MediaType.APPLICATION_JSON)) |
| 73 | + .map((writer) -> ((EncoderHttpMessageWriter<?>) writer).getEncoder()) |
| 74 | + .findFirst() |
| 75 | + .orElseThrow(() -> new IllegalArgumentException("No JSON Encoder")); |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + @SuppressWarnings("unchecked") |
| 80 | + public DataBuffer encode(GraphQlResponse response) { |
| 81 | + return ((Encoder<Map<String, Object>>) this.encoder) |
| 82 | + .encodeValue(response.toMap(), DefaultDataBufferFactory.sharedInstance, RESPONSE_TYPE, MimeTypeUtils.APPLICATION_JSON, null); |
| 83 | + } |
| 84 | + |
| 85 | + @SuppressWarnings("unchecked") |
| 86 | + public Mono<SerializableGraphQlRequest> decode(Publisher<DataBuffer> inputStream, MediaType contentType) { |
| 87 | + return (Mono<SerializableGraphQlRequest>) this.decoder.decodeToMono(inputStream, REQUEST_TYPE, contentType, null); |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments