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.util; |
---|
31 | |
---|
32 | import com.ctc.wstx.stax.WstxOutputFactory; |
---|
33 | import com.google.common.io.Files; |
---|
34 | import java.io.File; |
---|
35 | import java.io.FileWriter; |
---|
36 | import java.io.IOException; |
---|
37 | import java.io.StringWriter; |
---|
38 | import java.text.SimpleDateFormat; |
---|
39 | import java.util.Date; |
---|
40 | import java.util.HashSet; |
---|
41 | import java.util.Iterator; |
---|
42 | import java.util.Set; |
---|
43 | import java.util.StringTokenizer; |
---|
44 | import javax.xml.stream.XMLOutputFactory; |
---|
45 | import javax.xml.stream.XMLStreamException; |
---|
46 | import org.codehaus.staxmate.SMOutputFactory; |
---|
47 | import org.codehaus.staxmate.out.SMOutputDocument; |
---|
48 | import org.codehaus.staxmate.out.SMOutputElement; |
---|
49 | import serpro.mailarchiver.service.web.ServiceFault; |
---|
50 | |
---|
51 | public class GearsUtil { |
---|
52 | private static final String USER_LOG_FILENAME = "user_log_" + |
---|
53 | new SimpleDateFormat("dd.MM.yy.HH.mm.ss"). |
---|
54 | format(new Date()) + ".log"; |
---|
55 | |
---|
56 | private static final String USER_LOG_FILEPATH = System.getProperty("user.home"); |
---|
57 | |
---|
58 | private static final Logger log = Logger.getLocalLogger(); |
---|
59 | |
---|
60 | // apenas concatena o nome do arquivo ao path independentemente de Os |
---|
61 | public static String loadFilePaths(String pathGears, String fileName) { |
---|
62 | |
---|
63 | final String fullPath = pathGears + File.separatorChar + fileName; |
---|
64 | |
---|
65 | if(fullPath!=null){ |
---|
66 | |
---|
67 | File file = new File(fullPath); |
---|
68 | |
---|
69 | if(file.exists() && !file.isDirectory()){ |
---|
70 | return fullPath; |
---|
71 | } |
---|
72 | } return null; |
---|
73 | } |
---|
74 | |
---|
75 | public static String loadParentFilePath(String fileName) { |
---|
76 | |
---|
77 | File file = new File(fileName); |
---|
78 | |
---|
79 | if(file.exists()){ |
---|
80 | if (file.isDirectory()) { |
---|
81 | File[] lista = file.listFiles(); |
---|
82 | for (int i = 0; i < lista.length; i++) { |
---|
83 | if(lista[i].getName().equalsIgnoreCase(fileName)){ |
---|
84 | return lista[i].getPath(); |
---|
85 | }else{ |
---|
86 | String result = loadFilePaths(lista[i].getPath(), fileName); |
---|
87 | if (result != null){ |
---|
88 | return result; |
---|
89 | } |
---|
90 | } |
---|
91 | } |
---|
92 | } |
---|
93 | }return null; |
---|
94 | } |
---|
95 | |
---|
96 | // extair as url da tabela entries refernte ao fonte da mensagem |
---|
97 | public static String unserializeUrlEntries(String serializedMailString){ |
---|
98 | |
---|
99 | // System.out.println("GearsUtil.unserializeUrlEntries sendo executado... "); |
---|
100 | |
---|
101 | log.debug("GearsUtil.unserializeUrlEntries sendo executado... "); |
---|
102 | |
---|
103 | final String key = "url_export_file"; |
---|
104 | |
---|
105 | String result = null; |
---|
106 | |
---|
107 | int index = serializedMailString.indexOf(key); |
---|
108 | |
---|
109 | if(index!=-1){ |
---|
110 | |
---|
111 | StringTokenizer token = new StringTokenizer(serializedMailString.substring(index), ":", false); |
---|
112 | |
---|
113 | while(token.hasMoreElements()){ |
---|
114 | |
---|
115 | String next = token.nextElement().toString(); |
---|
116 | |
---|
117 | if(next.contains(key)){ |
---|
118 | |
---|
119 | int numberChars = Integer.parseInt(token.nextElement().toString()); |
---|
120 | |
---|
121 | // para escapar as aspas duplas finais |
---|
122 | numberChars++; |
---|
123 | |
---|
124 | //começa no indice 1 para escapar as aspas duplas iniciais |
---|
125 | result = token.nextElement().toString().substring(1, numberChars); |
---|
126 | |
---|
127 | // System.out.println("url_export_file estraÃda..: " + result); |
---|
128 | |
---|
129 | log.debug("url_export_file estraÃda..: " + result); |
---|
130 | } |
---|
131 | } |
---|
132 | } |
---|
133 | |
---|
134 | // System.out.println("GearsUtil.unserializeUrlEntries sendo finalizado... "); |
---|
135 | |
---|
136 | log.debug("GearsUtil.unserializeUrlEntries sendo finalizado... "); |
---|
137 | |
---|
138 | return result; |
---|
139 | } |
---|
140 | |
---|
141 | private static final String[] flagKeys = new String[]{"Importance", "Recent", "Unseen", "Answered", "Draft", "Deleted", "Flagged", "Forwarded" }; |
---|
142 | |
---|
143 | private static Set<String> getUnserializeHeaderParameters(String serializedHeaderString, String unseen){ |
---|
144 | |
---|
145 | // System.out.println("GearsUtil.unserializeHeaderParameters sendo executado... "); |
---|
146 | |
---|
147 | log.debug("GearsUtil.unserializeHeaderParameters sendo executado... "); |
---|
148 | |
---|
149 | Set<String> flags = new HashSet<String>(); |
---|
150 | |
---|
151 | for(String key : flagKeys){ |
---|
152 | int index = serializedHeaderString.indexOf(key); |
---|
153 | |
---|
154 | if(index!=-1){ |
---|
155 | |
---|
156 | StringTokenizer token = new StringTokenizer(serializedHeaderString.substring(index), ":", false); |
---|
157 | |
---|
158 | while(token.hasMoreElements()){ |
---|
159 | |
---|
160 | String next = token.nextElement().toString(); |
---|
161 | |
---|
162 | String valueType = next.substring(next.length()-1); |
---|
163 | |
---|
164 | if(next.contains(key)){ |
---|
165 | |
---|
166 | if(valueType.equalsIgnoreCase("s")){//flag do tipo String |
---|
167 | // outros tipos de flags serão desconsiderados |
---|
168 | |
---|
169 | int numberChars = Integer.parseInt(token.nextElement().toString()); |
---|
170 | |
---|
171 | // para escapar as aspas duplas finais |
---|
172 | numberChars++; |
---|
173 | |
---|
174 | String value = null; |
---|
175 | |
---|
176 | try{ |
---|
177 | value = token.nextElement().toString().substring(1, numberChars); |
---|
178 | }catch(StringIndexOutOfBoundsException ex){ |
---|
179 | log.error("Falha na extração de tags da mensagem de header..: " + serializedHeaderString, ex); |
---|
180 | // return null; |
---|
181 | } |
---|
182 | |
---|
183 | if(value != null){ |
---|
184 | if(key.equalsIgnoreCase("unseen") && ((value.equalsIgnoreCase(" ") || value.equalsIgnoreCase("n")) && unseen.equals("0"))){ |
---|
185 | flags.add("seen"); |
---|
186 | } else if(key.equalsIgnoreCase("recent") && value.equalsIgnoreCase("n")){ |
---|
187 | flags.add("unseen"); |
---|
188 | } else if(key.equalsIgnoreCase("flagged") && value.equalsIgnoreCase("f")){ |
---|
189 | flags.add("flagged"); |
---|
190 | } else if(key.equalsIgnoreCase("importance") && value.equalsIgnoreCase("high")){ |
---|
191 | flags.add("importance_high"); |
---|
192 | } else if(key.equalsIgnoreCase("draft") && value.equalsIgnoreCase("x")){ |
---|
193 | flags.add("draft"); |
---|
194 | if(flags.contains("answered")){ |
---|
195 | flags.add("forwarded"); |
---|
196 | } |
---|
197 | } else if(key.equalsIgnoreCase("answered") && value.equalsIgnoreCase("a")){ |
---|
198 | flags.add("answered"); |
---|
199 | if(flags.contains("draft")){ |
---|
200 | flags.add("forwarded"); |
---|
201 | } |
---|
202 | } else if(key.equalsIgnoreCase("forwarded") && (value.equalsIgnoreCase("f") |
---|
203 | || flags.contains("answered") || flags.contains("draft"))){ |
---|
204 | flags.add("forwarded"); |
---|
205 | } |
---|
206 | } |
---|
207 | }break; |
---|
208 | } |
---|
209 | } |
---|
210 | } |
---|
211 | } |
---|
212 | // System.out.println("GearsUtil.unserializeHeaderParameters sendo finalizado... "); |
---|
213 | |
---|
214 | log.debug("GearsUtil.unserializeHeaderParameters sendo finalizado... "); |
---|
215 | |
---|
216 | return flags; |
---|
217 | } |
---|
218 | |
---|
219 | public static String writeQueryConfigHeaders(String messageId, String serializedHeaderString, String unseen) throws ServiceFault{ |
---|
220 | // System.out.println("GearsUtil.writeQueryConfigHeaders sendo executado... "); |
---|
221 | |
---|
222 | log.debug("GearsUtil.writeQueryConfigHeaders sendo executado... "); |
---|
223 | |
---|
224 | String xml = null; |
---|
225 | |
---|
226 | Set<String> headerElements = getUnserializeHeaderParameters(serializedHeaderString, unseen); |
---|
227 | |
---|
228 | if(headerElements != null && headerElements.size()>0){ |
---|
229 | XMLOutputFactory xmloutf = new WstxOutputFactory(); |
---|
230 | |
---|
231 | SMOutputFactory smoutf = new SMOutputFactory(xmloutf); |
---|
232 | |
---|
233 | StringWriter strWriter = new StringWriter(); |
---|
234 | |
---|
235 | try { |
---|
236 | SMOutputDocument document = smoutf.createOutputDocument(strWriter); |
---|
237 | |
---|
238 | SMOutputElement tagElement = document.addElement("tag"); |
---|
239 | |
---|
240 | SMOutputElement messageElement = tagElement.addElement("message"); |
---|
241 | |
---|
242 | messageElement.addAttribute("id", messageId); |
---|
243 | |
---|
244 | Iterator flags = headerElements.iterator(); |
---|
245 | |
---|
246 | while(flags.hasNext()){ |
---|
247 | SMOutputElement value = messageElement.addElement("add"); |
---|
248 | value.addAttribute("value", flags.next().toString()); |
---|
249 | } |
---|
250 | document.closeRoot(); |
---|
251 | |
---|
252 | } catch (XMLStreamException ex) { |
---|
253 | log.error("Falha na extração dos flags da mensagem lida da column mail.header - MessageId.: " + messageId + ex); |
---|
254 | return null; |
---|
255 | } |
---|
256 | xml = strWriter.toString(); |
---|
257 | } |
---|
258 | |
---|
259 | // System.out.println("GearsUtil.writeQueryConfigHeaders sendo finalizado... "); |
---|
260 | |
---|
261 | log.debug("GearsUtil.writeQueryConfigHeaders sendo finalizado... "); |
---|
262 | |
---|
263 | return xml; |
---|
264 | } |
---|
265 | |
---|
266 | // extair o fonte da mensagem via stream de leitura |
---|
267 | // os dados lidos do arquivo serão empacotados em uma String |
---|
268 | public static String getMessageRFC822(String messagePath){ |
---|
269 | |
---|
270 | // System.out.println("GearsUtil.getMessageRFC822 sendo executado... "); |
---|
271 | |
---|
272 | log.debug("GearsUtil.getMessageRFC822 sendo executado... "); |
---|
273 | |
---|
274 | String result = null; |
---|
275 | |
---|
276 | try { |
---|
277 | // System.out.println("Path da mensagem no filesystem..: " + messagePath); |
---|
278 | |
---|
279 | log.debug("Path da mensagem no filesystem..: " + messagePath); |
---|
280 | |
---|
281 | if(new File(messagePath).exists()){ |
---|
282 | |
---|
283 | // result = Files.toString(new File(messagePath), Charsets.Windows_1252); |
---|
284 | result = Files.toString(new File(messagePath), Charsets.UTF_8); |
---|
285 | |
---|
286 | } |
---|
287 | } |
---|
288 | catch (IOException ioex) { |
---|
289 | |
---|
290 | log.error(ioex); |
---|
291 | } |
---|
292 | finally{ |
---|
293 | // System.out.println("GearsUtil.getMessageRFC822 sendo finalizado... "); |
---|
294 | |
---|
295 | log.debug("GearsUtil.getMessageRFC822 sendo finalizado... "); |
---|
296 | |
---|
297 | return result; |
---|
298 | } |
---|
299 | } |
---|
300 | |
---|
301 | // inserir informações de mensagens não arquivadas em arquivo de log |
---|
302 | public static void writeInUserLogFile(String messageError, Integer messageFails){ |
---|
303 | try { |
---|
304 | |
---|
305 | String pathFile = USER_LOG_FILEPATH + File.separatorChar + USER_LOG_FILENAME; |
---|
306 | |
---|
307 | FileWriter writer = null; |
---|
308 | |
---|
309 | if(!new File(pathFile).exists()){ |
---|
310 | writer = new FileWriter(pathFile, true); |
---|
311 | |
---|
312 | writer.write("Listagem de mensagens não arquivadas no MailArchiver"); |
---|
313 | } |
---|
314 | |
---|
315 | if(writer==null){ |
---|
316 | |
---|
317 | writer = new FileWriter(pathFile, true); |
---|
318 | } |
---|
319 | |
---|
320 | writer.write("\n" + messageFails + " - " + messageError); |
---|
321 | |
---|
322 | writer.close(); |
---|
323 | |
---|
324 | } catch (IOException ex) { |
---|
325 | log.error(ex); |
---|
326 | } |
---|
327 | } |
---|
328 | } |
---|