Search in sources :

Example 1 with MimeType

use of org.springframework.util.MimeType in project spring-framework by spring-projects.

the class MappingJackson2MessageConverterTests method mimetypeParametrizedConstructor.

// SPR-12724
@Test
public void mimetypeParametrizedConstructor() {
    MimeType mimetype = new MimeType("application", "xml", StandardCharsets.UTF_8);
    MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter(mimetype);
    assertThat(converter.getSupportedMimeTypes(), contains(mimetype));
    assertFalse(converter.getObjectMapper().getDeserializationConfig().isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
}
Also used : MimeType(org.springframework.util.MimeType) Test(org.junit.Test)

Example 2 with MimeType

use of org.springframework.util.MimeType in project spring-framework by spring-projects.

the class MappingJackson2MessageConverterTests method toMessageUtf16String.

@Test
public void toMessageUtf16String() {
    MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
    converter.setSerializedPayloadClass(String.class);
    MimeType contentType = new MimeType("application", "json", StandardCharsets.UTF_16BE);
    Map<String, Object> map = new HashMap<>();
    map.put(MessageHeaders.CONTENT_TYPE, contentType);
    MessageHeaders headers = new MessageHeaders(map);
    String payload = "Héllo Wörld";
    Message<?> message = converter.toMessage(payload, headers);
    assertEquals("\"" + payload + "\"", message.getPayload());
    assertEquals(contentType, message.getHeaders().get(MessageHeaders.CONTENT_TYPE));
}
Also used : HashMap(java.util.HashMap) MessageHeaders(org.springframework.messaging.MessageHeaders) MimeType(org.springframework.util.MimeType) Test(org.junit.Test)

Example 3 with MimeType

use of org.springframework.util.MimeType in project spring-framework by spring-projects.

the class DefaultStompSessionTests method handleErrorFrame.

@Test
public void handleErrorFrame() throws Exception {
    StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.ERROR);
    accessor.setContentType(new MimeType("text", "plain", StandardCharsets.UTF_8));
    accessor.addNativeHeader("foo", "bar");
    accessor.setLeaveMutable(true);
    String payload = "Oops";
    StompHeaders stompHeaders = StompHeaders.readOnlyStompHeaders(accessor.getNativeHeaders());
    when(this.sessionHandler.getPayloadType(stompHeaders)).thenReturn(String.class);
    this.session.handleMessage(MessageBuilder.createMessage(payload.getBytes(StandardCharsets.UTF_8), accessor.getMessageHeaders()));
    verify(this.sessionHandler).getPayloadType(stompHeaders);
    verify(this.sessionHandler).handleFrame(stompHeaders, payload);
    verifyNoMoreInteractions(this.sessionHandler);
}
Also used : MimeType(org.springframework.util.MimeType) Test(org.junit.Test)

Example 4 with MimeType

use of org.springframework.util.MimeType in project spring-framework by spring-projects.

the class Jackson2JsonEncoder method encode.

@Override
public Flux<DataBuffer> encode(Publisher<?> inputStream, DataBufferFactory bufferFactory, ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) {
    Assert.notNull(inputStream, "'inputStream' must not be null");
    Assert.notNull(bufferFactory, "'bufferFactory' must not be null");
    Assert.notNull(elementType, "'elementType' must not be null");
    if (inputStream instanceof Mono) {
        return Flux.from(inputStream).map(value -> encodeValue(value, bufferFactory, elementType, hints));
    } else if (APPLICATION_STREAM_JSON.isCompatibleWith(mimeType)) {
        return Flux.from(inputStream).map(value -> {
            DataBuffer buffer = encodeValue(value, bufferFactory, elementType, hints);
            buffer.write(new byte[] { '\n' });
            return buffer;
        });
    }
    ResolvableType listType = ResolvableType.forClassWithGenerics(List.class, elementType);
    return Flux.from(inputStream).collectList().map(list -> encodeValue(list, bufferFactory, listType, hints)).flux();
}
Also used : SerializationConfig(com.fasterxml.jackson.databind.SerializationConfig) CodecException(org.springframework.core.codec.CodecException) MimeType(org.springframework.util.MimeType) TypeFactory(com.fasterxml.jackson.databind.type.TypeFactory) Map(java.util.Map) DefaultPrettyPrinter(com.fasterxml.jackson.core.util.DefaultPrettyPrinter) ServerSentEventHttpMessageWriter(org.springframework.http.codec.ServerSentEventHttpMessageWriter) JavaType(com.fasterxml.jackson.databind.JavaType) ResolvableType(org.springframework.core.ResolvableType) Encoder(org.springframework.core.codec.Encoder) OutputStream(java.io.OutputStream) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) Publisher(org.reactivestreams.Publisher) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) DefaultIndenter(com.fasterxml.jackson.core.util.DefaultIndenter) DataBuffer(org.springframework.core.io.buffer.DataBuffer) APPLICATION_STREAM_JSON(org.springframework.http.MediaType.APPLICATION_STREAM_JSON) Flux(reactor.core.publisher.Flux) List(java.util.List) Jackson2ObjectMapperBuilder(org.springframework.http.converter.json.Jackson2ObjectMapperBuilder) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) SerializationFeature(com.fasterxml.jackson.databind.SerializationFeature) PrettyPrinter(com.fasterxml.jackson.core.PrettyPrinter) Assert(org.springframework.util.Assert) Mono(reactor.core.publisher.Mono) ResolvableType(org.springframework.core.ResolvableType) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Example 5 with MimeType

use of org.springframework.util.MimeType in project spring-framework by spring-projects.

the class EncoderHttpMessageWriterTests method testDefaultMediaType.

private void testDefaultMediaType(MediaType negotiatedMediaType) {
    this.response = new MockServerHttpResponse();
    this.mediaTypeCaptor = ArgumentCaptor.forClass(MediaType.class);
    MimeType defaultContentType = MimeTypeUtils.TEXT_XML;
    HttpMessageWriter<String> writer = getWriter(defaultContentType);
    writer.write(Mono.just("body"), forClass(String.class), negotiatedMediaType, this.response, NO_HINTS);
    assertEquals(defaultContentType, this.response.getHeaders().getContentType());
    assertEquals(defaultContentType, this.mediaTypeCaptor.getValue());
}
Also used : MediaType(org.springframework.http.MediaType) MockServerHttpResponse(org.springframework.mock.http.server.reactive.test.MockServerHttpResponse) MimeType(org.springframework.util.MimeType)

Aggregations

MimeType (org.springframework.util.MimeType)15 Test (org.junit.Test)11 MessageHeaders (org.springframework.messaging.MessageHeaders)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 ResolvableType (org.springframework.core.ResolvableType)2 MessageHeaderAccessor (org.springframework.messaging.support.MessageHeaderAccessor)2 PrettyPrinter (com.fasterxml.jackson.core.PrettyPrinter)1 DefaultIndenter (com.fasterxml.jackson.core.util.DefaultIndenter)1 DefaultPrettyPrinter (com.fasterxml.jackson.core.util.DefaultPrettyPrinter)1 JavaType (com.fasterxml.jackson.databind.JavaType)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)1 SerializationConfig (com.fasterxml.jackson.databind.SerializationConfig)1 SerializationFeature (com.fasterxml.jackson.databind.SerializationFeature)1 TypeFactory (com.fasterxml.jackson.databind.type.TypeFactory)1 File (java.io.File)1 OutputStream (java.io.OutputStream)1 List (java.util.List)1 Map (java.util.Map)1