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

View file

@ -20,6 +20,7 @@
"theme": {
"default": "keycloak",
"staticMaxAge": 2592000,
"folder": {
"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.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.CacheControl;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import java.io.InputStream;
@ -39,14 +40,18 @@ public class ThemeResource {
* @return
*/
@GET
@Path("/{themType}/{themeName}/{path:.*}")
public Response getResource(@PathParam("themType") String themType, @PathParam("themeName") String themeName, @PathParam("path") String path) {
@Path("/{themeType}/{themeName}/{path:.*}")
public Response getResource(@PathParam("themeType") String themType, @PathParam("themeName") String themeName, @PathParam("path") String path) {
try {
ExtendingThemeManager themeManager = new ExtendingThemeManager(providerSession);
Theme theme = themeManager.createTheme(themeName, Theme.Type.valueOf(themType.toUpperCase()));
InputStream resource = theme.getResourceAsStream(path);
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 {
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.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.CacheControl;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;
@ -314,13 +315,17 @@ public class AdminConsole {
}
try {
//logger.info("getting resource: " + path + " uri: " + uriInfo.getRequestUri().toString());
ExtendingThemeManager themeManager = new ExtendingThemeManager(providerSession);
Theme theme = themeManager.createTheme(realm.getAdminTheme(), Theme.Type.ADMIN);
InputStream resource = theme.getResourceAsStream(path);
if (resource != null) {
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 {
return Response.status(Response.Status.NOT_FOUND).build();
}

View file

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