View Javadoc

1   package org.apache.turbine.services.mimetype;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.IOException;
24  
25  import java.util.Locale;
26  
27  import org.apache.commons.configuration.Configuration;
28  
29  import org.apache.turbine.services.InitializationException;
30  import org.apache.turbine.services.TurbineBaseService;
31  import org.apache.turbine.services.mimetype.util.CharSetMap;
32  import org.apache.turbine.services.mimetype.util.MimeType;
33  import org.apache.turbine.services.mimetype.util.MimeTypeMap;
34  import org.apache.turbine.services.servlet.TurbineServlet;
35  
36  /***
37   * The MimeType Service maintains mappings between MIME types and
38   * the corresponding file name extensions, and between locales and
39   * character encodings.
40   *
41   * <p>The MIME type mappings can be defined in MIME type files
42   * located in user's home directory, Java home directory or
43   * the current class jar. The default mapping file is defined
44   * with the mime.type.file property. In addition, the service maintains
45   * a set of most common mappings.
46   *
47   * <p>The charset mappings can be defined in property files
48   * located in user's home directory, Java home directory or
49   * the current class jar. The default mapping file is defined
50   * with the charset.file property. In addition, the service maintains
51   * a set of most common mappings.
52   *
53   * @author <a href="mailto:ilkka.priha@simsoft.fi">Ilkka Priha</a>
54   * @version $Id: TurbineMimeTypeService.java 534527 2007-05-02 16:10:59Z tv $
55   */
56  public class TurbineMimeTypeService
57          extends TurbineBaseService
58          implements MimeTypeService
59  {
60      /***
61       * The MIME type file property.
62       */
63      public static final String MIME_TYPES = "mime.types";
64  
65      /***
66       * The charset file property.
67       */
68      public static final String CHARSETS = "charsets";
69  
70      /***
71       * The MIME type map used by the service.
72       */
73      private MimeTypeMap mimeTypeMap;
74  
75      /***
76       * The charset map used by the service.
77       */
78      private CharSetMap charSetMap;
79  
80      /***
81       * Constructs a new service.
82       */
83      public TurbineMimeTypeService()
84      {
85      }
86  
87      /***
88       * Initializes the service.
89       *
90       * @throws InitializationException if initialization fails.
91       */
92      public void init()
93              throws InitializationException
94      {
95          String path = null;
96          Configuration conf = getConfiguration();
97          if (conf != null)
98          {
99              path = conf.getString(MIME_TYPES);
100             if (path != null)
101             {
102                 path = TurbineServlet.getRealPath(path);
103             }
104         }
105         if (path != null)
106         {
107             try
108             {
109                 mimeTypeMap = new MimeTypeMap(path);
110             }
111             catch (IOException x)
112             {
113                 throw new InitializationException(path, x);
114             }
115         }
116         else
117         {
118             mimeTypeMap = new MimeTypeMap();
119         }
120 
121         if (conf != null)
122         {
123             path = conf.getString(CHARSETS);
124             if (path != null)
125             {
126                 path = TurbineServlet.getRealPath(path);
127             }
128         }
129         if (path != null)
130         {
131             try
132             {
133                 charSetMap = new CharSetMap(path);
134             }
135             catch (IOException x)
136             {
137                 throw new InitializationException(path, x);
138             }
139         }
140         else
141         {
142             charSetMap = new CharSetMap();
143         }
144         setInit(true);
145     }
146 
147     /***
148      * Sets a MIME content type mapping to extensions to the map.
149      * The extension is specified by a MIME type name followed
150      * by a list of file name extensions separated by a whitespace.
151      *
152      * @param spec a MIME type extension specification to add.
153      */
154     public void setContentType(String spec)
155     {
156         mimeTypeMap.setContentType(spec);
157     }
158 
159     /***
160      * Gets the MIME content type for a file as a string.
161      *
162      * @param file the file.
163      * @return the MIME type string.
164      */
165     public String getContentType(File file)
166     {
167         return mimeTypeMap.getContentType(file);
168     }
169 
170     /***
171      * Gets the MIME content type for a named file as a string.
172      *
173      * @param name the name of the file.
174      * @return the MIME type string.
175      */
176     public String getContentType(String name)
177     {
178         return mimeTypeMap.getContentType(name);
179     }
180 
181     /***
182      * Gets the MIME content type for a file name extension as a string.
183      *
184      * @param ext the file name extension.
185      * @param def the default type if none is found.
186      * @return the MIME type string.
187      */
188     public String getContentType(String ext,
189                                  String def)
190     {
191         return mimeTypeMap.getContentType(ext, def);
192     }
193 
194     /***
195      * Gets the MIME content type for a file.
196      *
197      * @param file the file.
198      * @return the MIME type.
199      */
200     public MimeType getMimeContentType(File file)
201     {
202         return mimeTypeMap.getMimeContentType(file);
203     }
204 
205     /***
206      * Gets the MIME content type for a named file.
207      *
208      * @param name the name of the file.
209      * @return the MIME type.
210      */
211     public MimeType getMimeContentType(String name)
212     {
213         return mimeTypeMap.getMimeContentType(name);
214     }
215 
216     /***
217      * Gets the MIME content type for a file name extension.
218      *
219      * @param ext the file name extension.
220      * @param def the default type if none is found.
221      * @return the MIME type.
222      */
223     public MimeType getMimeContentType(String ext,
224                                        String def)
225     {
226         return mimeTypeMap.getMimeContentType(ext, def);
227     }
228 
229     /***
230      * Gets the default file name extension for a MIME type.
231      * Note that the mappers are called in the reverse order.
232      *
233      * @param type the MIME type as a string.
234      * @return the file name extension or null.
235      */
236     public String getDefaultExtension(String type)
237     {
238         return mimeTypeMap.getDefaultExtension(type);
239     }
240 
241     /***
242      * Gets the default file name extension for a MIME type.
243      * Note that the mappers are called in the reverse order.
244      *
245      * @param mime the MIME type.
246      * @return the file name extension or null.
247      */
248     public String getDefaultExtension(MimeType mime)
249     {
250         return mimeTypeMap.getDefaultExtension(mime);
251     }
252 
253     /***
254      * Sets a locale-charset mapping.
255      *
256      * @param key the key for the charset.
257      * @param charset the corresponding charset.
258      */
259     public void setCharSet(String key,
260                            String charset)
261     {
262         charSetMap.setCharSet(key, charset);
263     }
264 
265     /***
266      * Gets the charset for a locale. First a locale specific charset
267      * is searched for, then a country specific one and lastly a language
268      * specific one. If none is found, the default charset is returned.
269      *
270      * @param locale the locale.
271      * @return the charset.
272      */
273     public String getCharSet(Locale locale)
274     {
275         return charSetMap.getCharSet(locale);
276     }
277 
278     /***
279      * Gets the charset for a locale with a variant. The search
280      * is performed in the following order:
281      * "lang"_"country"_"variant"="charset",
282      * _"counry"_"variant"="charset",
283      * "lang"__"variant"="charset",
284      * __"variant"="charset",
285      * "lang"_"country"="charset",
286      * _"country"="charset",
287      * "lang"="charset".
288      * If nothing of the above is found, the default charset is returned.
289      *
290      * @param locale the locale.
291      * @param variant a variant field.
292      * @return the charset.
293      */
294     public String getCharSet(Locale locale,
295                              String variant)
296     {
297         return charSetMap.getCharSet(locale, variant);
298     }
299 
300     /***
301      * Gets the charset for a specified key.
302      *
303      * @param key the key for the charset.
304      * @return the found charset or the default one.
305      */
306     public String getCharSet(String key)
307     {
308         return charSetMap.getCharSet(key);
309     }
310 
311     /***
312      * Gets the charset for a specified key.
313      *
314      * @param key the key for the charset.
315      * @param def the default charset if none is found.
316      * @return the found charset or the given default.
317      */
318     public String getCharSet(String key,
319                              String def)
320     {
321         return charSetMap.getCharSet(key, def);
322     }
323 }