[8056] | 1 | package pkg.crawler.data.conn; |
---|
| 2 | |
---|
| 3 | import java.util.ArrayList; |
---|
| 4 | import java.util.Date; |
---|
| 5 | import java.util.Properties; |
---|
| 6 | |
---|
| 7 | import javax.mail.Address; |
---|
| 8 | import javax.mail.Folder; |
---|
| 9 | import javax.mail.Message; |
---|
| 10 | import javax.mail.MessagingException; |
---|
| 11 | import javax.mail.Multipart; |
---|
| 12 | import javax.mail.Part; |
---|
| 13 | import javax.mail.Session; |
---|
| 14 | |
---|
| 15 | import org.apache.solr.client.solrj.SolrServer; |
---|
| 16 | import org.apache.solr.client.solrj.impl.HttpSolrServer; |
---|
| 17 | import org.apache.solr.common.SolrInputDocument; |
---|
| 18 | |
---|
| 19 | import pkg.crawler.entity.Message2SolrEntity; |
---|
| 20 | |
---|
| 21 | import com.sun.mail.imap.ACL; |
---|
| 22 | import com.sun.mail.imap.IMAPFolder; |
---|
| 23 | import com.sun.mail.imap.IMAPMessage; |
---|
| 24 | import com.sun.mail.imap.IMAPStore; |
---|
| 25 | import com.sun.mail.imap.Rights; |
---|
| 26 | import com.sun.mail.imap.Rights.Right; |
---|
| 27 | |
---|
| 28 | public class CopyOfCrawler{ |
---|
| 29 | |
---|
| 30 | private String host; |
---|
| 31 | private String user; |
---|
| 32 | private String password; |
---|
| 33 | private String mbox; |
---|
| 34 | private String url; |
---|
| 35 | private boolean debug; |
---|
| 36 | private String loginStartsWith; |
---|
| 37 | private static ArrayList<Message2SolrEntity> listIMAPMsgs = new ArrayList<Message2SolrEntity>(); |
---|
| 38 | |
---|
| 39 | |
---|
| 40 | // public CopyOfCrawler(String loginStartsWith) |
---|
| 41 | // { |
---|
| 42 | // this.host = "172.25.60.61"; |
---|
| 43 | // this.user = "expresso-admin"; |
---|
| 44 | // this.password = "pti123"; |
---|
| 45 | // this.mbox = "user"; |
---|
| 46 | // this.url = null; |
---|
| 47 | // this.debug = false; |
---|
| 48 | // this.loginStartsWith = loginStartsWith; |
---|
| 49 | // } |
---|
| 50 | // |
---|
| 51 | // |
---|
| 52 | // @Override |
---|
| 53 | // public void run() |
---|
| 54 | // { |
---|
| 55 | // |
---|
| 56 | // //Inicializando as propriedades default da máquina para conexão |
---|
| 57 | // //com o servidor IMAP |
---|
| 58 | // Properties props = System.getProperties(); |
---|
| 59 | // Session session = Session.getInstance(props, null); |
---|
| 60 | // |
---|
| 61 | // IMAPStore store = new IMAPStore(session, null); |
---|
| 62 | // |
---|
| 63 | // try { |
---|
| 64 | // |
---|
| 65 | // //Conectando com o servidor IMAP |
---|
| 66 | // store.connect(host, user, password); |
---|
| 67 | // //Definindo a pasta base a ser usada |
---|
| 68 | // IMAPFolder imapFolder = (IMAPFolder)store.getFolder(mbox); |
---|
| 69 | // |
---|
| 70 | // //Cria a ACL a ser configurada |
---|
| 71 | // ACL acl = new ACL("expresso-admin"); |
---|
| 72 | // |
---|
| 73 | // Rights rights = new Rights(); |
---|
| 74 | // rights.add(Right.READ); |
---|
| 75 | // |
---|
| 76 | // |
---|
| 77 | // //Configura direito de leitura para a ACL |
---|
| 78 | // acl.setRights(rights); |
---|
| 79 | // |
---|
| 80 | // SolrServer solrServer = new HttpSolrServer("http://172.25.60.61:8983/solr/"); |
---|
| 81 | // |
---|
| 82 | // solrServer.deleteByQuery("*:*"); |
---|
| 83 | // //Faz a iteração entre os usuários |
---|
| 84 | // for (Folder fINBOXAux : imapFolder.list()) |
---|
| 85 | // { |
---|
| 86 | // //INBOX |
---|
| 87 | // IMAPFolder imapFINBOXAux = (IMAPFolder)fINBOXAux; |
---|
| 88 | // |
---|
| 89 | // imapFINBOXAux.addACL(acl); |
---|
| 90 | // |
---|
| 91 | // imapFINBOXAux.open(Folder.READ_ONLY); |
---|
| 92 | // |
---|
| 93 | // Message[] msgsINBOX = imapFINBOXAux.getMessages(); |
---|
| 94 | // |
---|
| 95 | // for (Message msgAuxINBOX : msgsINBOX) |
---|
| 96 | // { |
---|
| 97 | // dumpPart(msgAuxINBOX); |
---|
| 98 | // } |
---|
| 99 | // imapFINBOXAux.removeACL("expresso-admin"); |
---|
| 100 | // for(int i = 0; i < CopyOfCrawler.listIMAPMsgs.size(); i++ ) |
---|
| 101 | // { |
---|
| 102 | // SolrInputDocument doc = new SolrInputDocument(); |
---|
| 103 | // doc.addField("id", listIMAPMsgs.get(i).getId()); |
---|
| 104 | // doc.addField("user", listIMAPMsgs.get(i).getUser()); |
---|
| 105 | // doc.addField("folder", listIMAPMsgs.get(i).getUser()); |
---|
| 106 | // doc.addField("from", listIMAPMsgs.get(i).getFrom()); |
---|
| 107 | // doc.addField("to", listIMAPMsgs.get(i).getTo()); |
---|
| 108 | // doc.addField("subject", listIMAPMsgs.get(i).getSubject()); |
---|
| 109 | // doc.addField("content", listIMAPMsgs.get(i).getContent()); |
---|
| 110 | // doc.addField("copyto", listIMAPMsgs.get(i).getCopyto()); |
---|
| 111 | // doc.addField("hiddencopyto", listIMAPMsgs.get(i).getHiddencopyto()); |
---|
| 112 | // |
---|
| 113 | // solrServer.add(doc); |
---|
| 114 | // } |
---|
| 115 | // |
---|
| 116 | // CopyOfCrawler.listIMAPMsgs.clear(); |
---|
| 117 | // |
---|
| 118 | // solrServer.commit(); |
---|
| 119 | // |
---|
| 120 | // for (Folder fAux : imapFINBOXAux.list()) |
---|
| 121 | // { |
---|
| 122 | // |
---|
| 123 | // //INBOX |
---|
| 124 | // IMAPFolder imapFAux = (IMAPFolder)fAux; |
---|
| 125 | // |
---|
| 126 | // imapFAux.addACL(acl); |
---|
| 127 | // |
---|
| 128 | // imapFAux.open(Folder.READ_ONLY); |
---|
| 129 | // |
---|
| 130 | // Message[] msgs= imapFAux.getMessages(); |
---|
| 131 | // |
---|
| 132 | // for (Message msgAux: msgs) |
---|
| 133 | // { |
---|
| 134 | // dumpPart(msgAux); |
---|
| 135 | // } |
---|
| 136 | // imapFAux.removeACL("expresso-admin"); |
---|
| 137 | // System.out.println(imapFAux.getFullName()); |
---|
| 138 | // for(int i = 0; i < CopyOfCrawler.listIMAPMsgs.size(); i++ ) |
---|
| 139 | // { |
---|
| 140 | // SolrInputDocument doc = new SolrInputDocument(); |
---|
| 141 | // doc.addField("id", listIMAPMsgs.get(i).getId()); |
---|
| 142 | // doc.addField("user", listIMAPMsgs.get(i).getUser()); |
---|
| 143 | // doc.addField("folder", listIMAPMsgs.get(i).getUser()); |
---|
| 144 | // doc.addField("from", listIMAPMsgs.get(i).getFrom()); |
---|
| 145 | // doc.addField("to", listIMAPMsgs.get(i).getTo()); |
---|
| 146 | // doc.addField("subject", listIMAPMsgs.get(i).getSubject()); |
---|
| 147 | // doc.addField("content", listIMAPMsgs.get(i).getContent()); |
---|
| 148 | // doc.addField("copyto", listIMAPMsgs.get(i).getCopyto()); |
---|
| 149 | // doc.addField("hiddencopyto", listIMAPMsgs.get(i).getHiddencopyto()); |
---|
| 150 | // |
---|
| 151 | // solrServer.add(doc); |
---|
| 152 | // } |
---|
| 153 | // CopyOfCrawler.listIMAPMsgs.clear(); |
---|
| 154 | // } |
---|
| 155 | // |
---|
| 156 | // |
---|
| 157 | // System.out.println(imapFINBOXAux.getFullName()); |
---|
| 158 | // // IMAPFolder imapFAux = (IMAPFolder)imapFolder.list()[0]; //INBOX |
---|
| 159 | // // IMAPFolder imapFAux = (IMAPFolder)imapFolder.list()[0].list()[0]; //Aprovações e talz |
---|
| 160 | // // |
---|
| 161 | // // imapFAux.addRights(acl); |
---|
| 162 | // // imapFAux.open(IMAPFolder.READ_ONLY); |
---|
| 163 | // // System.out.println(imapFAux.getMessageCount()); |
---|
| 164 | // // |
---|
| 165 | // // IMAPMessage message = (IMAPMessage)imapFAux.getMessages()[0]; |
---|
| 166 | // // |
---|
| 167 | // // System.out.println(message.getMessageID()); |
---|
| 168 | // // System.out.println(message.getContentType()); |
---|
| 169 | // // |
---|
| 170 | // // |
---|
| 171 | // // imapFAux.removeACL("expresso-admin"); |
---|
| 172 | // // imapFAux.close(false); |
---|
| 173 | // } |
---|
| 174 | // |
---|
| 175 | // |
---|
| 176 | // } catch (MessagingException e) { |
---|
| 177 | // // TODO Auto-generated catch block |
---|
| 178 | // e.printStackTrace(); |
---|
| 179 | // } |
---|
| 180 | // catch (Exception e) |
---|
| 181 | // { |
---|
| 182 | // e.printStackTrace(); |
---|
| 183 | // } |
---|
| 184 | // } |
---|
| 185 | // |
---|
| 186 | // |
---|
| 187 | // public static void dumpPart(Part p) throws Exception { |
---|
| 188 | // Message2SolrEntity msgEntity = null; |
---|
| 189 | // |
---|
| 190 | // if (p instanceof Message) { |
---|
| 191 | // msgEntity = new Message2SolrEntity(); |
---|
| 192 | // IMAPMessage m = (IMAPMessage)p; |
---|
| 193 | // |
---|
| 194 | // msgEntity.setId(m.getMessageID()); |
---|
| 195 | // |
---|
| 196 | // if(msgEntity.getId() == null || msgEntity.getId().equals("")){ |
---|
| 197 | // System.out.println("Damn it!"); |
---|
| 198 | // } |
---|
| 199 | // |
---|
| 200 | // String user = m.getFolder().getFullName().split("/")[1]; |
---|
| 201 | // msgEntity.setUser(user); |
---|
| 202 | // |
---|
| 203 | // String folder = m.getFolder().getFullName().split("/") |
---|
| 204 | // [m.getFolder().getFullName().split("/").length-1]; |
---|
| 205 | // |
---|
| 206 | // if(!user.equals(folder)) |
---|
| 207 | // { |
---|
| 208 | // msgEntity.setFolder(folder); |
---|
| 209 | // } |
---|
| 210 | // else |
---|
| 211 | // { |
---|
| 212 | // msgEntity.setFolder("INBOX"); |
---|
| 213 | // } |
---|
| 214 | // |
---|
| 215 | // |
---|
| 216 | // |
---|
| 217 | // msgEntity.setSubject(m.getSubject()); |
---|
| 218 | // Address[] a; |
---|
| 219 | // // FROM |
---|
| 220 | // if ((a = m.getFrom()) != null) { |
---|
| 221 | // for (int j = 0; j < a.length; j++) |
---|
| 222 | // { |
---|
| 223 | // if(msgEntity.getFrom() == null) |
---|
| 224 | // { |
---|
| 225 | // msgEntity.setFrom(a[j].toString()); |
---|
| 226 | // } |
---|
| 227 | // else |
---|
| 228 | // { |
---|
| 229 | // msgEntity.setFrom(msgEntity.getFrom() + ","+a[j].toString()); |
---|
| 230 | // } |
---|
| 231 | // // System.out.println("FROM: " + a[j].toString()); |
---|
| 232 | // } |
---|
| 233 | // |
---|
| 234 | // } |
---|
| 235 | // |
---|
| 236 | // // TO |
---|
| 237 | // if ((a = m.getRecipients(Message.RecipientType.TO)) != null) { |
---|
| 238 | // for (int j = 0; j < a.length; j++) |
---|
| 239 | // { |
---|
| 240 | // if(msgEntity.getTo() == null) |
---|
| 241 | // { |
---|
| 242 | // msgEntity.setTo(a[j].toString()); |
---|
| 243 | // } |
---|
| 244 | // else |
---|
| 245 | // { |
---|
| 246 | // msgEntity.setTo(msgEntity.getTo() + ","+a[j].toString()); |
---|
| 247 | // } |
---|
| 248 | // } |
---|
| 249 | // } |
---|
| 250 | // |
---|
| 251 | // // CC |
---|
| 252 | // if ((a = m.getRecipients(Message.RecipientType.CC)) != null) { |
---|
| 253 | // for (int j = 0; j < a.length; j++) |
---|
| 254 | // { |
---|
| 255 | // if(msgEntity.getCopyto() == null) |
---|
| 256 | // { |
---|
| 257 | // msgEntity.setCopyto(a[j].toString()); |
---|
| 258 | // } |
---|
| 259 | // else |
---|
| 260 | // { |
---|
| 261 | // msgEntity.setCopyto(msgEntity.getCopyto() + ","+a[j].toString()); |
---|
| 262 | // } |
---|
| 263 | // } |
---|
| 264 | // } |
---|
| 265 | // // CC |
---|
| 266 | // if ((a = m.getRecipients(Message.RecipientType.BCC)) != null) { |
---|
| 267 | // for (int j = 0; j < a.length; j++) |
---|
| 268 | // { |
---|
| 269 | // if(msgEntity.getHiddencopyto() == null) |
---|
| 270 | // { |
---|
| 271 | // msgEntity.setHiddencopyto(a[j].toString()); |
---|
| 272 | // } |
---|
| 273 | // else |
---|
| 274 | // { |
---|
| 275 | // msgEntity.setHiddencopyto(msgEntity.getHiddencopyto() + ","+a[j].toString()); |
---|
| 276 | // } |
---|
| 277 | // } |
---|
| 278 | // } |
---|
| 279 | // |
---|
| 280 | // // DATE |
---|
| 281 | // Date d = m.getSentDate(); |
---|
| 282 | // if(msgEntity != null) |
---|
| 283 | // { |
---|
| 284 | // msgEntity.setSent_date((d != null ? d.toLocaleString() : "UNKNOWN")); |
---|
| 285 | // } |
---|
| 286 | // |
---|
| 287 | // |
---|
| 288 | // } |
---|
| 289 | // |
---|
| 290 | // try{ |
---|
| 291 | // Object o = p.getContent(); |
---|
| 292 | // if (o instanceof String) { |
---|
| 293 | // if( msgEntity != null ) |
---|
| 294 | // { |
---|
| 295 | // msgEntity.setContent((String)o); |
---|
| 296 | // } |
---|
| 297 | // } else if (o instanceof Multipart) { |
---|
| 298 | // Multipart mp = (Multipart)o; |
---|
| 299 | // int count = mp.getCount(); |
---|
| 300 | // for (int i = 0; i < count; i++) |
---|
| 301 | // dumpPart(mp.getBodyPart(i)); |
---|
| 302 | // } |
---|
| 303 | // }catch (Exception e){ |
---|
| 304 | // System.out.println("Exception ocurred!"); |
---|
| 305 | // e.printStackTrace(); |
---|
| 306 | // } |
---|
| 307 | // |
---|
| 308 | // if(msgEntity != null && msgEntity.getId() != null){ |
---|
| 309 | // CopyOfCrawler.listIMAPMsgs.add(msgEntity); |
---|
| 310 | // } |
---|
| 311 | // |
---|
| 312 | // } |
---|
| 313 | |
---|
| 314 | } |
---|