Search in sources :

Example 6 with ServerWebExchange

use of org.springframework.web.server.ServerWebExchange in project spring-framework by spring-projects.

the class DefaultCorsProcessorTests method actualRequestCredentials.

@Test
public void actualRequestCredentials() throws Exception {
    ServerWebExchange exchange = actualRequest();
    this.conf.addAllowedOrigin("http://domain1.com");
    this.conf.addAllowedOrigin("http://domain2.com");
    this.conf.addAllowedOrigin("http://domain3.com");
    this.conf.setAllowCredentials(true);
    this.processor.processRequest(this.conf, exchange);
    ServerHttpResponse response = exchange.getResponse();
    assertTrue(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
    assertEquals("http://domain2.com", response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_ORIGIN));
    assertTrue(response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS));
    assertEquals("true", response.getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS));
    assertNull(response.getStatusCode());
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) Test(org.junit.Test)

Example 7 with ServerWebExchange

use of org.springframework.web.server.ServerWebExchange in project spring-framework by spring-projects.

the class DefaultCorsProcessorTests method preflightRequestValidRequestAndConfig.

@Test
public void preflightRequestValidRequestAndConfig() throws Exception {
    ServerWebExchange exchange = preFlightRequest().header(ACCESS_CONTROL_REQUEST_METHOD, "GET").header(ACCESS_CONTROL_REQUEST_HEADERS, "Header1").toExchange();
    this.conf.addAllowedOrigin("*");
    this.conf.addAllowedMethod("GET");
    this.conf.addAllowedMethod("PUT");
    this.conf.addAllowedHeader("header1");
    this.conf.addAllowedHeader("header2");
    this.processor.processRequest(this.conf, exchange);
    ServerHttpResponse response = exchange.getResponse();
    assertTrue(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
    assertEquals("*", response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_ORIGIN));
    assertTrue(response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS));
    assertEquals("GET,PUT", response.getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS));
    assertFalse(response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_MAX_AGE));
    assertNull(response.getStatusCode());
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) Test(org.junit.Test)

Example 8 with ServerWebExchange

use of org.springframework.web.server.ServerWebExchange in project spring-framework by spring-projects.

the class UrlBasedCorsConfigurationSourceTests method registerAndMatch.

@Test
public void registerAndMatch() {
    CorsConfiguration config = new CorsConfiguration();
    this.configSource.registerCorsConfiguration("/bar/**", config);
    ServerWebExchange exchange = MockServerHttpRequest.get("/foo/test.html").toExchange();
    assertNull(this.configSource.getCorsConfiguration(exchange));
    exchange = MockServerHttpRequest.get("/bar/test.html").toExchange();
    assertEquals(config, this.configSource.getCorsConfiguration(exchange));
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) CorsConfiguration(org.springframework.web.cors.CorsConfiguration) Test(org.junit.Test)

Example 9 with ServerWebExchange

use of org.springframework.web.server.ServerWebExchange in project spring-framework by spring-projects.

the class ModelAttributeMethodArgumentResolver method resolveArgument.

@Override
public Mono<Object> resolveArgument(MethodParameter parameter, BindingContext context, ServerWebExchange exchange) {
    ResolvableType type = ResolvableType.forMethodParameter(parameter);
    ReactiveAdapter adapter = getAdapterRegistry().getAdapter(type.resolve());
    ResolvableType valueType = (adapter != null ? type.getGeneric(0) : type);
    Assert.state(adapter == null || !adapter.isMultiValue(), getClass().getSimpleName() + " doesn't support multi-value reactive type wrapper: " + parameter.getGenericParameterType());
    String name = getAttributeName(valueType, parameter);
    Mono<?> valueMono = getAttributeMono(name, valueType, context.getModel());
    Map<String, Object> model = context.getModel().asMap();
    MonoProcessor<BindingResult> bindingResultMono = MonoProcessor.create();
    model.put(BindingResult.MODEL_KEY_PREFIX + name, bindingResultMono);
    return valueMono.then(value -> {
        WebExchangeDataBinder binder = context.createDataBinder(exchange, value, name);
        return binder.bind(exchange).doOnError(bindingResultMono::onError).doOnSuccess(aVoid -> {
            validateIfApplicable(binder, parameter);
            BindingResult errors = binder.getBindingResult();
            model.put(BindingResult.MODEL_KEY_PREFIX + name, errors);
            model.put(name, value);
            bindingResultMono.onNext(errors);
        }).then(Mono.fromCallable(() -> {
            BindingResult errors = binder.getBindingResult();
            if (adapter != null) {
                return adapter.fromPublisher(errors.hasErrors() ? Mono.error(new WebExchangeBindException(parameter, errors)) : valueMono);
            } else {
                if (errors.hasErrors() && !hasErrorsArgument(parameter)) {
                    throw new WebExchangeBindException(parameter, errors);
                }
                return value;
            }
        }));
    });
}
Also used : ReactiveAdapter(org.springframework.core.ReactiveAdapter) Errors(org.springframework.validation.Errors) Validated(org.springframework.validation.annotation.Validated) ClassUtils(org.springframework.util.ClassUtils) AnnotationUtils(org.springframework.core.annotation.AnnotationUtils) WebExchangeDataBinder(org.springframework.web.bind.support.WebExchangeDataBinder) MonoProcessor(reactor.core.publisher.MonoProcessor) Mono(reactor.core.publisher.Mono) BindingResult(org.springframework.validation.BindingResult) BindingContext(org.springframework.web.reactive.BindingContext) HandlerMethodArgumentResolver(org.springframework.web.reactive.result.method.HandlerMethodArgumentResolver) ServerWebExchange(org.springframework.web.server.ServerWebExchange) HandlerMethodArgumentResolverSupport(org.springframework.web.reactive.result.method.HandlerMethodArgumentResolverSupport) Model(org.springframework.ui.Model) ModelAttribute(org.springframework.web.bind.annotation.ModelAttribute) Map(java.util.Map) MethodParameter(org.springframework.core.MethodParameter) Annotation(java.lang.annotation.Annotation) ResolvableType(org.springframework.core.ResolvableType) ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) WebExchangeBindException(org.springframework.web.bind.support.WebExchangeBindException) BeanUtils(org.springframework.beans.BeanUtils) Assert(org.springframework.util.Assert) StringUtils(org.springframework.util.StringUtils) BindingResult(org.springframework.validation.BindingResult) ResolvableType(org.springframework.core.ResolvableType) WebExchangeDataBinder(org.springframework.web.bind.support.WebExchangeDataBinder) WebExchangeBindException(org.springframework.web.bind.support.WebExchangeBindException) ReactiveAdapter(org.springframework.core.ReactiveAdapter)

Example 10 with ServerWebExchange

use of org.springframework.web.server.ServerWebExchange in project spring-framework by spring-projects.

the class RequestMappingHandlerAdapter method handle.

@Override
public Mono<HandlerResult> handle(ServerWebExchange exchange, Object handler) {
    Assert.notNull(handler, "Expected handler");
    HandlerMethod handlerMethod = (HandlerMethod) handler;
    BindingContext bindingContext = new InitBinderBindingContext(getWebBindingInitializer(), getBinderMethods(handlerMethod));
    return this.modelInitializer.initModel(bindingContext, getAttributeMethods(handlerMethod), exchange).then(() -> {
        Function<Throwable, Mono<HandlerResult>> exceptionHandler = ex -> handleException(exchange, handlerMethod, bindingContext, ex);
        InvocableHandlerMethod invocable = new InvocableHandlerMethod(handlerMethod);
        invocable.setArgumentResolvers(getArgumentResolvers());
        return invocable.invoke(exchange, bindingContext).doOnNext(result -> result.setExceptionHandler(exceptionHandler)).otherwise(exceptionHandler);
    });
}
Also used : InvocableHandlerMethod(org.springframework.web.reactive.result.method.InvocableHandlerMethod) HttpMessageReader(org.springframework.http.codec.HttpMessageReader) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) BindingContext(org.springframework.web.reactive.BindingContext) MethodIntrospector.selectMethods(org.springframework.core.MethodIntrospector.selectMethods) Function(java.util.function.Function) HandlerMethodArgumentResolver(org.springframework.web.reactive.result.method.HandlerMethodArgumentResolver) InitializingBean(org.springframework.beans.factory.InitializingBean) ArrayList(java.util.ArrayList) ServerWebExchange(org.springframework.web.server.ServerWebExchange) LinkedHashMap(java.util.LinkedHashMap) HandlerMethod(org.springframework.web.method.HandlerMethod) ModelAttribute(org.springframework.web.bind.annotation.ModelAttribute) ExceptionHandlerMethodResolver(org.springframework.web.method.annotation.ExceptionHandlerMethodResolver) Map(java.util.Map) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) DataBufferDecoder(org.springframework.core.codec.DataBufferDecoder) Method(java.lang.reflect.Method) ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) HandlerAdapter(org.springframework.web.reactive.HandlerAdapter) WebBindingInitializer(org.springframework.web.bind.support.WebBindingInitializer) StringDecoder(org.springframework.core.codec.StringDecoder) AnnotationUtils(org.springframework.core.annotation.AnnotationUtils) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) SyncHandlerMethodArgumentResolver(org.springframework.web.reactive.result.method.SyncHandlerMethodArgumentResolver) Mono(reactor.core.publisher.Mono) ByteBufferDecoder(org.springframework.core.codec.ByteBufferDecoder) ApplicationContext(org.springframework.context.ApplicationContext) ByteArrayDecoder(org.springframework.core.codec.ByteArrayDecoder) HandlerResult(org.springframework.web.reactive.HandlerResult) DecoderHttpMessageReader(org.springframework.http.codec.DecoderHttpMessageReader) Objects(java.util.Objects) List(java.util.List) ReflectionUtils(org.springframework.util.ReflectionUtils) Optional(java.util.Optional) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) ControllerAdviceBean(org.springframework.web.method.ControllerAdviceBean) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) InitBinder(org.springframework.web.bind.annotation.InitBinder) SyncInvocableHandlerMethod(org.springframework.web.reactive.result.method.SyncInvocableHandlerMethod) ApplicationContextAware(org.springframework.context.ApplicationContextAware) AnnotationAwareOrderComparator(org.springframework.core.annotation.AnnotationAwareOrderComparator) Assert(org.springframework.util.Assert) InvocableHandlerMethod(org.springframework.web.reactive.result.method.InvocableHandlerMethod) SyncInvocableHandlerMethod(org.springframework.web.reactive.result.method.SyncInvocableHandlerMethod) Mono(reactor.core.publisher.Mono) BindingContext(org.springframework.web.reactive.BindingContext) InvocableHandlerMethod(org.springframework.web.reactive.result.method.InvocableHandlerMethod) HandlerMethod(org.springframework.web.method.HandlerMethod) SyncInvocableHandlerMethod(org.springframework.web.reactive.result.method.SyncInvocableHandlerMethod)

Aggregations

ServerWebExchange (org.springframework.web.server.ServerWebExchange)158 Test (org.junit.Test)138 MockServerWebExchange (org.springframework.mock.http.server.reactive.test.MockServerWebExchange)53 Mono (reactor.core.publisher.Mono)22 ServerHttpResponse (org.springframework.http.server.reactive.ServerHttpResponse)20 MediaType (org.springframework.http.MediaType)18 BindingContext (org.springframework.web.reactive.BindingContext)17 MethodParameter (org.springframework.core.MethodParameter)15 List (java.util.List)14 ResolvableType (org.springframework.core.ResolvableType)14 Map (java.util.Map)12 Collections (java.util.Collections)11 HttpStatus (org.springframework.http.HttpStatus)11 StepVerifier (reactor.test.StepVerifier)11 Assert.assertEquals (org.junit.Assert.assertEquals)10 MockServerHttpRequest (org.springframework.mock.http.server.reactive.test.MockServerHttpRequest)10 ResponseStatusException (org.springframework.web.server.ResponseStatusException)9 ServerWebInputException (org.springframework.web.server.ServerWebInputException)9 ReactiveAdapterRegistry (org.springframework.core.ReactiveAdapterRegistry)8 NotAcceptableStatusException (org.springframework.web.server.NotAcceptableStatusException)8