Top Description Fields Constructors Methods
org.springframework.http.codec

public Class ResourceHttpMessageWriter

extends Object
implements HttpMessageWriter<Resource>
Class Inheritance
All Implemented Interfaces
org.springframework.http.codec.HttpMessageWriter
Imports
java.io.File, .IOException, java.util.List, .Map, org.apache.commons.logging.Log, org.reactivestreams.Publisher, reactor.core.publisher.Flux, .Mono, reactor.core.scheduler.Schedulers, org.springframework.core.ResolvableType, org.springframework.core.codec.Hints, .ResourceDecoder, .ResourceEncoder, .ResourceRegionEncoder, org.springframework.core.io.InputStreamResource, .Resource, org.springframework.core.io.buffer.DataBuffer, .DataBufferFactory, org.springframework.core.io.support.ResourceRegion, org.springframework.http.HttpHeaders, .HttpLogging, .HttpRange, .HttpStatus, .MediaType, .MediaTypeFactory, .ReactiveHttpOutputMessage, .ZeroCopyHttpOutputMessage, org.springframework.http.server.reactive.ServerHttpRequest, .ServerHttpResponse, org.springframework.lang.Nullable, org.springframework.util.MimeTypeUtils

HttpMessageWriter that can write a Resource.

Also an implementation of HttpMessageWriter with support for writing one or more ResourceRegion's based on the HTTP ranges specified in the request.

For reading to a Resource, use ResourceDecoder wrapped with DecoderHttpMessageReader.

Authors
Arjen Poutsma, Brian Clozel, Rossen Stoyanchev
Since
5.0
See Also
ResourceEncoder, ResourceRegionEncoder, HttpRange

Field Summary

Modifier and TypeField and Description
private final ResourceEncoder
private static final Log
private final List<MediaType>
private static final ResolvableType
private final ResourceRegionEncoder

Constructor Summary

AccessConstructor and Description
public
public
ResourceHttpMessageWriter(int bufferSize)

Method Summary

Modifier and TypeMethod and Description
public Mono<Void>
addDefaultHeaders(ReactiveHttpOutputMessage message, Resource resource, MediaType contentType, Map<String, Object> hints)

Adds the default headers for the given resource to the given message.

public void
addHeaders(ReactiveHttpOutputMessage message, Resource resource, MediaType contentType, Map<String, Object> hints)
Deprecated for removal since 6.1. since 6.1, in favor of addDefaultHeaders(ReactiveHttpOutputMessage, Resource, MediaType, Map), for removal = 6.2

Adds the default headers for the given resource to the given message.

public boolean
canWrite(ResolvableType
the type of object to check
elementType
,
MediaType
the media type for the write (possibly null)
mediaType
)

Implements org.springframework.http.codec.HttpMessageWriter.canWrite.

Whether the given object type is supported by this writer.

private Mono<Void>
private static MediaType
getResourceMediaType(MediaType mediaType, Resource resource, Map<String, Object> hints)

public List<MediaType>
getWritableMediaTypes()

Implements org.springframework.http.codec.HttpMessageWriter.getWritableMediaTypes.

Return the list of media types supported by this Writer.

private static Mono<Long>
lengthOf(Resource resource)

public Mono<Void>
write(Publisher<? extends Resource>
the objects to write
inputStream
,
ResolvableType
the type of objects in the stream which must have been previously checked via canWrite(ResolvableType, MediaType)
elementType
,
MediaType
the content type for the write (possibly null to indicate that the default content type of the writer must be used)
mediaType
,
ReactiveHttpOutputMessage
the message to write to
message
,
Map<String, Object>
additional information about how to encode and write
hints
)

Implements org.springframework.http.codec.HttpMessageWriter.write.

Write a given stream of object to the output message.

public Mono<Void>
write(Publisher<? extends Resource> inputStream, ResolvableType
the actual return type of the method that returned the value; for annotated controllers, the MethodParameter can be accessed via ResolvableType#getSource().
actualType
,
ResolvableType
the type of Objects in the input stream
elementType
,
MediaType
the content type to use (possibly null indicating the default content type of the writer should be used)
mediaType
,
ServerHttpRequest
the current request
request
,
ServerHttpResponse
the current response
response
,
Map<String, Object> hints)

Overrides default org.springframework.http.codec.HttpMessageWriter.write.

Server-side only alternative to write(Publisher, ResolvableType, MediaType, ReactiveHttpOutputMessage, Map) with additional context available.

private Mono<Void>
private Mono<Void>
private static Mono<Void>
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

encoderback to summary
private final ResourceEncoder encoder
loggerback to summary
private static final Log logger
mediaTypesback to summary
private final List<MediaType> mediaTypes
REGION_TYPEback to summary
private static final ResolvableType REGION_TYPE
regionEncoderback to summary
private final ResourceRegionEncoder regionEncoder

Constructor Detail

ResourceHttpMessageWriterback to summary
public ResourceHttpMessageWriter()
ResourceHttpMessageWriterback to summary
public ResourceHttpMessageWriter(int bufferSize)

Method Detail

addDefaultHeadersback to summary
public Mono<Void> addDefaultHeaders(ReactiveHttpOutputMessage message, Resource resource, MediaType contentType, Map<String, Object> hints)

Adds the default headers for the given resource to the given message.

Since
6.1
addHeadersback to summary
public void addHeaders(ReactiveHttpOutputMessage message, Resource resource, MediaType contentType, Map<String, Object> hints)

Deprecated

for removal since 6.1.

since 6.1, in favor of addDefaultHeaders(ReactiveHttpOutputMessage, Resource, MediaType, Map), for removal = 6.2

Adds the default headers for the given resource to the given message.

Annotations
@Deprecated
since:6.1
forRemoval:true
Since
6.0
canWriteback to summary
public boolean canWrite(ResolvableType elementType, MediaType mediaType)

Implements org.springframework.http.codec.HttpMessageWriter.canWrite.

Doc from org.springframework.http.codec.HttpMessageWriter.canWrite.

Whether the given object type is supported by this writer.

Parameters
elementType:ResolvableType

the type of object to check

mediaType:MediaType

the media type for the write (possibly null)

Returns:boolean

true if writable, false otherwise

Annotations
@Override
encodeAndWriteRegionsback to summary
private Mono<Void> encodeAndWriteRegions(Publisher<? extends ResourceRegion> publisher, MediaType mediaType, ReactiveHttpOutputMessage message, Map<String, Object> hints)
getResourceMediaTypeback to summary
private static MediaType getResourceMediaType(MediaType mediaType, Resource resource, Map<String, Object> hints)
getWritableMediaTypesback to summary
public List<MediaType> getWritableMediaTypes()

Implements org.springframework.http.codec.HttpMessageWriter.getWritableMediaTypes.

Doc from org.springframework.http.codec.HttpMessageWriter.getWritableMediaTypes.

Return the list of media types supported by this Writer. The list may not apply to every possible target element type and calls to this method should typically be guarded via canWrite(elementType, null). The list may also exclude media types supported only for a specific element type. Alternatively, use getWritableMediaTypes(ResolvableType) for a more precise list.

Returns:List<MediaType>

the general list of supported media types

Annotations
@Override
lengthOfback to summary
private static Mono<Long> lengthOf(Resource resource)
writeback to summary
public Mono<Void> write(Publisher<? extends Resource> inputStream, ResolvableType elementType, MediaType mediaType, ReactiveHttpOutputMessage message, Map<String, Object> hints)

Implements org.springframework.http.codec.HttpMessageWriter.write.

Doc from org.springframework.http.codec.HttpMessageWriter.write.

Write a given stream of object to the output message.

Parameters
inputStream:Publisher<? extends Resource>

the objects to write

elementType:ResolvableType

the type of objects in the stream which must have been previously checked via canWrite(ResolvableType, MediaType)

mediaType:MediaType

the content type for the write (possibly null to indicate that the default content type of the writer must be used)

message:ReactiveHttpOutputMessage

the message to write to

hints:Map<String, Object>

additional information about how to encode and write

Returns:Mono<Void>

indicates completion or error

Annotations
@Override
writeback to summary
public Mono<Void> write(Publisher<? extends Resource> inputStream, ResolvableType actualType, ResolvableType elementType, MediaType mediaType, ServerHttpRequest request, ServerHttpResponse response, Map<String, Object> hints)

Overrides default org.springframework.http.codec.HttpMessageWriter.write.

Doc from org.springframework.http.codec.HttpMessageWriter.write.

Server-side only alternative to write(Publisher, ResolvableType, MediaType, ReactiveHttpOutputMessage, Map) with additional context available.

Parameters
actualType:ResolvableType

the actual return type of the method that returned the value; for annotated controllers, the MethodParameter can be accessed via ResolvableType#getSource().

elementType:ResolvableType

the type of Objects in the input stream

mediaType:MediaType

the content type to use (possibly null indicating the default content type of the writer should be used)

request:ServerHttpRequest

the current request

response:ServerHttpResponse

the current response

Returns:Mono<Void>

a Mono that indicates completion of writing or error

Annotations
@Override
writeResourceback to summary
private Mono<Void> writeResource(Resource resource, ResolvableType type, MediaType mediaType, ReactiveHttpOutputMessage message, Map<String, Object> hints)
writeSingleRegionback to summary
private Mono<Void> writeSingleRegion(ResourceRegion region, ReactiveHttpOutputMessage message, Map<String, Object> hints)
zeroCopyback to summary
private static Mono<Void> zeroCopy(Resource resource, ResourceRegion region, ReactiveHttpOutputMessage message, Map<String, Object> hints)
Annotations
@Nullable