src/main/kotlin/de/uapcore/lightpit/filter/StaticFilesFilter.kt

Sun, 03 Sep 2023 12:02:44 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 03 Sep 2023 12:02:44 +0200
changeset 289
d702bdd4da51
child 290
51b14787d826
permissions
-rw-r--r--

fonts and styles are now cachable - fixes #286

     1 import jakarta.servlet.FilterChain
     2 import jakarta.servlet.annotation.WebFilter
     3 import jakarta.servlet.http.HttpFilter
     4 import jakarta.servlet.http.HttpServletRequest
     5 import jakarta.servlet.http.HttpServletResponse
     7 /*
     8  * Copyright 2023 Mike Becker. All rights reserved.
     9  *
    10  * Redistribution and use in source and binary forms, with or without
    11  * modification, are permitted provided that the following conditions are met:
    12  *
    13  * 1. Redistributions of source code must retain the above copyright
    14  * notice, this list of conditions and the following disclaimer.
    15  *
    16  * 2. Redistributions in binary form must reproduce the above copyright
    17  * notice, this list of conditions and the following disclaimer in the
    18  * documentation and/or other materials provided with the distribution.
    19  *
    20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    23  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
    24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    30  */
    32 @WebFilter(urlPatterns = ["*.ttf", "*.css", "*.js"])
    33 class StaticFilesFilter : HttpFilter() {
    34     override fun doFilter(req: HttpServletRequest, res: HttpServletResponse, chain: FilterChain) {
    35         // Apply a Cache-Control Header for static files to allow storing them
    36         // in shared cache for almost indefinitely. We will cache-bust them when we need an update.
    37         // RFC-2616 mandates that servers SHOULD NOT specify more than one year, though
    38         res.addHeader("Cache-Control", "public, max-age=31536000, immutable")
    39         super.doFilter(req, res, chain)
    40     }
    41 }

mercurial