Skip to content

Commit

Permalink
Optimize object creation PartialMatchHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
koo-taejin authored and mdeinum committed Jun 29, 2023
1 parent aba24d6 commit 690b924
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 9 deletions.
Expand Up @@ -36,6 +36,7 @@
import org.springframework.http.server.PathContainer;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.filter.reactive.ServerHttpObservationFilter;
Expand Down Expand Up @@ -169,7 +170,7 @@ protected void handleMatch(RequestMappingInfo info, HandlerMethod handlerMethod,
protected HandlerMethod handleNoMatch(Set<RequestMappingInfo> infos,
ServerWebExchange exchange) throws Exception {

PartialMatchHelper helper = new PartialMatchHelper(infos, exchange);
PartialMatchHelper helper = PartialMatchHelper.from(infos, exchange);

if (helper.isEmpty()) {
return null;
Expand Down Expand Up @@ -219,17 +220,27 @@ protected HandlerMethod handleNoMatch(Set<RequestMappingInfo> infos,
/**
* Aggregate all partial matches and expose methods checking across them.
*/
private static class PartialMatchHelper {
private static final class PartialMatchHelper {

private static final PartialMatchHelper EMPTY_HELPER = new PartialMatchHelper(Collections.emptySet(), null);

private final List<PartialMatch> partialMatches = new ArrayList<>();


public PartialMatchHelper(Set<RequestMappingInfo> infos, ServerWebExchange exchange) {
this.partialMatches.addAll(infos.stream()
.filter(info -> info.getPatternsCondition().getMatchingCondition(exchange) != null)
.map(info -> new PartialMatch(info, exchange)).toList());
private PartialMatchHelper(Set<RequestMappingInfo> infos, ServerWebExchange exchange) {
for (RequestMappingInfo info : infos) {
if (info.getPatternsCondition().getMatchingCondition(exchange) != null) {
this.partialMatches.add(new PartialMatch(info, exchange));
}
}
}

public static PartialMatchHelper from(Set<RequestMappingInfo> infos, ServerWebExchange exchange) {
if (CollectionUtils.isEmpty(infos)) {
return EMPTY_HELPER;
}
return new PartialMatchHelper(infos, exchange);
}

/**
* Whether there are any partial matches.
Expand Down
Expand Up @@ -21,6 +21,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -345,6 +346,17 @@ public void handlePatchUnsupportedMediaType() {

}

@Test
public void handleNoMatchEmptyRequestMappingInfo() throws Exception {
ServerWebExchange exchange = MockServerWebExchange.from(post("/bar"));

HandlerMethod handlerMethod = this.handlerMapping.handleNoMatch(new HashSet<>(), exchange);
assertThat(handlerMethod).isNull();

handlerMethod = this.handlerMapping.handleNoMatch(null, exchange);
assertThat(handlerMethod).isNull();
}


@SuppressWarnings("unchecked")
private <T> void assertError(Mono<Object> mono, final Class<T> exceptionClass, final Consumer<T> consumer) {
Expand Down
Expand Up @@ -246,7 +246,7 @@ private Map<String, MultiValueMap<String, String>> extractMatrixVariables(
protected HandlerMethod handleNoMatch(
Set<RequestMappingInfo> infos, String lookupPath, HttpServletRequest request) throws ServletException {

PartialMatchHelper helper = new PartialMatchHelper(infos, request);
PartialMatchHelper helper = PartialMatchHelper.from(infos, request);
if (helper.isEmpty()) {
return null;
}
Expand Down Expand Up @@ -293,18 +293,27 @@ protected HandlerMethod handleNoMatch(
/**
* Aggregate all partial matches and expose methods checking across them.
*/
private static class PartialMatchHelper {
private static final class PartialMatchHelper {

private static final PartialMatchHelper EMPTY_HELPER = new PartialMatchHelper(Collections.emptySet(), null);

private final List<PartialMatch> partialMatches = new ArrayList<>();

public PartialMatchHelper(Set<RequestMappingInfo> infos, HttpServletRequest request) {
private PartialMatchHelper(Set<RequestMappingInfo> infos, HttpServletRequest request) {
for (RequestMappingInfo info : infos) {
if (info.getActivePatternsCondition().getMatchingCondition(request) != null) {
this.partialMatches.add(new PartialMatch(info, request));
}
}
}

public static PartialMatchHelper from(Set<RequestMappingInfo> infos, HttpServletRequest request) {
if (CollectionUtils.isEmpty(infos)) {
return EMPTY_HELPER;
}
return new PartialMatchHelper(infos, request);
}

/**
* Whether there are any partial matches.
*/
Expand Down
Expand Up @@ -25,6 +25,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -397,6 +398,17 @@ void handleMatchMatrixVariablesDecoding(TestRequestMappingInfoHandlerMapping map
assertThat(uriVariables.get("cars")).isEqualTo("cars");
}

@PathPatternsParameterizedTest
void handleNoMatchEmptyRequestMappingInfo(TestRequestMappingInfoHandlerMapping mapping) throws ServletException {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/cars;color=green");

HandlerMethod handlerMethod = mapping.handleNoMatch(new HashSet<>(), "/{cars}", request);
assertThat(handlerMethod).isNull();

handlerMethod = mapping.handleNoMatch(null, "/{cars}", request);
assertThat(handlerMethod).isNull();
}

private HandlerMethod getHandler(
TestRequestMappingInfoHandlerMapping mapping, MockHttpServletRequest request) throws Exception {

Expand Down

0 comments on commit 690b924

Please sign in to comment.