Skip to content

Package: ContentType

ContentType

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