author | chegar |
Sun, 17 Aug 2014 15:52:15 +0100 | |
changeset 25871 | b80b84e87032 |
parent 22679 | jaxws/src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/packaging/mime/internet/ContentType.java@d785acd84a14 |
permissions | -rw-r--r-- |
12009 | 1 |
/* |
22679
d785acd84a14
8032639: Update copyright year to match last edit in jaxws repository for 2013
mkos
parents:
16791
diff
changeset
|
2 |
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. |
12009 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
/* |
|
27 |
* @(#)ContentType.java 1.7 02/03/27 |
|
28 |
*/ |
|
29 |
||
30 |
||
31 |
||
32 |
package com.sun.xml.internal.messaging.saaj.packaging.mime.internet; |
|
33 |
||
34 |
||
35 |
/** |
|
36 |
* This class represents a MIME ContentType value. It provides |
|
37 |
* methods to parse a ContentType string into individual components |
|
38 |
* and to generate a MIME style ContentType string. |
|
39 |
* |
|
40 |
* @version 1.7, 02/03/27 |
|
41 |
* @author John Mani |
|
42 |
*/ |
|
43 |
public final class ContentType { |
|
44 |
||
45 |
private String primaryType; // primary type |
|
46 |
private String subType; // subtype |
|
47 |
private ParameterList list; // parameter list |
|
48 |
||
49 |
/** |
|
50 |
* No-arg Constructor. |
|
51 |
*/ |
|
52 |
public ContentType() { } |
|
53 |
||
54 |
/** |
|
55 |
* Constructor. |
|
56 |
* |
|
57 |
* @param primaryType primary type |
|
58 |
* @param subType subType |
|
59 |
* @param list ParameterList |
|
60 |
*/ |
|
61 |
public ContentType(String primaryType, String subType, |
|
62 |
ParameterList list) { |
|
63 |
this.primaryType = primaryType; |
|
64 |
this.subType = subType; |
|
65 |
if (list == null) |
|
66 |
list = new ParameterList(); |
|
67 |
this.list = list; |
|
68 |
} |
|
69 |
||
70 |
/** |
|
71 |
* Constructor that takes a Content-Type string. The String |
|
72 |
* is parsed into its constituents: primaryType, subType |
|
73 |
* and parameters. A ParseException is thrown if the parse fails. |
|
74 |
* |
|
75 |
* @param s the Content-Type string. |
|
76 |
* @exception ParseException if the parse fails. |
|
77 |
*/ |
|
78 |
public ContentType(String s) throws ParseException { |
|
79 |
HeaderTokenizer h = new HeaderTokenizer(s, HeaderTokenizer.MIME); |
|
80 |
HeaderTokenizer.Token tk; |
|
81 |
||
82 |
// First "type" .. |
|
83 |
tk = h.next(); |
|
84 |
if (tk.getType() != HeaderTokenizer.Token.ATOM) |
|
85 |
throw new ParseException(); |
|
86 |
primaryType = tk.getValue(); |
|
87 |
||
88 |
// The '/' separator .. |
|
89 |
tk = h.next(); |
|
90 |
if ((char)tk.getType() != '/') |
|
91 |
throw new ParseException(); |
|
92 |
||
93 |
// Then "subType" .. |
|
94 |
tk = h.next(); |
|
95 |
if (tk.getType() != HeaderTokenizer.Token.ATOM) |
|
96 |
throw new ParseException(); |
|
97 |
subType = tk.getValue(); |
|
98 |
||
99 |
// Finally parameters .. |
|
100 |
String rem = h.getRemainder(); |
|
101 |
if (rem != null) |
|
102 |
list = new ParameterList(rem); |
|
103 |
} |
|
104 |
||
105 |
public ContentType copy() { |
|
106 |
return new ContentType(primaryType,subType,list.copy()); |
|
107 |
} |
|
108 |
||
109 |
/** |
|
110 |
* Return the primary type. |
|
111 |
* @return the primary type |
|
112 |
*/ |
|
113 |
public String getPrimaryType() { |
|
114 |
return primaryType; |
|
115 |
} |
|
116 |
||
117 |
/** |
|
118 |
* Return the subType. |
|
119 |
* @return the subType |
|
120 |
*/ |
|
121 |
public String getSubType() { |
|
122 |
return subType; |
|
123 |
} |
|
124 |
||
125 |
/** |
|
126 |
* Return the MIME type string, without the parameters. |
|
127 |
* The returned value is basically the concatenation of |
|
128 |
* the primaryType, the '/' character and the secondaryType. |
|
129 |
* |
|
130 |
* @return the type |
|
131 |
*/ |
|
132 |
public String getBaseType() { |
|
133 |
return primaryType + '/' + subType; |
|
134 |
} |
|
135 |
||
136 |
/** |
|
137 |
* Return the specified parameter value. Returns <code>null</code> |
|
138 |
* if this parameter is absent. |
|
139 |
* @return parameter value |
|
140 |
*/ |
|
141 |
public String getParameter(String name) { |
|
142 |
if (list == null) |
|
143 |
return null; |
|
144 |
||
145 |
return list.get(name); |
|
146 |
} |
|
147 |
||
148 |
/** |
|
149 |
* Return a ParameterList object that holds all the available |
|
150 |
* parameters. Returns null if no parameters are available. |
|
151 |
* |
|
152 |
* @return ParameterList |
|
153 |
*/ |
|
154 |
public ParameterList getParameterList() { |
|
155 |
return list; |
|
156 |
} |
|
157 |
||
158 |
/** |
|
159 |
* Set the primary type. Overrides existing primary type. |
|
160 |
* @param primaryType primary type |
|
161 |
*/ |
|
162 |
public void setPrimaryType(String primaryType) { |
|
163 |
this.primaryType = primaryType; |
|
164 |
} |
|
165 |
||
166 |
/** |
|
167 |
* Set the subType. Overrides existing subType |
|
168 |
* @param subType subType |
|
169 |
*/ |
|
170 |
public void setSubType(String subType) { |
|
171 |
this.subType = subType; |
|
172 |
} |
|
173 |
||
174 |
/** |
|
175 |
* Set the specified parameter. If this parameter already exists, |
|
176 |
* it is replaced by this new value. |
|
177 |
* |
|
178 |
* @param name parameter name |
|
179 |
* @param value parameter value |
|
180 |
*/ |
|
181 |
public void setParameter(String name, String value) { |
|
182 |
if (list == null) |
|
183 |
list = new ParameterList(); |
|
184 |
||
185 |
list.set(name, value); |
|
186 |
} |
|
187 |
||
188 |
/** |
|
189 |
* Set a new ParameterList. |
|
190 |
* @param list ParameterList |
|
191 |
*/ |
|
192 |
public void setParameterList(ParameterList list) { |
|
193 |
this.list = list; |
|
194 |
} |
|
195 |
||
196 |
/** |
|
197 |
* Retrieve a RFC2045 style string representation of |
|
198 |
* this Content-Type. Returns <code>null</code> if |
|
199 |
* the conversion failed. |
|
200 |
* |
|
201 |
* @return RFC2045 style string |
|
202 |
*/ |
|
203 |
public String toString() { |
|
204 |
if (primaryType == null || subType == null) // need both |
|
205 |
return null; |
|
206 |
||
207 |
StringBuffer sb = new StringBuffer(); |
|
208 |
sb.append(primaryType).append('/').append(subType); |
|
209 |
if (list != null) |
|
210 |
// Http Binding section of the "SOAP with attachments" specification says, |
|
211 |
// "SOAP message senders should send Content-Type headers on a single long line." |
|
212 |
// (http://www.w3.org/TR/SOAP-attachments#HTTPBinding) |
|
213 |
sb.append(list.toString()); |
|
214 |
||
215 |
return sb.toString(); |
|
216 |
} |
|
217 |
||
218 |
/** |
|
219 |
* Match with the specified ContentType object. This method |
|
220 |
* compares <strong>only the <code>primaryType</code> and |
|
221 |
* <code>subType</code> </strong>. The parameters of both operands |
|
222 |
* are ignored. <p> |
|
223 |
* |
|
224 |
* For example, this method will return <code>true</code> when |
|
225 |
* comparing the ContentTypes for <strong>"text/plain"</strong> |
|
226 |
* and <strong>"text/plain; charset=foobar"</strong>. |
|
227 |
* |
|
228 |
* If the <code>subType</code> of either operand is the special |
|
229 |
* character '*', then the subtype is ignored during the match. |
|
230 |
* For example, this method will return <code>true</code> when |
|
231 |
* comparing the ContentTypes for <strong>"text/plain"</strong> |
|
232 |
* and <strong>"text/*" </strong> |
|
233 |
* |
|
234 |
* @param cType to compare this against |
|
235 |
*/ |
|
236 |
public boolean match(ContentType cType) { |
|
237 |
// Match primaryType |
|
238 |
if (!primaryType.equalsIgnoreCase(cType.getPrimaryType())) |
|
239 |
return false; |
|
240 |
||
241 |
String sType = cType.getSubType(); |
|
242 |
||
243 |
// If either one of the subTypes is wildcarded, return true |
|
244 |
if ((subType.charAt(0) == '*') || (sType.charAt(0) == '*')) |
|
245 |
return true; |
|
246 |
||
247 |
// Match subType |
|
248 |
if (!subType.equalsIgnoreCase(sType)) |
|
249 |
return false; |
|
250 |
||
251 |
return true; |
|
252 |
} |
|
253 |
||
254 |
/** |
|
255 |
* Match with the specified content-type string. This method |
|
256 |
* compares <strong>only the <code>primaryType</code> and |
|
257 |
* <code>subType</code> </strong>. |
|
258 |
* The parameters of both operands are ignored. <p> |
|
259 |
* |
|
260 |
* For example, this method will return <code>true</code> when |
|
261 |
* comparing the ContentType for <strong>"text/plain"</strong> |
|
262 |
* with <strong>"text/plain; charset=foobar"</strong>. |
|
263 |
* |
|
264 |
* If the <code>subType</code> of either operand is the special |
|
265 |
* character '*', then the subtype is ignored during the match. |
|
266 |
* For example, this method will return <code>true</code> when |
|
267 |
* comparing the ContentType for <strong>"text/plain"</strong> |
|
268 |
* with <strong>"text/*" </strong> |
|
269 |
*/ |
|
270 |
public boolean match(String s) { |
|
271 |
try { |
|
272 |
return match(new ContentType(s)); |
|
273 |
} catch (ParseException pex) { |
|
274 |
return false; |
|
275 |
} |
|
276 |
} |
|
277 |
} |