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