|
1 | 1 | /*
|
2 |
| - * Copyright 2020-2020 the original author or authors. |
| 2 | + * Copyright 2020-2021 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
59 | 59 | import org.springframework.messaging.support.MessageBuilder;
|
60 | 60 | import org.springframework.util.MimeTypeUtils;
|
61 | 61 | import org.springframework.util.ReflectionUtils;
|
| 62 | +import org.springframework.util.RouteMatcher.Route; |
62 | 63 | import org.springframework.util.StringUtils;
|
63 | 64 | import org.springframework.web.util.pattern.PathPatternRouteMatcher;
|
64 | 65 |
|
@@ -116,10 +117,7 @@ public MetadataExtractor getMetadataExtractor() {
|
116 | 117 | @Override
|
117 | 118 | public Mono<Void> handleMessage(Message<?> message) throws MessagingException {
|
118 | 119 | if (!FrameType.SETUP.equals(message.getHeaders().get("rsocketFrameType"))) {
|
119 |
| - String destination = this.getDestination(message).value(); |
120 |
| - if (!StringUtils.hasText(destination)) { |
121 |
| - destination = this.discoverAndInjectDestinationHeader(message); |
122 |
| - } |
| 120 | + String destination = this.discoverAndInjectDestinationHeader(message); |
123 | 121 |
|
124 | 122 | Set<String> mappings = this.getDestinationLookup().keySet();
|
125 | 123 | if (!mappings.contains(destination)) {
|
@@ -154,16 +152,33 @@ protected List<? extends HandlerMethodReturnValueHandler> initReturnValueHandler
|
154 | 152 |
|
155 | 153 | @SuppressWarnings("unchecked")
|
156 | 154 | private String discoverAndInjectDestinationHeader(Message<?> message) {
|
157 |
| - String destination = this.functionProperties.getDefinition(); |
158 |
| - if (!StringUtils.hasText(destination) && StringUtils.hasText(this.functionProperties.getRoutingExpression())) { |
| 155 | + |
| 156 | + String destination; |
| 157 | + if (StringUtils.hasText(this.functionProperties.getRoutingExpression())) { |
159 | 158 | destination = RoutingFunction.FUNCTION_NAME;
|
| 159 | + Map<String, Object> headersMap = (Map<String, Object>) ReflectionUtils |
| 160 | + .getField(this.headersField, message.getHeaders()); |
| 161 | + PathPatternRouteMatcher matcher = new PathPatternRouteMatcher(); |
| 162 | + headersMap.put(DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER, matcher.parseRoute(destination)); |
| 163 | + } |
| 164 | + else { |
| 165 | + Route route = (Route) message.getHeaders().get(DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER); |
| 166 | + destination = route.value(); |
| 167 | + if (!StringUtils.hasText(destination)) { |
| 168 | + destination = this.functionProperties.getDefinition(); |
| 169 | + Map<String, Object> headersMap = (Map<String, Object>) ReflectionUtils |
| 170 | + .getField(this.headersField, message.getHeaders()); |
| 171 | + PathPatternRouteMatcher matcher = new PathPatternRouteMatcher(); |
| 172 | + headersMap.put(DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER, matcher.parseRoute(destination)); |
| 173 | + } |
160 | 174 | }
|
161 |
| - Map<String, Object> headersMap = (Map<String, Object>) ReflectionUtils |
162 |
| - .getField(this.headersField, message.getHeaders()); |
163 |
| - |
164 |
| - PathPatternRouteMatcher matcher = new PathPatternRouteMatcher(); |
165 | 175 |
|
166 |
| - headersMap.put(DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER, matcher.parseRoute(destination)); |
| 176 | + if (!StringUtils.hasText(destination) && logger.isDebugEnabled()) { |
| 177 | + logger.debug("Failed to discover function definition. Neither " |
| 178 | + + "`spring.cloud.function.definition`, nor `.route(<function.definition>)`, nor " |
| 179 | + + "`spring.cloud.function.routing-expression` were provided. Wil use empty string " |
| 180 | + + "for lookup, which will work only if there is one function in Function Catalog"); |
| 181 | + } |
167 | 182 | return destination;
|
168 | 183 | }
|
169 | 184 |
|
|
0 commit comments