001: package org.apache.turbine.services.mimetype;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.io.File;
023: import java.io.IOException;
024:
025: import java.util.Locale;
026:
027: import org.apache.commons.configuration.Configuration;
028:
029: import org.apache.turbine.services.InitializationException;
030: import org.apache.turbine.services.TurbineBaseService;
031: import org.apache.turbine.services.mimetype.util.CharSetMap;
032: import org.apache.turbine.services.mimetype.util.MimeType;
033: import org.apache.turbine.services.mimetype.util.MimeTypeMap;
034: import org.apache.turbine.services.servlet.TurbineServlet;
035:
036: /**
037: * The MimeType Service maintains mappings between MIME types and
038: * the corresponding file name extensions, and between locales and
039: * character encodings.
040: *
041: * <p>The MIME type mappings can be defined in MIME type files
042: * located in user's home directory, Java home directory or
043: * the current class jar. The default mapping file is defined
044: * with the mime.type.file property. In addition, the service maintains
045: * a set of most common mappings.
046: *
047: * <p>The charset mappings can be defined in property files
048: * located in user's home directory, Java home directory or
049: * the current class jar. The default mapping file is defined
050: * with the charset.file property. In addition, the service maintains
051: * a set of most common mappings.
052: *
053: * @author <a href="mailto:ilkka.priha@simsoft.fi">Ilkka Priha</a>
054: * @version $Id: TurbineMimeTypeService.java 534527 2007-05-02 16:10:59Z tv $
055: */
056: public class TurbineMimeTypeService extends TurbineBaseService
057: implements MimeTypeService {
058: /**
059: * The MIME type file property.
060: */
061: public static final String MIME_TYPES = "mime.types";
062:
063: /**
064: * The charset file property.
065: */
066: public static final String CHARSETS = "charsets";
067:
068: /**
069: * The MIME type map used by the service.
070: */
071: private MimeTypeMap mimeTypeMap;
072:
073: /**
074: * The charset map used by the service.
075: */
076: private CharSetMap charSetMap;
077:
078: /**
079: * Constructs a new service.
080: */
081: public TurbineMimeTypeService() {
082: }
083:
084: /**
085: * Initializes the service.
086: *
087: * @throws InitializationException if initialization fails.
088: */
089: public void init() throws InitializationException {
090: String path = null;
091: Configuration conf = getConfiguration();
092: if (conf != null) {
093: path = conf.getString(MIME_TYPES);
094: if (path != null) {
095: path = TurbineServlet.getRealPath(path);
096: }
097: }
098: if (path != null) {
099: try {
100: mimeTypeMap = new MimeTypeMap(path);
101: } catch (IOException x) {
102: throw new InitializationException(path, x);
103: }
104: } else {
105: mimeTypeMap = new MimeTypeMap();
106: }
107:
108: if (conf != null) {
109: path = conf.getString(CHARSETS);
110: if (path != null) {
111: path = TurbineServlet.getRealPath(path);
112: }
113: }
114: if (path != null) {
115: try {
116: charSetMap = new CharSetMap(path);
117: } catch (IOException x) {
118: throw new InitializationException(path, x);
119: }
120: } else {
121: charSetMap = new CharSetMap();
122: }
123: setInit(true);
124: }
125:
126: /**
127: * Sets a MIME content type mapping to extensions to the map.
128: * The extension is specified by a MIME type name followed
129: * by a list of file name extensions separated by a whitespace.
130: *
131: * @param spec a MIME type extension specification to add.
132: */
133: public void setContentType(String spec) {
134: mimeTypeMap.setContentType(spec);
135: }
136:
137: /**
138: * Gets the MIME content type for a file as a string.
139: *
140: * @param file the file.
141: * @return the MIME type string.
142: */
143: public String getContentType(File file) {
144: return mimeTypeMap.getContentType(file);
145: }
146:
147: /**
148: * Gets the MIME content type for a named file as a string.
149: *
150: * @param name the name of the file.
151: * @return the MIME type string.
152: */
153: public String getContentType(String name) {
154: return mimeTypeMap.getContentType(name);
155: }
156:
157: /**
158: * Gets the MIME content type for a file name extension as a string.
159: *
160: * @param ext the file name extension.
161: * @param def the default type if none is found.
162: * @return the MIME type string.
163: */
164: public String getContentType(String ext, String def) {
165: return mimeTypeMap.getContentType(ext, def);
166: }
167:
168: /**
169: * Gets the MIME content type for a file.
170: *
171: * @param file the file.
172: * @return the MIME type.
173: */
174: public MimeType getMimeContentType(File file) {
175: return mimeTypeMap.getMimeContentType(file);
176: }
177:
178: /**
179: * Gets the MIME content type for a named file.
180: *
181: * @param name the name of the file.
182: * @return the MIME type.
183: */
184: public MimeType getMimeContentType(String name) {
185: return mimeTypeMap.getMimeContentType(name);
186: }
187:
188: /**
189: * Gets the MIME content type for a file name extension.
190: *
191: * @param ext the file name extension.
192: * @param def the default type if none is found.
193: * @return the MIME type.
194: */
195: public MimeType getMimeContentType(String ext, String def) {
196: return mimeTypeMap.getMimeContentType(ext, def);
197: }
198:
199: /**
200: * Gets the default file name extension for a MIME type.
201: * Note that the mappers are called in the reverse order.
202: *
203: * @param type the MIME type as a string.
204: * @return the file name extension or null.
205: */
206: public String getDefaultExtension(String type) {
207: return mimeTypeMap.getDefaultExtension(type);
208: }
209:
210: /**
211: * Gets the default file name extension for a MIME type.
212: * Note that the mappers are called in the reverse order.
213: *
214: * @param mime the MIME type.
215: * @return the file name extension or null.
216: */
217: public String getDefaultExtension(MimeType mime) {
218: return mimeTypeMap.getDefaultExtension(mime);
219: }
220:
221: /**
222: * Sets a locale-charset mapping.
223: *
224: * @param key the key for the charset.
225: * @param charset the corresponding charset.
226: */
227: public void setCharSet(String key, String charset) {
228: charSetMap.setCharSet(key, charset);
229: }
230:
231: /**
232: * Gets the charset for a locale. First a locale specific charset
233: * is searched for, then a country specific one and lastly a language
234: * specific one. If none is found, the default charset is returned.
235: *
236: * @param locale the locale.
237: * @return the charset.
238: */
239: public String getCharSet(Locale locale) {
240: return charSetMap.getCharSet(locale);
241: }
242:
243: /**
244: * Gets the charset for a locale with a variant. The search
245: * is performed in the following order:
246: * "lang"_"country"_"variant"="charset",
247: * _"counry"_"variant"="charset",
248: * "lang"__"variant"="charset",
249: * __"variant"="charset",
250: * "lang"_"country"="charset",
251: * _"country"="charset",
252: * "lang"="charset".
253: * If nothing of the above is found, the default charset is returned.
254: *
255: * @param locale the locale.
256: * @param variant a variant field.
257: * @return the charset.
258: */
259: public String getCharSet(Locale locale, String variant) {
260: return charSetMap.getCharSet(locale, variant);
261: }
262:
263: /**
264: * Gets the charset for a specified key.
265: *
266: * @param key the key for the charset.
267: * @return the found charset or the default one.
268: */
269: public String getCharSet(String key) {
270: return charSetMap.getCharSet(key);
271: }
272:
273: /**
274: * Gets the charset for a specified key.
275: *
276: * @param key the key for the charset.
277: * @param def the default charset if none is found.
278: * @return the found charset or the given default.
279: */
280: public String getCharSet(String key, String def) {
281: return charSetMap.getCharSet(key, def);
282: }
283: }
|