Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE #3918]RequestMapping, UrlMappingPattern can apply some optimizing #3919

Merged
merged 2 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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