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