|
18 | 18 |
|
19 | 19 | import java.io.IOException;
|
20 | 20 | import java.util.List;
|
| 21 | +import java.util.Map; |
21 | 22 |
|
22 | 23 | import jakarta.servlet.ServletException;
|
23 | 24 | import jakarta.servlet.http.Cookie;
|
| 25 | +import jakarta.servlet.http.HttpServletRequest; |
| 26 | +import jakarta.servlet.http.HttpServletResponse; |
24 | 27 | import org.apache.commons.logging.Log;
|
25 | 28 | import org.apache.commons.logging.LogFactory;
|
26 | 29 | import reactor.core.publisher.Mono;
|
|
36 | 39 | import org.springframework.http.HttpHeaders;
|
37 | 40 | import org.springframework.http.MediaType;
|
38 | 41 | import org.springframework.http.converter.HttpMessageConverter;
|
| 42 | +import org.springframework.http.server.ServerHttpRequest; |
39 | 43 | import org.springframework.http.server.ServletServerHttpRequest;
|
| 44 | +import org.springframework.http.server.ServletServerHttpResponse; |
40 | 45 | import org.springframework.lang.Nullable;
|
41 | 46 | import org.springframework.util.AlternativeJdkIdGenerator;
|
42 | 47 | import org.springframework.util.Assert;
|
|
46 | 51 | import org.springframework.util.MultiValueMap;
|
47 | 52 | import org.springframework.web.HttpMediaTypeNotSupportedException;
|
48 | 53 | import org.springframework.web.server.ServerWebInputException;
|
| 54 | +import org.springframework.web.servlet.ModelAndView; |
49 | 55 | import org.springframework.web.servlet.function.ServerRequest;
|
50 | 56 | import org.springframework.web.servlet.function.ServerResponse;
|
51 | 57 |
|
@@ -79,11 +85,19 @@ protected AbstractGraphQlHttpHandler(
|
79 | 85 |
|
80 | 86 |
|
81 | 87 | /**
|
82 |
| - * Return the custom message converter, if configured, to read and write with. |
| 88 | + * Exposes a {@link ServerResponse.HeadersBuilder.WriteFunction} that writes |
| 89 | + * with the {@code HttpMessageConverter} provided to the constructor. |
| 90 | + * @param resultMap the result map to write |
| 91 | + * @param contentType to set the response content type to |
| 92 | + * @return the write function, or {@code null} if a |
| 93 | + * {@code HttpMessageConverter} was not provided to the constructor |
83 | 94 | */
|
84 | 95 | @Nullable
|
85 |
| - public HttpMessageConverter<Object> getMessageConverter() { |
86 |
| - return this.messageConverter; |
| 96 | + protected ServerResponse.HeadersBuilder.WriteFunction getWriteFunction( |
| 97 | + Map<String, Object> resultMap, MediaType contentType) { |
| 98 | + |
| 99 | + return (this.messageConverter != null) ? |
| 100 | + new MessageConverterWriteFunction(resultMap, contentType, this.messageConverter) : null; |
87 | 101 | }
|
88 | 102 |
|
89 | 103 |
|
@@ -133,8 +147,8 @@ private GraphQlRequest readBody(ServerRequest request) throws ServletException {
|
133 | 147 | if (this.messageConverter != null) {
|
134 | 148 | MediaType contentType = request.headers().contentType().orElse(MediaType.APPLICATION_JSON);
|
135 | 149 | if (this.messageConverter.canRead(SerializableGraphQlRequest.class, contentType)) {
|
136 |
| - return (GraphQlRequest) this.messageConverter.read(SerializableGraphQlRequest.class, |
137 |
| - new ServletServerHttpRequest(request.servletRequest())); |
| 150 | + ServerHttpRequest httpRequest = new ServletServerHttpRequest(request.servletRequest()); |
| 151 | + return (GraphQlRequest) this.messageConverter.read(SerializableGraphQlRequest.class, httpRequest); |
138 | 152 | }
|
139 | 153 | throw new HttpMediaTypeNotSupportedException(
|
140 | 154 | contentType, this.messageConverter.getSupportedMediaTypes(), request.method());
|
@@ -181,4 +195,20 @@ private static SerializableGraphQlRequest applyApplicationGraphQlFallback(
|
181 | 195 | protected abstract ServerResponse prepareResponse(
|
182 | 196 | ServerRequest request, Mono<WebGraphQlResponse> responseMono) throws ServletException;
|
183 | 197 |
|
| 198 | + |
| 199 | + /** |
| 200 | + * WriteFunction that writes with a given, fixed {@link HttpMessageConverter}. |
| 201 | + */ |
| 202 | + private record MessageConverterWriteFunction( |
| 203 | + Map<String, Object> resultMap, MediaType contentType, HttpMessageConverter<Object> converter) |
| 204 | + implements ServerResponse.HeadersBuilder.WriteFunction { |
| 205 | + |
| 206 | + @Override |
| 207 | + public ModelAndView write(HttpServletRequest request, HttpServletResponse response) throws Exception { |
| 208 | + ServletServerHttpResponse httpResponse = new ServletServerHttpResponse(response); |
| 209 | + this.converter.write(this.resultMap, this.contentType, httpResponse); |
| 210 | + return null; |
| 211 | + } |
| 212 | + } |
| 213 | + |
184 | 214 | }
|
0 commit comments