Skip to content

Package: ContentType

ContentType

nameinstructionbranchcomplexitylinemethod
ContentType()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
ContentType(String)
M: 101 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 20 C: 0
0%
M: 1 C: 0
0%
ContentType(String, String, ParameterList)
M: 12 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getBaseType()
M: 21 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 3 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%
getPrimaryType()
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%
getSubType()
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%
match(ContentType)
M: 53 C: 0
0%
M: 24 C: 0
0%
M: 13 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
match(String)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 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%
setPrimaryType(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%
setSubType(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%
toString()
M: 38 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 1997, 2023 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: /**
20: * This class represents a MIME Content-Type value. It provides
21: * methods to parse a Content-Type string into individual components
22: * and to generate a MIME style Content-Type string.
23: *
24: * @author John Mani
25: */
26:
27: public class ContentType {
28:
29: private String primaryType; // primary type
30: private String subType; // subtype
31: private ParameterList list; // parameter list
32:
33: /**
34: * No-arg Constructor.
35: */
36: public ContentType() {
37: }
38:
39: /**
40: * Constructor.
41: *
42: * @param primaryType primary type
43: * @param subType subType
44: * @param list ParameterList
45: */
46: public ContentType(String primaryType, String subType,
47: ParameterList list) {
48: this.primaryType = primaryType;
49: this.subType = subType;
50: this.list = list;
51: }
52:
53: /**
54: * Constructor that takes a Content-Type string. The String
55: * is parsed into its constituents: primaryType, subType
56: * and parameters. A ParseException is thrown if the parse fails.
57: *
58: * @param s the Content-Type string.
59: * @throws ParseException if the parse fails.
60: */
61: public ContentType(String s) throws ParseException {
62: HeaderTokenizer h = new HeaderTokenizer(s, HeaderTokenizer.MIME);
63: HeaderTokenizer.Token tk;
64:
65: // First "type" ..
66: tk = h.next();
67:• if (tk.getType() != HeaderTokenizer.Token.ATOM)
68: throw new ParseException("In Content-Type string <" + s + ">" +
69: ", expected MIME type, got " +
70: tk.getValue());
71: primaryType = tk.getValue();
72:
73: // The '/' separator ..
74: tk = h.next();
75:• if ((char) tk.getType() != '/')
76: throw new ParseException("In Content-Type string <" + s + ">" +
77: ", expected '/', got " + tk.getValue());
78:
79: // Then "subType" ..
80: tk = h.next();
81:• if (tk.getType() != HeaderTokenizer.Token.ATOM)
82: throw new ParseException("In Content-Type string <" + s + ">" +
83: ", expected MIME subtype, got " +
84: tk.getValue());
85: subType = tk.getValue();
86:
87: // Finally parameters ..
88: String rem = h.getRemainder();
89:• if (rem != null)
90: list = new ParameterList(rem);
91: }
92:
93: /**
94: * Return the primary type.
95: *
96: * @return the primary type
97: */
98: public String getPrimaryType() {
99: return primaryType;
100: }
101:
102: /**
103: * Return the subType.
104: *
105: * @return the subType
106: */
107: public String getSubType() {
108: return subType;
109: }
110:
111: /**
112: * Return the MIME type string, without the parameters.
113: * The returned value is basically the concatenation of
114: * the primaryType, the '/' character and the secondaryType.
115: *
116: * @return the type
117: */
118: public String getBaseType() {
119:• if (primaryType == null || subType == null)
120: return "";
121: return primaryType + '/' + subType;
122: }
123:
124: /**
125: * Return the specified parameter value. Returns <code>null</code>
126: * if this parameter is absent.
127: *
128: * @param name the parameter name
129: * @return parameter value
130: */
131: public String getParameter(String name) {
132:• if (list == null)
133: return null;
134:
135: return list.get(name);
136: }
137:
138: /**
139: * Return a ParameterList object that holds all the available
140: * parameters. Returns null if no parameters are available.
141: *
142: * @return ParameterList
143: */
144: public ParameterList getParameterList() {
145: return list;
146: }
147:
148: /**
149: * Set the primary type. Overrides existing primary type.
150: *
151: * @param primaryType primary type
152: */
153: public void setPrimaryType(String primaryType) {
154: this.primaryType = primaryType;
155: }
156:
157: /**
158: * Set the subType. Replaces the existing subType.
159: *
160: * @param subType the subType
161: */
162: public void setSubType(String subType) {
163: this.subType = subType;
164: }
165:
166: /**
167: * Set the specified parameter. If this parameter already exists,
168: * it is replaced by this new value.
169: *
170: * @param name parameter name
171: * @param value parameter value
172: */
173: public void setParameter(String name, String value) {
174:• if (list == null)
175: list = new ParameterList();
176:
177: list.set(name, value);
178: }
179:
180: /**
181: * Set a new ParameterList.
182: *
183: * @param list ParameterList
184: */
185: public void setParameterList(ParameterList list) {
186: this.list = list;
187: }
188:
189: /**
190: * Retrieve a RFC2045 style string representation of
191: * this Content-Type. Returns an empty string if
192: * the conversion failed.
193: *
194: * @return RFC2045 style string
195: */
196: @Override
197: public String toString() {
198:• if (primaryType == null || subType == null) // need both
199: return "";
200:
201: StringBuilder sb = new StringBuilder();
202: sb.append(primaryType).append('/').append(subType);
203:• if (list != null)
204: // append the parameter list
205: // use the length of the string buffer + the length of
206: // the header name formatted as follows "Content-Type: "
207: sb.append(list.toString(sb.length() + 14));
208:
209: return sb.toString();
210: }
211:
212: /**
213: * Match with the specified ContentType object. This method
214: * compares <strong>only the <code>primaryType</code> and
215: * <code>subType</code> </strong>. The parameters of both operands
216: * are ignored. <p>
217: *
218: * For example, this method will return <code>true</code> when
219: * comparing the ContentTypes for <strong>"text/plain"</strong>
220: * and <strong>"text/plain; charset=foobar"</strong>.
221: *
222: * If the <code>subType</code> of either operand is the special
223: * character '*', then the subtype is ignored during the match.
224: * For example, this method will return <code>true</code> when
225: * comparing the ContentTypes for <strong>"text/plain"</strong>
226: * and <strong>"text/*" </strong>
227: *
228: * @param cType ContentType to compare this against
229: * @return true if it matches
230: */
231: public boolean match(ContentType cType) {
232: // Match primaryType
233:• if (!((primaryType == null && cType.getPrimaryType() == null) ||
234: (primaryType != null &&
235:• primaryType.equalsIgnoreCase(cType.getPrimaryType()))))
236: return false;
237:
238: String sType = cType.getSubType();
239:
240: // If either one of the subTypes is wildcarded, return true
241:• if ((subType != null && subType.startsWith("*")) ||
242:• (sType != null && sType.startsWith("*")))
243: return true;
244:
245: // Match subType
246:• return (subType == null && sType == null) ||
247:• (subType != null && subType.equalsIgnoreCase(sType));
248: }
249:
250: /**
251: * Match with the specified content-type string. This method
252: * compares <strong>only the <code>primaryType</code> and
253: * <code>subType</code> </strong>.
254: * The parameters of both operands are ignored. <p>
255: *
256: * For example, this method will return <code>true</code> when
257: * comparing the ContentType for <strong>"text/plain"</strong>
258: * with <strong>"text/plain; charset=foobar"</strong>.
259: *
260: * If the <code>subType</code> of either operand is the special
261: * character '*', then the subtype is ignored during the match.
262: * For example, this method will return <code>true</code> when
263: * comparing the ContentType for <strong>"text/plain"</strong>
264: * with <strong>"text/*" </strong>
265: *
266: * @param s the content-type string to match
267: * @return true if it matches
268: */
269: public boolean match(String s) {
270: try {
271: return match(new ContentType(s));
272: } catch (ParseException pex) {
273: return false;
274: }
275: }
276: }