use of org.springframework.http.MediaType in project spring-framework by spring-projects.
the class ContentNegotiationManagerFactoryBean method setMediaTypes.
/**
* Add a mapping from a key, extracted from a path extension or a query
* parameter, to a MediaType. This is required in order for the parameter
* strategy to work. Any extensions explicitly registered here are also
* whitelisted for the purpose of Reflected File Download attack detection
* (see Spring Framework reference documentation for more details on RFD
* attack protection).
* <p>The path extension strategy will also try to use
* {@link ServletContext#getMimeType} and JAF (if present) to resolve path
* extensions. To change this behavior see the {@link #useJaf} property.
* @param mediaTypes media type mappings
* @see #addMediaType(String, MediaType)
* @see #addMediaTypes(Map)
*/
public void setMediaTypes(Properties mediaTypes) {
if (!CollectionUtils.isEmpty(mediaTypes)) {
for (Entry<Object, Object> entry : mediaTypes.entrySet()) {
String extension = ((String) entry.getKey()).toLowerCase(Locale.ENGLISH);
MediaType mediaType = MediaType.valueOf((String) entry.getValue());
this.mediaTypes.put(extension, mediaType);
}
}
}
use of org.springframework.http.MediaType in project spring-framework by spring-projects.
the class MappingMediaTypeFileExtensionResolver method addMapping.
/**
* Map an extension to a MediaType. Ignore if extension already mapped.
*/
protected void addMapping(String extension, MediaType mediaType) {
MediaType previous = this.mediaTypes.putIfAbsent(extension, mediaType);
if (previous == null) {
this.fileExtensions.add(mediaType, extension);
this.allFileExtensions.add(extension);
}
}
use of org.springframework.http.MediaType in project spring-framework by spring-projects.
the class PathExtensionContentNegotiationStrategy method getMediaTypeForResource.
/**
* A public method exposing the knowledge of the path extension strategy to
* resolve file extensions to a {@link MediaType} in this case for a given
* {@link Resource}. The method first looks up any explicitly registered
* file extensions first and then falls back on JAF if available.
* @param resource the resource to look up
* @return the MediaType for the extension, or {@code null} if none found
* @since 4.3
*/
public MediaType getMediaTypeForResource(Resource resource) {
Assert.notNull(resource, "Resource must not be null");
MediaType mediaType = null;
String filename = resource.getFilename();
String extension = StringUtils.getFilenameExtension(filename);
if (extension != null) {
mediaType = lookupMediaType(extension);
}
if (mediaType == null && JAF_PRESENT) {
mediaType = MediaTypeFactory.getMediaType(filename);
}
if (MediaType.APPLICATION_OCTET_STREAM.equals(mediaType)) {
mediaType = null;
}
return mediaType;
}
use of org.springframework.http.MediaType in project spring-framework by spring-projects.
the class BufferedImageHttpMessageConverter method write.
@Override
public void write(final BufferedImage image, final MediaType contentType, final HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
final MediaType selectedContentType = getContentType(contentType);
outputMessage.getHeaders().setContentType(selectedContentType);
if (outputMessage instanceof StreamingHttpOutputMessage) {
StreamingHttpOutputMessage streamingOutputMessage = (StreamingHttpOutputMessage) outputMessage;
streamingOutputMessage.setBody(new StreamingHttpOutputMessage.Body() {
@Override
public void writeTo(OutputStream outputStream) throws IOException {
writeInternal(image, selectedContentType, outputStream);
}
});
} else {
writeInternal(image, selectedContentType, outputMessage.getBody());
}
}
use of org.springframework.http.MediaType in project spring-framework by spring-projects.
the class CommonsFileUploadSupport method determineEncoding.
private String determineEncoding(String contentTypeHeader, String defaultEncoding) {
if (!StringUtils.hasText(contentTypeHeader)) {
return defaultEncoding;
}
MediaType contentType = MediaType.parseMediaType(contentTypeHeader);
Charset charset = contentType.getCharset();
return (charset != null ? charset.name() : defaultEncoding);
}
Aggregations