KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > mail > javamail > ConfigurableMimeFileTypeMap


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.mail.javamail;
18
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21
22 import javax.activation.FileTypeMap JavaDoc;
23 import javax.activation.MimetypesFileTypeMap JavaDoc;
24
25 import org.springframework.beans.factory.InitializingBean;
26 import org.springframework.core.io.ClassPathResource;
27 import org.springframework.core.io.Resource;
28
29 /**
30  * Spring-configurable <code>FileTypeMap</code> implementation that will read
31  * MIME type to file extension mappings from a standard JavaMail MIME type
32  * mapping file, using a standard <code>MimetypesFileTypeMap</code> underneath.
33  *
34  * <p>The mapping file should be in the following format, as specified by the
35  * Java Activation Framework:
36  *
37  * <pre>
38  * # map text/html to .htm and .html files
39  * text/html html htm HTML HTM</pre>
40  *
41  * Lines starting with <code>#</code> are treated as comments and are ignored. All
42  * other lines are treated as mappings. Each mapping line should contain the MIME
43  * type as the first entry and then each file extension to map to that MIME type
44  * as subsequent entries. Each entry is separated by spaces or tabs.
45  *
46  * <p>By default, the mappings in the <code>mime.types</code> file located in the
47  * same package as this class are used, which cover many common file extensions
48  * (in contrast to the out-of-the-box mappings in <code>activation.jar</code>).
49  * This can be overridden using the <code>mappingLocation</code> property.
50  *
51  * <p>Additional mappings can be added via the <code>mappings</code> bean property,
52  * as lines that follow the <code>mime.types<code> file format.
53  *
54  * @author Rob Harrop
55  * @author Juergen Hoeller
56  * @since 1.2
57  * @see #setMappingLocation
58  * @see #setMappings
59  * @see javax.activation.MimetypesFileTypeMap
60  */

61 public class ConfigurableMimeFileTypeMap extends FileTypeMap JavaDoc implements InitializingBean {
62
63     /**
64      * The <code>Resource</code> to load the mapping file from.
65      */

66     private Resource mappingLocation = new ClassPathResource("mime.types", getClass());
67
68     /**
69      * Used to configure additional mappings.
70      */

71     private String JavaDoc[] mappings;
72
73     /**
74      * The delegate FileTypeMap, compiled from the mappings in the mapping file
75      * and the entries in the <code>mappings</code> property.
76      */

77     private FileTypeMap JavaDoc fileTypeMap;
78
79
80     /**
81      * Specify the <code>Resource</code> from which mappings are loaded.
82      * Needs to follow the <code>mime.types<code> file format, as specified
83      * by the Java Activation Framework, containing lines such as:<br>
84      * <code>text/html html htm HTML HTM</code>
85      */

86     public void setMappingLocation(Resource mappingLocation) {
87         this.mappingLocation = mappingLocation;
88     }
89
90     /**
91      * Specify additional MIME type mappings as lines that follow the
92      * <code>mime.types<code> file format, as specified by the
93      * Java Activation Framework, for example:<br>
94      * <code>text/html html htm HTML HTM</code>
95      */

96     public void setMappings(String JavaDoc[] mappings) {
97         this.mappings = mappings;
98     }
99
100
101     /**
102      * Creates the final merged mapping set.
103      */

104     public void afterPropertiesSet() {
105         getFileTypeMap();
106     }
107
108     /**
109      * Return the delegate FileTypeMap, compiled from the mappings in the mapping file
110      * and the entries in the <code>mappings</code> property.
111      * @see #setMappingLocation
112      * @see #setMappings
113      * @see #createFileTypeMap
114      */

115     protected final FileTypeMap JavaDoc getFileTypeMap() {
116         if (this.fileTypeMap == null) {
117             try {
118                 this.fileTypeMap = createFileTypeMap(this.mappingLocation, this.mappings);
119             }
120             catch (IOException JavaDoc ex) {
121                 throw new IllegalStateException JavaDoc(
122                         "Could not load specified MIME type mapping file: " + this.mappingLocation);
123             }
124         }
125         return fileTypeMap;
126     }
127
128     /**
129      * Compile a FileTypeMap from the mappings in the given mapping file and the
130      * given mapping entries.
131      * <p>Default implementation creates an Activation Framework MimetypesFileTypeMap,
132      * passing in an InputStream from the mapping resource (if any) and registering
133      * the mapping lines programmatically.
134      * @param mappingLocation a <code>mime.types</code> mapping resource (can be <code>null</code>)
135      * @param mappings MIME type mapping lines (can be <code>null</code>)
136      * @return the compiled FileTypeMap
137      * @throws IOException if resource access failed
138      * @see javax.activation.MimetypesFileTypeMap#MimetypesFileTypeMap(java.io.InputStream)
139      * @see javax.activation.MimetypesFileTypeMap#addMimeTypes(String)
140      */

141     protected FileTypeMap JavaDoc createFileTypeMap(Resource mappingLocation, String JavaDoc[] mappings) throws IOException JavaDoc {
142         MimetypesFileTypeMap JavaDoc fileTypeMap = (mappingLocation != null) ?
143                 new MimetypesFileTypeMap JavaDoc(mappingLocation.getInputStream()) : new MimetypesFileTypeMap JavaDoc();
144         if (mappings != null) {
145             for (int i = 0; i < mappings.length; i++) {
146                 fileTypeMap.addMimeTypes(mappings[i]);
147             }
148         }
149         return fileTypeMap;
150     }
151
152
153     /**
154      * Delegates to the underlying FileTypeMap.
155      * @see #getFileTypeMap()
156      */

157     public String JavaDoc getContentType(File JavaDoc file) {
158         return getFileTypeMap().getContentType(file);
159     }
160
161     /**
162      * Delegates to the underlying FileTypeMap.
163      * @see #getFileTypeMap()
164      */

165     public String JavaDoc getContentType(String JavaDoc fileName) {
166         return getFileTypeMap().getContentType(fileName);
167     }
168
169 }
170
Popular Tags