Search in sources :

Example 26 with Mono

use of reactor.core.publisher.Mono in project spring-framework by spring-projects.

the class ScriptTemplateView method renderInternal.

@Override
protected Mono<Void> renderInternal(Map<String, Object> model, MediaType contentType, ServerWebExchange exchange) {
    return Mono.defer(() -> {
        ServerHttpResponse response = exchange.getResponse();
        try {
            ScriptEngine engine = getEngine();
            Invocable invocable = (Invocable) engine;
            String url = getUrl();
            String template = getTemplate(url);
            Function<String, String> templateLoader = path -> {
                try {
                    return getTemplate(path);
                } catch (IOException ex) {
                    throw new IllegalStateException(ex);
                }
            };
            RenderingContext context = new RenderingContext(this.getApplicationContext(), this.locale, templateLoader, url);
            Object html;
            if (this.renderObject != null) {
                Object thiz = engine.eval(this.renderObject);
                html = invocable.invokeMethod(thiz, this.renderFunction, template, model, context);
            } else {
                html = invocable.invokeFunction(this.renderFunction, template, model, context);
            }
            byte[] bytes = String.valueOf(html).getBytes(StandardCharsets.UTF_8);
            DataBuffer buffer = response.bufferFactory().allocateBuffer(bytes.length).write(bytes);
            return response.writeWith(Mono.just(buffer));
        } catch (ScriptException ex) {
            throw new IllegalStateException("Failed to render script template", new StandardScriptEvalException(ex));
        } catch (Exception ex) {
            throw new IllegalStateException("Failed to render script template", ex);
        }
    });
}
Also used : ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) BeanFactoryUtils(org.springframework.beans.factory.BeanFactoryUtils) ApplicationContextException(org.springframework.context.ApplicationContextException) Function(java.util.function.Function) StandardScriptUtils(org.springframework.scripting.support.StandardScriptUtils) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Locale(java.util.Locale) Map(java.util.Map) ScriptException(javax.script.ScriptException) Resource(org.springframework.core.io.Resource) AbstractUrlBasedView(org.springframework.web.reactive.result.view.AbstractUrlBasedView) ResourceLoader(org.springframework.core.io.ResourceLoader) MediaType(org.springframework.http.MediaType) ObjectUtils(org.springframework.util.ObjectUtils) ScriptEngineManager(javax.script.ScriptEngineManager) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) BeansException(org.springframework.beans.BeansException) DataBuffer(org.springframework.core.io.buffer.DataBuffer) InputStreamReader(java.io.InputStreamReader) ApplicationContext(org.springframework.context.ApplicationContext) StandardCharsets(java.nio.charset.StandardCharsets) Invocable(javax.script.Invocable) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) ScriptEngine(javax.script.ScriptEngine) StandardScriptEvalException(org.springframework.scripting.support.StandardScriptEvalException) FileCopyUtils(org.springframework.util.FileCopyUtils) Assert(org.springframework.util.Assert) StringUtils(org.springframework.util.StringUtils) IOException(java.io.IOException) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) ScriptEngine(javax.script.ScriptEngine) ApplicationContextException(org.springframework.context.ApplicationContextException) ScriptException(javax.script.ScriptException) IOException(java.io.IOException) BeansException(org.springframework.beans.BeansException) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) StandardScriptEvalException(org.springframework.scripting.support.StandardScriptEvalException) Invocable(javax.script.Invocable) ScriptException(javax.script.ScriptException) StandardScriptEvalException(org.springframework.scripting.support.StandardScriptEvalException) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Example 27 with Mono

use of reactor.core.publisher.Mono in project spring-framework by spring-projects.

the class HttpEntityArgumentResolverTests method emptyBodyWithMono.

@Test
public void emptyBodyWithMono() throws Exception {
    ResolvableType type = httpEntityType(Mono.class, String.class);
    HttpEntity<Mono<String>> entity = resolveValueWithEmptyBody(type);
    StepVerifier.create(entity.getBody()).expectNextCount(0).expectComplete().verify();
}
Also used : Mono(reactor.core.publisher.Mono) ResolvableType(org.springframework.core.ResolvableType) Test(org.junit.Test)

Example 28 with Mono

use of reactor.core.publisher.Mono in project spring-framework by spring-projects.

the class ModelAttributeMethodArgumentResolverTests method validationErrorToMono.

@Test
@SuppressWarnings("unchecked")
public void validationErrorToMono() throws Exception {
    MethodParameter parameter = this.testMethod.annotNotPresent(ModelAttribute.class).arg(Mono.class, Foo.class);
    testValidationError(parameter, resolvedArgumentMono -> {
        Object value = resolvedArgumentMono.block(Duration.ofSeconds(5));
        assertNotNull(value);
        assertTrue(value instanceof Mono);
        return (Mono<?>) value;
    });
}
Also used : Mono(reactor.core.publisher.Mono) ModelAttribute(org.springframework.web.bind.annotation.ModelAttribute) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 29 with Mono

use of reactor.core.publisher.Mono in project spring-framework by spring-projects.

the class DispatcherHandlerErrorTests method requestBodyError.

@Test
public void requestBodyError() throws Exception {
    ServerWebExchange exchange = MockServerHttpRequest.post("/request-body").body(Mono.error(EXCEPTION)).toExchange();
    Mono<Void> publisher = this.dispatcherHandler.handle(exchange);
    StepVerifier.create(publisher).consumeErrorWith(error -> {
        assertThat(error, instanceOf(ServerWebInputException.class));
        assertSame(EXCEPTION, error.getCause());
    }).verify();
}
Also used : CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) RequestMappingHandlerMapping(org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping) StepVerifier(reactor.test.StepVerifier) RequestMappingHandlerAdapter(org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerAdapter) ServerWebInputException(org.springframework.web.server.ServerWebInputException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) CoreMatchers.startsWith(org.hamcrest.CoreMatchers.startsWith) Controller(org.springframework.stereotype.Controller) WebHandler(org.springframework.web.server.WebHandler) ServerWebExchange(org.springframework.web.server.ServerWebExchange) RequestBody(org.springframework.web.bind.annotation.RequestBody) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Assert.assertSame(org.junit.Assert.assertSame) Assert.assertThat(org.junit.Assert.assertThat) ResponseBodyResultHandler(org.springframework.web.reactive.result.method.annotation.ResponseBodyResultHandler) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) Duration(java.time.Duration) HeaderContentTypeResolver(org.springframework.web.reactive.accept.HeaderContentTypeResolver) APPLICATION_JSON(org.springframework.http.MediaType.APPLICATION_JSON) Before(org.junit.Before) ResponseStatusException(org.springframework.web.server.ResponseStatusException) ExceptionHandlingWebHandler(org.springframework.web.server.handler.ExceptionHandlingWebHandler) WebExceptionHandler(org.springframework.web.server.WebExceptionHandler) Publisher(org.reactivestreams.Publisher) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) NotAcceptableStatusException(org.springframework.web.server.NotAcceptableStatusException) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Configuration(org.springframework.context.annotation.Configuration) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) Matchers.is(org.hamcrest.Matchers.is) Bean(org.springframework.context.annotation.Bean) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Test(org.junit.Test)

Example 30 with Mono

use of reactor.core.publisher.Mono in project spring-framework by spring-projects.

the class DispatcherHandlerErrorTests method controllerThrowsException.

@Test
public void controllerThrowsException() throws Exception {
    ServerWebExchange exchange = MockServerHttpRequest.get("/raise-exception").toExchange();
    Mono<Void> publisher = this.dispatcherHandler.handle(exchange);
    StepVerifier.create(publisher).consumeErrorWith(error -> assertSame(EXCEPTION, error)).verify();
}
Also used : CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) RequestMappingHandlerMapping(org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping) StepVerifier(reactor.test.StepVerifier) RequestMappingHandlerAdapter(org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerAdapter) ServerWebInputException(org.springframework.web.server.ServerWebInputException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) CoreMatchers.startsWith(org.hamcrest.CoreMatchers.startsWith) Controller(org.springframework.stereotype.Controller) WebHandler(org.springframework.web.server.WebHandler) ServerWebExchange(org.springframework.web.server.ServerWebExchange) RequestBody(org.springframework.web.bind.annotation.RequestBody) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Assert.assertSame(org.junit.Assert.assertSame) Assert.assertThat(org.junit.Assert.assertThat) ResponseBodyResultHandler(org.springframework.web.reactive.result.method.annotation.ResponseBodyResultHandler) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) Duration(java.time.Duration) HeaderContentTypeResolver(org.springframework.web.reactive.accept.HeaderContentTypeResolver) APPLICATION_JSON(org.springframework.http.MediaType.APPLICATION_JSON) Before(org.junit.Before) ResponseStatusException(org.springframework.web.server.ResponseStatusException) ExceptionHandlingWebHandler(org.springframework.web.server.handler.ExceptionHandlingWebHandler) WebExceptionHandler(org.springframework.web.server.WebExceptionHandler) Publisher(org.reactivestreams.Publisher) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) NotAcceptableStatusException(org.springframework.web.server.NotAcceptableStatusException) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Configuration(org.springframework.context.annotation.Configuration) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) Matchers.is(org.hamcrest.Matchers.is) Bean(org.springframework.context.annotation.Bean) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Test(org.junit.Test)

Aggregations

Mono (reactor.core.publisher.Mono)40 Test (org.junit.Test)28 ServerWebExchange (org.springframework.web.server.ServerWebExchange)21 MockServerHttpRequest (org.springframework.mock.http.server.reactive.test.MockServerHttpRequest)18 HttpStatus (org.springframework.http.HttpStatus)17 StepVerifier (reactor.test.StepVerifier)17 List (java.util.List)16 Assert.assertEquals (org.junit.Assert.assertEquals)14 Collections (java.util.Collections)12 BindingContext (org.springframework.web.reactive.BindingContext)12 Matchers.is (org.hamcrest.Matchers.is)11 Assert.assertThat (org.junit.Assert.assertThat)11 MethodParameter (org.springframework.core.MethodParameter)11 Map (java.util.Map)10 ResolvableType (org.springframework.core.ResolvableType)10 ServerWebInputException (org.springframework.web.server.ServerWebInputException)10 Publisher (org.reactivestreams.Publisher)9 Assert (org.springframework.util.Assert)9 ResponseStatusException (org.springframework.web.server.ResponseStatusException)9 MediaType (org.springframework.http.MediaType)8