1 | /** |
---|
2 | * MailArchiver is an application that provides services for storing and managing e-mail messages through a Web Services SOAP interface. |
---|
3 | * Copyright (C) 2012 Marcio Andre Scholl Levien and Fernando Alberto Reuter Wendt and Jose Ronaldo Nogueira Fonseca Junior |
---|
4 | * |
---|
5 | * This program is free software: you can redistribute it and/or modify |
---|
6 | * it under the terms of the GNU Affero General Public License as |
---|
7 | * published by the Free Software Foundation, either version 3 of the |
---|
8 | * License, or (at your option) any later version. |
---|
9 | * |
---|
10 | * This program is distributed in the hope that it will be useful, |
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 | * GNU Affero General Public License for more details. |
---|
14 | * |
---|
15 | * You should have received a copy of the GNU Affero General Public License |
---|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
17 | */ |
---|
18 | |
---|
19 | /******************************************************************************\ |
---|
20 | * |
---|
21 | * This product was developed by |
---|
22 | * |
---|
23 | * SERVIÃO FEDERAL DE PROCESSAMENTO DE DADOS (SERPRO), |
---|
24 | * |
---|
25 | * a government company established under Brazilian law (5.615/70), |
---|
26 | * at Department of Development of Porto Alegre. |
---|
27 | * |
---|
28 | \******************************************************************************/ |
---|
29 | |
---|
30 | package serpro.mailarchiver.service.web; |
---|
31 | |
---|
32 | import java.io.IOException; |
---|
33 | import java.nio.file.Files; |
---|
34 | |
---|
35 | import javax.jdo.annotations.PersistenceAware; |
---|
36 | |
---|
37 | import org.springframework.beans.factory.annotation.Autowired; |
---|
38 | |
---|
39 | import serpro.mailarchiver.domain.metaarchive.Folder; |
---|
40 | import serpro.mailarchiver.domain.metaarchive.Message; |
---|
41 | import serpro.mailarchiver.service.BaseService; |
---|
42 | import serpro.mailarchiver.service.find.FFolder; |
---|
43 | import serpro.mailarchiver.service.find.FMessage; |
---|
44 | import serpro.mailarchiver.util.Charsets; |
---|
45 | import serpro.mailarchiver.util.Logger; |
---|
46 | import serpro.mailarchiver.util.transaction.WithReadOnlyTx; |
---|
47 | import serpro.mailarchiver.util.transaction.WithReadWriteTx; |
---|
48 | |
---|
49 | import static com.google.common.base.Strings.isNullOrEmpty; |
---|
50 | |
---|
51 | @PersistenceAware |
---|
52 | public class DefaultCopyMessagesOperation |
---|
53 | extends BaseService |
---|
54 | implements CopyMessagesOperation |
---|
55 | { |
---|
56 | private static final Logger log = Logger.getLocalLogger(); |
---|
57 | |
---|
58 | @Autowired |
---|
59 | private FFolder findFolder; |
---|
60 | |
---|
61 | @Autowired |
---|
62 | private FMessage findMessage; |
---|
63 | |
---|
64 | @Autowired |
---|
65 | private ArchiveOperation archiveOperation; |
---|
66 | |
---|
67 | @WithReadOnlyTx |
---|
68 | @Override |
---|
69 | public Integer apply(String folderId, String... messagesId) throws ServiceFault { |
---|
70 | |
---|
71 | Folder folder = findFolder.byId(folderId); |
---|
72 | |
---|
73 | if(folder == null) { |
---|
74 | ServiceFault.folderNotFound() |
---|
75 | .setActor("copyMessages") |
---|
76 | .setMessage("Destination folder not found.") |
---|
77 | .addValue("folderId", folderId) |
---|
78 | .raise(); |
---|
79 | } |
---|
80 | |
---|
81 | for(String messageId : messagesId) { |
---|
82 | if(isNullOrEmpty(messageId)) { |
---|
83 | continue; |
---|
84 | } |
---|
85 | copy(folder, messageId); |
---|
86 | } |
---|
87 | |
---|
88 | return 0; |
---|
89 | } |
---|
90 | |
---|
91 | @WithReadWriteTx |
---|
92 | private void copy(Folder folder, String messageId) throws ServiceFault { |
---|
93 | |
---|
94 | Message message = findMessage.byId(messageId); |
---|
95 | |
---|
96 | if(message == null) { |
---|
97 | ServiceFault.messageNotFound() |
---|
98 | .setActor("copyMessages") |
---|
99 | .setMessage("Message not found.") |
---|
100 | .addValue("messageId", messageId) |
---|
101 | .raise(); |
---|
102 | } |
---|
103 | |
---|
104 | String msg = null; |
---|
105 | try { |
---|
106 | msg = new String(Files.readAllBytes(message.getAbsolutePath()), Charsets.Windows_1252); |
---|
107 | } |
---|
108 | catch(IOException e) { |
---|
109 | ServiceFault.fileSystemFailure() |
---|
110 | .setActor("copyMessages") |
---|
111 | .setMessage("Filesystem read message failure.") |
---|
112 | .addValue("folderId", folder.getOid()) |
---|
113 | .addValue("folderPath", folder.getRelativePath()) |
---|
114 | .addValue("messageId", message.getOid()) |
---|
115 | .addValue("messagePath", message.getRelativePath()) |
---|
116 | .setCause(e) |
---|
117 | .raise(); |
---|
118 | } |
---|
119 | |
---|
120 | archiveOperation.apply(folder.getOid(), msg); |
---|
121 | } |
---|
122 | } |
---|