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: 33 C: 10
23%
M: 5 C: 1
17%
M: 3 C: 1
25%
M: 10 C: 5
33%
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: MimePart.class, String.class);
50: }
51: } catch (ClassNotFoundException | RuntimeException | NoSuchMethodException ex) {
52: // ignore it
53: } finally {
54: cleanContentType = meth;
55: }
56: }
57:
58: // No one should instantiate this class.
59: private MimeUtil() {
60: }
61:
62: /**
63: * If a Content-Type handler has been specified,
64: * call it to clean up the Content-Type value.
65: *
66: * @param mp the MimePart
67: * @param contentType the Content-Type value
68: * @return the cleaned Content-Type value
69: */
70: public static String cleanContentType(MimePart mp, String contentType) {
71:• if (cleanContentType != null) {
72: try {
73: return (String) cleanContentType.invoke(null,
74: new Object[]{mp, contentType});
75: } catch (Exception ex) {
76: return contentType;
77: }
78: } else
79: return contentType;
80: }
81:
82: /**
83: * Convenience method to get our context class loader.
84: * Assert any privileges we might have and then call the
85: * Thread.getContextClassLoader method.
86: */
87: private static ClassLoader getContextClassLoader() {
88: return
89: AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
90: @Override
91: public ClassLoader run() {
92: ClassLoader cl = null;
93: try {
94: cl = Thread.currentThread().getContextClassLoader();
95: } catch (SecurityException ex) {
96: }
97: return cl;
98: }
99: });
100: }
101: }