|
| 1 | +/* |
| 2 | + * Copyright 2019-2021 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.cloud.gateway.server.mvc.handler; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | +import java.util.stream.Collectors; |
| 21 | +import java.util.stream.StreamSupport; |
| 22 | + |
| 23 | +import org.apache.commons.logging.Log; |
| 24 | +import org.apache.commons.logging.LogFactory; |
| 25 | + |
| 26 | +import org.springframework.cloud.function.context.catalog.SimpleFunctionRegistry.FunctionInvocationWrapper; |
| 27 | +import org.springframework.http.HttpHeaders; |
| 28 | +import org.springframework.http.HttpMethod; |
| 29 | +import org.springframework.messaging.Message; |
| 30 | +import org.springframework.messaging.support.MessageBuilder; |
| 31 | +import org.springframework.util.CollectionUtils; |
| 32 | +import org.springframework.web.servlet.function.ServerRequest; |
| 33 | +import org.springframework.web.servlet.function.ServerResponse; |
| 34 | +import org.springframework.web.servlet.function.ServerResponse.BodyBuilder; |
| 35 | + |
| 36 | +import static org.springframework.cloud.gateway.server.mvc.handler.FunctionHandlerHeaderUtils.fromMessage; |
| 37 | +import static org.springframework.cloud.gateway.server.mvc.handler.FunctionHandlerHeaderUtils.sanitize; |
| 38 | + |
| 39 | +/** |
| 40 | + * !INTERNAL USE ONLY! |
| 41 | + * |
| 42 | + * @author Oleg Zhurakousky |
| 43 | + * |
| 44 | + */ |
| 45 | +final class FunctionHandlerRequestProcessingHelper { |
| 46 | + |
| 47 | + private static Log logger = LogFactory.getLog(FunctionHandlerRequestProcessingHelper.class); |
| 48 | + |
| 49 | + private FunctionHandlerRequestProcessingHelper() { |
| 50 | + |
| 51 | + } |
| 52 | + |
| 53 | + @SuppressWarnings({ "rawtypes", "unchecked" }) |
| 54 | + static ServerResponse processRequest(ServerRequest request, FunctionInvocationWrapper function, Object argument, |
| 55 | + boolean eventStream, List<String> ignoredHeaders, List<String> requestOnlyHeaders) { |
| 56 | + if (argument == null) { |
| 57 | + argument = ""; |
| 58 | + } |
| 59 | + |
| 60 | + if (function == null) { |
| 61 | + return ServerResponse.notFound().build(); |
| 62 | + } |
| 63 | + |
| 64 | + HttpHeaders headers = request.headers().asHttpHeaders(); |
| 65 | + |
| 66 | + Message<?> inputMessage = null; |
| 67 | + |
| 68 | + MessageBuilder builder = MessageBuilder.withPayload(argument); |
| 69 | + if (!CollectionUtils.isEmpty(request.params())) { |
| 70 | + builder = builder.setHeader(FunctionHandlerHeaderUtils.HTTP_REQUEST_PARAM, |
| 71 | + request.params().toSingleValueMap()); |
| 72 | + } |
| 73 | + inputMessage = builder.copyHeaders(headers.toSingleValueMap()).build(); |
| 74 | + |
| 75 | + if (function.isRoutingFunction()) { |
| 76 | + function.setSkipOutputConversion(true); |
| 77 | + } |
| 78 | + |
| 79 | + Object result = function.apply(inputMessage); |
| 80 | + if (function.isConsumer()) { |
| 81 | + /* |
| 82 | + * if (result instanceof Publisher) { Mono.from((Publisher) |
| 83 | + * result).subscribe(); } |
| 84 | + */ |
| 85 | + return HttpMethod.DELETE.equals(request.method()) ? ServerResponse.ok().build() |
| 86 | + : ServerResponse.accepted() |
| 87 | + .headers(h -> h.addAll(sanitize(headers, ignoredHeaders, requestOnlyHeaders))) |
| 88 | + .build(); |
| 89 | + // Mono.empty() : |
| 90 | + // Mono.just(ResponseEntity.accepted().headers(FunctionHandlerHeaderUtils.sanitize(headers, |
| 91 | + // ignoredHeaders, requestOnlyHeaders)).build()); |
| 92 | + } |
| 93 | + |
| 94 | + BodyBuilder responseOkBuilder = ServerResponse.ok() |
| 95 | + .headers(h -> h.addAll(sanitize(headers, ignoredHeaders, requestOnlyHeaders))); |
| 96 | + |
| 97 | + // FIXME: Mono/Flux |
| 98 | + /* |
| 99 | + * Publisher pResult; if (result instanceof Publisher) { pResult = (Publisher) |
| 100 | + * result; if (eventStream) { return Flux.from(pResult); } |
| 101 | + * |
| 102 | + * if (pResult instanceof Flux) { pResult = ((Flux) pResult).onErrorContinue((e, |
| 103 | + * v) -> { logger.error("Failed to process value: " + v, (Throwable) e); |
| 104 | + * }).collectList(); } pResult = Mono.from(pResult); } else { pResult = |
| 105 | + * Mono.just(result); } |
| 106 | + */ |
| 107 | + |
| 108 | + // return Mono.from(pResult).map(v -> { |
| 109 | + if (result instanceof Iterable i) { |
| 110 | + List aggregatedResult = (List) StreamSupport.stream(i.spliterator(), false).map(m -> { |
| 111 | + return m instanceof Message ? processMessage(responseOkBuilder, (Message<?>) m, ignoredHeaders) : m; |
| 112 | + }).collect(Collectors.toList()); |
| 113 | + return responseOkBuilder.header("content-type", "application/json").body(aggregatedResult); |
| 114 | + } |
| 115 | + else if (result instanceof Message message) { |
| 116 | + return responseOkBuilder.body(processMessage(responseOkBuilder, message, ignoredHeaders)); |
| 117 | + } |
| 118 | + else { |
| 119 | + return responseOkBuilder.body(result); |
| 120 | + } |
| 121 | + // }); |
| 122 | + } |
| 123 | + |
| 124 | + private static Object processMessage(BodyBuilder responseOkBuilder, Message<?> message, |
| 125 | + List<String> ignoredHeaders) { |
| 126 | + responseOkBuilder.headers(h -> h.addAll(fromMessage(message.getHeaders(), ignoredHeaders))); |
| 127 | + return message.getPayload(); |
| 128 | + } |
| 129 | + |
| 130 | +} |
0 commit comments