Search in sources :

Example 1 with RequestedContentTypeResolverBuilder

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

the class WebFluxConfigurationSupport method webFluxContentTypeResolver.

@Bean
public CompositeContentTypeResolver webFluxContentTypeResolver() {
    RequestedContentTypeResolverBuilder builder = new RequestedContentTypeResolverBuilder();
    builder.mediaTypes(getDefaultMediaTypeMappings());
    configureContentTypeResolver(builder);
    return builder.build();
}
Also used : RequestedContentTypeResolverBuilder(org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder) Bean(org.springframework.context.annotation.Bean)

Example 2 with RequestedContentTypeResolverBuilder

use of org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder 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<>(new CharSequenceEncoder()));
        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) CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) Jaxb2XmlEncoder(org.springframework.http.codec.xml.Jaxb2XmlEncoder) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) RequestedContentTypeResolverBuilder(org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder)

Example 3 with RequestedContentTypeResolverBuilder

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

the class ResponseBodyResultHandlerTests method setup.

@Before
public void setup() throws Exception {
    List<HttpMessageWriter<?>> writerList = new ArrayList<>(5);
    writerList.add(new EncoderHttpMessageWriter<>(new ByteBufferEncoder()));
    writerList.add(new EncoderHttpMessageWriter<>(new CharSequenceEncoder()));
    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) CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) Jaxb2XmlEncoder(org.springframework.http.codec.xml.Jaxb2XmlEncoder) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) RequestedContentTypeResolverBuilder(org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder) Before(org.junit.Before)

Example 4 with RequestedContentTypeResolverBuilder

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

the class ResourceWebHandlerTests method getMediaTypeWithFavorPathExtensionOff.

// SPR-14577
@Test
public void getMediaTypeWithFavorPathExtensionOff() throws Exception {
    CompositeContentTypeResolver contentTypeResolver = new RequestedContentTypeResolverBuilder().favorPathExtension(false).build();
    List<Resource> paths = Collections.singletonList(new ClassPathResource("test/", getClass()));
    ResourceWebHandler handler = new ResourceWebHandler();
    handler.setLocations(paths);
    handler.setContentTypeResolver(contentTypeResolver);
    handler.afterPropertiesSet();
    handler.afterSingletonsInstantiated();
    MockServerWebExchange exchange = MockServerHttpRequest.get("").header("Accept", "application/json,text/plain,*/*").toExchange();
    setPathWithinHandlerMapping(exchange, "foo.html");
    handler.handle(exchange).block(TIMEOUT);
    assertEquals(MediaType.TEXT_HTML, exchange.getResponse().getHeaders().getContentType());
}
Also used : UrlResource(org.springframework.core.io.UrlResource) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) ClassPathResource(org.springframework.core.io.ClassPathResource) CompositeContentTypeResolver(org.springframework.web.reactive.accept.CompositeContentTypeResolver) RequestedContentTypeResolverBuilder(org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder) Test(org.junit.Test)

Example 5 with RequestedContentTypeResolverBuilder

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

the class ResourceWebHandlerTests method getResourceWithRegisteredMediaType.

// SPR-13658
@Test
public void getResourceWithRegisteredMediaType() throws Exception {
    CompositeContentTypeResolver contentTypeResolver = new RequestedContentTypeResolverBuilder().mediaType("css", new MediaType("foo", "bar")).build();
    List<Resource> paths = Collections.singletonList(new ClassPathResource("test/", getClass()));
    ResourceWebHandler handler = new ResourceWebHandler();
    handler.setLocations(paths);
    handler.setContentTypeResolver(contentTypeResolver);
    handler.afterPropertiesSet();
    handler.afterSingletonsInstantiated();
    MockServerWebExchange exchange = MockServerHttpRequest.get("").toExchange();
    setPathWithinHandlerMapping(exchange, "foo.css");
    handler.handle(exchange).block(TIMEOUT);
    assertEquals(MediaType.parseMediaType("foo/bar"), exchange.getResponse().getHeaders().getContentType());
    assertResponseBody(exchange, "h1 { color:red; }");
}
Also used : UrlResource(org.springframework.core.io.UrlResource) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) MediaType(org.springframework.http.MediaType) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) ClassPathResource(org.springframework.core.io.ClassPathResource) CompositeContentTypeResolver(org.springframework.web.reactive.accept.CompositeContentTypeResolver) RequestedContentTypeResolverBuilder(org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder) Test(org.junit.Test)

Aggregations

RequestedContentTypeResolverBuilder (org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder)6 ByteBufferEncoder (org.springframework.core.codec.ByteBufferEncoder)3 CharSequenceEncoder (org.springframework.core.codec.CharSequenceEncoder)3 EncoderHttpMessageWriter (org.springframework.http.codec.EncoderHttpMessageWriter)3 HttpMessageWriter (org.springframework.http.codec.HttpMessageWriter)3 ResourceHttpMessageWriter (org.springframework.http.codec.ResourceHttpMessageWriter)3 Jackson2JsonEncoder (org.springframework.http.codec.json.Jackson2JsonEncoder)3 Jaxb2XmlEncoder (org.springframework.http.codec.xml.Jaxb2XmlEncoder)3 RequestedContentTypeResolver (org.springframework.web.reactive.accept.RequestedContentTypeResolver)3 Test (org.junit.Test)2 ClassPathResource (org.springframework.core.io.ClassPathResource)2 Resource (org.springframework.core.io.Resource)2 UrlResource (org.springframework.core.io.UrlResource)2 MockServerWebExchange (org.springframework.mock.http.server.reactive.test.MockServerWebExchange)2 CompositeContentTypeResolver (org.springframework.web.reactive.accept.CompositeContentTypeResolver)2 ArrayList (java.util.ArrayList)1 Before (org.junit.Before)1 Bean (org.springframework.context.annotation.Bean)1 MediaType (org.springframework.http.MediaType)1