1 /*
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
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
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 /**
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 StringBuilder sb = new StringBuilder();
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 }
|
1 /*
2 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
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
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 * @param name parameter name
140 * @return parameter value
141 */
142 public String getParameter(String name) {
143 if (list == null)
144 return null;
145
146 return list.get(name);
147 }
148
149 /**
150 * Return a ParameterList object that holds all the available
151 * parameters. Returns null if no parameters are available.
152 *
153 * @return ParameterList
154 */
155 public ParameterList getParameterList() {
156 return list;
157 }
158
159 /**
184 list = new ParameterList();
185
186 list.set(name, value);
187 }
188
189 /**
190 * Set a new ParameterList.
191 * @param list ParameterList
192 */
193 public void setParameterList(ParameterList list) {
194 this.list = list;
195 }
196
197 /**
198 * Retrieve a RFC2045 style string representation of
199 * this Content-Type. Returns <code>null</code> if
200 * the conversion failed.
201 *
202 * @return RFC2045 style string
203 */
204 @Override
205 public String toString() {
206 if (primaryType == null || subType == null) // need both
207 return null;
208
209 StringBuilder sb = new StringBuilder();
210 sb.append(primaryType).append('/').append(subType);
211 if (list != null)
212 // Http Binding section of the "SOAP with attachments" specification says,
213 // "SOAP message senders should send Content-Type headers on a single long line."
214 // (http://www.w3.org/TR/SOAP-attachments#HTTPBinding)
215 sb.append(list.toString());
216
217 return sb.toString();
218 }
219
220 /**
221 * Match with the specified ContentType object. This method
222 * compares <strong>only the <code>primaryType</code> and
223 * <code>primaryType</code> </strong>. The parameters of both operands
224 * are ignored. <p>
225 *
226 * For example, this method will return <code>true</code> when
227 * comparing the ContentTypes for <strong>"text/plain"</strong>
228 * and <strong>"text/plain; charset=foobar"</strong>.
229 *
230 * If the <code>subType</code> of either operand is the special
231 * character '*', then the subtype is ignored during the match.
232 * For example, this method will return <code>true</code> when
233 * comparing the ContentTypes for <strong>"text/plain"</strong>
234 * and <strong>"text/*" </strong>
235 *
236 * @param cType to compare this against
237 * @return true if <code>primaryType</code> and <code>subType</code>
238 * match specified content type.
239 */
240 public boolean match(ContentType cType) {
241 // Match primaryType
242 if (!primaryType.equalsIgnoreCase(cType.getPrimaryType()))
243 return false;
244
245 String sType = cType.getSubType();
246
247 // If either one of the subTypes is wildcarded, return true
248 if ((subType.charAt(0) == '*') || (sType.charAt(0) == '*'))
249 return true;
250
251 // Match subType
252 if (!subType.equalsIgnoreCase(sType))
253 return false;
254
255 return true;
256 }
257
258 /**
259 * Match with the specified content-type string. This method
260 * compares <strong>only the <code>primaryType</code> and
261 * <code>subType</code> </strong>.
262 * The parameters of both operands are ignored. <p>
263 *
264 * For example, this method will return <code>true</code> when
265 * comparing the ContentType for <strong>"text/plain"</strong>
266 * with <strong>"text/plain; charset=foobar"</strong>.
267 *
268 * If the <code>subType</code> of either operand is the special
269 * character '*', then the subtype is ignored during the match.
270 * For example, this method will return <code>true</code> when
271 * comparing the ContentType for <strong>"text/plain"</strong>
272 * with <strong>"text/*" </strong>
273 *
274 * @param s content type
275 * @return true if <code>primaryType</code> and <code>subType</code>
276 * match specified content type.
277 */
278 public boolean match(String s) {
279 try {
280 return match(new ContentType(s));
281 } catch (ParseException pex) {
282 return false;
283 }
284 }
285 }
|