Update PolarisRouterServiceInstanceListSupplier.java

pull/257/head
cheese8 3 years ago
parent 031ffe868c
commit 6d6ade6c0f

@ -17,6 +17,9 @@
package com.tencent.cloud.polaris.router; package com.tencent.cloud.polaris.router;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
@ -66,7 +69,7 @@ import org.springframework.util.CollectionUtils;
* And {@link PolarisRouterServiceInstanceListSupplier#get(Request)} provides the ability to pass in http headers, * And {@link PolarisRouterServiceInstanceListSupplier#get(Request)} provides the ability to pass in http headers,
* so routing capabilities are implemented through IRule. * so routing capabilities are implemented through IRule.
* *
* @author Haotian Zhang, lepdou * @author Haotian Zhang, lepdou, cheese8
*/ */
public class PolarisRouterServiceInstanceListSupplier extends DelegatingServiceInstanceListSupplier { public class PolarisRouterServiceInstanceListSupplier extends DelegatingServiceInstanceListSupplier {
@ -123,19 +126,15 @@ public class PolarisRouterServiceInstanceListSupplier extends DelegatingServiceI
routerContext.setLabels(PolarisRouterContext.TRANSITIVE_LABELS, MetadataContextHolder.get() routerContext.setLabels(PolarisRouterContext.TRANSITIVE_LABELS, MetadataContextHolder.get()
.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE)); .getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE));
labelHeaderValues.forEach(labelHeaderValue -> { Map<String, String> labelHeaderValuesMap = new HashMap<>();
Map<String, String> labels = JacksonUtils.deserialize2Map(labelHeaderValue); try {
if (!CollectionUtils.isEmpty(labels)) { String labelHeaderValuesContent = labelHeaderValues.stream().findFirst().get();
Map<String, String> unescapeLabels = new HashMap<>(labels.size()); labelHeaderValuesMap.putAll(JacksonUtils.deserialize2Map(URLDecoder.decode(labelHeaderValuesContent, StandardCharsets.UTF_8.name())));
for (Map.Entry<String, String> entry : labels.entrySet()) { }
String escapedKey = ExpressionLabelUtils.unescape(entry.getKey()); catch (UnsupportedEncodingException e) {
String escapedValue = ExpressionLabelUtils.unescape(entry.getValue()); throw new RuntimeException("unsupported charset exception " + StandardCharsets.UTF_8.name());
unescapeLabels.put(escapedKey, escapedValue); }
} routerContext.setLabels(PolarisRouterContext.RULE_ROUTER_LABELS, labelHeaderValuesMap);
routerContext.setLabels(PolarisRouterContext.RULE_ROUTER_LABELS, unescapeLabels);
}
});
return routerContext; return routerContext;
} }

Loading…
Cancel
Save