Skip to content

Package: ContentDisposition

ContentDisposition

nameinstructionbranchcomplexitylinemethod
ContentDisposition()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
ContentDisposition(String)
M: 52 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 16 C: 0
0%
M: 1 C: 0
0%
ContentDisposition(String, ParameterList)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
getDisposition()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getParameter(String)
M: 10 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getParameterList()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
setDisposition(String)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setParameter(String, String)
M: 14 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
setParameterList(ParameterList)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
toString()
M: 30 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 1997, 2021 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Public License v. 2.0, which is available at
6: * http://www.eclipse.org/legal/epl-2.0.
7: *
8: * This Source Code may also be made available under the following Secondary
9: * Licenses when the conditions for such availability set forth in the
10: * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11: * version 2 with the GNU Classpath Exception, which is available at
12: * https://www.gnu.org/software/classpath/license.html.
13: *
14: * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15: */
16:
17: package jakarta.mail.internet;
18:
19: import jakarta.mail.*;
20:
21: import java.util.*;
22:
23: import java.io.*;
24:
25: /**
26: * This class represents a MIME ContentDisposition value. It provides
27: * methods to parse a ContentDisposition string into individual components
28: * and to generate a MIME style ContentDisposition string.
29: *
30: * @author John Mani
31: */
32:
33: public class ContentDisposition {
34:
35: private static final boolean contentDispositionStrict =
36: MimeUtility.getBooleanSystemProperty("mail.mime.contentdisposition.strict", true);
37:
38: private String disposition; // disposition
39: private ParameterList list;        // parameter list
40:
41: /**
42: * No-arg Constructor.
43: */
44: public ContentDisposition() { }
45:
46: /**
47: * Constructor.
48: *
49: * @param        disposition        disposition
50: * @param        list        ParameterList
51: * @since                JavaMail 1.2
52: */
53: public ContentDisposition(String disposition, ParameterList list) {
54:         this.disposition = disposition;
55:         this.list = list;
56: }
57:
58: /**
59: * Constructor that takes a ContentDisposition string. The String
60: * is parsed into its constituents: dispostion and parameters.
61: * A ParseException is thrown if the parse fails.
62: *
63: * @param        s        the ContentDisposition string.
64: * @exception        ParseException if the parse fails.
65: * @since                JavaMail 1.2
66: */
67: public ContentDisposition(String s) throws ParseException {
68:         HeaderTokenizer h = new HeaderTokenizer(s, HeaderTokenizer.MIME);
69:         HeaderTokenizer.Token tk;
70:
71:         // First "disposition" ..
72:         tk = h.next();
73:•        if (tk.getType() != HeaderTokenizer.Token.ATOM) {
74:• if (contentDispositionStrict) {
75:          throw new ParseException("Expected disposition, got " +
76:                                  tk.getValue());
77: }
78: } else {
79:          disposition = tk.getValue();
80: }
81:
82:         // Then parameters ..
83:         String rem = h.getRemainder();
84:•        if (rem != null) {
85: try {
86: list = new ParameterList(rem);
87: } catch (ParseException px) {
88:• if (contentDispositionStrict) {
89: throw px;
90: }
91: }
92: }
93: }
94:
95: /**
96: * Return the disposition value.
97: * @return the disposition
98: * @since                JavaMail 1.2
99: */
100: public String getDisposition() {
101:         return disposition;
102: }
103:
104: /**
105: * Return the specified parameter value. Returns <code>null</code>
106: * if this parameter is absent.
107: *
108: * @param        name        the parameter name
109: * @return        parameter value
110: * @since                JavaMail 1.2
111: */
112: public String getParameter(String name) {
113:•        if (list == null)
114:          return null;
115:
116:         return list.get(name);
117: }
118:
119: /**
120: * Return a ParameterList object that holds all the available
121: * parameters. Returns null if no parameters are available.
122: *
123: * @return        ParameterList
124: * @since                JavaMail 1.2
125: */
126: public ParameterList getParameterList() {
127:         return list;
128: }
129:
130: /**
131: * Set the disposition. Replaces the existing disposition.
132: * @param        disposition        the disposition
133: * @since                JavaMail 1.2
134: */
135: public void setDisposition(String disposition) {
136:         this.disposition = disposition;
137: }
138:
139: /**
140: * Set the specified parameter. If this parameter already exists,
141: * it is replaced by this new value.
142: *
143: * @param        name        parameter name
144: * @param        value        parameter value
145: * @since                JavaMail 1.2
146: */
147: public void setParameter(String name, String value) {
148:•        if (list == null)
149:          list = new ParameterList();
150:
151:         list.set(name, value);
152: }
153:
154: /**
155: * Set a new ParameterList.
156: * @param        list        ParameterList
157: * @since                JavaMail 1.2
158: */
159: public void setParameterList(ParameterList list) {
160:         this.list = list;
161: }
162:
163: /**
164: * Retrieve a RFC2045 style string representation of
165: * this ContentDisposition. Returns an empty string if
166: * the conversion failed.
167: *
168: * @return        RFC2045 style string
169: * @since                JavaMail 1.2
170: */
171: @Override
172: public String toString() {
173:•        if (disposition == null)
174:          return "";
175:
176:•        if (list == null)
177:          return disposition;
178:
179:         StringBuilder sb = new StringBuilder(disposition);
180:
181: // append the parameter list
182: // use the length of the string buffer + the length of
183: // the header name formatted as follows "Content-Disposition: "
184:         sb.append(list.toString(sb.length() + 21));
185:         return sb.toString();
186: }
187: }