Salome HOME
a92954067ef2236be98aa5f6cadc71eeafa17dbb
[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
30
31 public class Do {
32
33     public static boolean containsIllicitCharacter (String name) {
34 //  ------------------------------------------------------------                
35       char parse[] = name.toCharArray();
36           
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;
49         return true;
50       }
51       return false;
52         }
53
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() + "'.");
58
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.");
61       else {
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.");
66       }
67       FileInputStream from = null;
68       FileOutputStream  to = null;
69       try {
70         from = new FileInputStream(fromFile);
71         to = new FileOutputStream(toFile);
72         byte[] buffer = new byte[4096];
73         int bytesRead;
74
75         while ((bytesRead = from.read(buffer)) != -1) to.write(buffer, 0, bytesRead);   // write
76         
77         from.close();
78         to.close();
79       }
80       catch (IOException e) {
81         throw new IOException();
82       }
83     }
84
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;
90           
91       Session mail = Session.getInstance(mprop, null);
92       Logger  log  = Logger.getLogger(Do.class);
93       try {
94         log.info("Preparation of mail to " + to.toString());
95
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);
100
101         BodyPart msgBody = new MimeBodyPart();
102         msgBody.setText(message);
103
104         Multipart multipart = new MimeMultipart();
105         multipart.addBodyPart(msgBody);
106
107                 if (attachement != null) {
108                   String attachname = attachement.getCanonicalPath();
109
110           msgBody = new MimeBodyPart();
111           DataSource attachment = new FileDataSource(attachname);
112           msgBody.setDataHandler(new DataHandler(attachment));
113                   msgBody.setFileName(attachname);
114           multipart.addBodyPart(msgBody);
115                 }
116                 msg.setContent(multipart);
117         msg.setSentDate(new Date());
118
119         Transport.send(msg);
120         log.info("sent.");
121
122       } catch (MessagingException mex) {
123         log.error("Send mail failed, reason " + mex);
124                 return false;
125
126       } catch (IOException mex) {
127         log.error("Send mail failed, reason " + mex);
128                 return false;
129       }
130           return true;
131     }
132 }