From 4198d217e5df2b26da2bb90d7c7a29f8a7d7d457 Mon Sep 17 00:00:00 2001 From: shannon312 Date: Mon, 29 Oct 2018 09:22:39 +0800 Subject: [PATCH] Extract slash as a constant to reuse in FilterUtil (#204) --- .../adapter/servlet/util/FilterUtil.java | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/sentinel-adapter/sentinel-web-servlet/src/main/java/com/alibaba/csp/sentinel/adapter/servlet/util/FilterUtil.java b/sentinel-adapter/sentinel-web-servlet/src/main/java/com/alibaba/csp/sentinel/adapter/servlet/util/FilterUtil.java index ad3434c525..4dbc1bfcd1 100755 --- a/sentinel-adapter/sentinel-web-servlet/src/main/java/com/alibaba/csp/sentinel/adapter/servlet/util/FilterUtil.java +++ b/sentinel-adapter/sentinel-web-servlet/src/main/java/com/alibaba/csp/sentinel/adapter/servlet/util/FilterUtil.java @@ -32,13 +32,15 @@ */ public final class FilterUtil { + private static final String PATH_SPLIT = "/"; + public static String filterTarget(HttpServletRequest request) { String pathInfo = getResourcePath(request); - if (!pathInfo.startsWith("/")) { - pathInfo = "/" + pathInfo; + if (!pathInfo.startsWith(PATH_SPLIT)) { + pathInfo = PATH_SPLIT + pathInfo; } - if ("/".equals(pathInfo)) { + if (PATH_SPLIT.equals(pathInfo)) { return pathInfo; } @@ -49,7 +51,7 @@ public static String filterTarget(HttpServletRequest request) { pathInfo = pathInfo.substring(0, lastSlashIndex) + "/" + StringUtil.trim(pathInfo.substring(lastSlashIndex + 1)); } else { - pathInfo = "/" + StringUtil.trim(pathInfo); + pathInfo = PATH_SPLIT + StringUtil.trim(pathInfo); } return pathInfo; @@ -102,8 +104,8 @@ private static String normalizePath(String path, boolean forceAbsolute, boolean char firstChar = pathChars[0]; char lastChar = pathChars[length - 1]; - startsWithSlash = firstChar == '/' || firstChar == '\\'; - endsWithSlash = lastChar == '/' || lastChar == '\\'; + startsWithSlash = firstChar == PATH_SPLIT.charAt(0) || firstChar == '\\'; + endsWithSlash = lastChar == PATH_SPLIT.charAt(0) || lastChar == '\\'; } StringBuilder buf = new StringBuilder(length); @@ -112,7 +114,7 @@ private static String normalizePath(String path, boolean forceAbsolute, boolean int level = 0; if (isAbsolutePath) { - buf.append("/"); + buf.append(PATH_SPLIT); } while (index < length) { @@ -138,7 +140,7 @@ private static String normalizePath(String path, boolean forceAbsolute, boolean if (isAbsolutePath) { throw new IllegalStateException(path); } else { - buf.append("../"); + buf.append("..").append(PATH_SPLIT); } } else { buf.setLength(pathChars[--level]); @@ -148,7 +150,7 @@ private static String normalizePath(String path, boolean forceAbsolute, boolean } pathChars[level++] = (char)buf.length(); - buf.append(element).append('/'); + buf.append(element).append(PATH_SPLIT); } // remove the last "/" @@ -168,11 +170,11 @@ private static int indexOfSlash(char[] chars, int beginIndex, boolean slash) { char ch = chars[i]; if (slash) { - if (ch == '/' || ch == '\\') { + if (ch == PATH_SPLIT.charAt(0) || ch == '\\') { break; // if a slash } } else { - if (ch != '/' && ch != '\\') { + if (ch != PATH_SPLIT.charAt(0) && ch != '\\') { break; // if not a slash } }