Search in sources :

Example 1 with FileBuffer

use of i2p.susi.util.FileBuffer in project i2p.i2p by i2p.

the class MailCache method getMail.

/**
 * Fetch any needed data from pop3 server, unless mode is CACHE_ONLY.
 * Blocking unless mode is CACHE_ONLY.
 *
 * @param uidl message id to get
 * @param mode CACHE_ONLY to not pull from pop server
 * @return An e-mail or null
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public Mail getMail(String uidl, FetchMode mode) {
    Mail mail = null, newMail = null;
    /*
		 * synchronize update to hashtable
		 */
    synchronized (mails) {
        mail = mails.get(uidl);
        if (mail == null) {
            newMail = new Mail(uidl);
            // TODO really?
            mails.put(uidl, newMail);
        }
    }
    if (mail == null) {
        mail = newMail;
        mail.setSize(mailbox.getSize(uidl));
    }
    if (mail.markForDeletion)
        return null;
    long sz = mail.getSize();
    if (mode == FetchMode.HEADER && sz > 0 && sz <= FETCH_ALL_SIZE)
        mode = FetchMode.ALL;
    if (mode == FetchMode.HEADER) {
        if (!mail.hasHeader())
            mail.setHeader(mailbox.getHeader(uidl));
    } else if (mode == FetchMode.ALL) {
        if (!mail.hasBody()) {
            File file = new File(_context.getTempDir(), "susimail-new-" + _context.random().nextLong());
            Buffer rb = mailbox.getBody(uidl, new FileBuffer(file));
            if (rb != null) {
                mail.setBody(rb);
                if (disk != null && disk.saveMail(mail) && !Boolean.parseBoolean(Config.getProperty(WebMail.CONFIG_LEAVE_ON_SERVER))) {
                    mailbox.queueForDeletion(mail.uidl);
                }
            }
        }
    } else {
    // else if it wasn't in cache, too bad
    }
    return mail;
}
Also used : MemoryBuffer(i2p.susi.util.MemoryBuffer) Buffer(i2p.susi.util.Buffer) FileBuffer(i2p.susi.util.FileBuffer) ReadBuffer(i2p.susi.util.ReadBuffer) FileBuffer(i2p.susi.util.FileBuffer) File(java.io.File)

Example 2 with FileBuffer

use of i2p.susi.util.FileBuffer in project i2p.i2p by i2p.

the class MailCache method getMail.

/**
 * Fetch any needed data from pop3 server.
 * Mail objects are inserted into the requests.
 * After this, call getUIDLs() to get all known mail UIDLs.
 * MUST already be connected, otherwise returns false.
 *
 * Blocking.
 *
 * @param mode HEADER or ALL only
 * @return true if any were fetched
 * @since 0.9.13
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public boolean getMail(FetchMode mode) {
    if (mode == FetchMode.CACHE_ONLY)
        throw new IllegalArgumentException();
    boolean hOnly = mode == FetchMode.HEADER;
    Collection<String> popKnown = mailbox.getUIDLs();
    if (popKnown == null)
        return false;
    List<POP3Request> fetches = new ArrayList<POP3Request>();
    // requests.to send off
    for (String uidl : popKnown) {
        Mail mail = null, newMail = null;
        boolean headerOnly = hOnly;
        /*
			 * synchronize update to hashtable
			 */
        synchronized (mails) {
            mail = mails.get(uidl);
            if (mail == null) {
                newMail = new Mail(uidl);
                mails.put(uidl, newMail);
            }
        }
        if (mail == null) {
            mail = newMail;
            mail.setSize(mailbox.getSize(uidl));
        }
        if (mail.markForDeletion)
            continue;
        long sz = mail.getSize();
        if (sz > 0 && sz <= FETCH_ALL_SIZE)
            headerOnly = false;
        if (headerOnly) {
            if (!mail.hasHeader()) {
                if (disk != null) {
                    if (disk.getMail(mail, true)) {
                        Debug.debug(Debug.DEBUG, "Loaded header from disk cache: " + uidl);
                        // note that disk loaded the full body if it had it
                        if (mail.hasBody() && !Boolean.parseBoolean(Config.getProperty(WebMail.CONFIG_LEAVE_ON_SERVER))) {
                            // we already have it, send delete
                            mailbox.queueForDeletion(mail.uidl);
                        }
                        // found on disk, woo
                        continue;
                    }
                }
                POP3Request pr = new POP3Request(mail, true, new MemoryBuffer(1024));
                fetches.add(pr);
            } else {
                if (mail.hasBody() && !Boolean.parseBoolean(Config.getProperty(WebMail.CONFIG_LEAVE_ON_SERVER))) {
                    // we already have it, send delete
                    mailbox.queueForDeletion(mail.uidl);
                }
            }
        } else {
            if (!mail.hasBody()) {
                if (disk != null) {
                    if (disk.getMail(mail, false)) {
                        Debug.debug(Debug.DEBUG, "Loaded body from disk cache: " + uidl);
                        // note that disk loaded the full body if it had it
                        if (!Boolean.parseBoolean(Config.getProperty(WebMail.CONFIG_LEAVE_ON_SERVER))) {
                            // we already have it, send delete
                            mailbox.queueForDeletion(mail.uidl);
                        }
                        // found on disk, woo
                        continue;
                    }
                }
                File file = new File(_context.getTempDir(), "susimail-new-" + _context.random().nextLong());
                POP3Request pr = new POP3Request(mail, false, new FileBuffer(file));
                fetches.add(pr);
            } else {
                if (!Boolean.parseBoolean(Config.getProperty(WebMail.CONFIG_LEAVE_ON_SERVER))) {
                    // we already have it, send delete
                    mailbox.queueForDeletion(mail.uidl);
                }
            }
        }
    }
    boolean rv = false;
    if (!fetches.isEmpty()) {
        // Send off the fetches
        // gaah compiler
        List foo = fetches;
        List<FetchRequest> bar = foo;
        mailbox.getBodies(bar);
        // Process results
        for (POP3Request pr : fetches) {
            if (pr.getSuccess()) {
                Mail mail = pr.mail;
                if (!mail.hasHeader())
                    mail.setNew(true);
                if (pr.getHeaderOnly()) {
                    mail.setHeader(pr.getBuffer());
                } else {
                    mail.setBody(pr.getBuffer());
                }
                rv = true;
                if (disk != null) {
                    if (disk.saveMail(mail) && mail.hasBody() && !Boolean.parseBoolean(Config.getProperty(WebMail.CONFIG_LEAVE_ON_SERVER))) {
                        mailbox.queueForDeletion(mail.uidl);
                    }
                }
            }
        }
    }
    return rv;
}
Also used : FileBuffer(i2p.susi.util.FileBuffer) ArrayList(java.util.ArrayList) MemoryBuffer(i2p.susi.util.MemoryBuffer) FetchRequest(i2p.susi.webmail.pop3.POP3MailBox.FetchRequest) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File)

Aggregations

FileBuffer (i2p.susi.util.FileBuffer)2 MemoryBuffer (i2p.susi.util.MemoryBuffer)2 File (java.io.File)2 Buffer (i2p.susi.util.Buffer)1 ReadBuffer (i2p.susi.util.ReadBuffer)1 FetchRequest (i2p.susi.webmail.pop3.POP3MailBox.FetchRequest)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1