Update ExpressionLabelUtils.java

pull/251/head
cheese8 3 years ago
parent e7add7115b
commit ea53d256e4

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

Loading…
Cancel
Save