Search in sources :

Example 6 with MimeType

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

the class WebFluxConfigurationSupportTests method viewResolutionResultHandler.

@Test
public void viewResolutionResultHandler() throws Exception {
    ApplicationContext context = loadConfig(CustomViewResolverConfig.class);
    String name = "viewResolutionResultHandler";
    ViewResolutionResultHandler handler = context.getBean(name, ViewResolutionResultHandler.class);
    assertNotNull(handler);
    assertEquals(Ordered.LOWEST_PRECEDENCE, handler.getOrder());
    List<ViewResolver> resolvers = handler.getViewResolvers();
    assertEquals(1, resolvers.size());
    assertEquals(FreeMarkerViewResolver.class, resolvers.get(0).getClass());
    List<View> views = handler.getDefaultViews();
    assertEquals(1, views.size());
    MimeType type = MimeTypeUtils.parseMimeType("application/json;charset=UTF-8");
    assertEquals(type, views.get(0).getSupportedMediaTypes().get(0));
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ViewResolutionResultHandler(org.springframework.web.reactive.result.view.ViewResolutionResultHandler) FreeMarkerViewResolver(org.springframework.web.reactive.result.view.freemarker.FreeMarkerViewResolver) ViewResolver(org.springframework.web.reactive.result.view.ViewResolver) HttpMessageWriterView(org.springframework.web.reactive.result.view.HttpMessageWriterView) View(org.springframework.web.reactive.result.view.View) MimeType(org.springframework.util.MimeType) Test(org.junit.Test)

Example 7 with MimeType

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

the class HttpMessageWriterViewTests method supportedMediaTypes.

@Test
public void supportedMediaTypes() throws Exception {
    List<MimeType> mimeTypes = Arrays.asList(new MimeType("application", "json", StandardCharsets.UTF_8), new MimeType("application", "*+json", StandardCharsets.UTF_8));
    assertEquals(mimeTypes, this.view.getSupportedMediaTypes());
}
Also used : MimeType(org.springframework.util.MimeType) Test(org.junit.Test)

Example 8 with MimeType

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

the class AbstractMessageConverter method toMessage.

@Override
public final Message<?> toMessage(Object payload, MessageHeaders headers, Object conversionHint) {
    if (!canConvertTo(payload, headers)) {
        return null;
    }
    payload = convertToInternal(payload, headers, conversionHint);
    if (payload == null) {
        return null;
    }
    MimeType mimeType = getDefaultContentType(payload);
    if (headers != null) {
        MessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(headers, MessageHeaderAccessor.class);
        if (accessor != null && accessor.isMutable()) {
            accessor.setHeaderIfAbsent(MessageHeaders.CONTENT_TYPE, mimeType);
            return MessageBuilder.createMessage(payload, accessor.getMessageHeaders());
        }
    }
    MessageBuilder<?> builder = MessageBuilder.withPayload(payload);
    if (headers != null) {
        builder.copyHeaders(headers);
    }
    builder.setHeaderIfAbsent(MessageHeaders.CONTENT_TYPE, mimeType);
    return builder.build();
}
Also used : MessageHeaderAccessor(org.springframework.messaging.support.MessageHeaderAccessor) MimeType(org.springframework.util.MimeType)

Example 9 with MimeType

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

the class ResourceRegionEncoderTests method canEncode.

@Test
public void canEncode() {
    ResolvableType resourceRegion = ResolvableType.forClass(ResourceRegion.class);
    MimeType allMimeType = MimeType.valueOf("*/*");
    assertFalse(this.encoder.canEncode(ResolvableType.forClass(Resource.class), MimeTypeUtils.APPLICATION_OCTET_STREAM));
    assertFalse(this.encoder.canEncode(ResolvableType.forClass(Resource.class), allMimeType));
    assertTrue(this.encoder.canEncode(resourceRegion, MimeTypeUtils.APPLICATION_OCTET_STREAM));
    assertTrue(this.encoder.canEncode(resourceRegion, allMimeType));
}
Also used : ResolvableType(org.springframework.core.ResolvableType) MimeType(org.springframework.util.MimeType) Test(org.junit.Test)

Example 10 with MimeType

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

the class DefaultStompSessionTests method send.

@Test
public void send() throws Exception {
    this.session.afterConnected(this.connection);
    assertTrue(this.session.isConnected());
    String destination = "/topic/foo";
    String payload = "sample payload";
    this.session.send(destination, payload);
    Message<byte[]> message = this.messageCaptor.getValue();
    StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
    assertEquals(StompCommand.SEND, accessor.getCommand());
    StompHeaders stompHeaders = StompHeaders.readOnlyStompHeaders(accessor.getNativeHeaders());
    assertEquals(stompHeaders.toString(), 2, stompHeaders.size());
    assertEquals(destination, stompHeaders.getDestination());
    assertEquals(new MimeType("text", "plain", StandardCharsets.UTF_8), stompHeaders.getContentType());
    // StompEncoder isn't involved
    assertEquals(-1, stompHeaders.getContentLength());
    assertEquals(payload, new String(message.getPayload(), StandardCharsets.UTF_8));
}
Also used : MimeType(org.springframework.util.MimeType) Test(org.junit.Test)

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