View Javadoc

1   /**
2    *
3    * Copyright 2003-2006 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  
18  package javax.mail.internet;
19  
20  // http://www.faqs.org/rfcs/rfc2183.html
21  
22  /**
23   * @version $Rev: 421852 $ $Date: 2006-07-14 03:02:19 -0700 (Fri, 14 Jul 2006) $
24   */
25  public class ContentDisposition {
26      private String _disposition;
27      private ParameterList _list;
28  
29      public ContentDisposition() {
30          setDisposition(null);
31          setParameterList(null);
32      }
33  
34      public ContentDisposition(String disposition) throws ParseException {
35          // get a token parser for the type information
36          HeaderTokenizer tokenizer = new HeaderTokenizer(disposition, HeaderTokenizer.MIME);
37  
38          // get the first token, which must be an ATOM
39          HeaderTokenizer.Token token = tokenizer.next();
40          if (token.getType() != HeaderTokenizer.Token.ATOM) {
41              throw new ParseException("Invalid content disposition");
42          }
43  
44          _disposition = token.getValue();
45  
46          // the remainder is parameters, which ParameterList will take care of parsing.
47          String remainder = tokenizer.getRemainder();
48          if (remainder != null) {
49              _list = new ParameterList(remainder);
50          }
51      }
52  
53      public ContentDisposition(String disposition, ParameterList list) {
54          setDisposition(disposition);
55          setParameterList(list);
56      }
57  
58      public String getDisposition() {
59          return _disposition;
60      }
61  
62      public String getParameter(String name) {
63          if (_list == null) {
64              return null;
65          } else {
66              return _list.get(name);
67          }
68      }
69  
70      public ParameterList getParameterList() {
71          return _list;
72      }
73  
74      public void setDisposition(String string) {
75          _disposition = string;
76      }
77  
78      public void setParameter(String name, String value) {
79          if (_list == null) {
80              _list = new ParameterList();
81          }
82          _list.set(name, value);
83      }
84  
85      public void setParameterList(ParameterList list) {
86          if (list == null) {
87              _list = new ParameterList();
88          } else {
89              _list = list;
90          }
91      }
92  
93      public String toString() {
94          // it is possible we might have a parameter list, but this is meaningless if
95          // there is no disposition string.  Return a failure.
96          if (_disposition == null) {
97              return null;
98          }
99  
100 
101         // no parameter list?  Just return the disposition string
102         if (_list == null) {
103             return _disposition;
104         }
105 
106         // format this for use on a Content-Disposition header, which means we need to
107         // account for the length of the header part too.
108         return _disposition + _list.toString(21 + _disposition.length());
109     }
110 }