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