return;
}
// get the asset in order to build etag and check permissions
long defLang=APILocator.getLanguageAPI().getDefaultLanguage().getId();
FileAsset fileasset;
try {
fileasset = APILocator.getFileAssetAPI().fromContentlet(
APILocator.getContentletAPI().findContentletByIdentifier(ident.getId(), live, defLang, user, true));
}
catch(DotSecurityException ex) {
resp.sendError(403);
return;
}
boolean userHasEditPerms = false;
if(!live) {
userHasEditPerms = APILocator.getPermissionAPI().doesUserHavePermission(fileasset,PermissionAPI.PERMISSION_EDIT,user);
if(req.getParameter("recompile")!=null && userHasEditPerms) {
CacheLocator.getCSSCache().remove(host.getIdentifier(), actualUri, false);
CacheLocator.getCSSCache().remove(host.getIdentifier(), actualUri, true);
}
}
CachedCSS cache = CacheLocator.getCSSCache().get(host.getIdentifier(), actualUri, live, user);
byte[] responseData=null;
Date cacheMaxDate=null;
CachedCSS cacheObject=null;
if(cache==null || cache.data==null) {
// do compile!
synchronized(ident.getId().intern()) {
cache = CacheLocator.getCSSCache().get(host.getIdentifier(), actualUri, live, user);
if(cache==null || cache.data==null) {
Logger.debug(this, "compiling css data for "+host.getHostname()+":"+uri);
try {
compiler.compile();
}
catch(Throwable ex) {
Logger.error(this, "Error compiling "+host.getHostname()+":"+uri, ex);
throw new Exception(ex);
}
// build cache object
ContentletVersionInfo vinfo = APILocator.getVersionableAPI().getContentletVersionInfo(ident.getId(), defLang);
CachedCSS newcache = new CachedCSS();
newcache.data = compiler.getOutput();
newcache.hostId = host.getIdentifier();
newcache.uri = actualUri;
newcache.live = live;
newcache.modDate = vinfo.getVersionTs();
newcache.imported = new ArrayList<ImportedAsset>();
for(String importUri : compiler.getAllImportedURI()) {
// newcache entry for the imported asset
ImportedAsset asset = new ImportedAsset();
asset.uri = importUri;
Identifier ii;
if(importUri.startsWith("//")) {
importUri=importUri.substring(2);
String hn=importUri.substring(0, importUri.indexOf('/'));
String uu=importUri.substring(importUri.indexOf('/'));
ii = APILocator.getIdentifierAPI().find(APILocator.getHostAPI().findByName(hn, user, live),uu);
}
else {
ii = APILocator.getIdentifierAPI().find(host, importUri);
}
ContentletVersionInfo impInfo = APILocator.getVersionableAPI().getContentletVersionInfo(ii.getId(), defLang);
asset.modDate = impInfo.getVersionTs();
newcache.imported.add(asset);
Logger.debug(this, host.getHostname()+":"+actualUri+" imports-> "+importUri);
// actual cache entry for the imported asset. If needed
synchronized(ii.getId().intern()) {
if(CacheLocator.getCSSCache().get(ii.getHostId(), importUri, live, user)==null) {
CachedCSS entry = new CachedCSS();
entry.data = null;
entry.hostId = ii.getHostId();
entry.imported = new ArrayList<ImportedAsset>();
entry.live = live;
entry.modDate = impInfo.getVersionTs();
entry.uri = importUri;
CacheLocator.getCSSCache().add(entry);
}
}
}
CacheLocator.getCSSCache().add(newcache);
cacheMaxDate = newcache.getMaxDate();
cacheObject = newcache;
responseData = compiler.getOutput();
}
}
}
if(responseData==null) {
if(cache!=null && cache.data!=null) {
// if css is cached an valid is used as response
responseData = cache.data;
cacheMaxDate = cache.getMaxDate();
cacheObject = cache;
Logger.debug(this, "using cached css data for "+host.getHostname()+":"+uri);
}
else {
resp.sendError(500, "no data!");
return;
}
}
boolean doDownload = true;
if(live) {
// we use etag dot:inode:cacheMaxDate:filesize and lastMod:cacheMaxDate
// so the browser downloads it if any of the imported files changes
doDownload = DownloadUtil.isModifiedEtag(req, resp, fileasset.getInode(),
cacheMaxDate.getTime(), fileasset.getFileSize());
}
if(doDownload) {
// write the actual response to the user
resp.setContentType("text/css");