Set cache-control on static resources from themes

This commit is contained in:
Stian Thorgersen 2014-05-30 16:01:01 +01:00
parent df28fb97ed
commit 5dafad71ac
5 changed files with 23 additions and 5 deletions

View file

@ -22,6 +22,7 @@ public class ExtendingThemeManager implements ThemeProvider {
private List<ThemeProvider> providers; private List<ThemeProvider> providers;
private String defaultTheme; private String defaultTheme;
private int staticMaxAge;
public ExtendingThemeManager(ProviderSession providerSession) { public ExtendingThemeManager(ProviderSession providerSession) {
providers = new LinkedList(); providers = new LinkedList();
@ -40,6 +41,11 @@ public class ExtendingThemeManager implements ThemeProvider {
}); });
this.defaultTheme = Config.scope("theme").get("default"); this.defaultTheme = Config.scope("theme").get("default");
this.staticMaxAge = Config.scope("theme").getInt("staticMaxAge");
}
public int getStaticMaxAge() {
return staticMaxAge;
} }
@Override @Override

View file

@ -20,6 +20,7 @@
"theme": { "theme": {
"default": "keycloak", "default": "keycloak",
"staticMaxAge": 2592000,
"folder": { "folder": {
"dir": "${jboss.server.config.dir}/themes" "dir": "${jboss.server.config.dir}/themes"
} }

View file

@ -11,6 +11,7 @@ import javax.activation.MimetypesFileTypeMap;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.PathParam; import javax.ws.rs.PathParam;
import javax.ws.rs.core.CacheControl;
import javax.ws.rs.core.Context; import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.io.InputStream; import java.io.InputStream;
@ -39,14 +40,18 @@ public class ThemeResource {
* @return * @return
*/ */
@GET @GET
@Path("/{themType}/{themeName}/{path:.*}") @Path("/{themeType}/{themeName}/{path:.*}")
public Response getResource(@PathParam("themType") String themType, @PathParam("themeName") String themeName, @PathParam("path") String path) { public Response getResource(@PathParam("themeType") String themType, @PathParam("themeName") String themeName, @PathParam("path") String path) {
try { try {
ExtendingThemeManager themeManager = new ExtendingThemeManager(providerSession); ExtendingThemeManager themeManager = new ExtendingThemeManager(providerSession);
Theme theme = themeManager.createTheme(themeName, Theme.Type.valueOf(themType.toUpperCase())); Theme theme = themeManager.createTheme(themeName, Theme.Type.valueOf(themType.toUpperCase()));
InputStream resource = theme.getResourceAsStream(path); InputStream resource = theme.getResourceAsStream(path);
if (resource != null) { if (resource != null) {
return Response.ok(resource).type(mimeTypes.getContentType(path)).build(); CacheControl cacheControl = new CacheControl();
cacheControl.setNoTransform(false);
cacheControl.setMaxAge(themeManager.getStaticMaxAge());
return Response.ok(resource).type(mimeTypes.getContentType(path)).cacheControl(cacheControl).build();
} else { } else {
return Response.status(Response.Status.NOT_FOUND).build(); return Response.status(Response.Status.NOT_FOUND).build();
} }

View file

@ -30,6 +30,7 @@ import javax.ws.rs.GET;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.PathParam; import javax.ws.rs.PathParam;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.core.CacheControl;
import javax.ws.rs.core.Context; import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
@ -314,13 +315,17 @@ public class AdminConsole {
} }
try { try {
//logger.info("getting resource: " + path + " uri: " + uriInfo.getRequestUri().toString());
ExtendingThemeManager themeManager = new ExtendingThemeManager(providerSession); ExtendingThemeManager themeManager = new ExtendingThemeManager(providerSession);
Theme theme = themeManager.createTheme(realm.getAdminTheme(), Theme.Type.ADMIN); Theme theme = themeManager.createTheme(realm.getAdminTheme(), Theme.Type.ADMIN);
InputStream resource = theme.getResourceAsStream(path); InputStream resource = theme.getResourceAsStream(path);
if (resource != null) { if (resource != null) {
String contentType = mimeTypes.getContentType(path); String contentType = mimeTypes.getContentType(path);
return Response.ok(resource).type(contentType).build();
CacheControl cacheControl = new CacheControl();
cacheControl.setNoTransform(false);
cacheControl.setMaxAge(themeManager.getStaticMaxAge());
return Response.ok(resource).type(contentType).cacheControl(cacheControl).build();
} else { } else {
return Response.status(Response.Status.NOT_FOUND).build(); return Response.status(Response.Status.NOT_FOUND).build();
} }

View file

@ -29,6 +29,7 @@
"theme": { "theme": {
"default": "keycloak", "default": "keycloak",
"staticMaxAge": 2592000,
"folder": { "folder": {
"dir": "${keycloak.theme.dir}" "dir": "${keycloak.theme.dir}"
} }