Salome HOME
Siman codebase is refactored. Spring beans are introduced in the context.
[tools/siman.git] / Workspace / Siman-Common / src / org / splat / kernel / Do.java
1 package org.splat.kernel;
2 /**
3  * 
4  * @author    Daniel Brunier-Coulin
5  * @copyright OPEN CASCADE 2012
6  */
7
8 import java.io.File;
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;
14
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;
27
28 import org.apache.log4j.Logger;
29 import org.splat.dal.bo.kernel.User;
30
31
32 public class Do {
33
34     public static boolean containsIllicitCharacter (String name) {
35 //  ------------------------------------------------------------                
36       char parse[] = name.toCharArray();
37           
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;
50         return true;
51       }
52       return false;
53         }
54
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() + "'.");
59
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.");
62       else {
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.");
67       }
68       FileInputStream from = null;
69       FileOutputStream  to = null;
70       try {
71         from = new FileInputStream(fromFile);
72         to = new FileOutputStream(toFile);
73         byte[] buffer = new byte[4096];
74         int bytesRead;
75
76         while ((bytesRead = from.read(buffer)) != -1) to.write(buffer, 0, bytesRead);   // write
77         
78         from.close();
79         to.close();
80       }
81       catch (IOException e) {
82         throw new IOException();
83       }
84     }
85
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;
91           
92       Session mail = Session.getInstance(mprop, null);
93       Logger  log  = Logger.getLogger(Do.class);
94       try {
95         log.info("Preparation of mail to " + to.toString());
96
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);
101
102         BodyPart msgBody = new MimeBodyPart();
103         msgBody.setText(message);
104
105         Multipart multipart = new MimeMultipart();
106         multipart.addBodyPart(msgBody);
107
108                 if (attachement != null) {
109                   String attachname = attachement.getCanonicalPath();
110
111           msgBody = new MimeBodyPart();
112           DataSource attachment = new FileDataSource(attachname);
113           msgBody.setDataHandler(new DataHandler(attachment));
114                   msgBody.setFileName(attachname);
115           multipart.addBodyPart(msgBody);
116                 }
117                 msg.setContent(multipart);
118         msg.setSentDate(new Date());
119
120         Transport.send(msg);
121         log.info("sent.");
122
123       } catch (MessagingException mex) {
124         log.error("Send mail failed, reason " + mex);
125                 return false;
126
127       } catch (IOException mex) {
128         log.error("Send mail failed, reason " + mex);
129                 return false;
130       }
131           return true;
132     }
133 }