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

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

mercurial