Search in sources :

Example 21 with MediaType

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);
        }
    }
}
Also used : MediaType(org.springframework.http.MediaType)

Example 22 with 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);
    }
}
Also used : MediaType(org.springframework.http.MediaType)

Example 23 with MediaType

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;
}
Also used : MediaType(org.springframework.http.MediaType)

Example 24 with 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());
    }
}
Also used : StreamingHttpOutputMessage(org.springframework.http.StreamingHttpOutputMessage) FileCacheImageOutputStream(javax.imageio.stream.FileCacheImageOutputStream) OutputStream(java.io.OutputStream) ImageOutputStream(javax.imageio.stream.ImageOutputStream) MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) MediaType(org.springframework.http.MediaType) IOException(java.io.IOException)

Example 25 with MediaType

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);
}
Also used : MediaType(org.springframework.http.MediaType) Charset(java.nio.charset.Charset)

Aggregations

MediaType (org.springframework.http.MediaType)253 Test (org.junit.Test)157 HttpHeaders (org.springframework.http.HttpHeaders)49 MockHttpInputMessage (org.springframework.http.MockHttpInputMessage)31 MockHttpOutputMessage (org.springframework.http.MockHttpOutputMessage)24 Charset (java.nio.charset.Charset)21 ArrayList (java.util.ArrayList)21 URI (java.net.URI)20 ByteArrayInputStream (java.io.ByteArrayInputStream)15 IOException (java.io.IOException)15 ServerWebExchange (org.springframework.web.server.ServerWebExchange)15 List (java.util.List)14 HttpInputMessage (org.springframework.http.HttpInputMessage)14 HttpStatus (org.springframework.http.HttpStatus)11 Resource (org.springframework.core.io.Resource)10 HttpEntity (org.springframework.http.HttpEntity)10 HttpMessageNotReadableException (org.springframework.http.converter.HttpMessageNotReadableException)9 MediaTypeRequestMatcher (org.springframework.security.web.util.matcher.MediaTypeRequestMatcher)9 HttpMediaTypeNotAcceptableException (org.springframework.web.HttpMediaTypeNotAcceptableException)9 InputStream (java.io.InputStream)8