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 * @(#)ContentDisposition.java 1.6 02/03/27 28 */ 29 30 31 32package com.sun.xml.internal.messaging.saaj.packaging.mime.internet; 33 34 35/** 36 * This class represents a MIME ContentDisposition value. It provides 37 * methods to parse a ContentDisposition string into individual components 38 * and to generate a MIME style ContentDisposition string. 39 * 40 * @version 1.6, 02/03/27 41 * @author John Mani 42 */ 43 44public class ContentDisposition { 45 46 private String disposition; // disposition 47 private ParameterList list; // parameter list 48 49 /** 50 * No-arg Constructor. 51 */ 52 public ContentDisposition() { } 53 54 /** 55 * Constructor. 56 * 57 * @param disposition disposition 58 * @param list ParameterList 59 * @since JavaMail 1.2 60 */ 61 public ContentDisposition(String disposition, ParameterList list) { 62 this.disposition = disposition; 63 this.list = list; 64 } 65 66 /** 67 * Constructor that takes a ContentDisposition string. The String 68 * is parsed into its constituents: dispostion and parameters. 69 * A ParseException is thrown if the parse fails. 70 * 71 * @param s the ContentDisposition string. 72 * @exception ParseException if the parse fails. 73 * @since JavaMail 1.2 74 */ 75 public ContentDisposition(String s) throws ParseException { 76 HeaderTokenizer h = new HeaderTokenizer(s, HeaderTokenizer.MIME); 77 HeaderTokenizer.Token tk; 78 79 // First "disposition" .. 80 tk = h.next(); 81 if (tk.getType() != HeaderTokenizer.Token.ATOM) 82 throw new ParseException(); 83 disposition = tk.getValue(); 84 85 // Then parameters .. 86 String rem = h.getRemainder(); 87 if (rem != null) 88 list = new ParameterList(rem); 89 } 90 91 /** 92 * Return the disposition value. 93 * @return the disposition 94 * @since JavaMail 1.2 95 */ 96 public String getDisposition() { 97 return disposition; 98 } 99 100 /** 101 * Return the specified parameter value. Returns <code>null</code> 102 * if this parameter is absent. 103 * @param name parameter name. 104 * @return parameter value 105 * @since JavaMail 1.2 106 */ 107 public String getParameter(String name) { 108 if (list == null) 109 return null; 110 111 return list.get(name); 112 } 113 114 /** 115 * Return a ParameterList object that holds all the available 116 * parameters. Returns null if no parameters are available. 117 * 118 * @return ParameterList 119 * @since JavaMail 1.2 120 */ 121 public ParameterList getParameterList() { 122 return list; 123 } 124 125 /** 126 * Set the primary type. Overrides existing primary type. 127 * @param disposition disposition value 128 * @since JavaMail 1.2 129 */ 130 public void setDisposition(String disposition) { 131 this.disposition = disposition; 132 } 133 134 /** 135 * Set the specified parameter. If this parameter already exists, 136 * it is replaced by this new value. 137 * 138 * @param name parameter name 139 * @param value parameter value 140 * @since JavaMail 1.2 141 */ 142 public void setParameter(String name, String value) { 143 if (list == null) 144 list = new ParameterList(); 145 146 list.set(name, value); 147 } 148 149 /** 150 * Set a new ParameterList. 151 * @param list ParameterList 152 * @since JavaMail 1.2 153 */ 154 public void setParameterList(ParameterList list) { 155 this.list = list; 156 } 157 158 /** 159 * Retrieve a RFC2045 style string representation of 160 * this ContentDisposition. Returns <code>null</code> if 161 * the conversion failed. 162 * 163 * @return RFC2045 style string 164 * @since JavaMail 1.2 165 */ 166 @Override 167 public String toString() { 168 if (disposition == null) 169 return null; 170 171 if (list == null) 172 return disposition; 173 174 StringBuilder sb = new StringBuilder(disposition); 175 176 // append the parameter list 177 // use the length of the string buffer + the length of 178 // the header name formatted as follows "Content-Disposition: " 179 sb.append(list.toString(sb.length() + 21)); 180 return sb.toString(); 181 } 182} 183