Skip to content

Package: MimeUtil

MimeUtil

nameinstructionbranchcomplexitylinemethod
cleanContentType(MimePart, String)
M: 18 C: 4
18%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 3 C: 2
40%
M: 0 C: 1
100%
getContextClassLoader()
M: 6 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: 35 C: 10
22%
M: 5 C: 1
17%
M: 3 C: 1
25%
M: 12 C: 5
29%
M: 0 C: 1
100%

Coverage

1: /*
2: * Copyright (c) 2010, 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: import java.lang.reflect.Method;
20: import java.security.AccessController;
21: import java.security.PrivilegedAction;
22:
23: /**
24: * General MIME-related utility methods.
25: *
26: * @author Bill Shannon
27: * @since JavaMail 1.4.4
28: */
29: class MimeUtil {
30:
31: private static final Method cleanContentType;
32:
33: static {
34: Method meth = null;
35: try {
36: String cth = System.getProperty("mail.mime.contenttypehandler");
37:• if (cth != null) {
38: ClassLoader cl = getContextClassLoader();
39: Class<?> clsHandler = null;
40:• if (cl != null) {
41: try {
42: clsHandler = Class.forName(cth, false, cl);
43: } catch (ClassNotFoundException cex) {
44: }
45: }
46:• if (clsHandler == null)
47: clsHandler = Class.forName(cth);
48: meth = clsHandler.getMethod("cleanContentType",
49: new Class<?>[]{MimePart.class, String.class});
50: }
51: } catch (ClassNotFoundException ex) {
52: // ignore it
53: } catch (NoSuchMethodException ex) {
54: // ignore it
55: } catch (RuntimeException ex) {
56: // ignore it
57: } finally {
58: cleanContentType = meth;
59: }
60: }
61:
62: // No one should instantiate this class.
63: private MimeUtil() {
64: }
65:
66: /**
67: * If a Content-Type handler has been specified,
68: * call it to clean up the Content-Type value.
69: *
70: * @param mp the MimePart
71: * @param contentType the Content-Type value
72: * @return the cleaned Content-Type value
73: */
74: public static String cleanContentType(MimePart mp, String contentType) {
75:• if (cleanContentType != null) {
76: try {
77: return (String) cleanContentType.invoke(null,
78: new Object[]{mp, contentType});
79: } catch (Exception ex) {
80: return contentType;
81: }
82: } else
83: return contentType;
84: }
85:
86: /**
87: * Convenience method to get our context class loader.
88: * Assert any privileges we might have and then call the
89: * Thread.getContextClassLoader method.
90: */
91: private static ClassLoader getContextClassLoader() {
92: return
93: AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
94: @Override
95: public ClassLoader run() {
96: ClassLoader cl = null;
97: try {
98: cl = Thread.currentThread().getContextClassLoader();
99: } catch (SecurityException ex) {
100: }
101: return cl;
102: }
103: });
104: }
105: }