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;
33 public static boolean containsIllicitCharacter (String name) {
34 // ------------------------------------------------------------
35 char parse[] = name.toCharArray();
37 for (int i=0; i<parse.length; i++) {
38 int k = java.lang.Character.getType(parse[i]);
39 if (k == java.lang.Character.DECIMAL_DIGIT_NUMBER) continue;
40 if (k == java.lang.Character.LOWERCASE_LETTER) continue;
41 if (k == java.lang.Character.UPPERCASE_LETTER) continue;
42 if (k == java.lang.Character.SPACE_SEPARATOR) continue;
43 if (k == java.lang.Character.END_PUNCTUATION) continue;
44 if (k == java.lang.Character.DASH_PUNCTUATION) continue;
45 if (parse[i] == '\'') continue;
46 if (parse[i] == '_') continue;
47 if (parse[i] == '&') continue;
48 if (parse[i] == '.') continue;
54 public static void copy (File fromFile, File toFile) throws IOException {
55 // ----------------------------------------------------
56 if (!fromFile.exists()) throw new IOException("ERROR File copy: no such '" + fromFile.getName() + "' source file.");
57 if (!fromFile.isFile()) throw new IOException("Error File copy: can't copy directory '" + fromFile.getName() + "'.");
59 if (toFile.isDirectory()) toFile = new File(toFile, fromFile.getName());
60 if (toFile.exists()) throw new IOException("ERROR File copy: file " + toFile.getName() + " already exist.");
62 String parent = toFile.getParent();
63 if (parent == null) throw new IOException("ERROR File copy: destination directory not defined.");
64 File dir = new File(parent);
65 if (!dir.exists()) throw new IOException("ERROR File copy: destination directory " + parent + " doesn't exist.");
67 FileInputStream from = null;
68 FileOutputStream to = null;
70 from = new FileInputStream(fromFile);
71 to = new FileOutputStream(toFile);
72 byte[] buffer = new byte[4096];
75 while ((bytesRead = from.read(buffer)) != -1) to.write(buffer, 0, bytesRead); // write
80 catch (IOException e) {
81 throw new IOException();
85 public static boolean sendMail (User to, String subject, String message, File attachement, Properties mprop) {
86 // ------------------------------------------------------------------------------------------------------------
87 if (mprop.getProperty("mail.smtp.host") == null) return false;
88 if (mprop.getProperty("mail.pop3.host") == null) return false;
89 if (mprop.getProperty("mail.from") == null) return false;
91 Session mail = Session.getInstance(mprop, null);
92 Logger log = Logger.getLogger(Do.class);
94 log.info("Preparation of mail to " + to.toString());
96 MimeMessage msg = new MimeMessage(mail);
97 msg.setFrom(); // Address defined in properties at mail.from
98 msg.setRecipients(Message.RecipientType.TO, to.getMailAddress());
99 msg.setSubject(subject);
101 BodyPart msgBody = new MimeBodyPart();
102 msgBody.setText(message);
104 Multipart multipart = new MimeMultipart();
105 multipart.addBodyPart(msgBody);
107 if (attachement != null) {
108 String attachname = attachement.getCanonicalPath();
110 msgBody = new MimeBodyPart();
111 DataSource attachment = new FileDataSource(attachname);
112 msgBody.setDataHandler(new DataHandler(attachment));
113 msgBody.setFileName(attachname);
114 multipart.addBodyPart(msgBody);
116 msg.setContent(multipart);
117 msg.setSentDate(new Date());
122 } catch (MessagingException mex) {
123 log.error("Send mail failed, reason " + mex);
126 } catch (IOException mex) {
127 log.error("Send mail failed, reason " + mex);