/** * Returns {@code true} if the request is a valid CORS one. */ public static boolean isCorsRequest(ServerHttpRequest request) { return (request.getHeaders().get(HttpHeaders.ORIGIN) != null); }
private Object createEntity(@Nullable Object body, Class<?> entityType, ServerHttpRequest request) { return (RequestEntity.class.equals(entityType) ? new RequestEntity<>(body, request.getHeaders(), request.getMethod(), request.getURI()) : new HttpEntity<>(body, request.getHeaders())); }
@Nullable private String getAcceptEncoding(ServerWebExchange exchange) { ServerHttpRequest request = exchange.getRequest(); String header = request.getHeaders().getFirst(HttpHeaders.ACCEPT_ENCODING); return (header != null ? header.toLowerCase() : null); }
@Override protected Object resolveNamedValue(String name, MethodParameter parameter, ServerWebExchange exchange) { List<String> headerValues = exchange.getRequest().getHeaders().get(name); Object result = null; if (headerValues != null) { result = (headerValues.size() == 1 ? headerValues.get(0) : headerValues); } return result; }
@Override public Object resolveArgumentValue( MethodParameter methodParameter, BindingContext context, ServerWebExchange exchange) { boolean isMultiValueMap = MultiValueMap.class.isAssignableFrom(methodParameter.getParameterType()); HttpHeaders headers = exchange.getRequest().getHeaders(); return (isMultiValueMap ? headers : headers.toSingleValueMap()); }
@Override protected boolean matchValue(ServerWebExchange exchange) { return (this.value != null && this.value.equals(exchange.getRequest().getHeaders().getFirst(this.name))); } }
@Override public List<String> resolveSessionIds(ServerWebExchange exchange) { HttpHeaders headers = exchange.getRequest().getHeaders(); return headers.getOrDefault(getHeaderName(), Collections.emptyList()); }
/** * On a pre-flight request match to the would-be, actual request. * Hence empty conditions is a match, otherwise try to match to the HTTP * method in the "Access-Control-Request-Method" header. */ private RequestMethodsRequestCondition matchPreFlight(ServerHttpRequest request) { if (getMethods().isEmpty()) { return this; } HttpMethod expectedMethod = request.getHeaders().getAccessControlRequestMethod(); return matchRequestMethod(expectedMethod); }
/** * Returns {@code true} if the request is a valid CORS pre-flight one. */ public static boolean isPreFlightRequest(ServerHttpRequest request) { return (request.getMethod() == HttpMethod.OPTIONS && isCorsRequest(request) && request.getHeaders().get(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD) != null); }
private HandshakeInfo createHandshakeInfo(ServerWebExchange exchange, ServerHttpRequest request, @Nullable String protocol, Map<String, Object> attributes) { URI uri = request.getURI(); // Copy request headers, as they might be pooled and recycled by // the server implementation once the handshake HTTP exchange is done. HttpHeaders headers = new HttpHeaders(); headers.addAll(request.getHeaders()); Mono<Principal> principal = exchange.getPrincipal(); String logPrefix = exchange.getLogPrefix(); InetSocketAddress remoteAddress = request.getRemoteAddress(); return new HandshakeInfo(uri, headers, principal, protocol, remoteAddress, attributes, logPrefix); }
@Override public LocaleContext resolveLocaleContext(ServerWebExchange exchange) { List<Locale> requestLocales = null; try { requestLocales = exchange.getRequest().getHeaders().getAcceptLanguageAsLocales(); } catch (IllegalArgumentException ex) { // Invalid Accept-Language header: treat as empty for matching purposes } return new SimpleLocaleContext(resolveSupportedLocale(requestLocales)); }
public DefaultServerHttpRequestBuilder(ServerHttpRequest original) { Assert.notNull(original, "ServerHttpRequest is required"); this.uri = original.getURI(); this.httpMethodValue = original.getMethodValue(); this.body = original.getBody(); this.httpHeaders = HttpHeaders.writableHttpHeaders(original.getHeaders()); this.cookies = new LinkedMultiValueMap<>(original.getCookies().size()); copyMultiValueMap(original.getCookies(), this.cookies); this.originalRequest = original; }
@SuppressWarnings("unchecked") private <T> HttpEntity<T> resolveValueWithEmptyBody(ResolvableType type) { ServerWebExchange exchange = MockServerWebExchange.from(post("/path")); MethodParameter param = this.testMethod.arg(type); Mono<Object> result = this.resolver.resolveArgument(param, new BindingContext(), exchange); HttpEntity<String> httpEntity = (HttpEntity<String>) result.block(Duration.ofSeconds(5)); assertEquals(exchange.getRequest().getHeaders(), httpEntity.getHeaders()); return (HttpEntity<T>) httpEntity; }
@Test public void httpEntityWithCompletableFutureBody() throws Exception { ServerWebExchange exchange = postExchange("line1"); ResolvableType type = httpEntityType(CompletableFuture.class, String.class); HttpEntity<CompletableFuture<String>> httpEntity = resolveValue(exchange, type); assertEquals(exchange.getRequest().getHeaders(), httpEntity.getHeaders()); assertEquals("line1", httpEntity.getBody().get()); }
@Test public void httpEntityWithMonoBody() throws Exception { ServerWebExchange exchange = postExchange("line1"); ResolvableType type = httpEntityType(Mono.class, String.class); HttpEntity<Mono<String>> httpEntity = resolveValue(exchange, type); assertEquals(exchange.getRequest().getHeaders(), httpEntity.getHeaders()); assertEquals("line1", httpEntity.getBody().block()); }
@Test public void httpEntityWithStringBody() throws Exception { ServerWebExchange exchange = postExchange("line1"); ResolvableType type = httpEntityType(String.class); HttpEntity<String> httpEntity = resolveValue(exchange, type); assertEquals(exchange.getRequest().getHeaders(), httpEntity.getHeaders()); assertEquals("line1", httpEntity.getBody()); }
@Test public void httpEntityWithRxJava2SingleBody() throws Exception { ServerWebExchange exchange = postExchange("line1"); ResolvableType type = httpEntityType(io.reactivex.Single.class, String.class); HttpEntity<io.reactivex.Single<String>> httpEntity = resolveValue(exchange, type); assertEquals(exchange.getRequest().getHeaders(), httpEntity.getHeaders()); assertEquals("line1", httpEntity.getBody().blockingGet()); }
@Test public void httpEntityWithRxJava2MaybeBody() throws Exception { ServerWebExchange exchange = postExchange("line1"); ResolvableType type = httpEntityType(Maybe.class, String.class); HttpEntity<Maybe<String>> httpEntity = resolveValue(exchange, type); assertEquals(exchange.getRequest().getHeaders(), httpEntity.getHeaders()); assertEquals("line1", httpEntity.getBody().blockingGet()); }
@Test public void httpEntityWithSingleBody() throws Exception { ServerWebExchange exchange = postExchange("line1"); ResolvableType type = httpEntityType(Single.class, String.class); HttpEntity<Single<String>> httpEntity = resolveValue(exchange, type); assertEquals(exchange.getRequest().getHeaders(), httpEntity.getHeaders()); assertEquals("line1", httpEntity.getBody().toBlocking().value()); }
@Test public void requestEntity() throws Exception { ServerWebExchange exchange = postExchange("line1"); ResolvableType type = forClassWithGenerics(RequestEntity.class, String.class); RequestEntity<String> requestEntity = resolveValue(exchange, type); assertEquals(exchange.getRequest().getMethod(), requestEntity.getMethod()); assertEquals(exchange.getRequest().getURI(), requestEntity.getUrl()); assertEquals(exchange.getRequest().getHeaders(), requestEntity.getHeaders()); assertEquals("line1", requestEntity.getBody()); }