/**
* MailArchiver is an application that provides services for storing and managing e-mail messages through a Web Services SOAP interface.
* Copyright (C) 2012 Marcio Andre Scholl Levien and Fernando Alberto Reuter Wendt and Jose Ronaldo Nogueira Fonseca Junior
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
/******************************************************************************\
*
* This product was developed by
*
* SERVIÇO FEDERAL DE PROCESSAMENTO DE DADOS (SERPRO),
*
* a government company established under Brazilian law (5.615/70),
* at Department of Development of Porto Alegre.
*
\******************************************************************************/
package serpro.mailarchiver.service;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.jdo.annotations.PersistenceAware;
import org.springframework.beans.factory.annotation.Autowired;
import serpro.mailarchiver.domain.metaarchive.*;
import serpro.mailarchiver.service.dto.TFolder;
import serpro.mailarchiver.service.dto.TMessage;
import serpro.mailarchiver.service.find.FFolder;
import serpro.mailarchiver.service.web.ArchiveServices;
import serpro.mailarchiver.service.web.ServiceFault;
import serpro.mailarchiver.session.Session;
import serpro.mailarchiver.util.*;
import serpro.mailarchiver.util.jdo.PersistenceManager;
import serpro.mailarchiver.util.transaction.WithReadWriteTx;
@PersistenceAware
public class Utils
extends BaseService
{
private static final Logger log = Logger.getLocalLogger();
@Autowired
private Environment env;
@Autowired
private FFolder findFolder;
@Autowired
private ArchiveServicesProxyFactory archiveServicesProxyFactory;
@Autowired
private UserAppConfig userAppConfig;
@Autowired
private TrayIconAdapter trayIcon;
@WithReadWriteTx
public void provideMailHome(String userId) throws IOException {
PersistenceManager pm = getPersistenceManager();
String homeFolderId = Session.getHomeFolderId();
Folder homeFolder = findFolder.byId(homeFolderId);
if(homeFolder == null) {
homeFolder = new Folder();
homeFolder.setOid(homeFolderId);
homeFolder.setName(userId);
homeFolder.setParent(null);
Path homeAbsolutePath = homeFolder.getAbsolutePath();
if(Files.notExists(homeAbsolutePath)) {
Files.createDirectory(homeAbsolutePath);
}
pm.makePersistent(homeFolder);
String inboxFolderId = Session.getInboxFolderId();
Folder inboxFolder = new Folder();
inboxFolder.setOid(inboxFolderId);
inboxFolder.setName("Inbox");
inboxFolder.setParent(homeFolder);
Path inboxAbsolutePath = inboxFolder.getAbsolutePath();
if(Files.notExists(inboxAbsolutePath)) {
Files.createDirectory(inboxAbsolutePath);
}
pm.makePersistent(inboxFolder);
String outboxFolderId = Session.getOutboxFolderId();
Folder outboxFolder = new Folder();
outboxFolder.setOid(outboxFolderId);
outboxFolder.setName("Outbox");
outboxFolder.setParent(homeFolder);
Path outboxAbsolutePath = outboxFolder.getAbsolutePath();
if(Files.notExists(outboxAbsolutePath)) {
Files.createDirectory(outboxAbsolutePath);
}
pm.makePersistent(outboxFolder);
String draftsFolderId = Session.getDraftsFolderId();
Folder draftsFolder = new Folder();
draftsFolder.setOid(draftsFolderId);
draftsFolder.setName("Drafts");
draftsFolder.setParent(homeFolder);
Path draftsAbsolutePath = draftsFolder.getAbsolutePath();
if(Files.notExists(draftsAbsolutePath)) {
Files.createDirectory(draftsAbsolutePath);
}
pm.makePersistent(draftsFolder);
String sentFolderId = Session.getSentFolderId();
Folder sentFolder = new Folder();
sentFolder.setOid(sentFolderId);
sentFolder.setName("Sent");
sentFolder.setParent(homeFolder);
Path sentAbsolutePath = sentFolder.getAbsolutePath();
if(Files.notExists(sentAbsolutePath)) {
Files.createDirectory(sentAbsolutePath);
}
pm.makePersistent(sentFolder);
String spamFolderId = Session.getSpamFolderId();
Folder spamFolder = new Folder();
spamFolder.setOid(spamFolderId);
spamFolder.setName("Spam");
spamFolder.setParent(homeFolder);
Path spamAbsolutePath = spamFolder.getAbsolutePath();
if(Files.notExists(spamAbsolutePath)) {
Files.createDirectory(spamAbsolutePath);
}
pm.makePersistent(spamFolder);
String trashFolderId = Session.getTrashFolderId();
Folder trashFolder = new Folder();
trashFolder.setOid(trashFolderId);
trashFolder.setName("Trash");
trashFolder.setParent(homeFolder);
Path trashAbsolutePath = trashFolder.getAbsolutePath();
if(Files.notExists(trashAbsolutePath)) {
Files.createDirectory(trashAbsolutePath);
}
pm.makePersistent(trashFolder);
}
}
@WithReadWriteTx
public final void bootstrapJdoMetadata() {
PersistenceManager pm = getPersistenceManager();
log.info("Beginning jdo metadata bootstrap process");
String tmp = "x-" + env.getExecTimestamp() + "-";
String oid;
AddressListField_Group_Mailbox alfGroupMailbox = new AddressListField_Group_Mailbox();
oid = tmp + "alf-group-mailbox";
alfGroupMailbox.setOid(oid);
pm.makePersistent(alfGroupMailbox);
log.debug("Persisted %s", oid);
pm.deletePersistent(alfGroupMailbox);
log.debug("Deleted %s", oid);
AddressListField_Group alfGroup = new AddressListField_Group();
oid = tmp + "alf-group";
alfGroup.setOid(oid);
pm.makePersistent(alfGroup);
log.debug("Persisted %s", oid);
pm.deletePersistent(alfGroup);
log.debug("Deleted %s", oid);
AddressListField_Mailbox alfMailbox = new AddressListField_Mailbox();
oid = tmp + "alf-mailbox";
alfMailbox.setOid(oid);
pm.makePersistent(alfMailbox);
log.debug("Persisted %s", oid);
pm.deletePersistent(alfMailbox);
log.debug("Deleted %s", oid);
MailboxListField_Mailbox mlfMailbox = new MailboxListField_Mailbox();
oid = tmp + "mlf-mailbox";
mlfMailbox.setOid(oid);
pm.makePersistent(mlfMailbox);
log.debug("Persisted %s", oid);
pm.deletePersistent(mlfMailbox);
log.debug("Deleted %s", oid);
MailboxField_Mailbox mfMailbox = new MailboxField_Mailbox();
oid = tmp + "mf-mailbox";
mfMailbox.setOid(oid);
pm.makePersistent(mfMailbox);
log.debug("Persisted %s", oid);
pm.deletePersistent(mfMailbox);
log.debug("Deleted %s", oid);
EmbeddedMessage embeddedMessage = new EmbeddedMessage();
oid = tmp + "embedded-message";
embeddedMessage.setOid(oid);
pm.makePersistent(embeddedMessage);
log.debug("Persisted %s", oid);
pm.deletePersistent(embeddedMessage);
log.debug("Deleted %s", oid);
BodyPart bodyPart = new BodyPart();
oid = tmp + "body-part";
bodyPart.setOid(oid);
pm.makePersistent(bodyPart);
log.debug("Persisted %s", oid);
pm.deletePersistent(bodyPart);
log.debug("Deleted %s", oid);
Message message = new Message();
oid = tmp + "message";
message.setOid(oid);
pm.makePersistent(message);
log.debug("Persisted %s", oid);
UnstructuredField unstructuredField = new UnstructuredField();
oid = tmp + "unstructured";
unstructuredField.setOid(oid);
unstructuredField.setName("x");
unstructuredField.setEntity(message);
pm.makePersistent(unstructuredField);
log.debug("Persisted %s", oid);
pm.deletePersistent(unstructuredField);
log.debug("Deleted %s", oid);
ContentTypeField contentTypeField = new ContentTypeField();
oid = tmp + "content-type";
contentTypeField.setOid(oid);
contentTypeField.setName("x");
contentTypeField.setEntity(message);
pm.makePersistent(contentTypeField);
log.debug("Persisted %s", oid);
pm.deletePersistent(contentTypeField);
log.debug("Deleted %s", oid);
ContentDispositionField contentDispositionField = new ContentDispositionField();
oid = tmp + "content-disposition";
contentDispositionField.setOid(oid);
contentDispositionField.setName("x");
contentDispositionField.setEntity(message);
pm.makePersistent(contentDispositionField);
log.debug("Persisted %s", oid);
pm.deletePersistent(contentDispositionField);
log.debug("Deleted %s", oid);
DateTimeField dateTimeField = new DateTimeField();
oid = tmp + "datetime";
dateTimeField.setOid(oid);
dateTimeField.setName("x");
dateTimeField.setEntity(message);
pm.makePersistent(dateTimeField);
log.debug("Persisted %s", oid);
pm.deletePersistent(dateTimeField);
log.debug("Deleted %s", oid);
AddressListField addressListField = new AddressListField();
oid = tmp + "address-list";
addressListField.setOid(oid);
addressListField.setName("x");
addressListField.setEntity(message);
pm.makePersistent(addressListField);
log.debug("Persisted %s", oid);
pm.deletePersistent(addressListField);
log.debug("Deleted %s", oid);
MailboxListField mailboxListField = new MailboxListField();
oid = tmp + "mailbox-list";
mailboxListField.setOid(oid);
mailboxListField.setName("x");
mailboxListField.setEntity(message);
pm.makePersistent(mailboxListField);
log.debug("Persisted %s", oid);
pm.deletePersistent(mailboxListField);
log.debug("Deleted %s", oid);
MailboxField mailboxField = new MailboxField();
oid = tmp + "mailbox";
mailboxField.setOid(oid);
mailboxField.setName("x");
mailboxField.setEntity(message);
pm.makePersistent(mailboxField);
log.debug("Persisted %s", oid);
pm.deletePersistent(mailboxField);
log.debug("Deleted %s", oid);
TextBody textBody = new TextBody();
oid = tmp + "text-body";
textBody.setOid(oid);
textBody.setEntity(message);
pm.makePersistent(textBody);
log.debug("Persisted %s", oid);
pm.deletePersistent(textBody);
log.debug("Deleted %s", oid);
BinaryBody binaryBody = new BinaryBody();
oid = tmp + "binary-body";
binaryBody.setOid(oid);
binaryBody.setEntity(message);
pm.makePersistent(binaryBody);
log.debug("Persisted %s", oid);
pm.deletePersistent(binaryBody);
log.debug("Deleted %s", oid);
Multipart multipart = new Multipart();
oid = tmp + "multipart";
multipart.setOid(oid);
multipart.setEntity(message);
pm.makePersistent(multipart);
log.debug("Persisted %s", oid);
pm.deletePersistent(multipart);
log.debug("Deleted %s", oid);
MessageBody messageBody = new MessageBody();
oid = tmp + "message-body";
messageBody.setOid(oid);
messageBody.setEntity(message);
pm.makePersistent(messageBody);
log.debug("Persisted %s", oid);
pm.deletePersistent(messageBody);
log.debug("Deleted %s", oid);
oid = message.getOid();
pm.deletePersistent(message);
log.debug("Deleted %s", oid);
Folder folder = new Folder();
oid = tmp + "folder";
folder.setOid(oid);
folder.setName("x");
pm.makePersistent(folder);
log.debug("Persisted %s", oid);
pm.deletePersistent(folder);
log.debug("Deleted %s", oid);
log.info("Jdo metadata bootstrapping finished");
}
@WithReadWriteTx
public final void bootstrapServices() throws Exception {
log.info("Beginning services bootstrap process");
Date startWarmUp = new Date();
System.out.println("WARM UP ---> Inicializando rotinas de warm up [" + new SimpleDateFormat("dd-MMMM-yyyy HH:mm:ss").format(startWarmUp) + "]");
String warmingUpTooltip = "Warming up...";
trayIcon.addToolTip(warmingUpTooltip);
ArchiveServices proxy = archiveServicesProxyFactory.getProxy();
proxy.getState();
Path mailPath = userAppConfig.SERVER.getArchiveDir().resolve("mail");
DirectoryStream.Filter filter = new DirectoryStream.Filter() {
@Override
public boolean accept(Path file) throws IOException {
return Files.isDirectory(file);
}
};
DirectoryStream dirStream = Files.newDirectoryStream(mailPath, filter);
try {
for(Path path: dirStream) {
String userId = path.getFileName().toString();
String sessionId = null;
try {
Session session = new Session(userId);
sessionId = session.getSessionId();
// System.out.println("##### LOGIN " + userId + " " + sessionId + " #####");
visitFolder(proxy, sessionId, "home");
}
finally {
if(sessionId != null) {
proxy.logout(sessionId);
// System.out.println("##### LOGOUT " + userId + " " + sessionId + " #####");
}
}
}
}
finally {
if(dirStream != null) {
dirStream.close();
}
}
trayIcon.removeToolTip(warmingUpTooltip);
Date endWarmUp = new Date();
System.out.println("WARM UP ---> Rotinas de warm up finalizadas. Meta arquivamento pronto [" + new SimpleDateFormat("dd-MMMM-yyyy HH:mm:ss").format(endWarmUp) + "]");
log.info("Services bootstrapping finished");
}
private void visitFolder(ArchiveServices proxy, String sessionId, String folderId) throws ServiceFault {
TFolder[] listFolders = proxy.listFolders(sessionId, folderId);
if(listFolders != null) {
for(TFolder child : listFolders) {
visitFolder(proxy, sessionId, child.getId());
}
}
TFolder folderDto = proxy.getFolderInfo(sessionId, folderId);
// System.out.println("VISITING FOLDER: " + folderDto.getPath());
String queryConfig =
" \n" +
" " +
// " " +
// " " +
" " +
" " +
" " +
" " +
" ";
TMessage[] listMessages = proxy.listMessages(sessionId, queryConfig);
if(listMessages != null) {
for(TMessage message : listMessages) {
TMessage messageDto = proxy.getMessageInfo(sessionId, message.getId());
// System.out.println("VISITING MESSAGE: " + messageDto.getId());
}
}
}
}