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 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 * @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 /** 160 * Set the primary type. Overrides existing primary type. 161 * @param primaryType primary type 162 */ 163 public void setPrimaryType(String primaryType) { 164 this.primaryType = primaryType; 165 } 166 167 /** 168 * Set the subType. Overrides existing subType 169 * @param subType subType 170 */ 171 public void setSubType(String subType) { 172 this.subType = subType; 173 } 174 175 /** 176 * Set the specified parameter. If this parameter already exists, 177 * it is replaced by this new value. 178 * 179 * @param name parameter name 180 * @param value parameter value 181 */ 182 public void setParameter(String name, String value) { 183 if (list == null) 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 }