Top Description Inners Fields Constructors Methods
org.springframework.web.reactive.resource

public Class EncodedResourceResolver

extends AbstractResourceResolver
Class Inheritance
Imports
java.io.File, .IOException, .InputStream, java.net.URI, .URL, java.nio.channels.ReadableByteChannel, java.nio.charset.Charset, java.util.ArrayList, .Arrays, .Collections, .LinkedHashMap, .List, .Map, reactor.core.publisher.Mono, org.springframework.core.io.AbstractResource, .Resource, org.springframework.http.HttpHeaders, org.springframework.http.server.reactive.ServerHttpRequest, org.springframework.lang.Nullable, org.springframework.util.Assert, org.springframework.web.server.ServerWebExchange

Resolver that delegates to the chain, and if a resource is found, it then attempts to find an encoded (e.g. gzip, brotli) variant that is acceptable based on the "Accept-Encoding" request header.

The list of supported contentCodings can be configured, in order of preference, and each coding must be associated with extensions.

Note that this resolver must be ordered ahead of a VersionResourceResolver with a content-based, version strategy to ensure the version calculation is not impacted by the encoding.

Author
Rossen Stoyanchev
Since
5.1

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class

Field Summary

Modifier and TypeField and Description
private final List<String>
public static final List<String>
DEFAULT_CODINGS

The default content codings.

private final Map<String, String>
Inherited from org.springframework.web.reactive.resource.AbstractResourceResolver:
logger

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private String
public List<String>
getContentCodings()

Return a read-only list with the supported content codings.

private String
public Map<String, String>
getExtensions()

Return a read-only map with coding-to-extension mappings.

public void
registerExtension(String
the content coding
coding
,
String
the associated file extension
extension
)

Java config friendly alternative to setExtensions(Map).

protected Mono<Resource>
protected Mono<String>
public void
setContentCodings(List<String>
one or more supported content codings
codings
)

Configure the supported content codings in order of preference.

public void
setExtensions(Map<String, String>
the extensions to use.
extensions
)

Configure mappings from content codings to file extensions.

Inherited from org.springframework.web.reactive.resource.AbstractResourceResolver:
resolveResourceresolveUrlPath

Field Detail

contentCodingsback to summary
private final List<String> contentCodings
DEFAULT_CODINGSback to summary
public static final List<String> DEFAULT_CODINGS

The default content codings.

extensionsback to summary
private final Map<String, String> extensions

Constructor Detail

EncodedResourceResolverback to summary
public EncodedResourceResolver()

Method Detail

getAcceptEncodingback to summary
private String getAcceptEncoding(ServerWebExchange exchange)
Annotations
@Nullable
getContentCodingsback to summary
public List<String> getContentCodings()

Return a read-only list with the supported content codings.

getExtensionback to summary
private String getExtension(String coding)
getExtensionsback to summary
public Map<String, String> getExtensions()

Return a read-only map with coding-to-extension mappings.

registerExtensionback to summary
public void registerExtension(String coding, String extension)

Java config friendly alternative to setExtensions(Map).

Parameters
coding:String

the content coding

extension:String

the associated file extension

resolveResourceInternalback to summary
protected Mono<Resource> resolveResourceInternal(ServerWebExchange exchange, String requestPath, List<? extends Resource> locations, ResourceResolverChain chain)

Implements abstract org.springframework.web.reactive.resource.AbstractResourceResolver.resolveResourceInternal.

Annotations
@Override
resolveUrlPathInternalback to summary
protected Mono<String> resolveUrlPathInternal(String resourceUrlPath, List<? extends Resource> locations, ResourceResolverChain chain)

Implements abstract org.springframework.web.reactive.resource.AbstractResourceResolver.resolveUrlPathInternal.

Annotations
@Override
setContentCodingsback to summary
public void setContentCodings(List<String> codings)

Configure the supported content codings in order of preference. The first coding that is present in the "Accept-Encoding" header for a given request, and that has a file present with the associated extension, is used.

Note

Each coding must be associated with a file extension via registerExtension or setExtensions. Also customizations to the list of codings here should be matched by customizations to the same list in CachingResourceResolver to ensure encoded variants of a resource are cached under separate keys.

By default this property is set to ["br", "gzip"].

Parameters
codings:List<String>

one or more supported content codings

setExtensionsback to summary
public void setExtensions(Map<String, String> extensions)

Configure mappings from content codings to file extensions. A dot "." will be prepended in front of the extension value if not present.

By default this is configured with ["br" -> ".br"] and ["gzip" -> ".gz"].

Parameters
extensions:Map<String, String>

the extensions to use.

See Also
registerExtension(String, String)
org.springframework.web.reactive.resource back to summary

pack-priv final Class EncodedResourceResolver.EncodedResource

extends AbstractResource
implements HttpResource
Class Inheritance
All Implemented Interfaces
org.springframework.web.reactive.resource.HttpResource, org.springframework.core.io.Resource, org.springframework.core.io.InputStreamSource

An encoded HttpResource.

Field Summary

Modifier and TypeField and Description
private final String
private final Resource
private final Resource

Constructor Summary

AccessConstructor and Description
pack-priv
EncodedResource(Resource original, String coding, String extension)

Method Summary

Modifier and TypeMethod and Description
public long
public Resource
createRelative(String
the relative path (relative to this resource)
relativePath
)

Overrides org.springframework.core.io.AbstractResource.createRelative.

Implements org.springframework.core.io.Resource.createRelative.

Create a resource relative to this resource.

public boolean
exists()

Overrides org.springframework.core.io.AbstractResource.exists.

Implements org.springframework.core.io.Resource.exists.

Determine whether this resource actually exists in physical form.

public byte[]
getContentAsByteArray()

Overrides default org.springframework.core.io.Resource.getContentAsByteArray.

Return the contents of this resource as a byte array.

public String
getContentAsString(Charset
the charset to use for decoding
charset
)

Overrides default org.springframework.core.io.Resource.getContentAsString.

Return the contents of this resource as a string, using the specified charset.

public String
getDescription()

Implements org.springframework.core.io.Resource.getDescription.

Return a description for this resource, to be used for error output when working with the resource.

public File
public String
getFilename()

Overrides org.springframework.core.io.AbstractResource.getFilename.

Implements org.springframework.core.io.Resource.getFilename.

Determine the filename for this resource — typically the last part of the path — for example, "myfile.txt".

public InputStream
getInputStream()

Implements org.springframework.core.io.InputStreamSource.getInputStream.

Return an InputStream for the content of an underlying resource.

public HttpHeaders
getResponseHeaders()

Implements org.springframework.web.reactive.resource.HttpResource.getResponseHeaders.

The HTTP headers to be contributed to the HTTP response that serves the current resource.

public URI
public URL
public boolean
isFile()

Overrides org.springframework.core.io.AbstractResource.isFile.

Overrides default org.springframework.core.io.Resource.isFile.

Determine whether this resource represents a file in a file system.

public boolean
isOpen()

Overrides org.springframework.core.io.AbstractResource.isOpen.

Overrides default org.springframework.core.io.Resource.isOpen.

Indicate whether this resource represents a handle with an open stream.

public boolean
isReadable()

Overrides org.springframework.core.io.AbstractResource.isReadable.

Overrides default org.springframework.core.io.Resource.isReadable.

Indicate whether non-empty contents of this resource can be read via getInputStream().

public long
lastModified()

Overrides org.springframework.core.io.AbstractResource.lastModified.

Implements org.springframework.core.io.Resource.lastModified.

Determine the last-modified timestamp for this resource.

public ReadableByteChannel
Inherited from org.springframework.core.io.AbstractResource:
equalsgetFileForLastModifiedCheckhashCodetoString

Field Detail

codingback to summary
private final String coding
encodedback to summary
private final Resource encoded
originalback to summary
private final Resource original

Constructor Detail

EncodedResourceback to summary
pack-priv EncodedResource(Resource original, String coding, String extension) throws IOException

Method Detail

contentLengthback to summary
public long contentLength() throws IOException

Overrides org.springframework.core.io.AbstractResource.contentLength.

Implements org.springframework.core.io.Resource.contentLength.

Doc from org.springframework.core.io.Resource.contentLength.

Determine the content length for this resource.

Annotations
@Override
Exceptions
IOException:
if the resource cannot be resolved (in the file system or as some other known physical resource type)
createRelativeback to summary
public Resource createRelative(String relativePath) throws IOException

Overrides org.springframework.core.io.AbstractResource.createRelative.

Implements org.springframework.core.io.Resource.createRelative.

Doc from org.springframework.core.io.Resource.createRelative.

Create a resource relative to this resource.

Parameters
relativePath:String

the relative path (relative to this resource)

Returns:Resource

the resource handle for the relative resource

Annotations
@Override
Exceptions
IOException:
if the relative resource cannot be determined
existsback to summary
public boolean exists()

Overrides org.springframework.core.io.AbstractResource.exists.

Implements org.springframework.core.io.Resource.exists.

Doc from org.springframework.core.io.Resource.exists.

Determine whether this resource actually exists in physical form.

This method performs a definitive existence check, whereas the existence of a Resource handle only guarantees a valid descriptor handle.

Annotations
@Override
getContentAsByteArrayback to summary
public byte[] getContentAsByteArray() throws IOException

Overrides default org.springframework.core.io.Resource.getContentAsByteArray.

Doc from org.springframework.core.io.Resource.getContentAsByteArray.

Return the contents of this resource as a byte array.

Returns:byte[]

the contents of this resource as byte array

Annotations
@Override
Exceptions
IOException:
in case of general resolution/reading failures
getContentAsStringback to summary
public String getContentAsString(Charset charset) throws IOException

Overrides default org.springframework.core.io.Resource.getContentAsString.

Doc from org.springframework.core.io.Resource.getContentAsString.

Return the contents of this resource as a string, using the specified charset.

Parameters
charset:Charset

the charset to use for decoding

Returns:String

the contents of this resource as a String

Annotations
@Override
Exceptions
IOException:
in case of general resolution/reading failures
getDescriptionback to summary
public String getDescription()

Implements org.springframework.core.io.Resource.getDescription.

Doc from org.springframework.core.io.Resource.getDescription.

Return a description for this resource, to be used for error output when working with the resource.

Implementations are also encouraged to return this value from their toString method.

Annotations
@Override
getFileback to summary
public File getFile() throws IOException

Overrides org.springframework.core.io.AbstractResource.getFile.

Implements org.springframework.core.io.Resource.getFile.

Doc from org.springframework.core.io.Resource.getFile.

Return a File handle for this resource.

Annotations
@Override
Exceptions
IOException:
in case of general resolution/reading failures
getFilenameback to summary
public String getFilename()

Overrides org.springframework.core.io.AbstractResource.getFilename.

Implements org.springframework.core.io.Resource.getFilename.

Doc from org.springframework.core.io.Resource.getFilename.

Determine the filename for this resource — typically the last part of the path — for example, "myfile.txt".

Returns null if this type of resource does not have a filename.

Implementations are encouraged to return the filename unencoded.

Annotations
@Override
@Nullable
getInputStreamback to summary
public InputStream getInputStream() throws IOException

Implements org.springframework.core.io.InputStreamSource.getInputStream.

Doc from org.springframework.core.io.InputStreamSource.getInputStream.

Return an InputStream for the content of an underlying resource.

It is expected that each call creates a fresh stream.

This requirement is particularly important when you consider an API such as JavaMail, which needs to be able to read the stream multiple times when creating mail attachments. For such a use case, it is required that each getInputStream() call returns a fresh stream.

Returns:InputStream

the input stream for the underlying resource (must not be null)

Annotations
@Override
Exceptions
IOException:
if the content stream could not be opened
getResponseHeadersback to summary
public HttpHeaders getResponseHeaders()

Implements org.springframework.web.reactive.resource.HttpResource.getResponseHeaders.

Doc from org.springframework.web.reactive.resource.HttpResource.getResponseHeaders.

The HTTP headers to be contributed to the HTTP response that serves the current resource.

Returns:HttpHeaders

the HTTP response headers

Annotations
@Override
getURIback to summary
public URI getURI() throws IOException

Overrides org.springframework.core.io.AbstractResource.getURI.

Implements org.springframework.core.io.Resource.getURI.

Doc from org.springframework.core.io.Resource.getURI.

Return a URI handle for this resource.

Annotations
@Override
Exceptions
IOException:
if the resource cannot be resolved as URI, i.e. if the resource is not available as a descriptor
getURLback to summary
public URL getURL() throws IOException

Overrides org.springframework.core.io.AbstractResource.getURL.

Implements org.springframework.core.io.Resource.getURL.

Doc from org.springframework.core.io.Resource.getURL.

Return a URL handle for this resource.

Annotations
@Override
Exceptions
IOException:
if the resource cannot be resolved as URL, i.e. if the resource is not available as a descriptor
isFileback to summary
public boolean isFile()

Overrides org.springframework.core.io.AbstractResource.isFile.

Overrides default org.springframework.core.io.Resource.isFile.

Doc from org.springframework.core.io.Resource.isFile.

Determine whether this resource represents a file in a file system.

A value of true strongly suggests (but does not guarantee) that a getFile() call will succeed.

This is conservatively false by default.

Annotations
@Override
isOpenback to summary
public boolean isOpen()

Overrides org.springframework.core.io.AbstractResource.isOpen.

Overrides default org.springframework.core.io.Resource.isOpen.

Doc from org.springframework.core.io.Resource.isOpen.

Indicate whether this resource represents a handle with an open stream. If true, the InputStream cannot be read multiple times, and must be read and closed to avoid resource leaks.

Will be false for typical resource descriptors.

Annotations
@Override
isReadableback to summary
public boolean isReadable()

Overrides org.springframework.core.io.AbstractResource.isReadable.

Overrides default org.springframework.core.io.Resource.isReadable.

Doc from org.springframework.core.io.Resource.isReadable.

Indicate whether non-empty contents of this resource can be read via getInputStream().

Will be true for typical resource descriptors that exist since it strictly implies exists() semantics as of 5.1. Note that actual content reading may still fail when attempted. However, a value of false is a definitive indication that the resource content cannot be read.

Annotations
@Override
lastModifiedback to summary
public long lastModified() throws IOException

Overrides org.springframework.core.io.AbstractResource.lastModified.

Implements org.springframework.core.io.Resource.lastModified.

Doc from org.springframework.core.io.Resource.lastModified.

Determine the last-modified timestamp for this resource.

Annotations
@Override
Exceptions
IOException:
if the resource cannot be resolved (in the file system or as some other known physical resource type)
readableChannelback to summary
public ReadableByteChannel readableChannel() throws IOException

Overrides org.springframework.core.io.AbstractResource.readableChannel.

Overrides default org.springframework.core.io.Resource.readableChannel.

Doc from org.springframework.core.io.Resource.readableChannel.

Return a ReadableByteChannel.

It is expected that each call creates a fresh channel.

The default implementation returns Channels#newChannel(InputStream) with the result of getInputStream().

Returns:ReadableByteChannel

the byte channel for the underlying resource (must not be null)

Annotations
@Override
Exceptions
IOException:
if the content channel could not be opened