Search in sources :

Example 11 with MimeType

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

the class MappingJackson2MessageConverterTests method mimetypesParametrizedConstructor.

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

Example 12 with MimeType

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

the class MappingJackson2MessageConverterTests method toMessageUtf16.

@Test
public void toMessageUtf16() {
    MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
    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 + "\"", new String((byte[]) message.getPayload(), StandardCharsets.UTF_16BE));
    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 13 with MimeType

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

the class MappingJackson2MessageConverterTests method toMessage.

@Test
public void toMessage() throws Exception {
    MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
    MyBean payload = new MyBean();
    payload.setString("Foo");
    payload.setNumber(42);
    payload.setFraction(42F);
    payload.setArray(new String[] { "Foo", "Bar" });
    payload.setBool(true);
    payload.setBytes(new byte[] { 0x1, 0x2 });
    Message<?> message = converter.toMessage(payload, null);
    String actual = new String((byte[]) message.getPayload(), StandardCharsets.UTF_8);
    assertTrue(actual.contains("\"string\":\"Foo\""));
    assertTrue(actual.contains("\"number\":42"));
    assertTrue(actual.contains("fraction\":42.0"));
    assertTrue(actual.contains("\"array\":[\"Foo\",\"Bar\"]"));
    assertTrue(actual.contains("\"bool\":true"));
    assertTrue(actual.contains("\"bytes\":\"AQI=\""));
    assertEquals("Invalid content-type", new MimeType("application", "json", StandardCharsets.UTF_8), message.getHeaders().get(MessageHeaders.CONTENT_TYPE, MimeType.class));
}
Also used : MimeType(org.springframework.util.MimeType) Test(org.junit.Test)

Example 14 with MimeType

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

the class TestMessagePostProcessor method convertAndSendPayloadAndMutableHeadersToDestination.

@Test
public void convertAndSendPayloadAndMutableHeadersToDestination() {
    MessageHeaderAccessor accessor = new MessageHeaderAccessor();
    accessor.setHeader("foo", "bar");
    accessor.setLeaveMutable(true);
    MessageHeaders messageHeaders = accessor.getMessageHeaders();
    this.template.setMessageConverter(new StringMessageConverter());
    this.template.convertAndSend("somewhere", "payload", messageHeaders);
    MessageHeaders actual = this.template.message.getHeaders();
    assertSame(messageHeaders, actual);
    assertEquals(new MimeType("text", "plain", StandardCharsets.UTF_8), actual.get(MessageHeaders.CONTENT_TYPE));
    assertEquals("bar", actual.get("foo"));
}
Also used : StringMessageConverter(org.springframework.messaging.converter.StringMessageConverter) MessageHeaderAccessor(org.springframework.messaging.support.MessageHeaderAccessor) MessageHeaders(org.springframework.messaging.MessageHeaders) MimeType(org.springframework.util.MimeType) Test(org.junit.Test)

Example 15 with MimeType

use of org.springframework.util.MimeType in project dhis2-core by dhis2.

the class StaticContentController method updateStaticContent.

/**
     * Uploads PNG images based on a key. Only accepts PNG and white listed keys.
     *
     * @param key  the key.
     * @param file the image file.
     */
@PreAuthorize("hasRole('ALL') or hasRole('F_SYSTEM_SETTING')")
@ResponseStatus(HttpStatus.NO_CONTENT)
@RequestMapping(value = "/{key}", method = RequestMethod.POST)
public void updateStaticContent(@PathVariable("key") String key, @RequestParam(value = "file") MultipartFile file) throws WebMessageException, IOException {
    if (file == null || file.isEmpty()) {
        throw new WebMessageException(WebMessageUtils.badRequest("Missing parameter 'file'"));
    }
    // Only PNG is accepted at the current time
    MimeType mimeType = MimeTypeUtils.parseMimeType(file.getContentType());
    if (!mimeType.isCompatibleWith(MimeTypeUtils.IMAGE_PNG)) {
        throw new WebMessageException(new WebMessage(Status.WARNING, HttpStatus.UNSUPPORTED_MEDIA_TYPE));
    }
    if (!KEY_WHITELIST_MAP.containsKey(key)) {
        throw new WebMessageException(WebMessageUtils.badRequest("This key is not supported."));
    }
    File out = null;
    try {
        out = locationManager.getFileForWriting(key + ".png", "static");
    } catch (LocationManagerException e) {
        throw new WebMessageException(WebMessageUtils.error(e.getMessage()));
    }
    try {
        file.transferTo(out);
    } catch (IOException e) {
        throw new WebMessageException(WebMessageUtils.error("Could not save file."));
    }
}
Also used : WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) IOException(java.io.IOException) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) MimeType(org.springframework.util.MimeType) LocationManagerException(org.hisp.dhis.external.location.LocationManagerException) WebMessage(org.hisp.dhis.dxf2.webmessage.WebMessage) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

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