1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.mail2.core;
19
20 import java.io.OutputStreamWriter;
21 import java.io.PrintStream;
22 import java.io.PrintWriter;
23 import java.nio.charset.Charset;
24 import java.util.Collection;
25 import java.util.concurrent.Callable;
26 import java.util.function.Supplier;
27
28 /**
29 * Exception thrown when a checked error occurs in commons-email.
30 * <p>
31 * Adapted from FunctorException in Commons Collections.
32 * </p>
33 * <p>
34 * Emulation support for nested exceptions has been removed in {@code Email 1.3}, supported by JDK 1.4 and above.
35 * </p>
36 *
37 * @since 1.0
38 */
39 public class EmailException extends Exception {
40
41 /** Serializable version identifier. */
42 private static final long serialVersionUID = 5550674499282474616L;
43
44 public static <V> V call(final Callable<V> callable) throws EmailException {
45 try {
46 return callable.call();
47 } catch (final Exception e) {
48 throw new EmailException(e);
49 }
50 }
51
52 public static <T> T check(final Supplier<Boolean> test, final T subject, final Supplier<String> message) throws EmailException {
53 if (test.get()) {
54 throw new EmailException(message.get());
55 }
56 return subject;
57 }
58
59 public static <T> Collection<T> checkNonEmpty(final Collection<T> value, final Supplier<String> message) throws EmailException {
60 return check(() -> EmailUtils.isEmpty(value), value, message);
61 }
62
63 public static String checkNonEmpty(final String value, final Supplier<String> message) throws EmailException {
64 return check(() -> EmailUtils.isEmpty(value), value, message);
65 }
66
67 public static <T> T[] checkNonEmpty(final T[] value, final Supplier<String> message) throws EmailException {
68 return check(() -> EmailUtils.isEmpty(value), value, message);
69 }
70
71 public static <T> T checkNonNull(final T test, final Supplier<String> message) throws EmailException {
72 if (test == null) {
73 throw new EmailException(message.get());
74 }
75 return test;
76 }
77
78 /**
79 * Constructs a new {@code EmailException} with no detail message.
80 */
81 public EmailException() {
82 }
83
84 /**
85 * Constructs a new {@code EmailException} with specified detail message.
86 *
87 * @param msg the error message.
88 */
89 public EmailException(final String msg) {
90 super(msg);
91 }
92
93 /**
94 * Constructs a new {@code EmailException} with specified detail message and nested {@code Throwable} root cause.
95 *
96 * @param msg the error message.
97 * @param rootCause the exception or error that caused this exception to be thrown.
98 */
99 public EmailException(final String msg, final Throwable rootCause) {
100 super(msg, rootCause);
101 }
102
103 /**
104 * Constructs a new {@code EmailException} with specified nested {@code Throwable} root cause.
105 *
106 * @param rootCause the exception or error that caused this exception to be thrown.
107 */
108 public EmailException(final Throwable rootCause) {
109 super(rootCause);
110 }
111
112 /**
113 * Prints the stack trace of this exception to the standard error stream.
114 */
115 @Override
116 public void printStackTrace() {
117 printStackTrace(System.err);
118 }
119
120 /**
121 * Prints the stack trace of this exception to the specified stream.
122 *
123 * @param out the {@code PrintStream} to use for output
124 */
125 @Override
126 public void printStackTrace(final PrintStream out) {
127 synchronized (out) {
128 final PrintWriter pw = new PrintWriter(new OutputStreamWriter(out, Charset.defaultCharset()), false);
129 printStackTrace(pw);
130 // Flush the PrintWriter before it's GC'ed.
131 pw.flush();
132 }
133 }
134
135 /**
136 * Prints the stack trace of this exception to the specified writer.
137 *
138 * @param out the {@code PrintWriter} to use for output
139 */
140 @Override
141 public void printStackTrace(final PrintWriter out) {
142 synchronized (out) {
143 super.printStackTrace(out);
144 }
145 }
146 }