⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 imapcollection.java

📁 很好的搜索代码,大家都很难下载!抓紧时间啊!不要错过!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                        indexMessage(doc, messages[i]);
                        // add it
                        writer.addDocument(doc);
                        md5DocumentCache.add(msgID);
                    } else {
                        log.debug("existing message skipped for message: " + msgID);
                    }
                }
                catch (Exception ioe) {
                    // can be side effect of hosed up mail headers
                    log.warn("Bad Message: " + messages[i], ioe);
                    continue;
                }
            }

        }
        // recurse if possible
        if ((thisFolder.getType() & Folder.HOLDS_FOLDERS) != 0) {
            Folder[] far = thisFolder.list();
            if (far != null) {
                for (int i = 0; i < far.length; i++) {
                    indexFolder(writer, far[i]);
                }
            }
        }
        if (thisFolder.isOpen()) {
            log.debug("Closing folder: " + thisFolder.getFullName());
            thisFolder.close(false); // false => do not expunge
        }

        return true;
    }

    /**
     * Index one message.
     */
    private void indexMessage(final Document doc, final Message m) throws MessagingException, IOException {
        if (stopRequested) {
            log.info("Indexing stops, due to request");
            return;
        }
        final long uid = ((UIDFolder) m.getFolder()).getUID(m);

        // form a URL that mozilla seems to accept. Couldn't get it to accept
        // what I thought was the standard

        String urlPrefix = "imap://" + user + "@" + host + ":143/fetch%3EUID%3E/";

        final String url = urlPrefix + m.getFolder().getFullName() + "%3E" + uid;
        doc.add(Field.Text("name", url));

        final String subject = m.getSubject();
        final Date recv = m.getReceivedDate();
        final Date sent = m.getSentDate();
        log.info("Folder: " + m.getFolder().getFullName() + ": Message received " + recv + ", subject: " + subject);
        // -------------------------------------------------------
        // data gathered, now add to doc

        if (subject != null) {
            doc.add(Field.Text(F_SUBJECT, m.getSubject()));
            doc.add(Field.Text("title", m.getSubject()));
        }

        if (recv != null) {
            doc.add(Field.Keyword(F_RECEIVED, DateTools.timeToString(recv.getTime(), DateTools.Resolution.SECOND)));
        }

        if (sent != null) {
            doc.add(Field.Keyword(F_SENT, DateTools.timeToString(sent.getTime(), DateTools.Resolution.SECOND)));
            // store date as yyyyMMdd
            DateFormat df = new SimpleDateFormat("yyyyMMdd");
            String dfString = df.format(new Date(sent.getTime()));
            doc.add(Field.Keyword("modified", dfString));
        }

        doc.add(Field.Keyword(F_URL, url));

        Address[] addrs = m.getAllRecipients();
        if (addrs != null) {
            for (int j = 0; j < addrs.length; j++) {
                doc.add(Field.Keyword(F_TO, "" + addrs[j]));
            }
        }

        addrs = m.getFrom();
        if (addrs != null) {
            for (int j = 0; j < addrs.length; j++) {
                doc.add(Field.Keyword(F_FROM, "" + addrs[j]));
                doc.add(Field.Keyword("author", "" + addrs[j]));
            }
        }
        addrs = m.getReplyTo();
        if (addrs != null) {
            for (int j = 0; j < addrs.length; j++) {
                doc.add(Field.Keyword(F_REPLY_TO, "" + addrs[j]));
            }
        }

        doc.add(Field.Keyword(F_UID, "" + uid));

        // could ignore docs that have the deleted flag set
        for (int j = 0; j < FLAGS.length; j++) {
            boolean val = m.isSet(FLAGS[j]);
            doc.add(Field.Keyword(SFLAGS[j], (val ? "true" : "false")));
        }

        // now special case for mime
        if (m instanceof MimeMessage) {
            // mime++;
            MimeMessage mm = (MimeMessage) m;
            log.debug("index, adding MimeMessage " + m.getFileName());
            indexMimeMessage(doc, mm);

        } else {
            // nmime++;

            final DataHandler dh = m.getDataHandler();
            log.debug("index, adding (non-MIME) Content " + m.getFileName());
            doc.add(Field.Text(F_CONTENTS, new InputStreamReader(dh.getInputStream())));
        }
    }

    /**
     * Index a MIME message, which seems to be all of them.
     */
    private void indexMimeMessage(final Document doc, final MimeMessage mm) throws MessagingException, IOException {
        // o.println( "\n\n[index mm]: " + mm.getSubject());

        long size = mm.getSize();
        int lines = mm.getLineCount();
        doc.add(Field.Keyword("hash", mm.getMessageID()));

        if (size > 0) {
            doc.add(Field.UnIndexed(F_SIZE, "" + size));
        } else {
            doc.add(Field.UnIndexed(F_SIZE, "" + 0));
        }
        indexPart(doc, mm);
    }

    /**
     * Index a part.
     */
    private void indexPart(final Document doc, final Part p) throws MessagingException, IOException {
        int size = p.getSize();
        String ct = p.getContentType();
        String cd = p.getDescription();
        log.debug("IndexContent, type: " + ct + ", description: " + cd);
        Object content = null;

        if (ct != null) {
            doc.add(Field.Keyword(F_CT, ct));
        }
        doc.add(Field.Keyword("type", "MAIL"));

        if (cd != null) {
            doc.add(Field.Keyword(F_CD, cd));
        }

        if (ct != null && ct.toLowerCase().startsWith("image/")) {
            // no point for now but maybe in the future we see if any forms such as jpegs have some strings
            return;
        }

        try {
            // get content object, indirectly calls into JAF which decodes based on MIME type and char
            content = p.getContent();
        }
        catch (IOException ioe) {
            log.warn("OUCH decoding attachment, p=" + p, ioe);
            doc.add(Field.Text(F_CONTENTS, new InputStreamReader(p.getInputStream())));
            return;
        }

        if (content instanceof MimeMultipart) {
            int n = ((MimeMultipart) content).getCount();
            for (int i = 0; i < n; i++) {
                BodyPart bp = ((MimeMultipart) content).getBodyPart(i);
                // same thing ends up happening regardless, if/else left it to show structure
                indexPart(doc, bp);
            }
        } else if (content instanceof MimePart) {
            indexPart(doc, (MimePart) content);
        } else if (content instanceof Part) {
            indexPart(doc, (Part) content);
        } else if (content instanceof String) {
            indexString(doc, (String) content, ct);
        } else if (content instanceof InputStream) {
            indexStream(doc, (InputStream) content, ct);
        } else {
            log.error("***** Strange content: " + content + "/" + content.getClass() + " ct=" + ct + " cd=" + cd);
        }
    }

    /**
     * Index a Stream.
     */
    private void indexStream(final Document doc, final InputStream content, final String type) throws MessagingException,
        IOException {
        log.debug("indexStream for type: " + type);
        ExtractorFactory ef = new ExtractorFactory();
        Extractor ex = ef.createExtractor(type);
        if (ex != null) {
            String parsedContent = ex.getContent(content);
            log.info("Adding content");
            doc.add(Field.Text(F_CONTENTS, new StringReader(parsedContent)));
        } else {
            log.warn("indexStream: Unknown mimetype: " + type);
        }
    }

    /**
     * Index a String.
     */
    private void indexString(final Document doc, final String content, final String type) throws MessagingException, IOException {
        log.debug("indexString for type: " + type);
        if (type.toLowerCase().startsWith("text/plain")) {
            log.info("Adding TEXT: ");
            doc.add(Field.Text(F_CONTENTS, new StringReader(content)));
        } else if (type.toLowerCase().startsWith("text/html")) {
            HTMLExtractor he = new HTMLExtractor();
            String parsedContent = he.getContent(content);
            log.info("Adding HTML: ");
            doc.add(Field.Text(F_CONTENTS, new StringReader(parsedContent)));
        } else {
            log.warn("indexString: Unknown mimetype: " + type);
        }
    }

    /**
     * Sets existsOnDisk based on whether the collection (contentDir) actually (now) sits on disk.
     * 
     * @todo the whole existsOnDisk construction is a little funny, refactor some time
     */
    protected void setExistsOnDisk() {
        existsOnDisk = false;
        Store store = null;
        try {
            // try to connect to server and find folder
            log.debug("Connecting to IMAP server: " + host);
            Properties props = System.getProperties();
            Session session = Session.getDefaultInstance(props, null);
            store = session.getStore("imap");
            log.debug("Connecting to " + host + " as " + user);
            store.connect(host, user, password);
            log.debug("Connected");
            // start at the proper folder
            Folder topFolder = null;
            if (StringUtils.hasText(folder)) {
                topFolder = store.getFolder(folder);
            } else {
                topFolder = store.getDefaultFolder();
            }
            existsOnDisk = (topFolder != null);
        }
        catch (NoSuchProviderException e) {
            log.warn("Can't connect to " + host, e);
        }
        catch (MessagingException e) {
            log.warn("Error while accessing IMAP server " + host, e);
        }
        finally {
            if (store != null) {
                try {
                    store.close();
                }
                catch (MessagingException e1) {
                    log.error("Error closing IMAP server " + host, e1);
                }
            }
        }
    }

    public void setFolder(String thisFolder) {
        this.folder = thisFolder;
    }

    /**
     * @param thisHost The host to set.
     */
    public void setHost(String thisHost) {
        this.host = thisHost;
    }

    /**
     * @param thisPassword The password to set.
     */
    public void setPassword(String thisPassword) {
        this.password = thisPassword;
    }

    /**
     * @param thisUser The user to set.
     */
    public void setUser(String thisUser) {
        this.user = thisUser;
    }

    /**
     * Prints Collection as String for logging.
     * 
     * @return pretty formatted information about the collection
     */
    public final String toString() {
        return "Collection(" + id + "), with name: " + name + ",\n\t\tdescription: " + description + ",\n\t\tcontentDir: "
            + contentDir + ",\n\t\turl: " + url + ",\n\t\texistsOnDisk: " + existsOnDisk + ",\n\t\tindexDir: " + indexDir
            + ",\n\t\tcacheDir: " + cacheDir + ",\n\t\tcacheUrl: " + cacheUrl + ",\n\t\tanalyzer: " + analyzer
            + ",\n\t\tkeepCache: " + keepCache + ",\n\t\tisKeepCacheSet: " + isKeepCacheSet + ",\n\t\tnumberOfDocs: "
            + numberOfDocs + ",\n\t\tmanager: " + manager + ",\n\t\tlastIndexed: " + lastIndexed;
        // +
        // ",\n\t\tmd5DocumentCache:
        // " + md5DocumentCache +
        // "\n\n";
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -