001 /**
002 *
003 * Copyright 2003-2004 The Apache Software Foundation
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package javax.mail.internet;
019
020 // http://www.faqs.org/rfcs/rfc2183.html
021
022 /**
023 * @version $Rev: 381393 $ $Date: 2006-02-27 09:38:03 -0800 (Mon, 27 Feb 2006) $
024 */
025 public class ContentDisposition {
026 private String _disposition;
027 private ParameterList _list;
028
029 public ContentDisposition() {
030 setDisposition(null);
031 setParameterList(null);
032 }
033
034 public ContentDisposition(String disposition) throws ParseException {
035 // get a token parser for the type information
036 HeaderTokenizer tokenizer = new HeaderTokenizer(disposition, HeaderTokenizer.MIME);
037
038 // get the first token, which must be an ATOM
039 HeaderTokenizer.Token token = tokenizer.next();
040 if (token.getType() != HeaderTokenizer.Token.ATOM) {
041 throw new ParseException("Invalid content disposition");
042 }
043
044 _disposition = token.getValue();
045
046 // the remainder is parameters, which ParameterList will take care of parsing.
047 String remainder = tokenizer.getRemainder();
048 if (remainder != null) {
049 _list = new ParameterList(remainder);
050 }
051 }
052
053 public ContentDisposition(String disposition, ParameterList list) {
054 setDisposition(disposition);
055 setParameterList(list);
056 }
057
058 public String getDisposition() {
059 return _disposition;
060 }
061
062 public String getParameter(String name) {
063 if (_list == null) {
064 return null;
065 } else {
066 return _list.get(name);
067 }
068 }
069
070 public ParameterList getParameterList() {
071 return _list;
072 }
073
074 public void setDisposition(String string) {
075 _disposition = string;
076 }
077
078 public void setParameter(String name, String value) {
079 if (_list == null) {
080 _list = new ParameterList();
081 }
082 _list.set(name, value);
083 }
084
085 public void setParameterList(ParameterList list) {
086 if (list == null) {
087 _list = new ParameterList();
088 } else {
089 _list = list;
090 }
091 }
092
093 public String toString() {
094 // it is possible we might have a parameter list, but this is meaningless if
095 // there is no disposition string. Return a failure.
096 if (_disposition == null) {
097 return null;
098 }
099
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 }