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