01: package it.unimi.dsi.mg4j.util;
02:
03: /*
04: * MG4J: Managing Gigabytes for Java
05: *
06: * Copyright (C) 2005-2007 Sebastiano Vigna
07: *
08: * This library is free software; you can redistribute it and/or modify it
09: * under the terms of the GNU Lesser General Public License as published by the Free
10: * Software Foundation; either version 2.1 of the License, or (at your option)
11: * any later version.
12: *
13: * This library is distributed in the hope that it will be useful, but
14: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16: * for more details.
17: *
18: * You should have received a copy of the GNU Lesser General Public License
19: * along with this program; if not, write to the Free Software
20: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21: *
22: */
23:
24: import java.io.File;
25: import java.io.IOException;
26:
27: import javax.activation.MimetypesFileTypeMap;
28:
29: /** A thin wrapper around a singleton instance of {@link javax.activation.MimetypesFileTypeMap}
30: * that tries to load <samp>/etc/mime.types</samp> into the map.
31: */
32:
33: public class MimeTypeResolver {
34: /** A MIME type file map, stuffed with some basic types. */
35: private final static String UNIX_MIME_TYPES_FILENAME = "/etc/mime.types";
36:
37: private final static MimetypesFileTypeMap MIME_TYPES_FILE_TYPE_MAP;
38:
39: private MimeTypeResolver() {
40: }
41:
42: static {
43: try {
44: MIME_TYPES_FILE_TYPE_MAP = (new File(
45: UNIX_MIME_TYPES_FILENAME)).exists() ? new MimetypesFileTypeMap(
46: UNIX_MIME_TYPES_FILENAME)
47: : new MimetypesFileTypeMap();
48: } catch (IOException e) {
49: throw new RuntimeException(e);
50: }
51: }
52:
53: public static String getContentType(final File file) {
54: return MIME_TYPES_FILE_TYPE_MAP.getContentType(file);
55: }
56:
57: public static String getContentType(final CharSequence name) {
58: return MIME_TYPES_FILE_TYPE_MAP.getContentType(name.toString());
59: }
60: }
|