1 /*
2 * Title: S/MIME Project
3 * Description: S/MIME email sending capabilities
4 * @Author Vladimir Radisic
5 * @Version 2.0.1
6 */
7
8
9 package org.webdocwf.util.smime.exception;
10
11
12 import java.io.*;
13 import java.util.Date;
14 import java.text.SimpleDateFormat;
15
16 import javax.crypto.BadPaddingException;
17 import javax.crypto.IllegalBlockSizeException;
18 import javax.crypto.NoSuchPaddingException;
19 import java.security.NoSuchProviderException;
20 import java.security.NoSuchAlgorithmException;
21 import java.security.InvalidKeyException;
22 import java.security.cert.CertificateException;
23 import java.security.UnrecoverableKeyException;
24 import java.security.KeyStoreException;
25 import java.net.UnknownHostException;
26 import java.io.FileNotFoundException;
27 import java.security.SignatureException;
28 import java.security.cert.CertificateEncodingException;
29
30
31 /***
32 * SMIMEException is exception which is thrown in the classes specific for SMIME
33 * and in the processes associated with SMIME. It contains messages describing
34 * appeared problems and contains name of the class where the exception was
35 * thrown.<BR>
36 *<BR>
37 * SMIMEException, as one of his arguments, can convey non SMIME exception (if it
38 * is reason of rising SMIMEException). Later this argument (Exception) can be
39 * restored by getNonSMIMEException method and examined (for example by using
40 * instanceof operator).<BR>
41 *<BR>
42 * This exception also has posibilities for displaying errors or warnings on
43 * screen, and for logging errors and warnings into SMIMEerr.log file (at
44 * defined directory).
45 */
46 public class SMIMEException extends Exception {
47
48 /***
49 * Full class name of object where exception happened.
50 */
51 private String className;
52
53 /***
54 * File for logging errors (SMIMEerr.log) .
55 */
56 public static final String errorFileName = "SMIMEerr.log";
57
58 /***
59 * Path for SMIMEerror.log file.
60 */
61 private static String errorFilePath = "";
62
63 /***
64 * Storage for external exception (non SMIME exception) which is the reason
65 * of rising SMIME exception.
66 */
67 private Exception foreignExc = null;
68
69 /***
70 * Storage for external exception (non SMIME exception) name.
71 */
72 private String exceptionName = "org.webdocwf.util.smime.exception.SMIMEException";
73
74 /***
75 * Construction with the given object and the error text message
76 * @param obj0 class type - origin of exception (represented by key word
77 * "this"). In static classes, objO is String which represents the class name.
78 * @param text0 message of the exception
79 */
80 public SMIMEException(Object obj0, String text0) {
81 super(text0);
82 if (obj0 instanceof String)
83 className = (String) obj0;
84 else
85 className = obj0.getClass().getName();
86 }
87
88 /***
89 * Construction with the given object and the appropriate error number.
90 * @param obj0 class type - origin of exception (represented by key word
91 * "this"). In static classes, objO is String which represents the class name.
92 * @param errNumb0 error number corresponds to appropriate error message stored
93 * in ErrorStorage class.
94 */
95 public SMIMEException(Object obj0, int errNumb0) {
96 this(obj0, ErrorStorage.getErrorMesage(errNumb0));
97 }
98
99 /***
100 * Static metod which constructs and returns SMIMEException object with the
101 * given object, exception, and name of method inside which this method is called.
102 * This construction is designed for handling non SMIMEExceptions. In
103 * the process of construction, given exception object is explored and stored in
104 * inner private variable. This constructor replaces many lines of code which
105 * can look like following:<BR>
106 * <BR>
107 * <DL>
108 * if (e instanceof javax.mail.MessagingException)<BR>
109 * <DD> excType = "javax.mail.MessagingException";<BR>
110 * else if (e instanceof java.io.IOException)<BR>
111 * <DD> excType = "java.io.IOException";<BR>
112 * else if(e instanceof java.io.UnsupportedEncodingException)
113 * <DD> excType = "java.io.UnsupportedEncodingException";<BR>
114 * <BR>
115 * String err = ErrorStorage.getErrorMesage(1999) +<BR>
116 * <DD> System.getProperty("line.separator") +<BR>
117 * <DD> e.getMessage() +<BR>
118 * <DD> System.getProperty("line.separator") +<BR>
119 * <DD> excType + " captured in MessageConvertor method of" +<BR>
120 * <DD> " MimeAssist class and thrown as SMIMEException.";<BR>
121 * <BR>
122 * SMIMEException es = new <BR>
123 * <DD> SMIMEException("org.webdocwf.util.smime.util.MimeAssist", err);<BR>
124 * es.addNonSMIMEException(e, excType);<BR>
125 * </DL>
126 * <BR>
127 * If unknown non SMIMEException is stored inside of SMIMEException object
128 * getNonSMIMEExceptionName() method will return string: "java.lang.Exception".
129 * SMIMEException could not be stored inside other SMIMEException object, and
130 * returned value of this method will be the same SMIMEException passed to this
131 * method as argument.
132 * @param obj0 class type - origin of exception (represented by key word
133 * "this"). In static classes, objO is String which represents the class name.
134 * @param exception0 non SMIME Exception which will by carried inside of
135 * the generated SMIMEException object.
136 * @param method0 name of method where this method is used. If it is left as
137 * null, it will get value "unknown"
138 */
139 public static SMIMEException getInstance(Object obj0, Exception e0, String method0) {
140
141 String excType = "java.lang.Exception";
142 String name = null;
143
144 if (obj0 instanceof String)
145 name = (String) obj0;
146 else
147 name = obj0.getClass().getName();
148
149 if (e0 instanceof java.io.UnsupportedEncodingException)
150 excType = "java.io.UnsupportedEncodingException";
151 else if (e0 instanceof java.security.cert.CertificateEncodingException)
152 excType = "java.security.cert.CertificateEncodingException";
153 else if (e0 instanceof java.lang.IndexOutOfBoundsException)
154 excType = "java.lang.IndexOutOfBoundsException";
155 else if (e0 instanceof java.lang.NullPointerException)
156 excType = "java.lang.NullPointerException";
157 else if (e0 instanceof javax.crypto.NoSuchPaddingException)
158 excType = "javax.crypto.NoSuchPaddingException";
159 else if (e0 instanceof java.security.SignatureException)
160 excType = "java.security.SignatureException";
161 else if (e0 instanceof java.security.NoSuchProviderException)
162 excType = "java.security.NoSuchProviderException";
163 else if (e0 instanceof java.security.NoSuchAlgorithmException)
164 excType = "java.security.NoSuchAlgorithmException";
165 else if (e0 instanceof java.security.InvalidKeyException)
166 excType = "java.security.InvalidKeyException";
167 else if (e0 instanceof javax.crypto.BadPaddingException)
168 excType = "javax.crypto.BadPaddingException";
169 else if (e0 instanceof javax.crypto.IllegalBlockSizeException)
170 excType = "javax.crypto.IllegalBlockSizeException";
171 else if (e0 instanceof java.security.KeyStoreException)
172 excType = "java.security.KeyStoreException";
173 else if (e0 instanceof java.security.UnrecoverableKeyException)
174 excType = "java.security.UnrecoverableKeyException";
175 else if (e0 instanceof java.net.UnknownHostException)
176 excType = "java.net.UnknownHostException";
177 else if (e0 instanceof java.io.FileNotFoundException)
178 excType = "java.io.FileNotFoundException";
179 else if (e0 instanceof java.lang.NumberFormatException)
180 excType = "java.lang.NumberFormatException";
181 else if (e0 instanceof java.security.cert.CertificateException)
182 excType = "java.security.cert.CertificateException";
183 else if (e0 instanceof org.webdocwf.util.smime.exception.SMIMEIOException)
184 return ((SMIMEIOException) e0).getSMIMEException();
185 else if (e0 instanceof javax.mail.MessagingException)
186 excType = "javax.mail.MessagingException";
187 else if (e0 instanceof java.io.IOException)
188 excType = "java.io.IOException";
189 else if (e0 instanceof org.webdocwf.util.smime.exception.SMIMEException)
190 return (SMIMEException) e0;
191
192 if (method0 == null)
193 method0 = "unknown";
194
195 String err = ErrorStorage.getErrorMesage(1999) +
196 System.getProperty("line.separator") +
197 e0.getMessage() +
198 System.getProperty("line.separator") +
199 excType + " captured in " + method0 + " method of " +
200 name + " class and thrown as SMIMEException.";
201
202 // @task do proper loggin but at the momen I have to see the stack trace
203 e0.printStackTrace();
204
205 SMIMEException es = new SMIMEException(name, err);
206
207 es.addNonSMIMEException(e0, excType);
208 return es;
209 }
210
211 /***
212 * Returns the class name of the object where exception arrised
213 * @return Name of the class
214 */
215 public String getClassOfException() {
216 return className;
217 }
218
219 /***
220 * Returns message of the exception with the specified location of the
221 * exception
222 * @return Information message about arrised Exception
223 */
224 public String getInformation() {
225 return super.getMessage() + System.getProperty("line.separator") +
226 "SMIMEException rised in instance of the class: " + className;
227 }
228
229 /***
230 * Sets path for SMIMEerr.log file.
231 * @param path0 path for logging error file. If entered path is
232 * invalid, SMIMEerr.log file will be placed at default location.
233 */
234 public static void setErrorFilePath(String path0) {
235 if (path0 != null & !path0.equalsIgnoreCase("")) {
236 errorFilePath = new String(path0);
237 if (errorFilePath.charAt(errorFilePath.length() - 1) == File.separatorChar)
238 errorFilePath.substring(0, errorFilePath.length() - 1);
239 }
240 }
241
242 /***
243 * Gets SMIMEerror.log file path.
244 * @return Error logging file path.
245 */
246 public static String getErrorFilePath(String path0) {
247 return errorFilePath;
248 }
249
250 /***
251 * Prints error messages to screen.
252 * @param errorText0 error text which will be displayed at screen. If it is
253 * null, error message returned by getInformation() method will be displayed.
254 */
255 public void displayErrors(String errorText0) {
256
257 if (errorText0 == null || errorText0.equalsIgnoreCase(""))
258 System.out.println(this.getInformation());
259 else
260 System.out.println(errorText0);
261 }
262
263 /***
264 * Prints error messages to SMIMEerror.log file. If destination of log directory
265 * or SMIMEerr.log file do not exist, they will be created.
266 * @param errorText0 error text which will be logged to file. If it is null,
267 * error message returned by getInformation() method will be printed to file.
268 */
269 public void loggingErrors(String errorText0) {
270
271 File logDir = new File(errorFilePath);
272
273 if (!(logDir.isFile() | logDir.isDirectory()))
274 logDir.mkdirs();
275
276 File logFile = new File(logDir, errorFileName);
277
278 PrintWriter outData = null;
279
280 try {
281 outData = new PrintWriter(new BufferedWriter(new FileWriter(
282 logFile.getAbsolutePath(), true)));
283
284 Date dat = new Date(System.currentTimeMillis());
285 SimpleDateFormat datForm = new SimpleDateFormat("dd.MM.yyyy. HH:mm:ss");
286
287 outData.println("-----> " + datForm.format(dat) +
288 " <-----------------------------------------------------------------");
289 outData.flush();
290
291 if (errorText0 == null || errorText0.equalsIgnoreCase(""))
292 outData.println(this.getInformation());
293 else
294 outData.println(errorText0);
295 outData.flush();
296 } catch (FileNotFoundException e) {
297 System.err.println(e.getMessage());
298 outData.close();
299 } catch (IOException e) {
300 System.err.println(e.getMessage());
301 outData.close();
302 }
303 outData.close();
304 }
305
306 /***
307 * Stores non SMIME exception (reason for rising SMIMEException) inside
308 * of SMIMEException object.
309 * @param exception0 non SMIME exception which must extend Exception class.
310 * @param exceptionName0 name of non SMIMEException. Default value is:
311 * org.webdocwf.util.smime.exception.SMIMEException which means that reason for
312 * that exception is not non SMIME exception.
313 */
314 public void addNonSMIMEException(Exception exception0, String exceptionName0) {
315 exceptionName = new String(exceptionName0);
316 foreignExc = exception0;
317 }
318
319 /***
320 * Returns non SMIME exception stored inside of SMIMEException object,
321 * which was the reason for rising SMIMEException. This method has sense only
322 * if non SMIME exception object was previosly added to SMIMEException object by
323 * addNonSMIMEException() method. Otherwise, it returns null.
324 * @returns Exception object which can be examined by user (for example by using
325 * instanceof operator).
326 */
327 public Exception getNonSMIMEException() {
328 return foreignExc;
329 }
330
331 /***
332 * Gets external exception name (non SMIME exception which is reason for rising
333 * SMIMEException).
334 * @return Name of external exception (non SMIME exception).
335 */
336 public String getNonSMIMEExceptionName() {
337 return exceptionName;
338 }
339
340 }
341
This page was automatically generated by Maven