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));
}
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));
}
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));
}
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"));
}
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."));
}
}
Aggregations