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 (final 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) {
43 if (k == java.lang.Character.LOWERCASE_LETTER) {
46 if (k == java.lang.Character.UPPERCASE_LETTER) {
49 if (k == java.lang.Character.SPACE_SEPARATOR) {
52 if (k == java.lang.Character.END_PUNCTUATION) {
55 if (k == java.lang.Character.DASH_PUNCTUATION) {
58 if (parse[i] == '\'') {
61 if (parse[i] == '_') {
64 if (parse[i] == '&') {
67 if (parse[i] == '.') {
75 public static void copy (final File fromFile, File toFile) throws IOException {
76 // ----------------------------------------------------
77 if (!fromFile.exists()) {
78 throw new IOException("ERROR File copy: no such '" + fromFile.getName() + "' source file.");
80 if (!fromFile.isFile()) {
81 throw new IOException("Error File copy: can't copy directory '" + fromFile.getName() + "'.");
84 if (toFile.isDirectory()) {
85 toFile = new File(toFile, fromFile.getName());
87 if (toFile.exists()) {
88 throw new IOException("ERROR File copy: file " + toFile.getName() + " already exist.");
90 String parent = toFile.getParent();
92 throw new IOException("ERROR File copy: destination directory not defined.");
94 File dir = new File(parent);
96 throw new IOException("ERROR File copy: destination directory " + parent + " doesn't exist.");
99 FileInputStream from = null;
100 FileOutputStream to = null;
102 from = new FileInputStream(fromFile);
103 to = new FileOutputStream(toFile);
104 byte[] buffer = new byte[4096];
105 int bytesRead = from.read(buffer);
107 while (bytesRead != -1) {
108 to.write(buffer, 0, bytesRead); // write
109 bytesRead = from.read(buffer);
115 catch (IOException e) {
128 public static boolean sendMail (final User to, final String subject, final String message, final File attachement, final Properties mprop) {
129 // ------------------------------------------------------------------------------------------------------------
130 if (mprop.getProperty("mail.smtp.host") == null) {
133 if (mprop.getProperty("mail.pop3.host") == null) {
136 if (mprop.getProperty("mail.from") == null) {
140 Session mail = Session.getInstance(mprop, null);
141 Logger log = Logger.getLogger(Do.class);
143 log.info("Preparation of mail to " + to.toString());
145 MimeMessage msg = new MimeMessage(mail);
146 msg.setFrom(); // Address defined in properties at mail.from
147 msg.setRecipients(Message.RecipientType.TO, to.getMailAddress());
148 msg.setSubject(subject);
150 BodyPart msgBody = new MimeBodyPart();
151 msgBody.setText(message);
153 Multipart multipart = new MimeMultipart();
154 multipart.addBodyPart(msgBody);
156 if (attachement != null) {
157 String attachname = attachement.getCanonicalPath();
159 msgBody = new MimeBodyPart();
160 DataSource attachment = new FileDataSource(attachname);
161 msgBody.setDataHandler(new DataHandler(attachment));
162 msgBody.setFileName(attachname);
163 multipart.addBodyPart(msgBody);
165 msg.setContent(multipart);
166 msg.setSentDate(new Date());
171 } catch (MessagingException mex) {
172 log.error("Send mail failed, reason " + mex);
175 } catch (IOException mex) {
176 log.error("Send mail failed, reason " + mex);