KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > common > MediaType


1 /*
2  * ========================================================================
3  *
4  * *
5  * JBoss Portal: The OpenSource Portal *
6  * *
7  * Distributable under LGPL license. *
8  * See terms of license at gnu.org. *
9  * *
10  *
11  * Copyright (c) Unpublished Work of Novell, Inc. All Rights Reserved.
12  *
13  * ========================================================================
14  */

15
16 package org.jboss.portal.common;
17
18 import javax.activation.MimeType JavaDoc;
19 import javax.activation.MimeTypeParseException JavaDoc;
20 import java.util.Arrays JavaDoc;
21 import java.util.Collections JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25
26 /**
27  * This is a immutable wrapper to the activation MimeTpye.
28  * <p>This class contains some extensions to the activation MimeType, such as the typesafe enum
29  * pattern, and allows for a mime type to specify allowed sub types.</p>
30  *
31  * @author <a HREF="mailto:mholzner@novell.com">Martin Holzner</a>
32  * @version $LastChangedRevision: 695 $, $LastChangedDate: 2004-12-15 14:41:10 -0500 (Wed, 15 Dec 2004) $
33  * @see javax.activation.MimeType
34  * see also ftp://ftp.isi.edu/in-notes/iana/assignments/media-types/
35  */

36 public final class MediaType
37 {
38    private static Map JavaDoc allowedTypes = new HashMap JavaDoc();
39    private static Map JavaDoc supportedExtensions = new HashMap JavaDoc();
40
41    /**
42     * Mime type 'Any' maps to an accept mime type of '*.*' (used by IE for css, images, etc.)
43     */

44    public static final MediaType ANY =
45       new MediaType("*", "*", new String JavaDoc[]{});
46
47    /**
48     * Mime type xhtml
49     */

50    public static final MediaType XHTML = new MediaType("application", "xhtml+xml",
51       new String JavaDoc[]{"xhtml"});
52
53    /**
54     * Mime type html
55     */

56    public static final MediaType HTML = new MediaType("text", "html",
57       new String JavaDoc[]{"html", "htm"},
58       new MediaType[]{XHTML});
59
60    /**
61     * Mime type form (application/x-www-form-urlencoded)
62     */

63    public static final MediaType FORM =
64       new MediaType("application", "x-www-form-urlencoded", new String JavaDoc[]{});
65
66    /**
67     * Mime type xml
68     */

69    public static final MediaType XML = new MediaType("text", "xml", new String JavaDoc[]{"xml"},
70       new MediaType[]{XHTML});
71
72    /**
73     * Mime type wml
74     */

75    public static final MediaType WML = new MediaType("text", "vnd.wap.wml",
76       new String JavaDoc[]{"wml"});
77
78    /**
79     * Mime type css
80     */

81    public static final MediaType CSS = new MediaType("text", "css",
82       new String JavaDoc[]{"css"});
83
84    /**
85     * Mime type text
86     */

87    public static final MediaType TEXT = new MediaType("text", "plain",
88       new String JavaDoc[]{"txt"});
89
90    /**
91     * Mime type js
92     */

93    public static final MediaType JS = new MediaType("text", "javascript",
94       new String JavaDoc[]{"js"});
95
96    /**
97     * Mime type svg
98     */

99    public static final MediaType SVG = new MediaType("image", "svg+xml",
100       new String JavaDoc[]{"svg"});
101
102    /**
103     * Mime type jpeg
104     */

105    public static final MediaType JPEG = new MediaType("image", "jpeg",
106       new String JavaDoc[]{"jpeg", "jpg"});
107
108    /**
109     * Mime type gif
110     */

111    public static final MediaType GIF = new MediaType("image", "gif",
112       new String JavaDoc[]{"gif"});
113
114    /**
115     * Mime type png
116     */

117    public static final MediaType PNG = new MediaType("image", "png",
118       new String JavaDoc[]{"png"});
119
120    /**
121     * Mime type wbmp
122     */

123    public static final MediaType WBMP = new MediaType("image", "vnd.wap.wbmp",
124       new String JavaDoc[]{"wbpm"});
125
126    /**
127     * Mime type rss
128     */

129    public static final MediaType RSS = new MediaType("application", "rss+xml",
130       new String JavaDoc[]{});
131
132    /**
133     * Mime type ico (see http://filext.com/detaillist.php?extdetail=ICO)
134     */

135    public static final MediaType ICO = new MediaType("application", "octet-stream",
136       new String JavaDoc[]{"ico"});
137    private MimeType JavaDoc m_mimeType = null;
138    private MediaType[] m_allowedSubTypes;
139
140    /**
141     * Construct a mime type instance without any allowed subtypes.
142     *
143     * @param primaryType the primary type of the mime type (i.e. 'text')
144     * @param subType the sub type of the mime type (i.e. 'html')
145     */

146    private MediaType(String JavaDoc primaryType, String JavaDoc subType, String JavaDoc[] extensions)
147    {
148       try
149       {
150          m_mimeType = new MimeType JavaDoc(primaryType, subType);
151          m_allowedSubTypes = null;
152          allowedTypes.put(m_mimeType.getBaseType(), this);
153          for (int i = 0; i < extensions.length; i++)
154          {
155             supportedExtensions.put(extensions[i], this);
156          }
157       }
158       catch (MimeTypeParseException JavaDoc e)
159       {
160          // +++TODO handle this , but where ?
161
System.out.println(e.getMessage());
162       }
163    }
164
165    /**
166     * Construct a mime type with the provided allowed subtypes.
167     *
168     * @param primaryType the primary type of the mime type (i.e. 'text')
169     * @param subType the sub type of the mime type (i.e. 'html')
170     * @param allowedSubTypes an array of <code>MediaType</code>s to allow as valid subtypes of this type
171     */

172    private MediaType(String JavaDoc primaryType, String JavaDoc subType, String JavaDoc[] extensions, MediaType[] allowedSubTypes)
173    {
174       this(primaryType, subType, extensions);
175       // only if the mime type was sucessfully created
176
if (m_mimeType != null)
177       {
178          m_allowedSubTypes = allowedSubTypes;
179       }
180    }
181
182    /**
183     * Get the mime type for the presented string, if the string contains a valid mime type.
184     *
185     * @param mimeType the <code>java.lang.String</code> to parse into a <code>RegistryMimeTpye</code>
186     * @return the <code>RegistryMimeTpye</code> that matches with the presented string
187     * @throws MimeTypeParseException if the presented mimetype is not supported
188     * @throws IllegalArgumentException if the presented string is null or empty
189     */

190    public static MediaType parseMimeType(String JavaDoc mimeType) throws MimeTypeParseException JavaDoc
191    {
192       if (mimeType == null || "".equals(mimeType))
193       {
194          throw new IllegalArgumentException JavaDoc("no valid mime type provided");
195       }
196
197       String JavaDoc type = mimeType.trim().toLowerCase();
198       if (allowedTypes.keySet().contains(type))
199       {
200          return (MediaType)allowedTypes.get(type);
201       }
202
203       throw new MimeTypeParseException JavaDoc("Type [" + mimeType + "] not supported");
204    }
205
206    /**
207     * Get the mime type for the presented string.
208     * <p>The string is handles as a file name extension. example: 'xml' returns MediaType.XML</p>
209     *
210     * @param extension the <code>java.lang.String</code> to parse into a <code>RegistryMimeTpye</code>
211     * @return the <code>RegistryMimeTpye</code> that matches with the presented string
212     * @throws MimeTypeParseException if the presented mimetype is not supported
213     * @throws IllegalArgumentException if the presented string is null or empty
214     */

215    public static MediaType parseMimeTypeByExtension(String JavaDoc extension)
216       throws MimeTypeParseException JavaDoc
217    {
218       if (extension == null || "".equals(extension))
219       {
220          throw new IllegalArgumentException JavaDoc("no valid mime type provided [" + extension + "]");
221       }
222
223       String JavaDoc ext = extension.trim().toLowerCase();
224       if (supportedExtensions.keySet().contains(ext))
225       {
226          return (MediaType)supportedExtensions.get(ext);
227       }
228
229       throw new MimeTypeParseException JavaDoc("Extension [" + extension + "] not supported");
230    }
231
232    /**
233     * Get a list of allowed sub types for the passed mime type.
234     *
235     * @param mimeType the <code>RegistryMimeTpye</code> to get the list of allowed subtypes for
236     * @return a <code>java.util.List</code> of <code>PortalMimeTpye</code>s
237     */

238    public static List JavaDoc getAllowedSubTypes(MediaType mimeType)
239    {
240       if (mimeType.m_allowedSubTypes == null)
241       {
242          return Collections.EMPTY_LIST;
243       }
244       else
245       {
246          return Collections.unmodifiableList(Arrays.asList(mimeType.m_allowedSubTypes));
247       }
248    }
249
250    /**
251     * Get a list of allowed sub types for for this mime type.
252     *
253     * @return a <code>java.util.List</code> of <code>RegistryMimeTpye</code>s
254     */

255    public List JavaDoc getAllowedSubTypes()
256    {
257       if (m_allowedSubTypes == null)
258       {
259          return Collections.EMPTY_LIST;
260       }
261       else
262       {
263          return Collections.unmodifiableList(Arrays.asList(m_allowedSubTypes));
264       }
265    }
266
267    /**
268     * Get the String representation of the mime type (i.e. 'text/html').
269     *
270     * @return the mime type as a <code>java/lang.String</code>
271     * @see java.lang.Object#toString
272     */

273    public String JavaDoc toString()
274    {
275       return m_mimeType.getBaseType();
276    }
277
278    /**
279     * compare the parameter with this instance and see if they are equals.
280     *
281     * @param o the Object to compare this instance to
282     * @return true if this and the paramters o are equal
283     * @see java.lang.Object#equals
284     */

285    public boolean equals(Object JavaDoc o)
286    {
287       if (this == o)
288       {
289          return true;
290       }
291       if (!(o instanceof MediaType))
292       {
293          return false;
294       }
295
296       final MediaType type = (MediaType)o;
297
298       return m_mimeType.equals(type.m_mimeType);
299    }
300
301    /**
302     * Get the hascode for this mime type.
303     *
304     * @return an int value representing this instance
305     * @see java.lang.Object#hashCode
306     */

307    public int hashCode()
308    {
309       return m_mimeType.hashCode();
310    }
311 }
312
Popular Tags