Icon servlet: Set Cache-Control header to enable icon caching (#4336)

I have removed the Modified-Since handling as it did not have an effect due to the missing Cache-Control header,
and instead added Cache-Control with a max-age set.
This enables "forever" caching of icons, which should be fine since they are static assets.

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
This commit is contained in:
Florian Hotze 2024-08-04 17:33:06 +02:00 committed by GitHub
parent 289f06378d
commit 1ce5b378d3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -14,7 +14,6 @@ package org.openhab.core.ui.icon.internal;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.time.Instant;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -63,8 +62,6 @@ public class IconServlet extends HttpServlet {
static final String PARAM_ANY_FORMAT = "anyFormat"; static final String PARAM_ANY_FORMAT = "anyFormat";
static final String PARAM_STATE = "state"; static final String PARAM_STATE = "state";
private long startupTime;
protected String defaultIconSetId = "classic"; protected String defaultIconSetId = "classic";
private final List<IconProvider> iconProvider = new ArrayList<>(); private final List<IconProvider> iconProvider = new ArrayList<>();
@ -80,7 +77,6 @@ public class IconServlet extends HttpServlet {
@Activate @Activate
protected void activate(Map<String, Object> config) { protected void activate(Map<String, Object> config) {
startupTime = System.currentTimeMillis();
modified(config); modified(config);
} }
@ -94,11 +90,6 @@ public class IconServlet extends HttpServlet {
@Override @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (req.getDateHeader("If-Modified-Since") > startupTime) {
resp.setStatus(304);
return;
}
String category = getCategory(req); String category = getCategory(req);
if (category.isEmpty()) { if (category.isEmpty()) {
logger.debug("URI must start with '{}' but is '{}'", SERVLET_PATH, req.getRequestURI()); logger.debug("URI must start with '{}' but is '{}'", SERVLET_PATH, req.getRequestURI());
@ -148,7 +139,7 @@ public class IconServlet extends HttpServlet {
} }
resp.setContentType(Format.SVG.equals(format) ? "image/svg+xml" : "image/png"); resp.setContentType(Format.SVG.equals(format) ? "image/svg+xml" : "image/png");
resp.setDateHeader("Last-Modified", Instant.now().toEpochMilli()); resp.setHeader("Cache-Control", "max-age=31536000");
is.transferTo(resp.getOutputStream()); is.transferTo(resp.getOutputStream());
resp.flushBuffer(); resp.flushBuffer();
} catch (IOException e) { } catch (IOException e) {