Skip to content

Commit

Permalink
[ISSUE #3918]RequestMapping, UrlMappingPattern can apply some optimizing
Browse files Browse the repository at this point in the history
  • Loading branch information
pandaapo authored May 31, 2023
1 parent 6fbfd7a commit fb144bb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,29 @@

import com.sun.net.httpserver.HttpExchange;

import lombok.experimental.UtilityClass;

@UtilityClass
public class RequestMapping {

public static boolean postMapping(String value, HttpExchange httpExchange) {
if (HttpMethod.POST.name().equalsIgnoreCase(httpExchange.getRequestMethod())) {
String requestUri = httpExchange.getRequestURI().getPath();
UrlMappingPattern matcher = new UrlMappingPattern(value);
return matcher.matches(requestUri);
}
return false;
public boolean postMapping(String value, HttpExchange httpExchange) {
return isUrlMatch(value, httpExchange, HttpMethod.POST.name());
}

public static boolean getMapping(String value, HttpExchange httpExchange) {
if (HttpMethod.GET.name().equalsIgnoreCase(httpExchange.getRequestMethod())) {
String requestUri = httpExchange.getRequestURI().getPath();
UrlMappingPattern matcher = new UrlMappingPattern(value);
return matcher.matches(requestUri);
}
return false;
public boolean getMapping(String value, HttpExchange httpExchange) {
return isUrlMatch(value, httpExchange, HttpMethod.GET.name());
}

public static boolean putMapping(String value, HttpExchange httpExchange) {
if (HttpMethod.PUT.name().equalsIgnoreCase(httpExchange.getRequestMethod())) {
String requestUri = httpExchange.getRequestURI().getPath();
UrlMappingPattern matcher = new UrlMappingPattern(value);
return matcher.matches(requestUri);
}
return false;
public boolean putMapping(String value, HttpExchange httpExchange) {
return isUrlMatch(value, httpExchange, HttpMethod.PUT.name());
}

public boolean deleteMapping(String value, HttpExchange httpExchange) {
return isUrlMatch(value, httpExchange, HttpMethod.DELETE.name());
}

public static boolean deleteMapping(String value, HttpExchange httpExchange) {
if (HttpMethod.DELETE.name().equalsIgnoreCase(httpExchange.getRequestMethod())) {
private boolean isUrlMatch(String value, HttpExchange httpExchange, String methodType) {
if (methodType.equalsIgnoreCase(httpExchange.getRequestMethod())) {
String requestUri = httpExchange.getRequestURI().getPath();
UrlMappingPattern matcher = new UrlMappingPattern(value);
return matcher.matches(requestUri);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,15 @@ public class UrlMappingPattern {

private Pattern compiledUrlMappingPattern;

private List<String> paramNames = new ArrayList<String>();
private List<String> paramNames = new ArrayList<>();

public UrlMappingPattern(String pattern) {
super();
setUrlMappingPattern(pattern);
this.urlMappingPattern = pattern;
compile();
}

public String getMappingPattern() {
return getUrlMappingPattern().replaceFirst(URL_FORMAT_REGEX, "");
}

private String getUrlMappingPattern() {
return urlMappingPattern;
return urlMappingPattern.replaceFirst(URL_FORMAT_REGEX, "");
}

public Map<String, String> extractPathParameterValues(String url) {
Expand All @@ -74,21 +69,20 @@ public boolean matches(String url) {

public void compile() {
acquireParamNames();
String parsedPattern =
getUrlMappingPattern().replaceFirst(URL_FORMAT_REGEX, URL_FORMAT_MATCH_REGEX);
String parsedPattern = urlMappingPattern.replaceFirst(URL_FORMAT_REGEX, URL_FORMAT_MATCH_REGEX);
parsedPattern = parsedPattern.replaceAll(URL_PARAMETER_REGEX, URL_PARAMETER_MATCH_REGEX);
this.compiledUrlMappingPattern = Pattern.compile(parsedPattern + URL_QUERY_STRING_REGEX);
}

private void acquireParamNames() {
Matcher m = URL_PARAMETER_PATTERN.matcher(getUrlMappingPattern());
Matcher m = URL_PARAMETER_PATTERN.matcher(urlMappingPattern);
while (m.find()) {
paramNames.add(m.group(1));
}
}

private Map<String, String> extractParameters(Matcher matcher) {
Map<String, String> values = new HashMap<String, String>();
Map<String, String> values = new HashMap<>((int) (matcher.groupCount() / 0.75f + 1));
for (int i = 0; i < matcher.groupCount(); i++) {
String value = matcher.group(i + 1);

Expand All @@ -99,10 +93,6 @@ private Map<String, String> extractParameters(Matcher matcher) {
return values;
}

private void setUrlMappingPattern(String pattern) {
this.urlMappingPattern = pattern;
}

public List<String> getParamNames() {
return Collections.unmodifiableList(paramNames);
}
Expand Down

0 comments on commit fb144bb

Please sign in to comment.