1 package org.splat.kernel;
4 * @author Daniel Brunier-Coulin
5 * @copyright OPEN CASCADE 2012
9 import java.io.FileInputStream;
10 import java.io.FileOutputStream;
11 import java.io.IOException;
12 import java.util.Date;
13 import java.util.Properties;
15 import javax.activation.DataHandler;
16 import javax.activation.DataSource;
17 import javax.activation.FileDataSource;
18 import javax.mail.BodyPart;
19 import javax.mail.Message;
20 import javax.mail.MessagingException;
21 import javax.mail.Multipart;
22 import javax.mail.Session;
23 import javax.mail.Transport;
24 import javax.mail.internet.MimeBodyPart;
25 import javax.mail.internet.MimeMessage;
26 import javax.mail.internet.MimeMultipart;
28 import org.apache.log4j.Logger;
29 import org.splat.dal.bo.kernel.User;
34 public static boolean containsIllicitCharacter (String name) {
35 // ------------------------------------------------------------
36 char parse[] = name.toCharArray();
38 for (int i=0; i<parse.length; i++) {
39 int k = java.lang.Character.getType(parse[i]);
40 if (k == java.lang.Character.DECIMAL_DIGIT_NUMBER) continue;
41 if (k == java.lang.Character.LOWERCASE_LETTER) continue;
42 if (k == java.lang.Character.UPPERCASE_LETTER) continue;
43 if (k == java.lang.Character.SPACE_SEPARATOR) continue;
44 if (k == java.lang.Character.END_PUNCTUATION) continue;
45 if (k == java.lang.Character.DASH_PUNCTUATION) continue;
46 if (parse[i] == '\'') continue;
47 if (parse[i] == '_') continue;
48 if (parse[i] == '&') continue;
49 if (parse[i] == '.') continue;
55 public static void copy (File fromFile, File toFile) throws IOException {
56 // ----------------------------------------------------
57 if (!fromFile.exists()) throw new IOException("ERROR File copy: no such '" + fromFile.getName() + "' source file.");
58 if (!fromFile.isFile()) throw new IOException("Error File copy: can't copy directory '" + fromFile.getName() + "'.");
60 if (toFile.isDirectory()) toFile = new File(toFile, fromFile.getName());
61 if (toFile.exists()) throw new IOException("ERROR File copy: file " + toFile.getName() + " already exist.");
63 String parent = toFile.getParent();
64 if (parent == null) throw new IOException("ERROR File copy: destination directory not defined.");
65 File dir = new File(parent);
66 if (!dir.exists()) throw new IOException("ERROR File copy: destination directory " + parent + " doesn't exist.");
68 FileInputStream from = null;
69 FileOutputStream to = null;
71 from = new FileInputStream(fromFile);
72 to = new FileOutputStream(toFile);
73 byte[] buffer = new byte[4096];
76 while ((bytesRead = from.read(buffer)) != -1) to.write(buffer, 0, bytesRead); // write
81 catch (IOException e) {
82 throw new IOException();
86 public static boolean sendMail (User to, String subject, String message, File attachement, Properties mprop) {
87 // ------------------------------------------------------------------------------------------------------------
88 if (mprop.getProperty("mail.smtp.host") == null) return false;
89 if (mprop.getProperty("mail.pop3.host") == null) return false;
90 if (mprop.getProperty("mail.from") == null) return false;
92 Session mail = Session.getInstance(mprop, null);
93 Logger log = Logger.getLogger(Do.class);
95 log.info("Preparation of mail to " + to.toString());
97 MimeMessage msg = new MimeMessage(mail);
98 msg.setFrom(); // Address defined in properties at mail.from
99 msg.setRecipients(Message.RecipientType.TO, to.getMailAddress());
100 msg.setSubject(subject);
102 BodyPart msgBody = new MimeBodyPart();
103 msgBody.setText(message);
105 Multipart multipart = new MimeMultipart();
106 multipart.addBodyPart(msgBody);
108 if (attachement != null) {
109 String attachname = attachement.getCanonicalPath();
111 msgBody = new MimeBodyPart();
112 DataSource attachment = new FileDataSource(attachname);
113 msgBody.setDataHandler(new DataHandler(attachment));
114 msgBody.setFileName(attachname);
115 multipart.addBodyPart(msgBody);
117 msg.setContent(multipart);
118 msg.setSentDate(new Date());
123 } catch (MessagingException mex) {
124 log.error("Send mail failed, reason " + mex);
127 } catch (IOException mex) {
128 log.error("Send mail failed, reason " + mex);