/** * Create a new {@link PartialMatch} instance. * @param info the RequestMappingInfo that matches the URL path * @param exchange the current exchange */ public PartialMatch(RequestMappingInfo info, ServerWebExchange exchange) { this.info = info; this.methodsMatch = info.getMethodsCondition().getMatchingCondition(exchange) != null; this.consumesMatch = info.getConsumesCondition().getMatchingCondition(exchange) != null; this.producesMatch = info.getProducesCondition().getMatchingCondition(exchange) != null; this.paramsMatch = info.getParamsCondition().getMatchingCondition(exchange) != null; }
/** * Checks if all conditions in this request mapping info match the provided request and returns * a potentially new request mapping info with conditions tailored to the current request. * <p>For example the returned instance may contain the subset of URL patterns that match to * the current request, sorted with best matching patterns on top. * @return a new instance in case all conditions match; or {@code null} otherwise */ @Override @Nullable public RequestMappingInfo getMatchingCondition(ServerWebExchange exchange) { RequestMethodsRequestCondition methods = this.methodsCondition.getMatchingCondition(exchange); ParamsRequestCondition params = this.paramsCondition.getMatchingCondition(exchange); HeadersRequestCondition headers = this.headersCondition.getMatchingCondition(exchange); ConsumesRequestCondition consumes = this.consumesCondition.getMatchingCondition(exchange); ProducesRequestCondition produces = this.producesCondition.getMatchingCondition(exchange); if (methods == null || params == null || headers == null || consumes == null || produces == null) { return null; } PatternsRequestCondition patterns = this.patternsCondition.getMatchingCondition(exchange); if (patterns == null) { return null; } RequestConditionHolder custom = this.customConditionHolder.getMatchingCondition(exchange); if (custom == null) { return null; } return new RequestMappingInfo(this.name, patterns, methods, params, headers, consumes, produces, custom.getCondition()); }
@Test public void getMatchingCondition() { MockServerWebExchange exchange = MockServerWebExchange.from(get("/").header("Accept", "text/plain")); ProducesRequestCondition condition = new ProducesRequestCondition("text/plain", "application/xml"); ProducesRequestCondition result = condition.getMatchingCondition(exchange); assertConditions(result, "text/plain"); condition = new ProducesRequestCondition("application/xml"); result = condition.getMatchingCondition(exchange); assertNull(result); }
@Test public void matchSingle() { MockServerWebExchange exchange = MockServerWebExchange.from(get("/").header("Accept", "application/xml")); ProducesRequestCondition condition = new ProducesRequestCondition("text/plain"); assertNull(condition.getMatchingCondition(exchange)); }
@Test public void match() { MockServerWebExchange exchange = MockServerWebExchange.from(get("/").header("Accept", "text/plain")); ProducesRequestCondition condition = new ProducesRequestCondition("text/plain"); assertNotNull(condition.getMatchingCondition(exchange)); }
@Test public void matchNegated() { MockServerWebExchange exchange = MockServerWebExchange.from(get("/").header("Accept", "text/plain")); ProducesRequestCondition condition = new ProducesRequestCondition("!text/plain"); assertNull(condition.getMatchingCondition(exchange)); }
@Test public void matchMultiple() { MockServerWebExchange exchange = MockServerWebExchange.from(get("/").header("Accept", "text/plain")); ProducesRequestCondition condition = new ProducesRequestCondition("text/plain", "application/xml"); assertNotNull(condition.getMatchingCondition(exchange)); }
@Test public void matchParseErrorWithNegation() { MockServerWebExchange exchange = MockServerWebExchange.from(get("/").header("Accept", "bogus")); ProducesRequestCondition condition = new ProducesRequestCondition("!text/plain"); assertNull(condition.getMatchingCondition(exchange)); }
@Test public void matchWildcard() { MockServerWebExchange exchange = MockServerWebExchange.from(get("/").header("Accept", "text/plain")); ProducesRequestCondition condition = new ProducesRequestCondition("text/*"); assertNotNull(condition.getMatchingCondition(exchange)); }
@Test public void matchParseError() { MockServerWebExchange exchange = MockServerWebExchange.from(get("/").header("Accept", "bogus")); ProducesRequestCondition condition = new ProducesRequestCondition("text/plain"); assertNull(condition.getMatchingCondition(exchange)); }
@Test // SPR-17550 public void matchWithNegationAndMediaTypeAllWithQualityParameter() { ProducesRequestCondition condition = new ProducesRequestCondition("!application/json"); MockServerWebExchange exchange = MockServerWebExchange.from(get("/").header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")); assertNotNull(condition.getMatchingCondition(exchange)); }
/** * @param info RequestMappingInfo that matches the URL path * @param exchange the current exchange */ public PartialMatch(RequestMappingInfo info, ServerWebExchange exchange) { this.info = info; this.methodsMatch = info.getMethodsCondition().getMatchingCondition(exchange) != null; this.consumesMatch = info.getConsumesCondition().getMatchingCondition(exchange) != null; this.producesMatch = info.getProducesCondition().getMatchingCondition(exchange) != null; this.paramsMatch = info.getParamsCondition().getMatchingCondition(exchange) != null; }
/** * Checks if all conditions in this request mapping info match the provided request and returns * a potentially new request mapping info with conditions tailored to the current request. * <p>For example the returned instance may contain the subset of URL patterns that match to * the current request, sorted with best matching patterns on top. * @return a new instance in case all conditions match; or {@code null} otherwise */ @Override public RequestMappingInfo getMatchingCondition(ServerWebExchange exchange) { RequestMethodsRequestCondition methods = this.methodsCondition.getMatchingCondition(exchange); ParamsRequestCondition params = this.paramsCondition.getMatchingCondition(exchange); HeadersRequestCondition headers = this.headersCondition.getMatchingCondition(exchange); ConsumesRequestCondition consumes = this.consumesCondition.getMatchingCondition(exchange); ProducesRequestCondition produces = this.producesCondition.getMatchingCondition(exchange); if (methods == null || params == null || headers == null || consumes == null || produces == null) { return null; } PatternsRequestCondition patterns = this.patternsCondition.getMatchingCondition(exchange); if (patterns == null) { return null; } RequestConditionHolder custom = this.customConditionHolder.getMatchingCondition(exchange); if (custom == null) { return null; } return new RequestMappingInfo(this.name, patterns, methods, params, headers, consumes, produces, custom.getCondition()); }