Salome HOME
7c6d4f2bd7b6a0b489a3521ff529cc7f6a155490
[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 (final 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) {
41                         continue;
42                 }
43         if (k == java.lang.Character.LOWERCASE_LETTER) {
44                         continue;
45                 }
46         if (k == java.lang.Character.UPPERCASE_LETTER) {
47                         continue;
48                 }
49         if (k == java.lang.Character.SPACE_SEPARATOR) {
50                         continue;
51                 }
52         if (k == java.lang.Character.END_PUNCTUATION) {
53                         continue;
54                 }
55         if (k == java.lang.Character.DASH_PUNCTUATION) {
56                         continue;
57                 }
58         if (parse[i] == '\'') {
59                         continue;
60                 }
61         if (parse[i] == '_') {
62                         continue;
63                 }
64         if (parse[i] == '&') {
65                         continue;
66                 }
67         if (parse[i] == '.') {
68                         continue;
69                 }
70         return true;
71       }
72       return false;
73         }
74
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.");
79         }
80       if (!fromFile.isFile()) {
81                 throw new IOException("Error File copy: can't copy directory '" + fromFile.getName() + "'.");
82         }
83
84       if (toFile.isDirectory()) {
85                 toFile = new File(toFile, fromFile.getName());
86         }
87       if (toFile.exists()) {
88                 throw new IOException("ERROR File copy: file " + toFile.getName() + " already exist.");
89         } else {
90         String parent = toFile.getParent();
91         if (parent == null) {
92                         throw new IOException("ERROR File copy: destination directory not defined.");
93                 }
94         File dir = new File(parent);
95         if (!dir.exists()) {
96                         throw new IOException("ERROR File copy: destination directory " + parent + " doesn't exist.");
97                 }
98       }
99       FileInputStream from = null;
100       FileOutputStream  to = null;
101       try {
102         from = new FileInputStream(fromFile);
103         to = new FileOutputStream(toFile);
104         byte[] buffer = new byte[4096];
105         int bytesRead = from.read(buffer);
106
107         while (bytesRead != -1) {
108                 to.write(buffer, 0, bytesRead);   // write
109                 bytesRead = from.read(buffer);
110         }
111         
112         from.close();
113         to.close();
114       }
115       catch (IOException e) {
116         throw e;
117       }
118       finally {
119           if (from != null) {
120                   from.close();
121           }
122           if (to != null) {
123                   to.close();
124           }
125       }
126     }
127
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) {
131                 return false;
132         }
133           if (mprop.getProperty("mail.pop3.host") == null) {
134                 return false;
135         }
136           if (mprop.getProperty("mail.from")      == null) {
137                 return false;
138         }
139           
140       Session mail = Session.getInstance(mprop, null);
141       Logger  log  = Logger.getLogger(Do.class);
142       try {
143         log.info("Preparation of mail to " + to.toString());
144
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);
149
150         BodyPart msgBody = new MimeBodyPart();
151         msgBody.setText(message);
152
153         Multipart multipart = new MimeMultipart();
154         multipart.addBodyPart(msgBody);
155
156                 if (attachement != null) {
157                   String attachname = attachement.getCanonicalPath();
158
159           msgBody = new MimeBodyPart();
160           DataSource attachment = new FileDataSource(attachname);
161           msgBody.setDataHandler(new DataHandler(attachment));
162                   msgBody.setFileName(attachname);
163           multipart.addBodyPart(msgBody);
164                 }
165                 msg.setContent(multipart);
166         msg.setSentDate(new Date());
167
168         Transport.send(msg);
169         log.info("sent.");
170
171       } catch (MessagingException mex) {
172         log.error("Send mail failed, reason " + mex);
173                 return false;
174
175       } catch (IOException mex) {
176         log.error("Send mail failed, reason " + mex);
177                 return false;
178       }
179           return true;
180     }
181 }