1 13 package info.magnolia.cms.beans.config; 14 15 import info.magnolia.cms.Aggregator; 16 import info.magnolia.cms.core.Content; 17 18 import java.util.Collection ; 19 import java.util.Hashtable ; 20 import java.util.Iterator ; 21 import java.util.Map ; 22 23 import javax.jcr.RepositoryException; 24 import javax.jcr.observation.Event; 25 import javax.jcr.observation.EventIterator; 26 import javax.jcr.observation.EventListener; 27 import javax.jcr.observation.ObservationManager; 28 import javax.servlet.http.HttpServletRequest ; 29 30 import org.apache.commons.lang.StringUtils; 31 import org.apache.log4j.Logger; 32 33 34 38 public final class MIMEMapping { 39 40 43 private static Logger log = Logger.getLogger(MIMEMapping.class); 44 45 private static final String START_PAGE = "server"; 47 private static Map cachedContent = new Hashtable (); 48 49 52 private MIMEMapping() { 53 } 55 56 59 public static void init() { 60 load(); 61 registerEventListener(); 62 } 63 64 67 public static void load() { 68 MIMEMapping.cachedContent.clear(); 69 try { 70 log.info("Config : loading MIMEMapping"); Content startPage = ContentRepository.getHierarchyManager(ContentRepository.CONFIG).getContent(START_PAGE); 72 Collection mimeList = startPage.getContent("MIMEMapping").getChildren(); MIMEMapping.cacheContent(mimeList); 74 log.info("Config : MIMEMapping loaded"); } 76 catch (RepositoryException re) { 77 log.error("Config : Failed to load MIMEMapping"); log.error(re.getMessage(), re); 79 } 80 } 81 82 public static void reload() { 83 log.info("Config : re-loading MIMEMapping"); MIMEMapping.load(); 85 } 86 87 90 private static void registerEventListener() { 91 92 log.info("Registering event listener for MIMEMapping"); 94 try { 95 ObservationManager observationManager = ContentRepository 96 .getHierarchyManager(ContentRepository.CONFIG) 97 .getWorkspace() 98 .getObservationManager(); 99 100 observationManager.addEventListener(new EventListener() { 101 102 public void onEvent(EventIterator iterator) { 103 reload(); 105 } 106 }, Event.NODE_ADDED 107 | Event.NODE_REMOVED 108 | Event.PROPERTY_ADDED 109 | Event.PROPERTY_CHANGED 110 | Event.PROPERTY_REMOVED, "/" + START_PAGE + "/" + "MIMEMapping", true, null, null, false); } 112 catch (RepositoryException e) { 113 log.error("Unable to add event listeners for MIMEMapping", e); } 115 } 116 117 120 private static void cacheContent(Collection mimeList) { 121 Iterator iterator = mimeList.iterator(); 122 while (iterator.hasNext()) { 123 Content c = (Content) iterator.next(); 124 try { 125 MIMEMapping.cachedContent.put(c.getNodeData("extension").getString(), c .getNodeData("mime-type") .getString()); 128 } 129 catch (Exception e) { 130 log.error("Failed to cache MIMEMapping"); } 132 } 133 } 134 135 140 public static String getMIMEType(String key) { 141 if (StringUtils.isEmpty(key)) { 142 return StringUtils.EMPTY; 143 } 144 return (String ) MIMEMapping.cachedContent.get(key.toLowerCase()); 145 } 146 147 152 public static String getMIMEType(HttpServletRequest request) { 153 String extension = (String ) request.getAttribute(Aggregator.EXTENSION); 154 if (StringUtils.isEmpty(extension)) { 155 extension = StringUtils.substringAfterLast(request.getRequestURI(), "."); if (StringUtils.isEmpty(extension)) { 157 extension = Server.getDefaultExtension(); 158 } 159 } 160 String mimeType = (String ) MIMEMapping.cachedContent.get(extension.toLowerCase()); 161 162 if (mimeType == null && StringUtils.isNotEmpty(extension)) { 163 log.info("Cannot find MIME type for extension \"" + extension + "\""); mimeType = (String ) MIMEMapping.cachedContent.get(Server.getDefaultExtension()); 165 } 166 return mimeType; 167 } 168 169 172 public static String getContentEncoding(HttpServletRequest request) { 173 String contentType = MIMEMapping.getMIMEType(request); 174 if (contentType != null) { 175 int index = contentType.lastIndexOf(";"); if (index > -1) { 177 String encoding = contentType.substring(index + 1).toLowerCase().trim(); 178 encoding = encoding.replaceAll("charset=", StringUtils.EMPTY); return encoding; 180 } 181 } 182 return StringUtils.EMPTY; 183 } 184 } 185 | Popular Tags |