Polish use of LookupPath

This commit is contained in:
Rossen Stoyanchev
2017-06-02 14:46:34 -04:00
parent 90df7dd279
commit a7020e419a
11 changed files with 67 additions and 63 deletions

View File

@@ -175,12 +175,12 @@ public abstract class AbstractHandlerMapping extends ApplicationObjectSupport im
}
protected LookupPath getLookupPath(ServerWebExchange exchange) {
Optional<LookupPath> attribute = exchange.getAttribute(LookupPath.LOOKUP_PATH_ATTRIBUTE);
return attribute.orElseGet(() -> {
LookupPath lookupPath = createLookupPath(exchange);
exchange.getAttributes().put(LookupPath.LOOKUP_PATH_ATTRIBUTE, lookupPath);
return lookupPath;
});
return exchange.<LookupPath>getAttribute(LookupPath.LOOKUP_PATH_ATTRIBUTE)
.orElseGet(() -> {
LookupPath lookupPath = createLookupPath(exchange);
exchange.getAttributes().put(LookupPath.LOOKUP_PATH_ATTRIBUTE, lookupPath);
return lookupPath;
});
}
protected LookupPath createLookupPath(ServerWebExchange exchange) {

View File

@@ -185,7 +185,9 @@ public class ResourceUrlProvider implements ApplicationListener<ContextRefreshed
private int getLookupPathIndex(ServerWebExchange exchange) {
ServerHttpRequest request = exchange.getRequest();
String requestPath = request.getURI().getPath();
LookupPath lookupPath = getPathHelper().getLookupPathForRequest(exchange);
LookupPath lookupPath = exchange
.<LookupPath>getAttribute(LookupPath.LOOKUP_PATH_ATTRIBUTE)
.orElseGet(() -> getPathHelper().getLookupPathForRequest(exchange));
return requestPath.indexOf(lookupPath.getPath());
}

View File

@@ -29,8 +29,8 @@ import java.util.Set;
import org.springframework.util.PathMatcher;
import org.springframework.util.StringUtils;
import org.springframework.web.server.support.LookupPath;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.support.LookupPath;
import org.springframework.web.util.pattern.ParsingPathMatcher;
/**
@@ -187,8 +187,7 @@ public final class PatternsRequestCondition extends AbstractRequestCondition<Pat
return this;
}
LookupPath lookupPath = exchange
.<LookupPath>getAttribute(LookupPath.LOOKUP_PATH_ATTRIBUTE).get();
LookupPath lookupPath = getLookupPath(exchange);
List<String> matches = getMatchingPatterns(lookupPath);
return matches.isEmpty() ? null :
@@ -196,6 +195,11 @@ public final class PatternsRequestCondition extends AbstractRequestCondition<Pat
this.useTrailingSlashMatch, this.fileExtensions);
}
private LookupPath getLookupPath(ServerWebExchange exchange) {
return exchange.<LookupPath>getAttribute(LookupPath.LOOKUP_PATH_ATTRIBUTE)
.orElseThrow(() -> new IllegalStateException("No LookupPath attribute."));
}
/**
* Find the patterns matching the given lookup path. Invoking this method should
* yield results equivalent to those of calling
@@ -259,8 +263,7 @@ public final class PatternsRequestCondition extends AbstractRequestCondition<Pat
*/
@Override
public int compareTo(PatternsRequestCondition other, ServerWebExchange exchange) {
LookupPath lookupPath = exchange
.<LookupPath>getAttribute(LookupPath.LOOKUP_PATH_ATTRIBUTE).get();
LookupPath lookupPath = getLookupPath(exchange);
Comparator<String> patternComparator = this.pathMatcher.getPatternComparator(lookupPath.getPath());
Iterator<String> iterator = this.patterns.iterator();
Iterator<String> iteratorOther = other.patterns.iterator();

View File

@@ -32,13 +32,11 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder;
import org.springframework.web.reactive.accept.RequestedContentTypeResolver;
import org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder;
import org.springframework.web.reactive.result.condition.RequestCondition;
import org.springframework.web.reactive.result.method.RequestMappingInfo;
import org.springframework.web.reactive.result.method.RequestMappingInfoHandlerMapping;
import org.springframework.web.server.support.LookupPath;
import org.springframework.web.server.ServerWebExchange;
/**
* An extension of {@link RequestMappingInfoHandlerMapping} that creates
@@ -169,11 +167,6 @@ public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMappi
AnnotatedElementUtils.hasAnnotation(beanType, RequestMapping.class));
}
@Override
protected LookupPath createLookupPath(ServerWebExchange exchange) {
return getPathHelper().getLookupPathForRequest(exchange);
}
/**
* Uses method and type-level @{@link RequestMapping} annotations to create
* the RequestMappingInfo.

View File

@@ -257,7 +257,9 @@ public class ViewResolutionResultHandler extends HandlerResultHandlerSupport
* Use the request path the leading and trailing slash stripped.
*/
private String getDefaultViewName(ServerWebExchange exchange) {
String path = exchange.<LookupPath>getAttribute(LookupPath.LOOKUP_PATH_ATTRIBUTE).get().getPath();
String path = exchange.<LookupPath>getAttribute(LookupPath.LOOKUP_PATH_ATTRIBUTE)
.map(LookupPath::getPath)
.orElseThrow(() -> new IllegalStateException("No LookupPath attribute."));
if (path.startsWith("/")) {
path = path.substring(1);
}