Skip to content

Package: MimeUtil$1

MimeUtil$1

nameinstructionbranchcomplexitylinemethod
run()
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%
{...}
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%

Coverage

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