Search in sources :

Example 1 with RequestedContentTypeResolver

use of org.springframework.web.reactive.accept.RequestedContentTypeResolver in project spring-framework by spring-projects.

the class ViewResolutionResultHandlerTests method resultHandler.

private ViewResolutionResultHandler resultHandler(List<View> defaultViews, ViewResolver... resolvers) {
    List<ViewResolver> resolverList = Arrays.asList(resolvers);
    RequestedContentTypeResolver contentTypeResolver = new HeaderContentTypeResolver();
    ViewResolutionResultHandler handler = new ViewResolutionResultHandler(resolverList, contentTypeResolver);
    handler.setDefaultViews(defaultViews);
    return handler;
}
Also used : HeaderContentTypeResolver(org.springframework.web.reactive.accept.HeaderContentTypeResolver) RequestedContentTypeResolver(org.springframework.web.reactive.accept.RequestedContentTypeResolver)

Example 2 with RequestedContentTypeResolver

use of org.springframework.web.reactive.accept.RequestedContentTypeResolver in project spring-framework by spring-projects.

the class ProducesRequestConditionTests method matchAndCompare.

// gh-22853
@Test
public void matchAndCompare() {
    RequestedContentTypeResolverBuilder builder = new RequestedContentTypeResolverBuilder();
    builder.headerResolver();
    builder.fixedResolver(MediaType.TEXT_HTML);
    RequestedContentTypeResolver resolver = builder.build();
    ProducesRequestCondition none = new ProducesRequestCondition(new String[0], null, resolver);
    ProducesRequestCondition html = new ProducesRequestCondition(new String[] { "text/html" }, null, resolver);
    MockServerWebExchange exchange = MockServerWebExchange.from(get("/").header("Accept", "*/*"));
    ProducesRequestCondition noneMatch = none.getMatchingCondition(exchange);
    ProducesRequestCondition htmlMatch = html.getMatchingCondition(exchange);
    assertThat(noneMatch.compareTo(htmlMatch, exchange)).isEqualTo(1);
}
Also used : MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) RequestedContentTypeResolver(org.springframework.web.reactive.accept.RequestedContentTypeResolver) RequestedContentTypeResolverBuilder(org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder) Test(org.junit.jupiter.api.Test)

Example 3 with RequestedContentTypeResolver

use of org.springframework.web.reactive.accept.RequestedContentTypeResolver in project spring-framework by spring-projects.

the class MessageWriterResultHandlerTests method initResultHandler.

private AbstractMessageWriterResultHandler initResultHandler(HttpMessageWriter<?>... writers) {
    List<HttpMessageWriter<?>> writerList;
    if (ObjectUtils.isEmpty(writers)) {
        writerList = new ArrayList<>();
        writerList.add(new EncoderHttpMessageWriter<>(new ByteBufferEncoder()));
        writerList.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
        writerList.add(new ResourceHttpMessageWriter());
        writerList.add(new EncoderHttpMessageWriter<>(new Jaxb2XmlEncoder()));
        writerList.add(new EncoderHttpMessageWriter<>(new Jackson2JsonEncoder()));
    } else {
        writerList = Arrays.asList(writers);
    }
    RequestedContentTypeResolver resolver = new RequestedContentTypeResolverBuilder().build();
    return new AbstractMessageWriterResultHandler(writerList, resolver) {
    };
}
Also used : ByteBufferEncoder(org.springframework.core.codec.ByteBufferEncoder) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) RequestedContentTypeResolver(org.springframework.web.reactive.accept.RequestedContentTypeResolver) Jaxb2XmlEncoder(org.springframework.http.codec.xml.Jaxb2XmlEncoder) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) RequestedContentTypeResolverBuilder(org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder)

Example 4 with RequestedContentTypeResolver

use of org.springframework.web.reactive.accept.RequestedContentTypeResolver in project spring-framework by spring-projects.

the class ResponseBodyResultHandlerTests method setup.

@BeforeEach
public void setup() throws Exception {
    List<HttpMessageWriter<?>> writerList = new ArrayList<>(5);
    writerList.add(new EncoderHttpMessageWriter<>(new ByteBufferEncoder()));
    writerList.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
    writerList.add(new ResourceHttpMessageWriter());
    writerList.add(new EncoderHttpMessageWriter<>(new Jaxb2XmlEncoder()));
    writerList.add(new EncoderHttpMessageWriter<>(new Jackson2JsonEncoder()));
    RequestedContentTypeResolver resolver = new RequestedContentTypeResolverBuilder().build();
    this.resultHandler = new ResponseBodyResultHandler(writerList, resolver);
}
Also used : ByteBufferEncoder(org.springframework.core.codec.ByteBufferEncoder) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) ArrayList(java.util.ArrayList) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) RequestedContentTypeResolver(org.springframework.web.reactive.accept.RequestedContentTypeResolver) Jaxb2XmlEncoder(org.springframework.http.codec.xml.Jaxb2XmlEncoder) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) RequestedContentTypeResolverBuilder(org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 5 with RequestedContentTypeResolver

use of org.springframework.web.reactive.accept.RequestedContentTypeResolver in project spring-framework by spring-projects.

the class WebFluxConfigurationSupportTests method requestMappingHandlerMapping.

@Test
public void requestMappingHandlerMapping() {
    ApplicationContext context = loadConfig(WebFluxConfig.class);
    final Field field = ReflectionUtils.findField(PathPatternParser.class, "matchOptionalTrailingSeparator");
    ReflectionUtils.makeAccessible(field);
    String name = "requestMappingHandlerMapping";
    RequestMappingHandlerMapping mapping = context.getBean(name, RequestMappingHandlerMapping.class);
    assertThat(mapping).isNotNull();
    assertThat(mapping.getOrder()).isEqualTo(0);
    PathPatternParser patternParser = mapping.getPathPatternParser();
    assertThat(patternParser).isNotNull();
    boolean matchOptionalTrailingSlash = (boolean) ReflectionUtils.getField(field, patternParser);
    assertThat(matchOptionalTrailingSlash).isTrue();
    name = "webFluxContentTypeResolver";
    RequestedContentTypeResolver resolver = context.getBean(name, RequestedContentTypeResolver.class);
    assertThat(mapping.getContentTypeResolver()).isSameAs(resolver);
    ServerWebExchange exchange = MockServerWebExchange.from(get("/path").accept(MediaType.APPLICATION_JSON));
    assertThat(resolver.resolveMediaTypes(exchange)).isEqualTo(Collections.singletonList(MediaType.APPLICATION_JSON));
}
Also used : Field(java.lang.reflect.Field) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) ServerWebExchange(org.springframework.web.server.ServerWebExchange) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) PathPatternParser(org.springframework.web.util.pattern.PathPatternParser) RequestMappingHandlerMapping(org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping) RequestedContentTypeResolver(org.springframework.web.reactive.accept.RequestedContentTypeResolver) Test(org.junit.jupiter.api.Test)

Aggregations

RequestedContentTypeResolver (org.springframework.web.reactive.accept.RequestedContentTypeResolver)8 HttpMessageWriter (org.springframework.http.codec.HttpMessageWriter)5 Test (org.junit.jupiter.api.Test)4 RequestedContentTypeResolverBuilder (org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder)4 ApplicationContext (org.springframework.context.ApplicationContext)3 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)3 ByteBufferEncoder (org.springframework.core.codec.ByteBufferEncoder)3 EncoderHttpMessageWriter (org.springframework.http.codec.EncoderHttpMessageWriter)3 ResourceHttpMessageWriter (org.springframework.http.codec.ResourceHttpMessageWriter)3 Jackson2JsonEncoder (org.springframework.http.codec.json.Jackson2JsonEncoder)3 Jaxb2XmlEncoder (org.springframework.http.codec.xml.Jaxb2XmlEncoder)3 Message (com.google.protobuf.Message)2 ByteBuffer (java.nio.ByteBuffer)2 Resource (org.springframework.core.io.Resource)2 MediaType (org.springframework.http.MediaType)2 MockServerWebExchange (org.springframework.web.testfixture.server.MockServerWebExchange)2 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 HeaderContentTypeResolver (org.springframework.web.reactive.accept.HeaderContentTypeResolver)1