View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package javax.mail.internet;
21  
22  // can be in the form major/minor; charset=jobby
23  
24  /**
25   * @version $Rev: 669445 $ $Date: 2008-06-19 06:48:18 -0400 (Thu, 19 Jun 2008) $
26   */
27  public class ContentType {
28      private ParameterList _list;
29      private String _minor;
30      private String _major;
31  
32      public ContentType() {
33          // the Sun version makes everything null here.
34      }
35  
36      public ContentType(String major, String minor, ParameterList list) {
37          _major = major;
38          _minor = minor;
39          _list = list;
40      }
41  
42      public ContentType(String type) throws ParseException {
43          // get a token parser for the type information
44          HeaderTokenizer tokenizer = new HeaderTokenizer(type, HeaderTokenizer.MIME);
45  
46          // get the first token, which must be an ATOM
47          HeaderTokenizer.Token token = tokenizer.next();
48          if (token.getType() != HeaderTokenizer.Token.ATOM) {
49              throw new ParseException("Invalid content type");
50          }
51  
52          _major = token.getValue();
53  
54          // the MIME type must be major/minor
55          token = tokenizer.next();
56          if (token.getType() != '/') {
57              throw new ParseException("Invalid content type");
58          }
59  
60  
61          // this must also be an atom.  Content types are not permitted to be wild cards.
62          token = tokenizer.next();
63          if (token.getType() != HeaderTokenizer.Token.ATOM) {
64              throw new ParseException("Invalid content type");
65          }
66  
67          _minor = token.getValue();
68  
69          // the remainder is parameters, which ParameterList will take care of parsing.
70          String remainder = tokenizer.getRemainder();
71          if (remainder != null) {
72              _list = new ParameterList(remainder);
73          }
74      }
75  
76      public String getPrimaryType() {
77          return _major;
78      }
79  
80      public String getSubType() {
81          return _minor;
82      }
83  
84      public String getBaseType() {
85          return _major + "/" + _minor;
86      }
87  
88      public String getParameter(String name) {
89          return (_list == null ? null : _list.get(name));
90      }
91  
92      public ParameterList getParameterList() {
93          return _list;
94      }
95  
96      public void setPrimaryType(String major) {
97          _major = major;
98      }
99  
100     public void setSubType(String minor) {
101         _minor = minor;
102     }
103 
104     public void setParameter(String name, String value) {
105         if (_list == null) {
106             _list = new ParameterList();
107         }
108         _list.set(name, value);
109     }
110 
111     public void setParameterList(ParameterList list) {
112         _list = list;
113     }
114 
115     public String toString() {
116         if (_major == null || _minor == null) {
117             return null;
118         }
119         
120         // We need to format this as if we're doing it to set into the Content-Type
121         // header.  So the parameter list gets added on as if the header name was 
122         // also included. 
123         String baseType = getBaseType(); 
124         if (_list != null) {
125             baseType += _list.toString(baseType.length() + "Content-Type: ".length()); 
126         }
127         
128         return baseType;
129     }
130 
131     public boolean match(ContentType other) {
132         return _major.equalsIgnoreCase(other._major)
133                 && (_minor.equalsIgnoreCase(other._minor)
134                 || _minor.equals("*")
135                 || other._minor.equals("*"));
136     }
137 
138     public boolean match(String contentType) {
139         try {
140             return match(new ContentType(contentType));
141         } catch (ParseException e) {
142             return false;
143         }
144     }
145 }