public RequestMappingInfo(@Nullable String name, @Nullable PatternsRequestCondition patterns, @Nullable RequestMethodsRequestCondition methods, @Nullable ParamsRequestCondition params, @Nullable HeadersRequestCondition headers, @Nullable ConsumesRequestCondition consumes, @Nullable ProducesRequestCondition produces, @Nullable RequestCondition<?> custom) { this.name = (StringUtils.hasText(name) ? name : null); this.patternsCondition = (patterns != null ? patterns : new PatternsRequestCondition()); this.methodsCondition = (methods != null ? methods : new RequestMethodsRequestCondition()); this.paramsCondition = (params != null ? params : new ParamsRequestCondition()); this.headersCondition = (headers != null ? headers : new HeadersRequestCondition()); this.consumesCondition = (consumes != null ? consumes : new ConsumesRequestCondition()); this.producesCondition = (produces != null ? produces : new ProducesRequestCondition()); this.customConditionHolder = new RequestConditionHolder(custom); }
/** * Checks if any of the contained media type expressions match the given * request 'Content-Type' header and returns an instance that is guaranteed * to contain matching expressions only. The match is performed via * {@link MediaType#includes(MediaType)}. * @param exchange the current exchange * @return the same instance if the condition contains no expressions; * or a new condition with matching expressions only; * or {@code null} if no expressions match. */ @Override public ConsumesRequestCondition getMatchingCondition(ServerWebExchange exchange) { if (CorsUtils.isPreFlightRequest(exchange.getRequest())) { return PRE_FLIGHT_MATCH; } if (isEmpty()) { return this; } Set<ConsumeMediaTypeExpression> result = new LinkedHashSet<>(this.expressions); result.removeIf(expression -> !expression.match(exchange)); return (!result.isEmpty() ? new ConsumesRequestCondition(result) : null); }
@Test public void combine() throws Exception { ConsumesRequestCondition condition1 = new ConsumesRequestCondition("text/plain"); ConsumesRequestCondition condition2 = new ConsumesRequestCondition("application/xml"); ConsumesRequestCondition result = condition1.combine(condition2); assertEquals(condition2, result); }
@Test public void combineWithDefault() throws Exception { ConsumesRequestCondition condition1 = new ConsumesRequestCondition("text/plain"); ConsumesRequestCondition condition2 = new ConsumesRequestCondition(); ConsumesRequestCondition result = condition1.combine(condition2); assertEquals(condition1, result); }
@Test public void parseConsumesAndHeaders() throws Exception { String[] consumes = new String[] {"text/plain"}; String[] headers = new String[]{"foo=bar", "content-type=application/xml,application/pdf"}; ConsumesRequestCondition condition = new ConsumesRequestCondition(consumes, headers); assertConditions(condition, "text/plain", "application/xml", "application/pdf"); }
@Test public void getConsumableMediaTypesNegatedExpression() throws Exception { ConsumesRequestCondition condition = new ConsumesRequestCondition("!application/xml"); assertEquals(Collections.emptySet(), condition.getConsumableMediaTypes()); }
@Test public void getMatchingCondition() throws Exception { MockServerWebExchange exchange = postExchange("text/plain"); ConsumesRequestCondition condition = new ConsumesRequestCondition("text/plain", "application/xml"); ConsumesRequestCondition result = condition.getMatchingCondition(exchange); assertConditions(result, "text/plain"); condition = new ConsumesRequestCondition("application/xml"); result = condition.getMatchingCondition(exchange); assertNull(result); }
@Test public void compareToMultiple() throws Exception { MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/")); ConsumesRequestCondition condition1 = new ConsumesRequestCondition("*/*", "text/plain"); ConsumesRequestCondition condition2 = new ConsumesRequestCondition("text/*", "text/plain;q=0.7"); int result = condition1.compareTo(condition2, exchange); assertTrue("Invalid comparison result: " + result, result < 0); result = condition2.compareTo(condition1, exchange); assertTrue("Invalid comparison result: " + result, result > 0); }
@Override public RequestMappingInfo build() { RequestedContentTypeResolver contentTypeResolver = this.options.getContentTypeResolver(); PathPatternParser parser = (this.options.getPatternParser() != null ? this.options.getPatternParser() : new PathPatternParser()); PatternsRequestCondition patternsCondition = new PatternsRequestCondition(parse(this.paths, parser)); return new RequestMappingInfo(this.mappingName, patternsCondition, new RequestMethodsRequestCondition(this.methods), new ParamsRequestCondition(this.params), new HeadersRequestCondition(this.headers), new ConsumesRequestCondition(this.consumes, this.headers), new ProducesRequestCondition(this.produces, this.headers, contentTypeResolver), this.customCondition); }
@Test public void compareToSingle() throws Exception { MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/")); ConsumesRequestCondition condition1 = new ConsumesRequestCondition("text/plain"); ConsumesRequestCondition condition2 = new ConsumesRequestCondition("text/*"); int result = condition1.compareTo(condition2, exchange); assertTrue("Invalid comparison result: " + result, result < 0); result = condition2.compareTo(condition1, exchange); assertTrue("Invalid comparison result: " + result, result > 0); }
@Test public void consumesParseError() throws Exception { MockServerWebExchange exchange = postExchange("01"); ConsumesRequestCondition condition = new ConsumesRequestCondition("text/plain"); assertNull(condition.getMatchingCondition(exchange)); }
@Test public void negatedConsumesMatch() throws Exception { MockServerWebExchange exchange = postExchange("text/plain"); ConsumesRequestCondition condition = new ConsumesRequestCondition("!text/plain"); assertNull(condition.getMatchingCondition(exchange)); }
@Test public void consumesWildcardMatch() throws Exception { MockServerWebExchange exchange = postExchange("text/plain"); ConsumesRequestCondition condition = new ConsumesRequestCondition("text/*"); assertNotNull(condition.getMatchingCondition(exchange)); }
@Test public void consumesParseErrorWithNegation() throws Exception { MockServerWebExchange exchange = postExchange("01"); ConsumesRequestCondition condition = new ConsumesRequestCondition("!text/plain"); assertNull(condition.getMatchingCondition(exchange)); }
@Test public void consumesMultipleMatch() throws Exception { MockServerWebExchange exchange = postExchange("text/plain"); ConsumesRequestCondition condition = new ConsumesRequestCondition("text/plain", "application/xml"); assertNotNull(condition.getMatchingCondition(exchange)); }
@Test public void consumesSingleNoMatch() throws Exception { MockServerWebExchange exchange = postExchange("application/xml"); ConsumesRequestCondition condition = new ConsumesRequestCondition("text/plain"); assertNull(condition.getMatchingCondition(exchange)); }
@Test public void consumesMatch() throws Exception { MockServerWebExchange exchange = postExchange("text/plain"); ConsumesRequestCondition condition = new ConsumesRequestCondition("text/plain"); assertNotNull(condition.getMatchingCondition(exchange)); }
private RequestMappingInfo createRequestMappingInfo(WebOperation operation) { WebOperationRequestPredicate predicate = operation.getRequestPredicate(); PatternsRequestCondition patterns = new PatternsRequestCondition(pathPatternParser .parse(this.endpointMapping.createSubPath(predicate.getPath()))); RequestMethodsRequestCondition methods = new RequestMethodsRequestCondition( RequestMethod.valueOf(predicate.getHttpMethod().name())); ConsumesRequestCondition consumes = new ConsumesRequestCondition( StringUtils.toStringArray(predicate.getConsumes())); ProducesRequestCondition produces = new ProducesRequestCondition( StringUtils.toStringArray(predicate.getProduces())); return new RequestMappingInfo(null, patterns, methods, null, null, consumes, produces, null); }
public RequestMappingInfo(String name, PatternsRequestCondition patterns, RequestMethodsRequestCondition methods, ParamsRequestCondition params, HeadersRequestCondition headers, ConsumesRequestCondition consumes, ProducesRequestCondition produces, RequestCondition<?> custom) { this.name = (StringUtils.hasText(name) ? name : null); this.patternsCondition = (patterns != null ? patterns : new PatternsRequestCondition()); this.methodsCondition = (methods != null ? methods : new RequestMethodsRequestCondition()); this.paramsCondition = (params != null ? params : new ParamsRequestCondition()); this.headersCondition = (headers != null ? headers : new HeadersRequestCondition()); this.consumesCondition = (consumes != null ? consumes : new ConsumesRequestCondition()); this.producesCondition = (produces != null ? produces : new ProducesRequestCondition()); this.customConditionHolder = new RequestConditionHolder(custom); }
@Override public RequestMappingInfo build() { RequestedContentTypeResolver contentTypeResolver = this.options.getContentTypeResolver(); PatternsRequestCondition patternsCondition = new PatternsRequestCondition( this.paths, this.options.getPathHelper(), this.options.getPathMatcher(), this.options.useSuffixPatternMatch(), this.options.useTrailingSlashMatch(), this.options.getFileExtensions()); return new RequestMappingInfo(this.mappingName, patternsCondition, new RequestMethodsRequestCondition(methods), new ParamsRequestCondition(this.params), new HeadersRequestCondition(this.headers), new ConsumesRequestCondition(this.consumes, this.headers), new ProducesRequestCondition(this.produces, this.headers, contentTypeResolver), this.customCondition); } }