1 /*
   2  * Copyright (c) 1997, 2012, 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 
  32 package 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 
  44 public 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      * @return  parameter value
 104      * @since           JavaMail 1.2
 105      */
 106     public String getParameter(String name) {
 107         if (list == null)
 108             return null;
 109 
 110         return list.get(name);
 111     }
 112 
 113     /**
 114      * Return a ParameterList object that holds all the available
 115      * parameters. Returns null if no parameters are available.
 116      *
 117      * @return  ParameterList
 118      * @since           JavaMail 1.2
 119      */
 120     public ParameterList getParameterList() {
 121         return list;
 122     }
 123 
 124     /**
 125      * Set the primary type. Overrides existing primary type.
 126      * @param   primaryType     primary type
 127      * @since           JavaMail 1.2
 128      */
 129     public void setDisposition(String disposition) {
 130         this.disposition = disposition;
 131     }
 132 
 133     /**
 134      * Set the specified parameter. If this parameter already exists,
 135      * it is replaced by this new value.
 136      *
 137      * @param   name    parameter name
 138      * @param   value   parameter value
 139      * @since           JavaMail 1.2
 140      */
 141     public void setParameter(String name, String value) {
 142         if (list == null)
 143             list = new ParameterList();
 144 
 145         list.set(name, value);
 146     }
 147 
 148     /**
 149      * Set a new ParameterList.
 150      * @param   list    ParameterList
 151      * @since           JavaMail 1.2
 152      */
 153     public void setParameterList(ParameterList list) {
 154         this.list = list;
 155     }
 156 
 157     /**
 158      * Retrieve a RFC2045 style string representation of
 159      * this ContentDisposition. Returns <code>null</code> if
 160      * the conversion failed.
 161      *
 162      * @return  RFC2045 style string
 163      * @since           JavaMail 1.2
 164      */
 165     public String toString() {
 166         if (disposition == null)
 167             return null;
 168 
 169         if (list == null)
 170             return disposition;
 171 
 172         StringBuilder sb = new StringBuilder(disposition);
 173 
 174         // append the parameter list
 175         // use the length of the string buffer + the length of
 176         // the header name formatted as follows "Content-Disposition: "
 177         sb.append(list.toString(sb.length() + 21));
 178         return sb.toString();
 179     }
 180 }