package mail;
import acsdataexchange.MyLog;
import acsdataexchange.UploadData;
import acsdataexchange.Work;
import acsdataexchange.XMLReader;
import java.io.*;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.*;
import javax.mail.*;
/**
*
* @author ferrante
*/
public class ReadMail {
//public FileWriter fw;
/**
*
*/
public String path;
private MyLog myLog;
private boolean test;
private Work work;
private boolean fileCreated = false;
/**
* Controlla la posta e ne gestisce il contenuto
* @param test true if this instance of the program is only for a test
* @param myLog
* @param storeMailContent
* @param attachmentPath
* @throws NoSuchProviderException
* @throws IOException
* @throws MessagingException
* @throws Exception
*
*/
public ReadMail(Work work, boolean test, MyLog myLog, String path) throws NoSuchProviderException, IOException,
MessagingException, Exception {
this.work = work;
this.test = test;
this.myLog = myLog;
String host = "172.20.15.12";
String user = "acsservice";
String password = "$3003css";
File savedir = new File(path);
savedir.mkdirs();
this.path = savedir.getPath();
// Get system properties
Properties properties = System.getProperties();
// Get the default Session object.
Session session = Session.getDefaultInstance(properties);
// Get a Store object that implements the specified protocol.
Store store = session.getStore("pop3");
//Connect to the current host using the specified username and password.
store.connect(host, user, password);
//Create a Folder object corresponding to the given name.
Folder folder = store.getFolder("inbox");
// Open the Folder.
folder.open(Folder.READ_WRITE);
Message[] message;
/*
SearchTerm st = new NotTerm(new FlagTerm(new Flags(Flags.Flag.SEEN), false));
message = folder.search(st);*/
//message = folder.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false));
message = folder.getMessages();
//Flags f = folder.getPermanentFlags();
int a = 0;
UploadData uploadData = new UploadData(this.myLog);
for (a = 0; a < message.length; a++) {
this.myLog.info("" +
"Ricevuta mail da: " + message[a].getFrom()[0] + "\n" +
"Oggetto: "+ message[a].getSubject()+"\n" +
"Inviata il: " + message[a].getSentDate() +"\n" +
"Tipo: " + message[a].getContentType());
if(uploadData.checkMailSubject(message[a].getSubject())){
boolean uploadSuccess = false;
uploadSuccess = processTextMessage(message[a], a);
//Delete the message in the inbox
if (!test && uploadSuccess)
message[a].setFlag(Flags.Flag.DELETED,true);
}
else
this.myLog.warning("QUESTA MAIL NON E' UN TICKET");
}
if (a==0)
this.myLog.info("Nessuna nuova mail");
folder.close(true);
store.close();
}
/**
* Processa il messaggi di tipo testo
* @param message
* @param messageCount
* @throws IOException
* @throws MessagingException
*/
private boolean processTextMessage(Message message, int messageCount) throws IOException,
MessagingException {
//Crea il file
String fileName = this.path + "/" + message.getSubject() + ".xml";
FileWriter file = this.createFile(fileName);
//Scrive i dati su tale file e lo chiude
this.writeOnFile(file, message.getContent().toString());
this.closeFile(file, fileName);
//Legge i dati dal file XML
XMLReader xmlReader = new XMLReader(fileName, this.myLog);
return xmlReader.uploadSuccess;
}
/**
*
* @param isText
* @throws IOException
*/
public FileWriter createFile(String fileName) throws IOException {
FileWriter file = new FileWriter(fileName);
this.fileCreated = true;
return file;
}
public void copyFile(String fileNameSrc,String fileNameDest) throws IOException {
File source = new File(fileNameSrc);
File dest = new File(fileNameDest);
FileChannel in = null, out = null;
try {
in = new FileInputStream(source).getChannel();
out = new FileOutputStream(dest).getChannel();
long size = in.size();
MappedByteBuffer buf =
in.map(FileChannel.MapMode.READ_ONLY, 0, size);
out.write(buf);
} finally {
if (in != null)
in.close();
if (out != null)
out.close();
}
}
void deleteFile(String fileName){
File file = new File(fileName);
boolean ctrlDel = file.delete();
if (!ctrlDel) this.myLog.error("Impossibile cancellare il file " + fileName);
}
/**
*
* @param row
* @throws IOException
*/
public void writeOnFile(FileWriter file, String row) throws IOException {
if (this.fileCreated)
{file.write(row+"\n");
file.flush();}
//System.out.println(row);
}
/**
*
* @throws IOException
*/
public void closeFile(FileWriter file, String fileName) throws IOException {
if (this.fileCreated)
file.close();
else
this.myLog.warning("File " + fileName + " non esistente");
}
}