1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/packaging/mime/internet/ContentType.java Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,277 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +/* 1.30 + * @(#)ContentType.java 1.7 02/03/27 1.31 + */ 1.32 + 1.33 + 1.34 + 1.35 +package com.sun.xml.internal.messaging.saaj.packaging.mime.internet; 1.36 + 1.37 + 1.38 +/** 1.39 + * This class represents a MIME ContentType value. It provides 1.40 + * methods to parse a ContentType string into individual components 1.41 + * and to generate a MIME style ContentType string. 1.42 + * 1.43 + * @version 1.7, 02/03/27 1.44 + * @author John Mani 1.45 + */ 1.46 +public final class ContentType { 1.47 + 1.48 + private String primaryType; // primary type 1.49 + private String subType; // subtype 1.50 + private ParameterList list; // parameter list 1.51 + 1.52 + /** 1.53 + * No-arg Constructor. 1.54 + */ 1.55 + public ContentType() { } 1.56 + 1.57 + /** 1.58 + * Constructor. 1.59 + * 1.60 + * @param primaryType primary type 1.61 + * @param subType subType 1.62 + * @param list ParameterList 1.63 + */ 1.64 + public ContentType(String primaryType, String subType, 1.65 + ParameterList list) { 1.66 + this.primaryType = primaryType; 1.67 + this.subType = subType; 1.68 + if (list == null) 1.69 + list = new ParameterList(); 1.70 + this.list = list; 1.71 + } 1.72 + 1.73 + /** 1.74 + * Constructor that takes a Content-Type string. The String 1.75 + * is parsed into its constituents: primaryType, subType 1.76 + * and parameters. A ParseException is thrown if the parse fails. 1.77 + * 1.78 + * @param s the Content-Type string. 1.79 + * @exception ParseException if the parse fails. 1.80 + */ 1.81 + public ContentType(String s) throws ParseException { 1.82 + HeaderTokenizer h = new HeaderTokenizer(s, HeaderTokenizer.MIME); 1.83 + HeaderTokenizer.Token tk; 1.84 + 1.85 + // First "type" .. 1.86 + tk = h.next(); 1.87 + if (tk.getType() != HeaderTokenizer.Token.ATOM) 1.88 + throw new ParseException(); 1.89 + primaryType = tk.getValue(); 1.90 + 1.91 + // The '/' separator .. 1.92 + tk = h.next(); 1.93 + if ((char)tk.getType() != '/') 1.94 + throw new ParseException(); 1.95 + 1.96 + // Then "subType" .. 1.97 + tk = h.next(); 1.98 + if (tk.getType() != HeaderTokenizer.Token.ATOM) 1.99 + throw new ParseException(); 1.100 + subType = tk.getValue(); 1.101 + 1.102 + // Finally parameters .. 1.103 + String rem = h.getRemainder(); 1.104 + if (rem != null) 1.105 + list = new ParameterList(rem); 1.106 + } 1.107 + 1.108 + public ContentType copy() { 1.109 + return new ContentType(primaryType,subType,list.copy()); 1.110 + } 1.111 + 1.112 + /** 1.113 + * Return the primary type. 1.114 + * @return the primary type 1.115 + */ 1.116 + public String getPrimaryType() { 1.117 + return primaryType; 1.118 + } 1.119 + 1.120 + /** 1.121 + * Return the subType. 1.122 + * @return the subType 1.123 + */ 1.124 + public String getSubType() { 1.125 + return subType; 1.126 + } 1.127 + 1.128 + /** 1.129 + * Return the MIME type string, without the parameters. 1.130 + * The returned value is basically the concatenation of 1.131 + * the primaryType, the '/' character and the secondaryType. 1.132 + * 1.133 + * @return the type 1.134 + */ 1.135 + public String getBaseType() { 1.136 + return primaryType + '/' + subType; 1.137 + } 1.138 + 1.139 + /** 1.140 + * Return the specified parameter value. Returns <code>null</code> 1.141 + * if this parameter is absent. 1.142 + * @return parameter value 1.143 + */ 1.144 + public String getParameter(String name) { 1.145 + if (list == null) 1.146 + return null; 1.147 + 1.148 + return list.get(name); 1.149 + } 1.150 + 1.151 + /** 1.152 + * Return a ParameterList object that holds all the available 1.153 + * parameters. Returns null if no parameters are available. 1.154 + * 1.155 + * @return ParameterList 1.156 + */ 1.157 + public ParameterList getParameterList() { 1.158 + return list; 1.159 + } 1.160 + 1.161 + /** 1.162 + * Set the primary type. Overrides existing primary type. 1.163 + * @param primaryType primary type 1.164 + */ 1.165 + public void setPrimaryType(String primaryType) { 1.166 + this.primaryType = primaryType; 1.167 + } 1.168 + 1.169 + /** 1.170 + * Set the subType. Overrides existing subType 1.171 + * @param subType subType 1.172 + */ 1.173 + public void setSubType(String subType) { 1.174 + this.subType = subType; 1.175 + } 1.176 + 1.177 + /** 1.178 + * Set the specified parameter. If this parameter already exists, 1.179 + * it is replaced by this new value. 1.180 + * 1.181 + * @param name parameter name 1.182 + * @param value parameter value 1.183 + */ 1.184 + public void setParameter(String name, String value) { 1.185 + if (list == null) 1.186 + list = new ParameterList(); 1.187 + 1.188 + list.set(name, value); 1.189 + } 1.190 + 1.191 + /** 1.192 + * Set a new ParameterList. 1.193 + * @param list ParameterList 1.194 + */ 1.195 + public void setParameterList(ParameterList list) { 1.196 + this.list = list; 1.197 + } 1.198 + 1.199 + /** 1.200 + * Retrieve a RFC2045 style string representation of 1.201 + * this Content-Type. Returns <code>null</code> if 1.202 + * the conversion failed. 1.203 + * 1.204 + * @return RFC2045 style string 1.205 + */ 1.206 + public String toString() { 1.207 + if (primaryType == null || subType == null) // need both 1.208 + return null; 1.209 + 1.210 + StringBuffer sb = new StringBuffer(); 1.211 + sb.append(primaryType).append('/').append(subType); 1.212 + if (list != null) 1.213 + // Http Binding section of the "SOAP with attachments" specification says, 1.214 + // "SOAP message senders should send Content-Type headers on a single long line." 1.215 + // (http://www.w3.org/TR/SOAP-attachments#HTTPBinding) 1.216 + sb.append(list.toString()); 1.217 + 1.218 + return sb.toString(); 1.219 + } 1.220 + 1.221 + /** 1.222 + * Match with the specified ContentType object. This method 1.223 + * compares <strong>only the <code>primaryType</code> and 1.224 + * <code>subType</code> </strong>. The parameters of both operands 1.225 + * are ignored. <p> 1.226 + * 1.227 + * For example, this method will return <code>true</code> when 1.228 + * comparing the ContentTypes for <strong>"text/plain"</strong> 1.229 + * and <strong>"text/plain; charset=foobar"</strong>. 1.230 + * 1.231 + * If the <code>subType</code> of either operand is the special 1.232 + * character '*', then the subtype is ignored during the match. 1.233 + * For example, this method will return <code>true</code> when 1.234 + * comparing the ContentTypes for <strong>"text/plain"</strong> 1.235 + * and <strong>"text/*" </strong> 1.236 + * 1.237 + * @param cType to compare this against 1.238 + */ 1.239 + public boolean match(ContentType cType) { 1.240 + // Match primaryType 1.241 + if (!primaryType.equalsIgnoreCase(cType.getPrimaryType())) 1.242 + return false; 1.243 + 1.244 + String sType = cType.getSubType(); 1.245 + 1.246 + // If either one of the subTypes is wildcarded, return true 1.247 + if ((subType.charAt(0) == '*') || (sType.charAt(0) == '*')) 1.248 + return true; 1.249 + 1.250 + // Match subType 1.251 + if (!subType.equalsIgnoreCase(sType)) 1.252 + return false; 1.253 + 1.254 + return true; 1.255 + } 1.256 + 1.257 + /** 1.258 + * Match with the specified content-type string. This method 1.259 + * compares <strong>only the <code>primaryType</code> and 1.260 + * <code>subType</code> </strong>. 1.261 + * The parameters of both operands are ignored. <p> 1.262 + * 1.263 + * For example, this method will return <code>true</code> when 1.264 + * comparing the ContentType for <strong>"text/plain"</strong> 1.265 + * with <strong>"text/plain; charset=foobar"</strong>. 1.266 + * 1.267 + * If the <code>subType</code> of either operand is the special 1.268 + * character '*', then the subtype is ignored during the match. 1.269 + * For example, this method will return <code>true</code> when 1.270 + * comparing the ContentType for <strong>"text/plain"</strong> 1.271 + * with <strong>"text/*" </strong> 1.272 + */ 1.273 + public boolean match(String s) { 1.274 + try { 1.275 + return match(new ContentType(s)); 1.276 + } catch (ParseException pex) { 1.277 + return false; 1.278 + } 1.279 + } 1.280 +}