/** * 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.web; import java.io.IOException; import java.nio.file.Files; import javax.jdo.annotations.PersistenceAware; import org.springframework.beans.factory.annotation.Autowired; import serpro.mailarchiver.domain.metaarchive.Folder; import serpro.mailarchiver.domain.metaarchive.Message; import serpro.mailarchiver.service.BaseService; import serpro.mailarchiver.service.find.FFolder; import serpro.mailarchiver.service.find.FMessage; import serpro.mailarchiver.util.Charsets; import serpro.mailarchiver.util.Logger; import serpro.mailarchiver.util.transaction.WithReadOnlyTx; import serpro.mailarchiver.util.transaction.WithReadWriteTx; import static com.google.common.base.Strings.isNullOrEmpty; @PersistenceAware public class DefaultCopyMessagesOperation extends BaseService implements CopyMessagesOperation { private static final Logger log = Logger.getLocalLogger(); @Autowired private FFolder findFolder; @Autowired private FMessage findMessage; @Autowired private ArchiveOperation archiveOperation; @WithReadOnlyTx @Override public Integer apply(String folderId, String... messagesId) throws ServiceFault { Folder folder = findFolder.byId(folderId); if(folder == null) { ServiceFault.folderNotFound() .setActor("copyMessages") .setMessage("Destination folder not found.") .addValue("folderId", folderId) .raise(); } for(String messageId : messagesId) { if(isNullOrEmpty(messageId)) { continue; } copy(folder, messageId); } return 0; } @WithReadWriteTx private void copy(Folder folder, String messageId) throws ServiceFault { Message message = findMessage.byId(messageId); if(message == null) { ServiceFault.messageNotFound() .setActor("copyMessages") .setMessage("Message not found.") .addValue("messageId", messageId) .raise(); } String msg = null; try { msg = new String(Files.readAllBytes(message.getAbsolutePath()), Charsets.Windows_1252); } catch(IOException e) { ServiceFault.fileSystemFailure() .setActor("copyMessages") .setMessage("Filesystem read message failure.") .addValue("folderId", folder.getOid()) .addValue("folderPath", folder.getRelativePath()) .addValue("messageId", message.getOid()) .addValue("messagePath", message.getRelativePath()) .setCause(e) .raise(); } archiveOperation.apply(folder.getOid(), msg); } }