View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.jetspeed.util;
19  
20  
21  /***
22   *
23   * <p>Utility class for declaring MIME types to use for various requests and provide
24   * utility manipulation methods.</p>
25   * <p>Added Content-Encoding capability, with defaults
26   *
27   * @author <a href="mailto:raphael@apache.org">Rapha\u00ebl Luta</a>
28   * @author <a href="mailto:sgala@apache.org">Santiago Gala</a>
29   * @version $Id: MimeType.java 516448 2007-03-09 16:25:47Z ate $
30   */
31  public class MimeType
32  {
33      
34      public static final MimeType HTML  = new MimeType("text/html", "UTF-8"); //FIXME: test
35      public static final MimeType XHTML = new MimeType("text/xhtml");
36      public static final MimeType WML   = new MimeType("text/vnd.wap.wml");
37      public static final MimeType XML   = new MimeType("text/xml");
38      public static final MimeType VXML  = new MimeType("text/vxml");
39      
40      /***
41       * Standard ContentType String, with no encoding appended.
42       */
43      private String mimeType = "";
44      /***
45       * null value means default encoding.
46       * Otherwise, charset to be used.
47       */
48      private String charSet = null;
49      
50      public MimeType(String mimeType)
51      {
52          if (mimeType == null)
53          {
54              throw new NullPointerException();
55          }
56          this.mimeType = mimeType;
57      }
58      
59      /***
60       *
61       */
62      public MimeType(String mimeType, String charSet)
63      {
64          if (mimeType == null)
65          {
66              throw new NullPointerException();
67          }
68          this.mimeType = mimeType;
69          this.charSet = charSet;
70      }
71      
72      /*** Extracts from this MimeType a user-friendly identifying code
73       * ie "html" for "text/html" or "wml" for "text/vnd.wap.wml"
74       *
75       * @return the simplified type
76       */
77      public String getCode()
78      {
79          String type = this.mimeType;
80          // get everything after "/"
81          type = type.substring(type.indexOf("/") + 1);
82          // remove any dot in the name
83          int idx = type.lastIndexOf(".");
84          if (idx >= 0)
85          {
86              type = type.substring(idx + 1);
87          }
88          //remove anything before a "-"
89          idx = type.lastIndexOf("-");
90          if (idx >= 0)
91          {
92              type = type.substring(idx + 1);
93          }
94          
95          return type.toLowerCase();
96      }
97      
98      /***
99       * Return the media type associated
100      */
101     public String getContentType()
102     {
103         return this.mimeType;
104     }
105     
106     /***
107      * Return the character encoding associated, if any
108      */
109     public String getCharSet()
110     {
111         return this.charSet;
112     }
113     
114     /***
115      * Convert this MimeType to its external String representation
116      */
117     public String toString()
118     {
119         if (null == this.charSet)
120         {
121             return this.mimeType;
122         }
123         return this.mimeType + "; charset=" + this.charSet;
124     }
125     
126     /***
127      * Compare one MimeType to another
128      */
129     public boolean equals(Object obj)
130     {
131         if (this == obj)
132         {
133             return true;
134         }
135         
136         if (obj instanceof MimeType)
137         {
138             MimeType comp = (MimeType) obj;
139             return this.toString().equals(comp.toString());
140         }
141         else
142         {
143             return false;
144         }
145     }
146     
147 }