src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/packaging/mime/internet/ContentType.java

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 368
0989ad8c0860
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

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

mercurial