KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > beans > config > MIMEMapping


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2005 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

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 JavaDoc;
19 import java.util.Hashtable JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Map JavaDoc;
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 JavaDoc;
29
30 import org.apache.commons.lang.StringUtils;
31 import org.apache.log4j.Logger;
32
33
34 /**
35  * @author Sameer Charles
36  * @version 1.1
37  */

38 public final class MIMEMapping {
39
40     /**
41      * Logger.
42      */

43     private static Logger log = Logger.getLogger(MIMEMapping.class);
44
45     private static final String JavaDoc START_PAGE = "server"; //$NON-NLS-1$
46

47     private static Map JavaDoc cachedContent = new Hashtable JavaDoc();
48
49     /**
50      * Utility class, don't instantiate.
51      */

52     private MIMEMapping() {
53         // unused
54
}
55
56     /**
57      * Reads all configured mime mapping (config/server/MIMEMapping).
58      */

59     public static void init() {
60         load();
61         registerEventListener();
62     }
63
64     /**
65      * Reads all configured mime mapping (config/server/MIMEMapping).
66      */

67     public static void load() {
68         MIMEMapping.cachedContent.clear();
69         try {
70             log.info("Config : loading MIMEMapping"); //$NON-NLS-1$
71
Content startPage = ContentRepository.getHierarchyManager(ContentRepository.CONFIG).getContent(START_PAGE);
72             Collection JavaDoc mimeList = startPage.getContent("MIMEMapping").getChildren(); //$NON-NLS-1$
73
MIMEMapping.cacheContent(mimeList);
74             log.info("Config : MIMEMapping loaded"); //$NON-NLS-1$
75
}
76         catch (RepositoryException re) {
77             log.error("Config : Failed to load MIMEMapping"); //$NON-NLS-1$
78
log.error(re.getMessage(), re);
79         }
80     }
81
82     public static void reload() {
83         log.info("Config : re-loading MIMEMapping"); //$NON-NLS-1$
84
MIMEMapping.load();
85     }
86
87     /**
88      * Register an event listener: reload cache configuration when something changes.
89      */

90     private static void registerEventListener() {
91
92         log.info("Registering event listener for MIMEMapping"); //$NON-NLS-1$
93

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 everything
104
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); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
111
}
112         catch (RepositoryException e) {
113             log.error("Unable to add event listeners for MIMEMapping", e); //$NON-NLS-1$
114
}
115     }
116
117     /**
118      * Cache all MIME types configured.
119      */

120     private static void cacheContent(Collection JavaDoc mimeList) {
121         Iterator JavaDoc iterator = mimeList.iterator();
122         while (iterator.hasNext()) {
123             Content c = (Content) iterator.next();
124             try {
125                 MIMEMapping.cachedContent.put(c.getNodeData("extension").getString(), c //$NON-NLS-1$
126
.getNodeData("mime-type") //$NON-NLS-1$
127
.getString());
128             }
129             catch (Exception JavaDoc e) {
130                 log.error("Failed to cache MIMEMapping"); //$NON-NLS-1$
131
}
132         }
133     }
134
135     /**
136      * Get MIME type String.
137      * @param key extension for which MIME type is requested
138      * @return MIME type
139      */

140     public static String JavaDoc getMIMEType(String JavaDoc key) {
141         if (StringUtils.isEmpty(key)) {
142             return StringUtils.EMPTY;
143         }
144         return (String JavaDoc) MIMEMapping.cachedContent.get(key.toLowerCase());
145     }
146
147     /**
148      * Get MIME type String.
149      * @param request
150      * @return MIME type
151      */

152     public static String JavaDoc getMIMEType(HttpServletRequest JavaDoc request) {
153         String JavaDoc extension = (String JavaDoc) request.getAttribute(Aggregator.EXTENSION);
154         if (StringUtils.isEmpty(extension)) {
155             extension = StringUtils.substringAfterLast(request.getRequestURI(), "."); //$NON-NLS-1$
156
if (StringUtils.isEmpty(extension)) {
157                 extension = Server.getDefaultExtension();
158             }
159         }
160         String JavaDoc mimeType = (String JavaDoc) MIMEMapping.cachedContent.get(extension.toLowerCase());
161
162         if (mimeType == null && StringUtils.isNotEmpty(extension)) {
163             log.info("Cannot find MIME type for extension \"" + extension + "\""); //$NON-NLS-1$ //$NON-NLS-2$
164
mimeType = (String JavaDoc) MIMEMapping.cachedContent.get(Server.getDefaultExtension());
165         }
166         return mimeType;
167     }
168
169     /**
170      * @param request
171      */

172     public static String JavaDoc getContentEncoding(HttpServletRequest JavaDoc request) {
173         String JavaDoc contentType = MIMEMapping.getMIMEType(request);
174         if (contentType != null) {
175             int index = contentType.lastIndexOf(";"); //$NON-NLS-1$
176
if (index > -1) {
177                 String JavaDoc encoding = contentType.substring(index + 1).toLowerCase().trim();
178                 encoding = encoding.replaceAll("charset=", StringUtils.EMPTY); //$NON-NLS-1$
179
return encoding;
180             }
181         }
182         return StringUtils.EMPTY;
183     }
184 }
185
Popular Tags