use of com.zimbra.cs.session.AdminSession in project zm-mailbox by Zimbra.
the class GetQuotaUsage method handle.
@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
ZimbraSoapContext zsc = getZimbraSoapContext(context);
Provisioning prov = Provisioning.getInstance();
int limit = (int) request.getAttributeLong(AdminConstants.A_LIMIT, Integer.MAX_VALUE);
if (limit == 0)
limit = Integer.MAX_VALUE;
int offset = (int) request.getAttributeLong(AdminConstants.A_OFFSET, 0);
String domain = request.getAttribute(AdminConstants.A_DOMAIN, null);
String sortBy = request.getAttribute(AdminConstants.A_SORT_BY, SORT_TOTAL_USED);
boolean sortAscending = request.getAttributeBool(AdminConstants.A_SORT_ASCENDING, false);
boolean refresh = request.getAttributeBool(AdminConstants.A_REFRESH, false);
if (!(sortBy.equals(SORT_TOTAL_USED) || sortBy.equals(SORT_PERCENT_USED) || sortBy.equals(SORT_QUOTA_LIMIT) || sortBy.equals(SORT_ACCOUNT)))
throw ServiceException.INVALID_REQUEST("sortBy must be percentUsed or totalUsed", null);
// Note: isDomainAdminOnly *always* returns false for pure ACL based AccessManager
if (isDomainAdminOnly(zsc)) {
// need a domain, if domain is not specified, use the authed admins own domain.
if (domain == null)
domain = getAuthTokenAccountDomain(zsc).getName();
// sanity check
if (domain == null)
throw AccountServiceException.INVALID_REQUEST("no domain", null);
}
Domain d = null;
if (domain != null) {
d = prov.get(Key.DomainBy.name, domain);
if (d == null)
throw AccountServiceException.NO_SUCH_DOMAIN(domain);
}
// if we don't have a domain, only allow system admin
if (d != null)
checkDomainRight(zsc, d, Admin.R_getDomainQuotaUsage);
else
checkRight(zsc, null, AdminRight.PR_SYSTEM_ADMIN_ONLY);
boolean allServers = d != null && request.getAttributeBool(AdminConstants.A_ALL_SERVERS, false);
List<AccountQuota> quotas = null;
QuotaUsageParams params = new QuotaUsageParams(d, sortBy, sortAscending);
AdminSession session = (AdminSession) getSession(zsc, Session.Type.ADMIN);
if (session != null) {
QuotaUsageParams cachedParams = getCachedQuotaUsage(session, allServers);
if (cachedParams != null && cachedParams.equals(params) && !refresh) {
quotas = cachedParams.getResult();
}
}
if (quotas == null) {
if (allServers) {
quotas = delegateRequestToAllServers(request.clone(), zsc.getRawAuthToken(), sortBy, sortAscending, prov);
// explicitly set the result
params.setResult(quotas);
} else {
quotas = params.doSearch();
}
if (session != null) {
setCachedQuotaUsage(session, params, allServers);
}
}
Element response = zsc.createElement(AdminConstants.GET_QUOTA_USAGE_RESPONSE);
int i, limitMax = offset + limit;
for (i = offset; i < limitMax && i < quotas.size(); i++) {
AccountQuota quota = quotas.get(i);
Element account = response.addElement(AdminConstants.E_ACCOUNT);
account.addAttribute(AdminConstants.A_NAME, quota.name);
account.addAttribute(AdminConstants.A_ID, quota.id);
account.addAttribute(AdminConstants.A_QUOTA_USED, quota.quotaUsed);
account.addAttribute(AdminConstants.A_QUOTA_LIMIT, quota.quotaLimit);
}
response.addAttribute(AdminConstants.A_MORE, i < quotas.size());
response.addAttribute(AdminConstants.A_SEARCH_TOTAL, quotas.size());
return response;
}
Aggregations