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.StringReader; |
---|
33 | |
---|
34 | import javax.jdo.annotations.PersistenceAware; |
---|
35 | import javax.xml.stream.XMLInputFactory; |
---|
36 | import javax.xml.stream.XMLStreamException; |
---|
37 | |
---|
38 | import org.springframework.beans.factory.annotation.Autowired; |
---|
39 | |
---|
40 | import org.codehaus.jettison.mapped.Configuration; |
---|
41 | import org.codehaus.jettison.mapped.MappedXMLInputFactory; |
---|
42 | import org.codehaus.staxmate.SMInputFactory; |
---|
43 | import org.codehaus.staxmate.in.SMHierarchicCursor; |
---|
44 | import org.codehaus.staxmate.in.SMInputCursor; |
---|
45 | |
---|
46 | import com.ctc.wstx.stax.WstxInputFactory; |
---|
47 | |
---|
48 | import serpro.mailarchiver.domain.metaarchive.Folder; |
---|
49 | import serpro.mailarchiver.domain.metaarchive.Message; |
---|
50 | import serpro.mailarchiver.service.BaseService; |
---|
51 | import serpro.mailarchiver.service.find.FFolder; |
---|
52 | import serpro.mailarchiver.service.find.FMessage; |
---|
53 | import serpro.mailarchiver.util.Logger; |
---|
54 | import serpro.mailarchiver.util.transaction.WithReadWriteTx; |
---|
55 | |
---|
56 | @PersistenceAware |
---|
57 | public class DefaultTagMessagesOperation |
---|
58 | extends BaseService |
---|
59 | implements TagMessagesOperation |
---|
60 | { |
---|
61 | private static final Logger log = Logger.getLocalLogger(); |
---|
62 | |
---|
63 | @Autowired |
---|
64 | private FMessage findMessage; |
---|
65 | |
---|
66 | @Autowired |
---|
67 | private FFolder findFolder; |
---|
68 | |
---|
69 | @Override |
---|
70 | public Integer apply(String tagConfig) throws ServiceFault { |
---|
71 | |
---|
72 | /* |
---|
73 | * tagConfig: |
---|
74 | * |
---|
75 | * <tag> |
---|
76 | * |
---|
77 | * <message id="id"> |
---|
78 | * <add value="tag"/> |
---|
79 | * <remove value="tag"/> |
---|
80 | * </message> |
---|
81 | * |
---|
82 | * <folder id="id"> |
---|
83 | * <add value="tag"/> |
---|
84 | * <remove value="tag"/> |
---|
85 | * </folder> |
---|
86 | * |
---|
87 | * </tag> |
---|
88 | */ |
---|
89 | |
---|
90 | if(tagConfig.isEmpty()) { |
---|
91 | ServiceFault.invalidTagConfig() |
---|
92 | .setActor("tagMessages") |
---|
93 | .setMessage("Tag config is null or empty.") |
---|
94 | .raise(); |
---|
95 | } |
---|
96 | |
---|
97 | XMLInputFactory inf = null; |
---|
98 | switch(tagConfig.charAt(0)) { |
---|
99 | case '<': |
---|
100 | inf = new WstxInputFactory(); |
---|
101 | break; |
---|
102 | |
---|
103 | case '{': |
---|
104 | Configuration config = new Configuration(); |
---|
105 | config.setIgnoreNamespaces(true); |
---|
106 | inf = new MappedXMLInputFactory(config); |
---|
107 | break; |
---|
108 | |
---|
109 | default: |
---|
110 | ServiceFault.invalidTagConfig() |
---|
111 | .setActor("tagMessages") |
---|
112 | .setMessage("Invalid tag config.") |
---|
113 | .raise(); |
---|
114 | } |
---|
115 | |
---|
116 | SMInputFactory sminf = new SMInputFactory(inf); |
---|
117 | StringReader strReader = new StringReader(tagConfig); |
---|
118 | |
---|
119 | try { |
---|
120 | SMHierarchicCursor cursor0 = sminf.rootElementCursor(strReader); |
---|
121 | cursor0.advance(); |
---|
122 | String localName0 = cursor0.getLocalName(); |
---|
123 | if(localName0.equalsIgnoreCase("tag")) { |
---|
124 | SMInputCursor cursor1 = cursor0.childElementCursor(); |
---|
125 | while(cursor1.getNext() != null) { |
---|
126 | String localName1 = cursor1.getLocalName(); |
---|
127 | if(localName1.equalsIgnoreCase("message")) { |
---|
128 | String messageId = ""; |
---|
129 | for(int i = 0; i < cursor1.getAttrCount(); i++) { |
---|
130 | String attrLocalName1 = cursor1.getAttrLocalName(i); |
---|
131 | if(attrLocalName1.equalsIgnoreCase("id")) { |
---|
132 | messageId = cursor1.getAttrValue(i); |
---|
133 | } |
---|
134 | else { |
---|
135 | //ignore ? |
---|
136 | } |
---|
137 | } |
---|
138 | SMInputCursor cursor2 = cursor1.childElementCursor(); |
---|
139 | while(cursor2.getNext() != null) { |
---|
140 | String localName2 = cursor2.getLocalName(); |
---|
141 | if(localName2.equalsIgnoreCase("add")) { |
---|
142 | for(int i = 0; i < cursor2.getAttrCount(); i++) { |
---|
143 | String attrLocalName2 = cursor2.getAttrLocalName(i); |
---|
144 | if(attrLocalName2.equalsIgnoreCase("value")) { |
---|
145 | String value = cursor2.getAttrValue(i); |
---|
146 | addMessageTag(messageId, value); |
---|
147 | } |
---|
148 | else { |
---|
149 | //ignore ? |
---|
150 | } |
---|
151 | } |
---|
152 | } |
---|
153 | else if(localName2.equalsIgnoreCase("remove")) { |
---|
154 | for(int i = 0; i < cursor2.getAttrCount(); i++) { |
---|
155 | String attrLocalName2 = cursor2.getAttrLocalName(i); |
---|
156 | if(attrLocalName2.equalsIgnoreCase("value")) { |
---|
157 | String value = cursor2.getAttrValue(i); |
---|
158 | removeMessageTag(messageId, value); |
---|
159 | } |
---|
160 | else { |
---|
161 | //ignore ? |
---|
162 | } |
---|
163 | } |
---|
164 | } |
---|
165 | else { |
---|
166 | //ignore ? |
---|
167 | } |
---|
168 | } |
---|
169 | } |
---|
170 | else if(localName1.equalsIgnoreCase("folder")) { |
---|
171 | String folderId = ""; |
---|
172 | for(int i = 0; i < cursor1.getAttrCount(); i++) { |
---|
173 | String attrLocalName1 = cursor1.getAttrLocalName(i); |
---|
174 | if(attrLocalName1.equalsIgnoreCase("id")) { |
---|
175 | folderId = cursor1.getAttrValue(i); |
---|
176 | } |
---|
177 | else { |
---|
178 | //ignore ? |
---|
179 | } |
---|
180 | } |
---|
181 | SMInputCursor cursor2 = cursor1.childElementCursor(); |
---|
182 | while(cursor2.getNext() != null) { |
---|
183 | String localName2 = cursor2.getLocalName(); |
---|
184 | if(localName2.equalsIgnoreCase("add")) { |
---|
185 | for(int i = 0; i < cursor2.getAttrCount(); i++) { |
---|
186 | String attrLocalName2 = cursor2.getAttrLocalName(i); |
---|
187 | if(attrLocalName2.equalsIgnoreCase("value")) { |
---|
188 | String value = cursor2.getAttrValue(i); |
---|
189 | addFolderTag(folderId, value); |
---|
190 | } |
---|
191 | else { |
---|
192 | //ignore ? |
---|
193 | } |
---|
194 | } |
---|
195 | } |
---|
196 | else if(localName2.equalsIgnoreCase("remove")) { |
---|
197 | for(int i = 0; i < cursor2.getAttrCount(); i++) { |
---|
198 | String attrLocalName2 = cursor2.getAttrLocalName(i); |
---|
199 | if(attrLocalName2.equalsIgnoreCase("value")) { |
---|
200 | String value = cursor2.getAttrValue(i); |
---|
201 | removeFolderTag(folderId, value); |
---|
202 | } |
---|
203 | else { |
---|
204 | //ignore ? |
---|
205 | } |
---|
206 | } |
---|
207 | } |
---|
208 | else { |
---|
209 | //ignore ? |
---|
210 | } |
---|
211 | } |
---|
212 | } |
---|
213 | else { |
---|
214 | //ignore ? |
---|
215 | } |
---|
216 | } |
---|
217 | } |
---|
218 | else { |
---|
219 | //ignore ? |
---|
220 | } |
---|
221 | } |
---|
222 | catch(XMLStreamException ex) { |
---|
223 | ServiceFault.runtimeException() |
---|
224 | .setActor("tagMessages") |
---|
225 | .setMessage("Tag config parse exception.") |
---|
226 | .setCause(ex) |
---|
227 | .raise(); |
---|
228 | } |
---|
229 | |
---|
230 | return 0; |
---|
231 | } |
---|
232 | |
---|
233 | @WithReadWriteTx |
---|
234 | private void addMessageTag(String messageId, String value) throws ServiceFault { |
---|
235 | |
---|
236 | if(messageId.isEmpty()) { |
---|
237 | ServiceFault.invalidMessageId() |
---|
238 | .setActor("tagMessages") |
---|
239 | .setMessage("Message id is empty or null.") |
---|
240 | .raise(); |
---|
241 | } |
---|
242 | |
---|
243 | Message message = findMessage.byId(messageId); |
---|
244 | |
---|
245 | if(message == null) { |
---|
246 | ServiceFault.messageNotFound() |
---|
247 | .setActor("tagMessages") |
---|
248 | .setMessage("Message not found.") |
---|
249 | .addValue("messageId", messageId) |
---|
250 | .raise(); |
---|
251 | } |
---|
252 | |
---|
253 | message.addTag(value); |
---|
254 | } |
---|
255 | |
---|
256 | @WithReadWriteTx |
---|
257 | private void removeMessageTag(String messageId, String value) throws ServiceFault { |
---|
258 | |
---|
259 | if(messageId.isEmpty()) { |
---|
260 | ServiceFault.invalidMessageId() |
---|
261 | .setActor("tagMessages") |
---|
262 | .setMessage("Message id is empty or null.") |
---|
263 | .raise(); |
---|
264 | } |
---|
265 | |
---|
266 | Message message = findMessage.byId(messageId); |
---|
267 | |
---|
268 | if(message == null) { |
---|
269 | ServiceFault.messageNotFound() |
---|
270 | .setActor("tagMessages") |
---|
271 | .setMessage("Message not found.") |
---|
272 | .addValue("messageId", messageId) |
---|
273 | .raise(); |
---|
274 | } |
---|
275 | |
---|
276 | message.removeTag(value); |
---|
277 | } |
---|
278 | |
---|
279 | @WithReadWriteTx |
---|
280 | private void addFolderTag(String folderId, String value) throws ServiceFault { |
---|
281 | |
---|
282 | if(folderId.isEmpty()) { |
---|
283 | ServiceFault.invalidFolderId() |
---|
284 | .setActor("tagMessages") |
---|
285 | .setMessage("Folder id is empty or null.") |
---|
286 | .raise(); |
---|
287 | } |
---|
288 | |
---|
289 | Folder folder = findFolder.byId(folderId); |
---|
290 | |
---|
291 | if(folder == null) { |
---|
292 | ServiceFault.folderNotFound() |
---|
293 | .setActor("tagMessages") |
---|
294 | .setMessage("Folder not found.") |
---|
295 | .addValue("folderId", folderId) |
---|
296 | .raise(); |
---|
297 | } |
---|
298 | |
---|
299 | for(Message message : folder.getMessages()) { |
---|
300 | message.addTag(value); |
---|
301 | } |
---|
302 | } |
---|
303 | |
---|
304 | @WithReadWriteTx |
---|
305 | private void removeFolderTag(String folderId, String value) throws ServiceFault { |
---|
306 | |
---|
307 | if(folderId.isEmpty()) { |
---|
308 | ServiceFault.invalidFolderId() |
---|
309 | .setActor("tagMessages") |
---|
310 | .setMessage("Folder id is empty or null.") |
---|
311 | .raise(); |
---|
312 | } |
---|
313 | |
---|
314 | Folder folder = findFolder.byId(folderId); |
---|
315 | |
---|
316 | if(folder == null) { |
---|
317 | ServiceFault.folderNotFound() |
---|
318 | .setActor("tagMessages") |
---|
319 | .setMessage("Folder not found.") |
---|
320 | .addValue("folderId", folderId) |
---|
321 | .raise(); |
---|
322 | } |
---|
323 | |
---|
324 | for(Message message : folder.getMessages()) { |
---|
325 | message.removeTag(value); |
---|
326 | } |
---|
327 | } |
---|
328 | } |
---|