Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
ContentType |
|
| 1.5714285714285714;1.571 |
1 | // Copyright 2004, 2005 The Apache Software Foundation |
|
2 | // |
|
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
|
4 | // you may not use this file except in compliance with the License. |
|
5 | // You may obtain a copy of the License at |
|
6 | // |
|
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
|
8 | // |
|
9 | // Unless required by applicable law or agreed to in writing, software |
|
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
12 | // See the License for the specific language governing permissions and |
|
13 | // limitations under the License. |
|
14 | ||
15 | package org.apache.tapestry.util; |
|
16 | ||
17 | import java.util.HashMap; |
|
18 | import java.util.Map; |
|
19 | import java.util.Set; |
|
20 | import java.util.StringTokenizer; |
|
21 | ||
22 | import org.apache.hivemind.util.Defense; |
|
23 | ||
24 | /** |
|
25 | * Represents an HTTP content type. Allows to set various elements like the mime type, the character |
|
26 | * set, and other parameters. This is similar to a number of other implementations of the same |
|
27 | * concept in JAF, etc. We have created this simple implementation to avoid including the whole |
|
28 | * libraries. |
|
29 | * |
|
30 | * @author mindbridge |
|
31 | * @since 3.0 |
|
32 | */ |
|
33 | public class ContentType |
|
34 | { |
|
35 | 30 | private String _baseType = ""; |
36 | ||
37 | 30 | private String _subType = ""; |
38 | ||
39 | 30 | private final Map _parameters = new HashMap(); |
40 | ||
41 | /** |
|
42 | * Creates a new empty content type. |
|
43 | */ |
|
44 | public ContentType() |
|
45 | 30 | { |
46 | 30 | } |
47 | ||
48 | /** |
|
49 | * Creates a new content type from the argument. The format of the argument has to be |
|
50 | * basetype/subtype(;key=value)* |
|
51 | * |
|
52 | * @param contentType |
|
53 | * the content type that needs to be represented |
|
54 | */ |
|
55 | public ContentType(String contentType) |
|
56 | { |
|
57 | 28 | this(); |
58 | 28 | parse(contentType); |
59 | 28 | } |
60 | ||
61 | /** |
|
62 | * Returns true only if the other object is another instance of ContentType, and has the ssame |
|
63 | * baseType, subType and set of parameters. |
|
64 | */ |
|
65 | public boolean equals(Object o) |
|
66 | { |
|
67 | 16 | if (o == null) |
68 | 1 | return false; |
69 | ||
70 | 15 | if (o.getClass() != this.getClass()) |
71 | 1 | return false; |
72 | ||
73 | 14 | ContentType ct = (ContentType) o; |
74 | ||
75 | 14 | return _baseType.equals(ct._baseType) && _subType.equals(ct._subType) |
76 | && _parameters.equals(ct._parameters); |
|
77 | } |
|
78 | ||
79 | /** |
|
80 | * @return the base type of the content type |
|
81 | */ |
|
82 | public String getBaseType() |
|
83 | { |
|
84 | 2 | return _baseType; |
85 | } |
|
86 | ||
87 | /** |
|
88 | * @param baseType |
|
89 | */ |
|
90 | public void setBaseType(String baseType) |
|
91 | { |
|
92 | 30 | Defense.notNull(baseType, "baseType"); |
93 | ||
94 | 30 | _baseType = baseType; |
95 | 30 | } |
96 | ||
97 | /** |
|
98 | * @return the sub-type of the content type |
|
99 | */ |
|
100 | public String getSubType() |
|
101 | { |
|
102 | 2 | return _subType; |
103 | } |
|
104 | ||
105 | /** |
|
106 | * @param subType |
|
107 | */ |
|
108 | public void setSubType(String subType) |
|
109 | { |
|
110 | 30 | Defense.notNull(subType, "subType"); |
111 | ||
112 | 30 | _subType = subType; |
113 | 30 | } |
114 | ||
115 | /** |
|
116 | * @return the MIME type of the content type |
|
117 | */ |
|
118 | public String getMimeType() |
|
119 | { |
|
120 | 16 | return _baseType + "/" + _subType; |
121 | } |
|
122 | ||
123 | /** |
|
124 | * @return the list of names of parameters in this content type |
|
125 | */ |
|
126 | public String[] getParameterNames() |
|
127 | { |
|
128 | 14 | Set parameterNames = _parameters.keySet(); |
129 | 14 | return (String[]) parameterNames.toArray(new String[parameterNames.size()]); |
130 | } |
|
131 | ||
132 | /** |
|
133 | * @param key |
|
134 | * the name of the content type parameter |
|
135 | * @return the value of the content type parameter |
|
136 | */ |
|
137 | public String getParameter(String key) |
|
138 | { |
|
139 | 5 | Defense.notNull(key, "key"); |
140 | ||
141 | 5 | return (String) _parameters.get(key); |
142 | } |
|
143 | ||
144 | /** |
|
145 | * @param key |
|
146 | * the name of the content type parameter |
|
147 | * @param value |
|
148 | * the value of the content type parameter |
|
149 | */ |
|
150 | public void setParameter(String key, String value) |
|
151 | { |
|
152 | 16 | Defense.notNull(key, "key"); |
153 | 16 | Defense.notNull(value, "value"); |
154 | ||
155 | 16 | _parameters.put(key.toLowerCase(), value); |
156 | 16 | } |
157 | ||
158 | /** |
|
159 | * Parses the argument and configures the content type accordingly. The format of the argument |
|
160 | * has to be type/subtype(;key=value)* |
|
161 | * |
|
162 | * @param contentType |
|
163 | * the content type that needs to be represented |
|
164 | */ |
|
165 | public void parse(String contentType) |
|
166 | { |
|
167 | 28 | _baseType = ""; |
168 | 28 | _subType = ""; |
169 | 28 | _parameters.clear(); |
170 | ||
171 | 28 | StringTokenizer tokens = new StringTokenizer(contentType, ";"); |
172 | 28 | if (!tokens.hasMoreTokens()) |
173 | 0 | return; |
174 | ||
175 | 28 | String mimeType = tokens.nextToken(); |
176 | 28 | StringTokenizer mimeTokens = new StringTokenizer(mimeType, "/"); |
177 | 28 | setBaseType(mimeTokens.hasMoreTokens() ? mimeTokens.nextToken() : ""); |
178 | 28 | setSubType(mimeTokens.hasMoreTokens() ? mimeTokens.nextToken() : ""); |
179 | ||
180 | 43 | while (tokens.hasMoreTokens()) |
181 | { |
|
182 | 15 | String parameter = tokens.nextToken(); |
183 | ||
184 | 15 | StringTokenizer parameterTokens = new StringTokenizer(parameter, "="); |
185 | 15 | String key = parameterTokens.hasMoreTokens() ? parameterTokens.nextToken() : ""; |
186 | 15 | String value = parameterTokens.hasMoreTokens() ? parameterTokens.nextToken() : ""; |
187 | 15 | setParameter(key, value); |
188 | 15 | } |
189 | 28 | } |
190 | ||
191 | /** |
|
192 | * @return the string representation of this content type |
|
193 | */ |
|
194 | public String unparse() |
|
195 | { |
|
196 | 12 | StringBuffer buf = new StringBuffer(getMimeType()); |
197 | ||
198 | 12 | String[] parameterNames = getParameterNames(); |
199 | 13 | for (int i = 0; i < parameterNames.length; i++) |
200 | { |
|
201 | 1 | String key = parameterNames[i]; |
202 | 1 | String value = getParameter(key); |
203 | 1 | buf.append(";" + key + "=" + value); |
204 | } |
|
205 | ||
206 | 12 | return buf.toString(); |
207 | } |
|
208 | ||
209 | /** |
|
210 | * @return the string representation of this content type. Same as unparse(). |
|
211 | */ |
|
212 | public String toString() |
|
213 | { |
|
214 | 10 | return unparse(); |
215 | } |
|
216 | ||
217 | } |