javax.mail.Quota Java Examples

The following examples show how to use javax.mail.Quota. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: QuotaCommand.java    From greenmail with Apache License 2.0 6 votes vote down vote up
@Override
protected void doProcess(final ImapRequestLineReader request, final ImapResponse response,
                         final ImapSession session) throws ProtocolException, FolderException, AuthorizationException {
    if(!session.getHost().getStore().isQuotaSupported()) {
        response.commandFailed(this,"Quota is not supported. Activate quota capability first");
    }

    String root = parser.mailbox(request);
    // NAME root (name usage limit)

    Quota[] quota = session.getHost().getStore().getQuota(
            root, session.getUser().getQualifiedMailboxName());
    for(Quota q: quota) {
        StringBuilder buf = new StringBuilder();
        appendQuota(q, buf);
        response.untaggedResponse(buf.toString());
    }

    response.commandComplete(this);
}
 
Example #2
Source File: QuotaCommand.java    From greenmail with Apache License 2.0 6 votes vote down vote up
protected void appendQuota(Quota quota, StringBuilder buf) {
    buf.append("QUOTA ");
    appendQuotaRootName(quota, buf);
    buf.append(SP);
    buf.append('(');
    boolean pad = false;
    for(Quota.Resource resource:quota.resources) {
        if(pad) {
           buf.append(SP);
        }
        buf.append(resource.name);
        buf.append(SP).append(resource.usage);
        buf.append(SP).append(resource.limit);
        pad = true;
    }
    buf.append(')');
}
 
Example #3
Source File: SetQuotaCommand.java    From greenmail with Apache License 2.0 6 votes vote down vote up
@Override
protected void doProcess(final ImapRequestLineReader request, final ImapResponse response,
                         final ImapSession session) {
    if(!session.getHost().getStore().isQuotaSupported()) {
        response.commandFailed(this,"Quota is not supported. Activate quota capability first");
    }
    try {
        String root = parser.mailbox(request);
        Quota quota = new Quota(root);
        parser.consumeChar(request, ' ');
        parser.consumeChar(request, '(');
        quota.setResourceLimit(parser.astring(request), parser.consumeLong(request));
        char c =request.nextWordChar();
        if(')' != c) {
            quota.setResourceLimit(parser.astring(request), parser.consumeLong(request));
        }
        parser.consumeChar(request, ')');
        session.getHost().getStore().setQuota(
                quota, session.getUser().getQualifiedMailboxName());
        response.commandComplete(this);
    } catch (ProtocolException e) {
        response.commandFailed(this, "Can not parse command"+e.getMessage());
    }
}
 
Example #4
Source File: InMemoryStore.java    From greenmail with Apache License 2.0 6 votes vote down vote up
@Override
public Quota[] getQuota(final String root, final String qualifiedRootPrefix) {
    Set<String> rootPaths = new HashSet<>();
    if (!root.contains(ImapConstants.HIERARCHY_DELIMITER)) {
        rootPaths.add(qualifiedRootPrefix + root);
    } else {
        for (String r : root.split(ImapConstants.HIERARCHY_DELIMITER)) {
            rootPaths.add(qualifiedRootPrefix + r);
        }
    }
    rootPaths.add(qualifiedRootPrefix); // Add default root

    Set<Quota> collectedQuotas = new HashSet<>();
    for (String p : rootPaths) {
        Set<Quota> quotas = quotaMap.get(p);
        if (null != quotas) {
            collectedQuotas.addAll(quotas);
        }
    }
    updateQuotas(collectedQuotas, qualifiedRootPrefix);
    return collectedQuotas.toArray(new Quota[collectedQuotas.size()]);
}
 
Example #5
Source File: InMemoryStore.java    From greenmail with Apache License 2.0 6 votes vote down vote up
private void updateQuota(final Quota quota, final String pQualifiedRootPrefix) {
    MailFolder folder = getMailbox(
            ImapConstants.USER_NAMESPACE + ImapConstants.HIERARCHY_DELIMITER +
                    pQualifiedRootPrefix + ImapConstants.HIERARCHY_DELIMITER +
                    quota.quotaRoot);
    try {
        for (Quota.Resource r : quota.resources) {
            if (STORAGE.equals(r.name)) {
                long size = 0;
                for (StoredMessage m : folder.getMessages()) {
                    size += m.getMimeMessage().getSize();
                }
                r.usage = size;
            } else if (MESSAGES.equals(r.name)) {
                r.usage = folder.getMessageCount();
            } else {
                throw new IllegalStateException("Quota " + r.name + " not supported");
            }
        }
    } catch (MessagingException ex) {
        throw new IllegalStateException("Can not update/verify quota " + quota, ex);
    }
}
 
Example #6
Source File: InMemoryStore.java    From greenmail with Apache License 2.0 6 votes vote down vote up
@Override
public void setQuota(final Quota quota, String qualifiedRootPrefix) {
    // Validate
    for (Quota.Resource r : quota.resources) {
        if (!STORAGE.equals(r.name) && !MESSAGES.equals(r.name)) {
            throw new IllegalStateException("Quota " + r.name + " not supported");
        }
    }

    // Save quota
    Set<Quota> quotas = quotaMap.get(qualifiedRootPrefix + quota.quotaRoot);
    if (null == quotas) {
        quotas = new HashSet<>();
        quotaMap.put(qualifiedRootPrefix + quota.quotaRoot, quotas);
    } else {
        quotas.clear(); // " Any previous resource limits for the named quota root are discarded"
    }
    quotas.add(quota);
}
 
Example #7
Source File: QuotaCommand.java    From greenmail with Apache License 2.0 5 votes vote down vote up
protected void appendQuotaRootName(Quota quota, StringBuilder buf) {
    String rootName = quota.quotaRoot;
    if(null == rootName || rootName.length()==0) {
        rootName = "\"\"";
    }
    buf.append('\"').append(rootName).append('\"');
}
 
Example #8
Source File: IMAPMockStore.java    From javamail-mock2 with Apache License 2.0 4 votes vote down vote up
@Override
public synchronized Quota[] getQuota(final String root) throws MessagingException {
    throw new MessagingException("QUOTA not supported");
}
 
Example #9
Source File: IMAPMockFolder.java    From javamail-mock2 with Apache License 2.0 4 votes vote down vote up
@Override
public Quota[] getQuota() throws MessagingException {
    throw new MessagingException("QUOTA not supported");
}
 
Example #10
Source File: IMAPMockFolder.java    From javamail-mock2 with Apache License 2.0 4 votes vote down vote up
@Override
public void setQuota(final Quota quota) throws MessagingException {
    throw new MessagingException("QUOTA not supported");
}
 
Example #11
Source File: InMemoryStore.java    From greenmail with Apache License 2.0 4 votes vote down vote up
private void updateQuotas(final Set<Quota> quotas,
                          final String qualifiedRootPrefix) {
    for (Quota q : quotas) {
        updateQuota(q, qualifiedRootPrefix);
    }
}
 
Example #12
Source File: IMAPMockStore.java    From javamail-mock2 with Apache License 2.0 2 votes vote down vote up
@Override
public synchronized void setQuota(final Quota quota) throws MessagingException {

    throw new MessagingException("QUOTA not supported");

}
 
Example #13
Source File: Store.java    From greenmail with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the quotas.
 *
 * @link http://www.ietf.org/rfc/rfc2087.txt
 * @see com.sun.mail.imap.IMAPStore#getQuota(String)
 * @param root the quota root
 * @param qualifiedRootPrefix the user specific prefix
 * @return the quotas, or an empty array.
 */
Quota[] getQuota(String root, String qualifiedRootPrefix);
 
Example #14
Source File: Store.java    From greenmail with Apache License 2.0 2 votes vote down vote up
/**
 * Sets the quota.
 *
 * @link http://www.ietf.org/rfc/rfc2087.txt
 * @see com.sun.mail.imap.IMAPStore#setQuota(javax.mail.Quota)
 * @param quota the quota.
 * @param qualifiedRootPrefix the user specific prefix
 */
void setQuota(Quota quota, String qualifiedRootPrefix);