From ea53d256e45dfaefcec375339c3284b468e14491 Mon Sep 17 00:00:00 2001 From: cheese8 Date: Sat, 18 Jun 2022 16:41:16 +0800 Subject: [PATCH] Update ExpressionLabelUtils.java --- .../common/util/ExpressionLabelUtils.java | 80 ++++++++++--------- 1 file changed, 41 insertions(+), 39 deletions(-) diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/ExpressionLabelUtils.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/ExpressionLabelUtils.java index eb799276f..73a7d4d60 100644 --- a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/ExpressionLabelUtils.java +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/ExpressionLabelUtils.java @@ -18,7 +18,17 @@ package com.tencent.cloud.common.util; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; + import org.apache.commons.lang.StringUtils; + import org.springframework.http.HttpCookie; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpRequest; @@ -27,21 +37,13 @@ import org.springframework.util.CollectionUtils; import org.springframework.util.MultiValueMap; import org.springframework.web.server.ServerWebExchange; -import javax.servlet.http.Cookie; -import javax.servlet.http.HttpServletRequest; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; -import java.util.Set; - /** * the utils for parse label expression. * *@author lepdou 2022-05-13 */ public class ExpressionLabelUtils { - + /** * the expression prefix of header label. */ @@ -86,7 +88,7 @@ public class ExpressionLabelUtils { * the escape prefix of label. */ public static final String LABEL_ESCAPE_PREFIX = "##@$@##"; - + public static boolean isExpressionLabel(String labelKey) { if (StringUtils.isEmpty(labelKey)) { return false; @@ -100,22 +102,22 @@ public class ExpressionLabelUtils { StringUtils.startsWithIgnoreCase(labelKey, LABEL_COOKIE_PREFIX)) && StringUtils.endsWith(labelKey, LABEL_SUFFIX); } - + public static String escape(String str) { return StringUtils.replace(str, LABEL_PREFIX, LABEL_ESCAPE_PREFIX); } - + public static String unescape(String str) { return StringUtils.replace(str, LABEL_ESCAPE_PREFIX, LABEL_PREFIX); } - + public static Map resolve(HttpServletRequest request, Set labelKeys) { if (CollectionUtils.isEmpty(labelKeys)) { return Collections.emptyMap(); } - + Map labels = new HashMap<>(); - + for (String labelKey : labelKeys) { if (!isExpressionLabel(labelKey)) { continue; @@ -148,17 +150,17 @@ public class ExpressionLabelUtils { labels.put(labelKey, request.getRequestURI()); } } - + return labels; } - + public static Map resolve(ServerWebExchange exchange, Set labelKeys) { if (CollectionUtils.isEmpty(labelKeys)) { return Collections.emptyMap(); } - + Map labels = new HashMap<>(); - + for (String labelKey : labelKeys) { if (!isExpressionLabel(labelKey)) { continue; @@ -191,17 +193,17 @@ public class ExpressionLabelUtils { labels.put(labelKey, exchange.getRequest().getURI().getPath()); } } - + return labels; } - + public static Map resolve(HttpRequest request, Set labelKeys) { if (CollectionUtils.isEmpty(labelKeys)) { return Collections.emptyMap(); } - + Map labels = new HashMap<>(); - + for (String labelKey : labelKeys) { if (!isExpressionLabel(labelKey)) { continue; @@ -227,22 +229,22 @@ public class ExpressionLabelUtils { labels.put(labelKey, request.getURI().getPath()); } } - + return labels; } - + public static String parseHeaderKey(String expression) { return expression.substring(LABEL_HEADER_PREFIX_LEN, expression.length() - 1); } - + public static String parseQueryKey(String expression) { return expression.substring(LABEL_QUERY_PREFIX_LEN, expression.length() - 1); } - + public static String parseCookieKey(String expression) { return expression.substring(LABEL_COOKIE_PREFIX_LEN, expression.length() - 1); } - + public static String getQueryValue(String queryString, String queryKey) { if (StringUtils.isBlank(queryString)) { return StringUtils.EMPTY; @@ -259,7 +261,7 @@ public class ExpressionLabelUtils { } return StringUtils.EMPTY; } - + public static String getCookieValue(Cookie[] cookies, String key) { if (cookies == null || cookies.length == 0) { return StringUtils.EMPTY; @@ -271,7 +273,7 @@ public class ExpressionLabelUtils { } return StringUtils.EMPTY; } - + public static String getHeaderValue(ServerHttpRequest request, String key) { String value = request.getHeaders().getFirst(key); if (value == null) { @@ -279,7 +281,7 @@ public class ExpressionLabelUtils { } return value; } - + public static String getQueryValue(ServerHttpRequest request, String key) { MultiValueMap queries = request.getQueryParams(); if (CollectionUtils.isEmpty(queries)) { @@ -291,7 +293,7 @@ public class ExpressionLabelUtils { } return value; } - + public static String getCookieValue(ServerHttpRequest request, String key) { HttpCookie cookie = request.getCookies().getFirst(key); if (cookie == null) { @@ -299,32 +301,32 @@ public class ExpressionLabelUtils { } return cookie.getValue(); } - + public static String getHeaderValue(HttpRequest request, String key) { HttpHeaders headers = request.getHeaders(); return headers.getFirst(key); } - + public static String getQueryValue(HttpRequest request, String key) { String query = request.getURI().getQuery(); return getQueryValue(query, key); } - + public static String getFirstValue(Map> valueMaps, String key) { if (CollectionUtils.isEmpty(valueMaps)) { return StringUtils.EMPTY; } - + Collection values = valueMaps.get(key); - + if (CollectionUtils.isEmpty(values)) { return StringUtils.EMPTY; } - + for (String value : values) { return value; } - + return StringUtils.EMPTY; } }