/** * Tells us if the quota is reached * * @return True if the user over uses the resource of this quota */ public boolean isOverQuota() { return isOverQuotaWithAdditionalValue(0); }
private void tryCountAddition(long count) throws OverQuotaException { Quota<QuotaCount> afterAdditionQuotaCount = messageQuota.addValueToQuota(QuotaCount.count(count)); if (afterAdditionQuotaCount.isOverQuota()) { throw new OverQuotaException( "You have too many messages in " + quotaRoot.getValue(), messageQuota.getLimit(), messageQuota.getUsed()); } }
@Override public Quota<QuotaCount> getMessageQuota(QuotaRoot quotaRoot) { return Quota.<QuotaCount>builder() .used(QuotaCount.count(0)) .computedLimit(QuotaCount.unlimited()) .build(); }
@Test public void isOverQuotaWithAdditionalValueShouldReturnFalseWhenAtLimit() { Quota<QuotaCount> quota = Quota.<QuotaCount>builder().used(QuotaCount.count(36)).computedLimit(QuotaCount.count(36)).build(); assertThat(quota.isOverQuotaWithAdditionalValue(0)).isFalse(); }
@Test public void isOverQuotaShouldReturnFalseWhenMaxValueIsUnlimited() { Quota<QuotaCount> quota = Quota.<QuotaCount>builder().used(QuotaCount.count(36)).computedLimit(QuotaCount.unlimited()).build(); assertThat(quota.isOverQuota()).isFalse(); }
@Test public void deletingAMailboxShouldDecreaseCurrentQuota() throws Exception { resources.fillMailbox(); mailboxManager.deleteMailbox(inbox, session); QuotaRoot quotaRoot = quotaRootResolver.getQuotaRoot(inbox); Quota<QuotaCount> messageQuota = resources.getQuotaManager().getMessageQuota(quotaRoot); Quota<QuotaSize> storageQuota = resources.getQuotaManager().getStorageQuota(quotaRoot); SoftAssertions.assertSoftly(softly -> { softly.assertThat(messageQuota.getUsed()).isEqualTo(QuotaCount.count(0)); softly.assertThat(storageQuota.getUsed()).isEqualTo(QuotaSize.size(0)); }); }
@Override protected void doProcess(GetQuotaRequest message, ImapSession session, String tag, ImapCommand command, Responder responder) { try { QuotaRoot quotaRoot = quotaRootResolver.fromString(message.getQuotaRoot()); if (hasRight(quotaRoot, session)) { Quota<QuotaCount> messageQuota = quotaManager.getMessageQuota(quotaRoot); Quota<QuotaSize> storageQuota = quotaManager.getStorageQuota(quotaRoot); if (messageQuota.getLimit().isLimited()) { responder.respond(new QuotaResponse(ImapConstants.MESSAGE_QUOTA_RESOURCE, quotaRoot.getValue(), messageQuota)); } if (storageQuota.getLimit().isLimited()) { responder.respond(new QuotaResponse(ImapConstants.STORAGE_QUOTA_RESOURCE, quotaRoot.getValue(), storageQuota)); } okComplete(command, tag, responder); } else { Object[] params = new Object[]{ MailboxACL.Right.Read.toString(), command.getName(), "Any mailbox of this user USER" }; HumanReadableText humanReadableText = new HumanReadableText(HumanReadableText.UNSUFFICIENT_RIGHTS_KEY, HumanReadableText.UNSUFFICIENT_RIGHTS_DEFAULT_VALUE, params); no(command, tag, responder, humanReadableText); } } catch (MailboxException me) { taggedBad(command, tag, responder, HumanReadableText.FAILURE_NO_SUCH_MAILBOX); } }
private void trySizeAddition(long size) throws OverQuotaException { Quota<QuotaSize> afterAdditionQuotaSize = sizeQuota.addValueToQuota(QuotaSize.size(size)); if (afterAdditionQuotaSize.isOverQuota()) { throw new OverQuotaException( "You use too much space in " + quotaRoot.getValue(), afterAdditionQuotaSize.getLimit(), afterAdditionQuotaSize.getUsed()); } }
@Override public Quota<QuotaSize> getStorageQuota(QuotaRoot quotaRoot) { return Quota.<QuotaSize>builder() .used(QuotaSize.size(0)) .computedLimit(QuotaSize.unlimited()) .build(); } }
@Test public void isOverQuotaWithAdditionalValueShouldReturnTrueWhenOverLimit() { Quota<QuotaCount> quota = Quota.<QuotaCount>builder().used(QuotaCount.count(36)).computedLimit(QuotaCount.count(36)).build(); assertThat(quota.isOverQuotaWithAdditionalValue(1)).isTrue(); }
@Test public void isOverQuotaShouldReturnFalseWhenQuotaIsNotExceeded() { Quota<QuotaCount> quota = Quota.<QuotaCount>builder().used(QuotaCount.count(36)).computedLimit(QuotaCount.count(360)).build(); assertThat(quota.isOverQuota()).isFalse(); }
@Test public void deletingAMailboxShouldPreserveQuotaOfOtherMailboxes() throws Exception { resources.fillMailbox(); mailboxManager.getMailbox(subFolder, session) .appendMessage(MessageManager.AppendCommand.from( Message.Builder.of() .setSubject("test") .setBody("testmail", StandardCharsets.UTF_8) .build()), session); mailboxManager.deleteMailbox(subFolder, session); QuotaRoot quotaRoot = quotaRootResolver.getQuotaRoot(inbox); Quota<QuotaCount> messageQuota = resources.getQuotaManager().getMessageQuota(quotaRoot); Quota<QuotaSize> storageQuota = resources.getQuotaManager().getStorageQuota(quotaRoot); SoftAssertions.assertSoftly(softly -> { softly.assertThat(messageQuota.getUsed()).isEqualTo(QuotaCount.count(16)); softly.assertThat(storageQuota.getUsed()).isEqualTo(QuotaSize.size(16 * 247)); }); } }
Quota<QuotaSize> storageQuota = quotaManager.getStorageQuota(quotaRoot); responder.respond(new QuotaRootResponse(message.getMailboxName(), quotaRoot.getValue())); if (messageQuota.getLimit().isLimited()) { responder.respond(new QuotaResponse(ImapConstants.MESSAGE_QUOTA_RESOURCE, quotaRoot.getValue(), messageQuota)); if (storageQuota.getLimit().isLimited()) { responder.respond(new QuotaResponse(ImapConstants.STORAGE_QUOTA_RESOURCE, quotaRoot.getValue(), storageQuota));
@Override public Quota<QuotaCount> getMessageQuota(QuotaRoot quotaRoot) throws MailboxException { return Quota.<QuotaCount>builder() .used(currentQuotaManager.getCurrentMessageCount(quotaRoot)) .computedLimit(maxQuotaManager.getMaxMessage(quotaRoot).orElse(QuotaCount.unlimited())) .limitsByScope(maxQuotaManager.listMaxMessagesDetails(quotaRoot)) .build(); }
@Test public void isOverQuotaWithAdditionalValueShouldReturnTrueWhenUnderLimit() { Quota<QuotaCount> quota = Quota.<QuotaCount>builder().used(QuotaCount.count(34)).computedLimit(QuotaCount.count(36)).build(); assertThat(quota.isOverQuotaWithAdditionalValue(1)).isFalse(); }