or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

api-versioning.mdasync.mdconfiguration.mdcontent-negotiation.mdcontroller-annotations.mdcore-framework.mdcors.mddata-binding.mdexception-handling.mdflash-attributes.mdfunctional-endpoints.mdi18n.mdindex.mdinterceptors.mdmultipart.mdrequest-binding.mdresource-handling.mdresponse-handling.mduri-building.mdview-resolution.md
tile.json

resource-handling.mddocs/

Static Resource Handling

Configuration for serving static resources like CSS, JavaScript, and images with support for caching, versioning, content compression, and resource transformation.

Capabilities

ResourceHttpRequestHandler

Handles requests for static resources by resolving them through a configured chain of resource resolvers and transformers.

/**
 * HttpRequestHandler that serves static resources in an optimized way according to the
 * guidelines of Page Speed, YSlow, etc.
 *
 * The "locations" property takes one or more Spring Resource locations from which to serve
 * static content. Resources could be served from the classpath, the file system, or other
 * locations.
 */
public class ResourceHttpRequestHandler extends WebContentGenerator implements HttpRequestHandler {
    /**
     * Set the locations where static resources are located.
     *
     * @param locations the resource locations
     */
    public void setLocations(List<Resource> locations) {}

    /**
     * Configure the list of ResourceResolvers to use.
     *
     * @param resourceResolvers the resource resolvers
     */
    public void setResourceResolvers(List<ResourceResolver> resourceResolvers) {}

    /**
     * Configure the list of ResourceTransformers to use.
     *
     * @param resourceTransformers the resource transformers
     */
    public void setResourceTransformers(List<ResourceTransformer> resourceTransformers) {}

    /**
     * Set the CacheControl instance to build the Cache-Control HTTP response header.
     *
     * @param cacheControl the cache control settings
     */
    public void setCacheControl(CacheControl cacheControl) {}

    /**
     * Set the value for the HTTP caching header, in seconds.
     *
     * @param cacheSeconds the cache period in seconds
     */
    public void setCacheSeconds(int cacheSeconds) {}

    /**
     * Configure a generator for the ETag information for resources.
     *
     * @param etagGenerator the ETag generator
     */
    public void setEtagGenerator(EtagGenerator etagGenerator) {}

    @Override
    public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}
}

ResourceResolver

Strategy for resolving a request to a server-side resource.

/**
 * A strategy for resolving a request to a server-side resource.
 *
 * Provides mechanisms for resolving an incoming request to an actual Resource
 * and for obtaining the public URL path that clients should use when requesting
 * the resource.
 */
public interface ResourceResolver {
    /**
     * Resolve the supplied request and request path to a Resource that exists
     * under one of the given resource locations.
     *
     * @param request the current request
     * @param requestPath the portion of the request path to use
     * @param locations the locations to search in
     * @param chain the chain of remaining resolvers to delegate to
     * @return the resolved resource or null
     */
    Resource resolveResource(HttpServletRequest request, String requestPath,
                           List<? extends Resource> locations,
                           ResourceResolverChain chain);

    /**
     * Resolve the externally facing public URL path for clients to use to
     * access the resource that is located at the given internal resource path.
     *
     * @param resourcePath the internal resource path
     * @param locations the locations to search in
     * @param chain the chain of resolvers to delegate to
     * @return the resolved public URL path or null
     */
    String resolveUrlPath(String resourcePath, List<? extends Resource> locations,
                         ResourceResolverChain chain);
}

ResourceTransformer

Strategy for transforming resource content.

/**
 * An abstraction for transforming the content of a resource.
 */
public interface ResourceTransformer {
    /**
     * Transform the given resource.
     *
     * @param request the current request
     * @param resource the resource to transform
     * @param transformerChain the chain of remaining transformers to delegate to
     * @return the transformed resource or the same
     * @throws IOException if the transformation fails
     */
    Resource transform(HttpServletRequest request, Resource resource,
                      ResourceTransformerChain transformerChain) throws IOException;
}

VersionResourceResolver

Resolves request paths containing a version string for static resources.

/**
 * A ResourceResolver that resolves request paths containing a version string that can be
 * used as part of an HTTP caching strategy to reliably cache static resources for long periods.
 */
public class VersionResourceResolver extends AbstractResourceResolver {
    /**
     * Add a content-based version strategy. The strategy computes a hash from the resource
     * content which is then inserted into the resource URL path.
     *
     * @param pathPatterns one or more resource path patterns
     * @return this resolver for chaining
     */
    public VersionResourceResolver addContentVersionStrategy(String... pathPatterns) {}

    /**
     * Add a fixed version strategy. The version is a prefix to the resource URL path.
     *
     * @param version the version string to use
     * @param pathPatterns one or more resource path patterns
     * @return this resolver for chaining
     */
    public VersionResourceResolver addFixedVersionStrategy(String version, String... pathPatterns) {}

    /**
     * Add a custom VersionStrategy.
     *
     * @param strategy the version strategy
     * @param pathPatterns one or more resource path patterns
     * @return this resolver for chaining
     */
    public VersionResourceResolver addVersionStrategy(VersionStrategy strategy, String... pathPatterns) {}
}

Usage Example:

@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // Basic resource handling
        registry.addResourceHandler("/static/**")
            .addResourceLocations("classpath:/static/")
            .setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS));

        // With versioning
        registry.addResourceHandler("/resources/**")
            .addResourceLocations("/public-resources/")
            .resourceChain(true)
            .addResolver(new VersionResourceResolver()
                .addContentVersionStrategy("/**"));

        // WebJars support
        registry.addResourceHandler("/webjars/**")
            .addResourceLocations("classpath:/META-INF/resources/webjars/")
            .resourceChain(false)
            .addResolver(new WebJarsResourceResolver());
    }
}

Types

ResourceResolverChain

Contract for invoking a chain of ResourceResolvers.

public interface ResourceResolverChain {
    Resource resolveResource(HttpServletRequest request, String requestPath,
                           List<? extends Resource> locations);
    String resolveUrlPath(String resourcePath, List<? extends Resource> locations);
}

ResourceTransformerChain

Contract for invoking a chain of ResourceTransformers.

public interface ResourceTransformerChain {
    Resource transform(HttpServletRequest request, Resource resource) throws IOException;
}