001/* 002 * Copyright 2010 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.site; 017 018import java.io.File; 019import java.io.FilenameFilter; 020import java.util.Map; 021 022import org.apache.avalon.framework.parameters.Parameters; 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.thread.ThreadSafe; 025import org.apache.cocoon.acting.ServiceableAction; 026import org.apache.cocoon.environment.Redirector; 027import org.apache.commons.io.FileUtils; 028import org.slf4j.Logger; 029import org.slf4j.LoggerFactory; 030 031/** 032 * Invalidates cached data for a given site. 033 */ 034public class InvalidateImagesAction extends ServiceableAction implements ThreadSafe 035{ 036 private Logger _logger = LoggerFactory.getLogger("site.cache.log"); 037 private CacheAccessManager _cacheAccess; 038 039 /** 040 * Get the cache access manager 041 * @return the CacheAccessManager 042 */ 043 protected CacheAccessManager _getCacheAccessManager() 044 { 045 if (_cacheAccess == null) 046 { 047 try 048 { 049 _cacheAccess = (CacheAccessManager) manager.lookup(CacheAccessManager.ROLE); 050 } 051 catch (ServiceException e) 052 { 053 throw new IllegalStateException("Cannot get CacheAccessManager", e); 054 } 055 } 056 return _cacheAccess; 057 } 058 059 @Override 060 public Map act(Redirector redirector, org.apache.cocoon.environment.SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 061 { 062 String path = parameters.getParameter("path"); 063 064 File root = SiteCacheHelper.getRootCache(); 065 066 // files to delete are like path/to/file_100x200.jpg 067 068 int j = path.lastIndexOf('/'); 069 String folder = j != -1 ? path.substring(0, j) : ""; 070 final String fileName = j != -1 ? path.substring(j + 1) : path; 071 072 int i = fileName.lastIndexOf('.'); 073 final String ext = i != -1 ? fileName.substring(i) : ""; 074 final String prefix = i != -1 ? fileName.substring(0, i) : fileName; 075 076 File parentFolder = new File(root, folder); 077 078 File[] files = parentFolder.listFiles(new FilenameFilter() 079 { 080 @Override 081 public boolean accept(File dir, String name) 082 { 083 return name.matches("^" + prefix + "_[0-9]+x[0-9]+" + ext + "$"); 084 } 085 }); 086 087 if (files != null) 088 { 089 for (File file : files) 090 { 091 FileUtils.forceDelete(file); 092 093 _logger.info("Invalidate cache for file '" + file + "'"); 094 } 095 } 096 097 _getCacheAccessManager().resetPage(path); 098 099 return EMPTY_MAP; 100 } 101}