1 | package pkg.crawler.tread; |
---|
2 | |
---|
3 | import java.io.IOException; |
---|
4 | import java.util.ArrayList; |
---|
5 | import java.util.Date; |
---|
6 | import java.util.List; |
---|
7 | |
---|
8 | import javax.mail.Address; |
---|
9 | import javax.mail.BodyPart; |
---|
10 | import javax.mail.Folder; |
---|
11 | import javax.mail.Message; |
---|
12 | import javax.mail.MessagingException; |
---|
13 | import javax.mail.Multipart; |
---|
14 | import javax.mail.Part; |
---|
15 | |
---|
16 | import org.apache.solr.client.solrj.SolrServer; |
---|
17 | import org.apache.solr.client.solrj.impl.HttpSolrServer; |
---|
18 | import org.apache.solr.common.SolrInputDocument; |
---|
19 | |
---|
20 | import pkg.crawler.entity.Message2SolrEntity; |
---|
21 | |
---|
22 | import com.sun.mail.imap.ACL; |
---|
23 | import com.sun.mail.imap.IMAPFolder; |
---|
24 | import com.sun.mail.imap.IMAPMessage; |
---|
25 | import com.sun.mail.imap.Rights; |
---|
26 | import com.sun.mail.imap.Rights.Right; |
---|
27 | |
---|
28 | |
---|
29 | public class CrawlerThread implements Runnable{ |
---|
30 | |
---|
31 | private String hostSolr; |
---|
32 | private ACL acl; |
---|
33 | private IMAPFolder imapFINBOXAux; |
---|
34 | |
---|
35 | /** |
---|
36 | * @param args |
---|
37 | */ |
---|
38 | public static void main(String[] args) { |
---|
39 | // TODO Auto-generated method stub |
---|
40 | |
---|
41 | } |
---|
42 | |
---|
43 | |
---|
44 | public CrawlerThread(IMAPFolder imapFINBOXAux, String hostSolr) |
---|
45 | { |
---|
46 | this.hostSolr = hostSolr; |
---|
47 | this.imapFINBOXAux = imapFINBOXAux; |
---|
48 | |
---|
49 | //Cria a ACL a ser configurada |
---|
50 | acl = new ACL("expresso-admin"); |
---|
51 | |
---|
52 | //Define direito de leitura |
---|
53 | Rights rights = new Rights(); |
---|
54 | rights.add(Right.READ); |
---|
55 | |
---|
56 | //Configura direito de leitura para a ACL |
---|
57 | acl.setRights(rights); |
---|
58 | } |
---|
59 | @Override |
---|
60 | public void run() { |
---|
61 | try { |
---|
62 | Thread.sleep(5000); |
---|
63 | } catch (InterruptedException e1) { |
---|
64 | // TODO Auto-generated catch block |
---|
65 | e1.printStackTrace(); |
---|
66 | } |
---|
67 | //Cria uma instância de conexão com o servidor Solr |
---|
68 | SolrServer solrServer = new HttpSolrServer(hostSolr); |
---|
69 | |
---|
70 | //Cria |
---|
71 | List<Message2SolrEntity> listIMAPMsgs = new ArrayList<Message2SolrEntity>(); |
---|
72 | //INBOX |
---|
73 | |
---|
74 | //Adiciona ACL de permissão de leitura, para o usuário expresso-admin conseguir fazer a leitura |
---|
75 | try |
---|
76 | { |
---|
77 | imapFINBOXAux.addACL(acl); |
---|
78 | |
---|
79 | |
---|
80 | //Abre a caixa de e-mail |
---|
81 | imapFINBOXAux.open(Folder.READ_ONLY); |
---|
82 | |
---|
83 | //Carrega as mensagens da caixa para um array |
---|
84 | Message[] msgsINBOX = imapFINBOXAux.getMessages(); |
---|
85 | |
---|
86 | //Faz a iteração entre as mensagens |
---|
87 | for (Message msgAuxINBOX : msgsINBOX) |
---|
88 | { |
---|
89 | |
---|
90 | IMAPMessage m = (IMAPMessage)msgAuxINBOX; |
---|
91 | |
---|
92 | //adiciona as mensagens na varÃavel listIMAPMsgs |
---|
93 | try |
---|
94 | { |
---|
95 | dumpPart(m, listIMAPMsgs); |
---|
96 | }catch(Exception e) |
---|
97 | { |
---|
98 | continue; |
---|
99 | } |
---|
100 | } |
---|
101 | imapFINBOXAux.removeACL("expresso-admin"); |
---|
102 | for(int i = 0; i < listIMAPMsgs.size(); i++ ) |
---|
103 | { |
---|
104 | SolrInputDocument doc = new SolrInputDocument(); |
---|
105 | doc.addField("id", listIMAPMsgs.get(i).getId().toString()); |
---|
106 | doc.addField("user", listIMAPMsgs.get(i).getUser().toString()); |
---|
107 | doc.addField("folder", listIMAPMsgs.get(i).getFolder().toString()); |
---|
108 | doc.addField("msg_no", listIMAPMsgs.get(i).getMsgNo()); |
---|
109 | doc.addField("from", listIMAPMsgs.get(i).getFrom().toString()); |
---|
110 | doc.addField("to", listIMAPMsgs.get(i).getTo().toString()); |
---|
111 | doc.addField("subject", listIMAPMsgs.get(i).getSubject().toString()); |
---|
112 | doc.addField("content", listIMAPMsgs.get(i).getContent().toString()); |
---|
113 | doc.addField("copyto", listIMAPMsgs.get(i).getCopyto().toString()); |
---|
114 | doc.addField("sent_date", listIMAPMsgs.get(i).getSent_date()); |
---|
115 | doc.addField("hiddencopyto", listIMAPMsgs.get(i).getHiddencopyto().toString()); |
---|
116 | |
---|
117 | try { |
---|
118 | solrServer.add(doc); |
---|
119 | } catch (Exception e) { |
---|
120 | System.err.println("solr -> " + e.getMessage()); |
---|
121 | } |
---|
122 | |
---|
123 | } |
---|
124 | |
---|
125 | listIMAPMsgs = null; |
---|
126 | |
---|
127 | solrServer.commit(); |
---|
128 | |
---|
129 | solrServer = null; |
---|
130 | |
---|
131 | for (Folder fAux : imapFINBOXAux.list()) |
---|
132 | { |
---|
133 | //Verifica se não é uma pasta compartilhada |
---|
134 | if(fAux.getFullName().split("/").length <= 3 && !fAux.getFullName().split("/")[2].equals("user")) |
---|
135 | { |
---|
136 | //INBOX |
---|
137 | IMAPFolder imapFAux = (IMAPFolder)fAux; |
---|
138 | |
---|
139 | crawIntoUserFolders(imapFAux); |
---|
140 | |
---|
141 | } |
---|
142 | } |
---|
143 | } |
---|
144 | catch (Exception e) |
---|
145 | { |
---|
146 | //do nothing |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | private void crawIntoUserFolders(IMAPFolder imapFAux) throws Exception |
---|
151 | { |
---|
152 | SolrServer solrServer = new HttpSolrServer(hostSolr); |
---|
153 | |
---|
154 | List<Message2SolrEntity> listIMAPMsgs = new ArrayList<Message2SolrEntity>(); |
---|
155 | |
---|
156 | try |
---|
157 | { |
---|
158 | imapFAux.addACL(acl); |
---|
159 | }catch(Exception e) |
---|
160 | { |
---|
161 | return; |
---|
162 | } |
---|
163 | |
---|
164 | imapFAux.open(Folder.READ_ONLY); |
---|
165 | |
---|
166 | Message[] msgs= imapFAux.getMessages(); |
---|
167 | |
---|
168 | for (Message msgAux: msgs) |
---|
169 | { |
---|
170 | IMAPMessage m = (IMAPMessage)msgAux; |
---|
171 | try |
---|
172 | { |
---|
173 | dumpPart(m, listIMAPMsgs); |
---|
174 | }catch(Exception e) |
---|
175 | { |
---|
176 | continue; |
---|
177 | } |
---|
178 | } |
---|
179 | imapFAux.removeACL("expresso-admin"); |
---|
180 | System.out.println(imapFAux.getFullName()); |
---|
181 | for(int i = 0; i < listIMAPMsgs.size(); i++ ) |
---|
182 | { |
---|
183 | SolrInputDocument doc = new SolrInputDocument(); |
---|
184 | doc.addField("id", listIMAPMsgs.get(i).getId()); |
---|
185 | doc.addField("user", listIMAPMsgs.get(i).getUser()); |
---|
186 | doc.addField("folder", listIMAPMsgs.get(i).getFolder()); |
---|
187 | doc.addField("msg_no", listIMAPMsgs.get(i).getMsgNo()); |
---|
188 | doc.addField("from", listIMAPMsgs.get(i).getFrom()); |
---|
189 | doc.addField("to", listIMAPMsgs.get(i).getTo()); |
---|
190 | doc.addField("subject", listIMAPMsgs.get(i).getSubject()); |
---|
191 | doc.addField("content", listIMAPMsgs.get(i).getContent()); |
---|
192 | doc.addField("copyto", listIMAPMsgs.get(i).getCopyto()); |
---|
193 | doc.addField("sent_date", listIMAPMsgs.get(i).getSent_date()); |
---|
194 | doc.addField("hiddencopyto", listIMAPMsgs.get(i).getHiddencopyto()); |
---|
195 | |
---|
196 | } |
---|
197 | listIMAPMsgs = null; |
---|
198 | solrServer.commit(); |
---|
199 | solrServer = null; |
---|
200 | } |
---|
201 | |
---|
202 | public void dumpPart(IMAPMessage m, List<Message2SolrEntity> listIMAPMsgs) throws Exception |
---|
203 | { |
---|
204 | //Verifica se possui identificador para poder continuar |
---|
205 | if(m.getMessageID() == null || m.getMessageID().equals("")){ |
---|
206 | return; |
---|
207 | } |
---|
208 | |
---|
209 | Message2SolrEntity msgEntity = null; |
---|
210 | msgEntity = new Message2SolrEntity(); |
---|
211 | |
---|
212 | msgEntity.setId(new StringBuilder( m.getMessageID())); |
---|
213 | |
---|
214 | String user = m.getFolder().getFullName().split("/")[1]; |
---|
215 | msgEntity.setUser(new StringBuilder(user)); |
---|
216 | |
---|
217 | String folder = m.getFolder().getFullName().split("/") |
---|
218 | [m.getFolder().getFullName().split("/").length-1]; |
---|
219 | |
---|
220 | if(!user.trim().equals(folder.trim())) |
---|
221 | { |
---|
222 | msgEntity.setFolder(new StringBuilder(folder)); |
---|
223 | } |
---|
224 | else |
---|
225 | { |
---|
226 | msgEntity.setFolder(new StringBuilder("INBOX")); |
---|
227 | } |
---|
228 | |
---|
229 | msgEntity.setMsgNo(new StringBuilder(String.valueOf(m.getMessageNumber()))); |
---|
230 | |
---|
231 | |
---|
232 | if(m.getSubject() != null){ |
---|
233 | msgEntity.setSubject(new StringBuilder( m.getSubject() )); |
---|
234 | } |
---|
235 | Address[] a; |
---|
236 | // FROM |
---|
237 | if ((a = m.getFrom()) != null) { |
---|
238 | for (int j = 0; j < a.length; j++) |
---|
239 | { |
---|
240 | if(msgEntity.getFrom() == null) |
---|
241 | { |
---|
242 | msgEntity.setFrom(new StringBuilder(a[j].toString())); |
---|
243 | } |
---|
244 | else |
---|
245 | { |
---|
246 | msgEntity.setFrom(msgEntity.getFrom().append(", ").append(a[j].toString())); |
---|
247 | } |
---|
248 | // System.out.println("FROM: " + a[j].toString()); |
---|
249 | } |
---|
250 | |
---|
251 | } |
---|
252 | |
---|
253 | // TO |
---|
254 | if ((a = m.getRecipients(Message.RecipientType.TO)) != null) { |
---|
255 | for (int j = 0; j < a.length; j++) |
---|
256 | { |
---|
257 | if(msgEntity.getTo() == null) |
---|
258 | { |
---|
259 | msgEntity.setTo(new StringBuilder(a[j].toString())); |
---|
260 | } |
---|
261 | else |
---|
262 | { |
---|
263 | msgEntity.setTo(msgEntity.getTo().append(", ").append(a[j].toString())); |
---|
264 | } |
---|
265 | } |
---|
266 | } |
---|
267 | |
---|
268 | // CC |
---|
269 | if ((a = m.getRecipients(Message.RecipientType.CC)) != null) { |
---|
270 | for (int j = 0; j < a.length; j++) |
---|
271 | { |
---|
272 | if(msgEntity.getCopyto() == null) |
---|
273 | { |
---|
274 | msgEntity.setCopyto(new StringBuilder(a[j].toString())); |
---|
275 | } |
---|
276 | else |
---|
277 | { |
---|
278 | msgEntity.setCopyto(msgEntity.getCopyto().append(", ").append(a[j].toString())); |
---|
279 | } |
---|
280 | } |
---|
281 | } |
---|
282 | // CC |
---|
283 | if ((a = m.getRecipients(Message.RecipientType.BCC)) != null) { |
---|
284 | for (int j = 0; j < a.length; j++) |
---|
285 | { |
---|
286 | if(msgEntity.getHiddencopyto() == null) |
---|
287 | { |
---|
288 | msgEntity.setHiddencopyto(new StringBuilder(a[j].toString())); |
---|
289 | } |
---|
290 | else |
---|
291 | { |
---|
292 | msgEntity.setHiddencopyto(new StringBuilder(msgEntity.getHiddencopyto() + ","+a[j].toString())); |
---|
293 | } |
---|
294 | } |
---|
295 | } |
---|
296 | |
---|
297 | // DATE |
---|
298 | Date d = m.getSentDate(); |
---|
299 | if(msgEntity != null) |
---|
300 | { |
---|
301 | if(d != null) |
---|
302 | { |
---|
303 | msgEntity.setSent_date(new StringBuilder(String.valueOf(d.getTime()))); |
---|
304 | } |
---|
305 | else |
---|
306 | { |
---|
307 | msgEntity.setSent_date(new StringBuilder(String.valueOf(new Date().getTime()))); |
---|
308 | } |
---|
309 | } |
---|
310 | |
---|
311 | try{ |
---|
312 | Object o = m.getContent(); |
---|
313 | if (o instanceof String) { |
---|
314 | if( msgEntity != null ) |
---|
315 | { |
---|
316 | msgEntity.setContent(new StringBuilder( o.toString())); |
---|
317 | } |
---|
318 | } else if (o instanceof Multipart) { |
---|
319 | Multipart multipart = (Multipart)o; |
---|
320 | int count = multipart.getCount(); |
---|
321 | |
---|
322 | for (int i = 0; i < count; i++) |
---|
323 | { |
---|
324 | if(msgEntity.getContent() == null || msgEntity.getContent().toString().trim().equals("")) |
---|
325 | { |
---|
326 | msgEntity.setContent(new StringBuilder("")); |
---|
327 | } |
---|
328 | if(multipart.getBodyPart(i).getContentType() != Part.ATTACHMENT) |
---|
329 | { |
---|
330 | msgEntity.setContent(msgEntity.getContent().append(" ").append(getPlainContent(multipart.getBodyPart(i)))); |
---|
331 | } |
---|
332 | |
---|
333 | } |
---|
334 | |
---|
335 | } |
---|
336 | }catch (Exception e){ |
---|
337 | System.out.println("Exception ocurred!"); |
---|
338 | e.printStackTrace(); |
---|
339 | } |
---|
340 | |
---|
341 | if(msgEntity != null && msgEntity.getId() != null){ |
---|
342 | listIMAPMsgs.add(msgEntity); |
---|
343 | } |
---|
344 | |
---|
345 | } |
---|
346 | |
---|
347 | private StringBuilder getPlainContent(BodyPart bodyPart) throws IOException, MessagingException |
---|
348 | { |
---|
349 | try |
---|
350 | { |
---|
351 | |
---|
352 | if(bodyPart.getContent() instanceof String) |
---|
353 | { |
---|
354 | // ByteArrayOutputStream out = new ByteArrayOutputStream(); |
---|
355 | // InputStream in = bodyPart.getInputStream(); |
---|
356 | // |
---|
357 | // byte[] buffer = new byte[1024]; |
---|
358 | // byte[] data = null; |
---|
359 | // |
---|
360 | // while(in.read(buffer) != -1) { |
---|
361 | // out.write(buffer); |
---|
362 | // } |
---|
363 | // |
---|
364 | // data = out.toByteArray(); |
---|
365 | |
---|
366 | StringBuilder text = new StringBuilder(bodyPart.getContent().toString()); |
---|
367 | |
---|
368 | // out.close(); |
---|
369 | // in.close(); |
---|
370 | |
---|
371 | return text; |
---|
372 | } |
---|
373 | }catch(Exception e) |
---|
374 | { |
---|
375 | System.out.println(e.getMessage()); |
---|
376 | e.printStackTrace(); |
---|
377 | return new StringBuilder(""); |
---|
378 | } |
---|
379 | |
---|
380 | |
---|
381 | return new StringBuilder(""); |
---|
382 | } |
---|
383 | |
---|
384 | } |
---|