Use Matcher from pre-compiled Pattern rather than String for replaceAll

Closes gh-14483
This commit is contained in:
durigon
2018-09-16 11:42:49 +09:00
committed by Andy Wilkinson
parent 11016bc7f4
commit 7aaeefbc0e
8 changed files with 44 additions and 10 deletions

View File

@@ -18,6 +18,7 @@ package org.springframework.boot.actuate.endpoint.web;
import java.util.Collection;
import java.util.Collections;
import java.util.regex.Pattern;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
@@ -30,6 +31,8 @@ import org.springframework.util.StringUtils;
*/
public final class WebOperationRequestPredicate {
private static final Pattern PATH_VAR_PATTERN = Pattern.compile("\\{.*?}");
private final String path;
private final String canonicalPath;
@@ -50,7 +53,7 @@ public final class WebOperationRequestPredicate {
public WebOperationRequestPredicate(String path, WebEndpointHttpMethod httpMethod,
Collection<String> consumes, Collection<String> produces) {
this.path = path;
this.canonicalPath = path.replaceAll("\\{.*?}", "{*}");
this.canonicalPath = PATH_VAR_PATTERN.matcher(path).replaceAll("{*}");
this.httpMethod = httpMethod;
this.consumes = consumes;
this.produces = produces;

View File

@@ -18,6 +18,7 @@ package org.springframework.boot.actuate.metrics.web.client;
import java.io.IOException;
import java.net.URI;
import java.util.regex.Pattern;
import io.micrometer.core.instrument.Tag;
@@ -36,6 +37,8 @@ import org.springframework.web.client.RestTemplate;
*/
public final class RestTemplateExchangeTags {
private static final Pattern STRIP_URI_PATTERN = Pattern.compile("^https?://[^/]+/");
private RestTemplateExchangeTags() {
}
@@ -69,7 +72,7 @@ public final class RestTemplateExchangeTags {
}
private static String stripUri(String uri) {
return uri.replaceAll("^https?://[^/]+/", "");
return STRIP_URI_PATTERN.matcher(uri).replaceAll("");
}
private static String ensureLeadingSlash(String url) {

View File

@@ -16,6 +16,8 @@
package org.springframework.boot.actuate.metrics.web.servlet;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@@ -50,6 +52,10 @@ public final class WebMvcTags {
private static final Tag METHOD_UNKNOWN = Tag.of("method", "UNKNOWN");
private static final Pattern TRAILING_SLASH_PATTERN = Pattern.compile("/$");
private static final Pattern MULTIPLE_SLASH_PATTERN = Pattern.compile("//+");
private WebMvcTags() {
}
@@ -124,7 +130,8 @@ public final class WebMvcTags {
private static String getPathInfo(HttpServletRequest request) {
String pathInfo = request.getPathInfo();
String uri = StringUtils.hasText(pathInfo) ? pathInfo : "/";
return uri.replaceAll("//+", "/").replaceAll("/$", "");
uri = MULTIPLE_SLASH_PATTERN.matcher(uri).replaceAll("/");
return TRAILING_SLASH_PATTERN.matcher(uri).replaceAll("");
}
/**