|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ContentType.java | 64.3% | 97.3% | 100% | 90.8% |
|
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 |
/**
|
|
23 |
* Represents an HTTP content type. Allows to set various elements like
|
|
24 |
* the mime type, the character set, and other parameters.
|
|
25 |
* This is similar to a number of other implementations of the same concept in JAF, etc.
|
|
26 |
* We have created this simple implementation to avoid including the whole libraries.
|
|
27 |
*
|
|
28 |
* @author mindbridge
|
|
29 |
* @since 3.0
|
|
30 |
**/
|
|
31 |
public class ContentType |
|
32 |
{ |
|
33 |
private String _baseType;
|
|
34 |
private String _subType;
|
|
35 |
private Map _parameters;
|
|
36 |
|
|
37 |
/**
|
|
38 |
* Creates a new empty content type
|
|
39 |
*/
|
|
40 | 338 |
public ContentType()
|
41 |
{ |
|
42 | 338 |
initialize(); |
43 |
} |
|
44 |
|
|
45 |
/**
|
|
46 |
* Creates a new content type from the argument.
|
|
47 |
* The format of the argument has to be basetype/subtype(;key=value)*
|
|
48 |
*
|
|
49 |
* @param contentType the content type that needs to be represented
|
|
50 |
*/
|
|
51 | 336 |
public ContentType(String contentType)
|
52 |
{ |
|
53 | 336 |
this();
|
54 | 336 |
parse(contentType); |
55 |
} |
|
56 |
|
|
57 | 674 |
private void initialize() |
58 |
{ |
|
59 | 674 |
_baseType = "";
|
60 | 674 |
_subType = "";
|
61 | 674 |
_parameters = new HashMap();
|
62 |
} |
|
63 |
|
|
64 |
/**
|
|
65 |
* @return the base type of the content type
|
|
66 |
*/
|
|
67 | 2 |
public String getBaseType()
|
68 |
{ |
|
69 | 2 |
return _baseType;
|
70 |
} |
|
71 |
|
|
72 |
/**
|
|
73 |
* @param baseType
|
|
74 |
*/
|
|
75 | 338 |
public void setBaseType(String baseType) |
76 |
{ |
|
77 | 338 |
_baseType = baseType; |
78 |
} |
|
79 |
|
|
80 |
/**
|
|
81 |
* @return the sub-type of the content type
|
|
82 |
*/
|
|
83 | 2 |
public String getSubType()
|
84 |
{ |
|
85 | 2 |
return _subType;
|
86 |
} |
|
87 |
|
|
88 |
/**
|
|
89 |
* @param subType
|
|
90 |
*/
|
|
91 | 338 |
public void setSubType(String subType) |
92 |
{ |
|
93 | 338 |
_subType = subType; |
94 |
} |
|
95 |
|
|
96 |
/**
|
|
97 |
* @return the MIME type of the content type
|
|
98 |
*/
|
|
99 | 510 |
public String getMimeType()
|
100 |
{ |
|
101 | 510 |
return _baseType + "/" + _subType; |
102 |
} |
|
103 |
|
|
104 |
/**
|
|
105 |
* @return the list of names of parameters in this content type
|
|
106 |
*/
|
|
107 | 484 |
public String[] getParameterNames()
|
108 |
{ |
|
109 | 484 |
Set parameterNames = _parameters.keySet(); |
110 | 484 |
return (String[]) parameterNames.toArray(new String[parameterNames.size()]); |
111 |
} |
|
112 |
|
|
113 |
/**
|
|
114 |
* @param key the name of the content type parameter
|
|
115 |
* @return the value of the content type parameter
|
|
116 |
*/
|
|
117 | 805 |
public String getParameter(String key)
|
118 |
{ |
|
119 | 805 |
return (String) _parameters.get(key);
|
120 |
} |
|
121 |
|
|
122 |
/**
|
|
123 |
* @param key the name of the content type parameter
|
|
124 |
* @param value the value of the content type parameter
|
|
125 |
*/
|
|
126 | 329 |
public void setParameter(String key, String value) |
127 |
{ |
|
128 | 329 |
_parameters.put(key.toLowerCase(), value); |
129 |
} |
|
130 |
|
|
131 |
/**
|
|
132 |
* Parses the argument and configures the content type accordingly.
|
|
133 |
* The format of the argument has to be type/subtype(;key=value)*
|
|
134 |
*
|
|
135 |
* @param contentType the content type that needs to be represented
|
|
136 |
*/
|
|
137 | 336 |
public void parse(String contentType) |
138 |
{ |
|
139 | 336 |
initialize(); |
140 |
|
|
141 | 336 |
StringTokenizer tokens = new StringTokenizer(contentType, ";"); |
142 | 336 |
if (!tokens.hasMoreTokens())
|
143 | 0 |
return;
|
144 |
|
|
145 | 336 |
String mimeType = tokens.nextToken(); |
146 | 336 |
StringTokenizer mimeTokens = new StringTokenizer(mimeType, "/"); |
147 | 336 |
setBaseType(mimeTokens.hasMoreTokens() ? mimeTokens.nextToken() : "");
|
148 | 336 |
setSubType(mimeTokens.hasMoreTokens() ? mimeTokens.nextToken() : "");
|
149 |
|
|
150 | 336 |
while (tokens.hasMoreTokens()) {
|
151 | 170 |
String parameter = tokens.nextToken(); |
152 |
|
|
153 | 170 |
StringTokenizer parameterTokens = new StringTokenizer(parameter, "="); |
154 | 170 |
String key = parameterTokens.hasMoreTokens() ? parameterTokens.nextToken() : "";
|
155 | 170 |
String value = parameterTokens.hasMoreTokens() ? parameterTokens.nextToken() : "";
|
156 | 170 |
setParameter(key, value); |
157 |
} |
|
158 |
} |
|
159 |
|
|
160 |
|
|
161 |
|
|
162 |
/**
|
|
163 |
* @return the string representation of this content type
|
|
164 |
*/
|
|
165 | 482 |
public String unparse()
|
166 |
{ |
|
167 | 482 |
StringBuffer buf = new StringBuffer(getMimeType());
|
168 |
|
|
169 | 482 |
String[] parameterNames = getParameterNames(); |
170 | 482 |
for (int i = 0; i < parameterNames.length; i++) |
171 |
{ |
|
172 | 475 |
String key = parameterNames[i]; |
173 | 475 |
String value = getParameter(key); |
174 | 475 |
buf.append(";" + key + "=" + value); |
175 |
} |
|
176 |
|
|
177 | 482 |
return buf.toString();
|
178 |
} |
|
179 |
|
|
180 |
/**
|
|
181 |
* @return the string representation of this content type. Same as unparse().
|
|
182 |
*/
|
|
183 | 480 |
public String toString()
|
184 |
{ |
|
185 | 480 |
return unparse();
|
186 |
} |
|
187 |
|
|
188 |
} |
|
189 |
|
|