source: sandbox/expresso-solr/imapCrawler/SRC/JUserCrawler4IMAP/src/pkg/crawler/data/conn/Crawler.java @ 8056

Revision 8056, 12.9 KB checked in by gustavo, 11 years ago (diff)

Ticket #000 - Commit contendo o expresso com solr funcionando corretamente

Line 
1package pkg.crawler.data.conn;
2
3import java.io.IOException;
4import java.io.ObjectOutputStream;
5import java.net.Socket;
6import java.net.UnknownHostException;
7import java.util.ArrayList;
8import java.util.Date;
9import java.util.List;
10import java.util.Properties;
11
12import javax.mail.Address;
13import javax.mail.BodyPart;
14import javax.mail.Folder;
15import javax.mail.Message;
16import javax.mail.MessagingException;
17import javax.mail.Multipart;
18import javax.mail.Part;
19import javax.mail.Session;
20
21import org.apache.solr.client.solrj.SolrServer;
22import org.apache.solr.client.solrj.impl.HttpSolrServer;
23import org.apache.solr.common.SolrInputDocument;
24
25import pkg.crawler.entity.Message2SolrEntity;
26
27import com.sun.mail.imap.ACL;
28import com.sun.mail.imap.IMAPFolder;
29import com.sun.mail.imap.IMAPMessage;
30import com.sun.mail.imap.IMAPStore;
31import com.sun.mail.imap.Rights;
32import com.sun.mail.imap.Rights.Right;
33
34public class Crawler {
35       
36        private String hostSolr;
37        private ACL acl;
38        private IMAPFolder imapFINBOXAux;
39        private IMAPStore store;
40        private String strIMAPFolderAux;
41       
42        public Crawler(String host, String user, String password, String imapFINBOXAux, String hostSolr) throws MessagingException
43        {
44                Properties props = System.getProperties();
45                Session session = Session.getInstance(props, null);
46                strIMAPFolderAux = imapFINBOXAux;
47                store = new IMAPStore(session, null);
48               
49                store.connect(host, user, password);
50               
51                this.hostSolr = hostSolr;
52                this.imapFINBOXAux = (IMAPFolder)store.getFolder(imapFINBOXAux);
53               
54                //Cria a ACL a ser configurada
55                acl = new ACL("expresso-admin");
56
57                //Define direito de leitura
58                Rights rights = new Rights();
59                rights.add(Right.READ);
60
61                //Configura direito de leitura para a ACL
62                acl.setRights(rights);
63        }
64       
65        public void run() {
66                //Cria uma instância de conexão com o servidor Solr
67                SolrServer solrServer = new HttpSolrServer(hostSolr);
68
69                //Cria
70                List<Message2SolrEntity> listIMAPMsgs = new ArrayList<Message2SolrEntity>();
71                //INBOX
72
73                //Adiciona ACL de permissão de leitura, para o usuário expresso-admin conseguir fazer a leitura
74                try
75                {
76                        imapFINBOXAux.addACL(acl);
77
78
79                        //Abre a caixa de e-mail
80                        imapFINBOXAux.open(Folder.READ_ONLY);
81
82                        //Carrega as mensagens da caixa para um array
83                        Message[] msgsINBOX = imapFINBOXAux.getMessages();
84                       
85                       
86                        int iCount = 0;
87                        //Faz a iteração entre as mensagens
88                        for (Message msgAuxINBOX : msgsINBOX)
89                        {
90
91                                IMAPMessage m = (IMAPMessage)msgAuxINBOX;
92
93                                //adiciona as mensagens na varíavel listIMAPMsgs
94                                try
95                                {
96                                        dumpPart(m, listIMAPMsgs);
97                                        iCount++;
98                                }catch(Exception e)
99                                {
100                                        continue;
101                                }
102                                msgAuxINBOX = null;
103                                m = null;
104                               
105                               
106                                SolrInputDocument doc = new SolrInputDocument();
107                                doc.addField("id", listIMAPMsgs.get(listIMAPMsgs.size()-1).getId().toString());
108                                doc.addField("user", listIMAPMsgs.get(listIMAPMsgs.size()-1).getUser().toString());
109                                doc.addField("folder", listIMAPMsgs.get(listIMAPMsgs.size()-1).getFolder().toString());
110                                doc.addField("msg_no", listIMAPMsgs.get(listIMAPMsgs.size()-1).getMsgNo());
111                                doc.addField("from", listIMAPMsgs.get(listIMAPMsgs.size()-1).getFrom().toString());
112                                doc.addField("to", listIMAPMsgs.get(listIMAPMsgs.size()-1).getTo().toString());
113                                doc.addField("subject", listIMAPMsgs.get(listIMAPMsgs.size()-1).getSubject().toString());
114                                doc.addField("content", listIMAPMsgs.get(listIMAPMsgs.size()-1).getContent().toString());
115                                doc.addField("copyto", listIMAPMsgs.get(listIMAPMsgs.size()-1).getCopyto().toString());
116                                doc.addField("sent_date", listIMAPMsgs.get(listIMAPMsgs.size()-1).getSent_date());
117                                doc.addField("hiddencopyto", listIMAPMsgs.get(listIMAPMsgs.size()-1).getHiddencopyto().toString());
118
119                                try {
120                                        solrServer.add(doc);
121                                } catch (Exception e) {
122                                        System.err.println("solr -> " + e.getMessage());
123                                }
124                               
125                               
126                                if(iCount%200 == 0)
127                                {
128                                        solrServer.commit();
129                                       
130                                        listIMAPMsgs.clear();
131                                       
132                                        solrServer = new HttpSolrServer(hostSolr);
133                                }
134                               
135                        }
136                        imapFINBOXAux.removeACL("expresso-admin");
137                        for(int i = 0; i < listIMAPMsgs.size(); i++ )
138                        {
139                                SolrInputDocument doc = new SolrInputDocument();
140                                doc.addField("id", listIMAPMsgs.get(i).getId().toString());
141                                doc.addField("user", listIMAPMsgs.get(i).getUser().toString());
142                                doc.addField("folder", listIMAPMsgs.get(i).getFolder().toString());
143                                doc.addField("msg_no", listIMAPMsgs.get(i).getMsgNo());
144                                doc.addField("from", listIMAPMsgs.get(i).getFrom().toString());
145                                doc.addField("to", listIMAPMsgs.get(i).getTo().toString());
146                                doc.addField("subject", listIMAPMsgs.get(i).getSubject().toString());
147                                doc.addField("content", listIMAPMsgs.get(i).getContent().toString());
148                                doc.addField("copyto", listIMAPMsgs.get(i).getCopyto().toString());
149                                doc.addField("sent_date", listIMAPMsgs.get(i).getSent_date());
150                                doc.addField("hiddencopyto", listIMAPMsgs.get(i).getHiddencopyto().toString());
151
152                                try {
153                                        solrServer.add(doc);
154                                } catch (Exception e) {
155                                        System.err.println("solr -> " + e.getMessage());
156                                }
157
158                        }
159
160                        listIMAPMsgs = null;
161
162                        solrServer.commit();
163
164                        solrServer = null;
165
166                        for (Folder fAux : imapFINBOXAux.list())
167                        {
168                                //Verifica se não é uma pasta compartilhada
169                                if(fAux.getFullName().split("/").length <= 3 && !fAux.getFullName().split("/")[2].equals("user"))
170                                {
171                                        //INBOX
172                                        IMAPFolder imapFAux = (IMAPFolder)fAux;
173                                       
174                                        crawIntoUserFolders(imapFAux);
175                                       
176                                }
177                        }
178                        imapFINBOXAux.close(true);
179                        imapFINBOXAux = null;
180                       
181                       
182                        Socket client = null;
183                        ObjectOutputStream oos = null;
184                        try {
185                                client = new Socket("127.1.1.1", 8090);
186                               
187                                oos = new ObjectOutputStream(client.getOutputStream());
188                               
189                                oos.writeObject(strIMAPFolderAux);
190                               
191                                oos.close();
192                               
193                                client.close();
194                        } catch (UnknownHostException e) {
195                                e.printStackTrace();
196                        } catch (IOException e) {
197                                e.printStackTrace();
198                        }
199                }
200                catch (Exception e)
201                {
202                        Socket client = null;
203                        ObjectOutputStream oos = null;
204                        try {
205                                client = new Socket("127.1.1.1", 8090);
206                               
207                                oos = new ObjectOutputStream(client.getOutputStream());
208                               
209                                oos.writeObject(strIMAPFolderAux);
210                               
211                                oos.close();
212                               
213                                client.close();
214                        } catch (UnknownHostException ee) {
215                                e.printStackTrace();
216                        } catch (IOException ee) {
217                                e.printStackTrace();
218                        }
219                        return;
220                }
221               
222               
223               
224        }
225
226        private void crawIntoUserFolders(IMAPFolder imapFAux) throws Exception
227        {
228                SolrServer solrServer = new HttpSolrServer(hostSolr);
229
230                List<Message2SolrEntity> listIMAPMsgs = new ArrayList<Message2SolrEntity>();
231
232                try
233                {
234                        imapFAux.addACL(acl);
235                }catch(Exception e)
236                {
237                        return;
238                }
239
240                imapFAux.open(Folder.READ_ONLY);
241
242                Message[] msgs= imapFAux.getMessages();
243               
244                int iCount = 0;
245                for (Message msgAux: msgs)
246                {
247                        IMAPMessage m = (IMAPMessage)msgAux;
248                        try
249                        {
250                                dumpPart(m, listIMAPMsgs);
251                                iCount++;
252                        }catch(Exception e)
253                        {
254                                continue;
255                        }
256                        msgAux = null;
257                        m = null;
258                       
259                        SolrInputDocument doc = new SolrInputDocument();
260                        doc.addField("id", listIMAPMsgs.get(listIMAPMsgs.size()-1).getId().toString());
261                        doc.addField("user", listIMAPMsgs.get(listIMAPMsgs.size()-1).getUser().toString());
262                        doc.addField("folder", listIMAPMsgs.get(listIMAPMsgs.size()-1).getFolder().toString());
263                        doc.addField("msg_no", listIMAPMsgs.get(listIMAPMsgs.size()-1).getMsgNo());
264                        doc.addField("from", listIMAPMsgs.get(listIMAPMsgs.size()-1).getFrom().toString());
265                        doc.addField("to", listIMAPMsgs.get(listIMAPMsgs.size()-1).getTo().toString());
266                        doc.addField("subject", listIMAPMsgs.get(listIMAPMsgs.size()-1).getSubject().toString());
267                        doc.addField("content", listIMAPMsgs.get(listIMAPMsgs.size()-1).getContent().toString());
268                        doc.addField("copyto", listIMAPMsgs.get(listIMAPMsgs.size()-1).getCopyto().toString());
269                        doc.addField("sent_date", listIMAPMsgs.get(listIMAPMsgs.size()-1).getSent_date());
270                        doc.addField("hiddencopyto", listIMAPMsgs.get(listIMAPMsgs.size()-1).getHiddencopyto().toString());
271
272                        try {
273                                solrServer.add(doc);
274                        } catch (Exception e) {
275                                System.err.println("solr -> " + e.getMessage());
276                        }
277                       
278                       
279                        if(iCount%200 == 0)
280                        {
281                                solrServer.commit();
282                               
283                                listIMAPMsgs.clear();
284                               
285                                solrServer = new HttpSolrServer(hostSolr);
286                        }
287                       
288                }
289                imapFAux.removeACL("expresso-admin");
290                System.out.println(imapFAux.getFullName());
291                for(int i = 0; i < listIMAPMsgs.size(); i++ )
292                {
293                        SolrInputDocument doc = new SolrInputDocument();
294                        doc.addField("id", listIMAPMsgs.get(i).getId());
295                        doc.addField("user", listIMAPMsgs.get(i).getUser());
296                        doc.addField("folder", listIMAPMsgs.get(i).getFolder());
297                        doc.addField("msg_no", listIMAPMsgs.get(i).getMsgNo());
298                        doc.addField("from", listIMAPMsgs.get(i).getFrom());
299                        doc.addField("to", listIMAPMsgs.get(i).getTo());
300                        doc.addField("subject", listIMAPMsgs.get(i).getSubject());
301                        doc.addField("content", listIMAPMsgs.get(i).getContent());
302                        doc.addField("copyto", listIMAPMsgs.get(i).getCopyto());
303                        doc.addField("sent_date", listIMAPMsgs.get(i).getSent_date());
304                        doc.addField("hiddencopyto", listIMAPMsgs.get(i).getHiddencopyto());
305                       
306                }
307                listIMAPMsgs = null;
308                imapFAux.close(true);
309                imapFAux = null;
310                solrServer.commit();
311                solrServer = null;
312        }
313
314        private void dumpPart(IMAPMessage m, List<Message2SolrEntity> listIMAPMsgs) throws Exception
315        {
316                //Verifica se possui identificador para poder continuar
317                if(m.getMessageID() == null || m.getMessageID().equals("")){
318                        return;
319                }
320
321                Message2SolrEntity msgEntity = null;
322                msgEntity = new Message2SolrEntity();
323
324                msgEntity.setId(new StringBuilder( m.getMessageID()));
325
326                String user = m.getFolder().getFullName().split("/")[1];
327                msgEntity.setUser(new StringBuilder(user));
328
329                String folder = m.getFolder().getFullName().split("/")
330                                [m.getFolder().getFullName().split("/").length-1];
331
332                if(!user.trim().equals(folder.trim()))
333                {
334                        msgEntity.setFolder(new StringBuilder(folder));
335                }
336                else
337                {
338                        msgEntity.setFolder(new StringBuilder("INBOX"));
339                }
340
341                msgEntity.setMsgNo(new StringBuilder(String.valueOf(m.getMessageNumber())));
342
343
344                if(m.getSubject() != null){
345                        msgEntity.setSubject(new StringBuilder( m.getSubject() ));
346                }
347                Address[] a;
348                // FROM
349                if ((a = m.getFrom()) != null) {
350                        for (int j = 0; j < a.length; j++)
351                        {
352                                if(msgEntity.getFrom() == null)
353                                {
354                                        msgEntity.setFrom(new StringBuilder(a[j].toString()));
355                                }
356                                else
357                                {
358                                        msgEntity.setFrom(msgEntity.getFrom().append(", ").append(a[j].toString()));
359                                }
360                                //                                      System.out.println("FROM: " + a[j].toString());
361                        }
362
363                }
364
365                // TO
366                if ((a = m.getRecipients(Message.RecipientType.TO)) != null) {
367                        for (int j = 0; j < a.length; j++)
368                        {
369                                if(msgEntity.getTo() == null)
370                                {
371                                        msgEntity.setTo(new StringBuilder(a[j].toString()));
372                                }
373                                else
374                                {
375                                        msgEntity.setTo(msgEntity.getTo().append(", ").append(a[j].toString()));
376                                }
377                        }
378                }
379
380                // CC
381                if ((a = m.getRecipients(Message.RecipientType.CC)) != null) {
382                        for (int j = 0; j < a.length; j++)
383                        {
384                                if(msgEntity.getCopyto() == null)
385                                {
386                                        msgEntity.setCopyto(new StringBuilder(a[j].toString()));
387                                }
388                                else
389                                {
390                                        msgEntity.setCopyto(msgEntity.getCopyto().append(", ").append(a[j].toString()));
391                                }
392                        }
393                }
394                // CC
395                if ((a = m.getRecipients(Message.RecipientType.BCC)) != null) {
396                        for (int j = 0; j < a.length; j++)
397                        {
398                                if(msgEntity.getHiddencopyto() == null)
399                                {
400                                        msgEntity.setHiddencopyto(new StringBuilder(a[j].toString()));
401                                }
402                                else
403                                {
404                                        msgEntity.setHiddencopyto(new StringBuilder(msgEntity.getHiddencopyto() + ","+a[j].toString()));
405                                }
406                        }
407                }
408
409                // DATE
410                Date d = m.getSentDate();
411                if(msgEntity != null)
412                {
413                        if(d != null)
414                        {
415                                msgEntity.setSent_date(new StringBuilder(String.valueOf(d.getTime())));
416                        }
417                        else
418                        {
419                                msgEntity.setSent_date(new StringBuilder(String.valueOf(new Date().getTime())));
420                        }
421                }
422
423                try{
424                        Object o = m.getContent();
425                        if (o instanceof String) {
426                                if( msgEntity != null )
427                                {
428                                        msgEntity.setContent(new StringBuilder( o.toString()));
429                                }
430                        } else if (o instanceof Multipart) {
431                                Multipart multipart = (Multipart)o;
432                                int count = multipart.getCount();
433
434                                for (int i = 0; i < count; i++)
435                                {
436                                        if(msgEntity.getContent() == null || msgEntity.getContent().toString().trim().equals(""))
437                                        {
438                                                msgEntity.setContent(new StringBuilder(""));
439                                        }
440                                        if(multipart.getBodyPart(i).getContentType() != Part.ATTACHMENT)
441                                        {
442                                                msgEntity.setContent(msgEntity.getContent().append(" ").append(getPlainContent(multipart.getBodyPart(i))));
443                                        }
444
445                                }
446
447                        }
448                }catch (Exception e){
449                        System.out.println("Exception ocurred!");
450                        e.printStackTrace();
451                }
452
453                if(msgEntity != null && msgEntity.getId() != null){
454                        listIMAPMsgs.add(msgEntity);
455                }
456                m = null;
457        }
458
459        private StringBuilder getPlainContent(BodyPart bodyPart) throws IOException, MessagingException
460        {
461                try
462                {
463
464                        if(bodyPart.getContent() instanceof String)
465                        {
466                                StringBuilder text = new StringBuilder(bodyPart.getContent().toString());
467
468                                return text;
469                        }
470                }catch(Exception e)
471                {
472                        System.out.println(e.getMessage());
473                        e.printStackTrace();
474                        return new StringBuilder("");
475                }
476
477
478                return new StringBuilder("");
479        }
480
481}
Note: See TracBrowser for help on using the repository browser.