From 811cfd52c96191d346817e642b33c01dba83f0d9 Mon Sep 17 00:00:00 2001 From: Stian Thorgersen Date: Fri, 12 Sep 2014 13:46:56 +0200 Subject: [PATCH] KEYCLOAK-657 Set cache-control for keycloak.js and login-iframe --- .../keycloak/services/resources/JsResource.java | 15 +++++++++++++-- .../services/resources/RealmsResource.java | 14 +++++++++++--- .../services/resources/admin/AdminConsole.java | 6 +++++- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/services/src/main/java/org/keycloak/services/resources/JsResource.java b/services/src/main/java/org/keycloak/services/resources/JsResource.java index ce012e76f9..6c59c78c04 100755 --- a/services/src/main/java/org/keycloak/services/resources/JsResource.java +++ b/services/src/main/java/org/keycloak/services/resources/JsResource.java @@ -1,8 +1,11 @@ package org.keycloak.services.resources; +import org.keycloak.Config; + import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; +import javax.ws.rs.core.CacheControl; import javax.ws.rs.core.Response; import java.io.InputStream; @@ -25,7 +28,11 @@ public class JsResource { public Response getJs() { InputStream inputStream = getClass().getClassLoader().getResourceAsStream("keycloak.js"); if (inputStream != null) { - return Response.ok(inputStream).build(); + CacheControl cacheControl = new CacheControl(); + cacheControl.setNoTransform(false); + cacheControl.setMaxAge(Config.scope("theme").getInt("staticMaxAge", -1)); + + return Response.ok(inputStream).type("text/javascript").cacheControl(cacheControl).build(); } else { return Response.status(Response.Status.NOT_FOUND).build(); } @@ -37,7 +44,11 @@ public class JsResource { public Response getMinJs() { InputStream inputStream = getClass().getClassLoader().getResourceAsStream("keycloak.min.js"); if (inputStream != null) { - return Response.ok(inputStream).build(); + CacheControl cacheControl = new CacheControl(); + cacheControl.setNoTransform(false); + cacheControl.setMaxAge(Config.scope("theme").getInt("staticMaxAge", -1)); + + return Response.ok(inputStream).type("text/javascript").cacheControl(cacheControl).build(); } else { return Response.status(Response.Status.NOT_FOUND).build(); } diff --git a/services/src/main/java/org/keycloak/services/resources/RealmsResource.java b/services/src/main/java/org/keycloak/services/resources/RealmsResource.java index 04491d02a2..e618c9e681 100755 --- a/services/src/main/java/org/keycloak/services/resources/RealmsResource.java +++ b/services/src/main/java/org/keycloak/services/resources/RealmsResource.java @@ -6,6 +6,7 @@ import org.jboss.resteasy.spi.BadRequestException; import org.jboss.resteasy.spi.NotFoundException; import org.jboss.resteasy.spi.ResteasyProviderFactory; import org.keycloak.ClientConnection; +import org.keycloak.Config; import org.keycloak.events.EventBuilder; import org.keycloak.models.ApplicationModel; import org.keycloak.models.ClientModel; @@ -24,9 +25,11 @@ import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; +import javax.ws.rs.core.CacheControl; import javax.ws.rs.core.Context; import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import javax.ws.rs.core.UriBuilder; import javax.ws.rs.core.UriInfo; import java.io.IOException; @@ -88,8 +91,7 @@ public class RealmsResource { @Path("{realm}/login-status-iframe.html") @GET @Produces(MediaType.TEXT_HTML) - @NoCache - public String getLoginStatusIframe(final @PathParam("realm") String name, + public Response getLoginStatusIframe(final @PathParam("realm") String name, @QueryParam("client_id") String client_id, @QueryParam("origin") String origin) { AuthenticationManager auth = new AuthenticationManager(); @@ -130,7 +132,13 @@ public class RealmsResource { try { String file = StreamUtil.readString(is); - return file.replace("ORIGIN", origin); + file = file.replace("ORIGIN", origin); + + CacheControl cacheControl = new CacheControl(); + cacheControl.setNoTransform(false); + cacheControl.setMaxAge(Config.scope("theme").getInt("staticMaxAge", -1)); + + return Response.ok(file).cacheControl(cacheControl).build(); } catch (IOException e) { throw new RuntimeException(e); } diff --git a/services/src/main/java/org/keycloak/services/resources/admin/AdminConsole.java b/services/src/main/java/org/keycloak/services/resources/admin/AdminConsole.java index 6287a311d3..ce5f486c46 100755 --- a/services/src/main/java/org/keycloak/services/resources/admin/AdminConsole.java +++ b/services/src/main/java/org/keycloak/services/resources/admin/AdminConsole.java @@ -292,7 +292,11 @@ public class AdminConsole { public Response getKeycloakJs() { InputStream inputStream = getClass().getClassLoader().getResourceAsStream("keycloak.js"); if (inputStream != null) { - return Response.ok(inputStream).build(); + CacheControl cacheControl = new CacheControl(); + cacheControl.setNoTransform(false); + cacheControl.setMaxAge(Config.scope("theme").getInt("staticMaxAge", -1)); + + return Response.ok(inputStream).type("text/javascript").cacheControl(cacheControl).build(); } else { return Response.status(Response.Status.NOT_FOUND).build(); }