/** * Returns a new instance with a union of the HTTP request methods * from "this" and the "other" instance. */ @Override public RequestMethodsRequestCondition combine(RequestMethodsRequestCondition other) { Set<RequestMethod> set = new LinkedHashSet<>(this.methods); set.addAll(other.methods); return new RequestMethodsRequestCondition(set); }
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); }
@Nullable private RequestMethodsRequestCondition matchRequestMethod(@Nullable HttpMethod httpMethod) { if (httpMethod != null) { for (RequestMethod method : getMethods()) { if (httpMethod.matches(method.name())) { return new RequestMethodsRequestCondition(method); } } if (httpMethod == HttpMethod.HEAD && getMethods().contains(RequestMethod.GET)) { return GET_CONDITION; } } return null; }
@Test public void getMatchingConditionWithHttpHead() throws Exception { testMatch(new RequestMethodsRequestCondition(HEAD), HEAD); testMatch(new RequestMethodsRequestCondition(GET), GET); testNoMatch(new RequestMethodsRequestCondition(POST), HEAD); }
@Test public void getMatchingCondition() throws Exception { testMatch(new RequestMethodsRequestCondition(GET), GET); testMatch(new RequestMethodsRequestCondition(GET, POST), GET); testNoMatch(new RequestMethodsRequestCondition(GET), POST); }
@Test public void combine() { RequestMethodsRequestCondition condition1 = new RequestMethodsRequestCondition(GET); RequestMethodsRequestCondition condition2 = new RequestMethodsRequestCondition(POST); RequestMethodsRequestCondition result = condition1.combine(condition2); assertEquals(2, result.getContent().size()); }
@Test public void match() { RequestMethodsRequestCondition rm = new RequestMethodsRequestCondition(RequestMethod.GET, RequestMethod.POST); RequestConditionHolder custom = new RequestConditionHolder(rm); RequestMethodsRequestCondition expected = new RequestMethodsRequestCondition(RequestMethod.GET); RequestConditionHolder holder = custom.getMatchingCondition(this.exchange); assertNotNull(holder); assertEquals(expected, holder.getCondition()); }
@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 compareTo() throws Exception { RequestMethodsRequestCondition c1 = new RequestMethodsRequestCondition(GET, HEAD); RequestMethodsRequestCondition c2 = new RequestMethodsRequestCondition(POST); RequestMethodsRequestCondition c3 = new RequestMethodsRequestCondition(); ServerWebExchange exchange = getExchange("GET"); int result = c1.compareTo(c2, exchange); assertTrue("Invalid comparison result: " + result, result < 0); result = c2.compareTo(c1, exchange); assertTrue("Invalid comparison result: " + result, result > 0); result = c2.compareTo(c3, exchange); assertTrue("Invalid comparison result: " + result, result < 0); result = c1.compareTo(c1, exchange); assertEquals("Invalid comparison result ", 0, result); }
@Test @Ignore public void getMatchingConditionWithCorsPreFlight() throws Exception { ServerWebExchange exchange = getExchange("OPTIONS"); exchange.getRequest().getHeaders().add("Origin", "http://example.com"); exchange.getRequest().getHeaders().add(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "PUT"); assertNotNull(new RequestMethodsRequestCondition().getMatchingCondition(exchange)); assertNotNull(new RequestMethodsRequestCondition(PUT).getMatchingCondition(exchange)); assertNull(new RequestMethodsRequestCondition(DELETE).getMatchingCondition(exchange)); }
@Test @Ignore public void getMatchingConditionWithCustomMethod() throws Exception { ServerWebExchange exchange = getExchange("PROPFIND"); assertNotNull(new RequestMethodsRequestCondition().getMatchingCondition(exchange)); assertNull(new RequestMethodsRequestCondition(GET, POST).getMatchingCondition(exchange)); }
@Test public void match() { MockServerHttpRequest request = MockServerHttpRequest.get("/path?param1=paramValue1").build(); MockServerWebExchange exchange = MockServerWebExchange.from(request); RequestCondition<?> condition1 = new RequestMethodsRequestCondition(RequestMethod.GET, RequestMethod.POST); RequestCondition<?> condition2 = new RequestMethodsRequestCondition(RequestMethod.GET); CompositeRequestCondition composite1 = new CompositeRequestCondition(this.param1, condition1); CompositeRequestCondition composite2 = new CompositeRequestCondition(this.param1, condition2); assertEquals(composite2, composite1.getMatchingCondition(exchange)); }
@Test public void noMatch() { RequestMethodsRequestCondition rm = new RequestMethodsRequestCondition(RequestMethod.POST); RequestConditionHolder custom = new RequestConditionHolder(rm); assertNull(custom.getMatchingCondition(this.exchange)); }
@Test public void getMatchingConditionWithEmptyConditions() throws Exception { RequestMethodsRequestCondition condition = new RequestMethodsRequestCondition(); for (RequestMethod method : RequestMethod.values()) { if (method != OPTIONS) { ServerWebExchange exchange = getExchange(method.name()); assertNotNull(condition.getMatchingCondition(exchange)); } } testNoMatch(condition, OPTIONS); }
private void registerLinksMapping() { PatternsRequestCondition patterns = new PatternsRequestCondition( pathPatternParser.parse(this.endpointMapping.getPath())); RequestMethodsRequestCondition methods = new RequestMethodsRequestCondition( RequestMethod.GET); ProducesRequestCondition produces = new ProducesRequestCondition( StringUtils.toStringArray(this.endpointMediaTypes.getProduced())); RequestMappingInfo mapping = new RequestMappingInfo(patterns, methods, null, null, null, produces, null); LinksHandler linksHandler = getLinksHandler(); registerMapping(mapping, linksHandler, ReflectionUtils .findMethod(linksHandler.getClass(), "links", ServerWebExchange.class)); }
/** * Returns a new instance with a union of the HTTP request methods * from "this" and the "other" instance. */ @Override public RequestMethodsRequestCondition combine(RequestMethodsRequestCondition other) { Set<RequestMethod> set = new LinkedHashSet<>(this.methods); set.addAll(other.methods); return new RequestMethodsRequestCondition(set); }
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); }
private RequestMethodsRequestCondition matchRequestMethod(String httpMethodValue) { HttpMethod httpMethod = HttpMethod.resolve(httpMethodValue); if (httpMethod != null) { for (RequestMethod method : getMethods()) { if (httpMethod.matches(method.name())) { return new RequestMethodsRequestCondition(method); } } if (httpMethod == HttpMethod.HEAD && getMethods().contains(RequestMethod.GET)) { return HEAD_CONDITION; } } return 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); } }