Search in sources :

Example 1 with ReIndexRequest

use of com.zimbra.soap.admin.message.ReIndexRequest in project zm-mailbox by Zimbra.

the class ReIndex method handle.

@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    ReIndexRequest req = JaxbUtil.elementToJaxb(request);
    String action = req.getAction();
    ReindexMailboxInfo reIndexMboxInfo = req.getMbox();
    String accountId = reIndexMboxInfo.getAccountId();
    Provisioning prov = Provisioning.getInstance();
    Account account = prov.get(AccountBy.id, accountId, zsc.getAuthToken());
    defendAgainstAccountOrCalendarResourceHarvesting(account, AccountBy.id, accountId, zsc, Admin.R_reindexMailbox, Admin.R_reindexCalendarResourceMailbox);
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account, false);
    if (mbox == null) {
        throw ServiceException.FAILURE("mailbox not found for account " + accountId, null);
    }
    Element response = zsc.createElement(AdminConstants.REINDEX_RESPONSE);
    if (ACTION_START.equalsIgnoreCase(action)) {
        if (mbox.index.isReIndexInProgress()) {
            response.addAttribute(AdminConstants.A_STATUS, STATUS_RUNNING);
        } else {
            String typesStr = reIndexMboxInfo.getTypes();
            String idsStr = reIndexMboxInfo.getIds();
            if (typesStr != null && idsStr != null) {
                ServiceException.INVALID_REQUEST("Can't specify both 'types' and 'ids'", null);
            }
            if (typesStr != null) {
                Set<MailItem.Type> types;
                try {
                    types = MailItem.Type.setOf(typesStr);
                } catch (IllegalArgumentException e) {
                    throw MailServiceException.INVALID_TYPE(e.getMessage());
                }
                mbox.index.startReIndexByType(types);
            } else if (idsStr != null) {
                Set<Integer> ids = new HashSet<Integer>();
                for (String id : Splitter.on(',').trimResults().split(idsStr)) {
                    try {
                        ids.add(Integer.parseInt(id));
                    } catch (NumberFormatException e) {
                        ServiceException.INVALID_REQUEST("invalid item ID: " + id, e);
                    }
                }
                mbox.index.startReIndexById(ids);
            } else {
                mbox.index.startReIndex();
            }
            response.addAttribute(AdminConstants.A_STATUS, STATUS_STARTED);
        }
    } else if (ACTION_STATUS.equalsIgnoreCase(action)) {
        MailboxIndex.ReIndexStatus status = mbox.index.getReIndexStatus();
        if (status != null) {
            addProgressInfo(response, status);
            response.addAttribute(AdminConstants.A_STATUS, STATUS_RUNNING);
        } else {
            response.addAttribute(AdminConstants.A_STATUS, STATUS_IDLE);
        }
    } else if (ACTION_CANCEL.equalsIgnoreCase(action)) {
        MailboxIndex.ReIndexStatus status = mbox.index.cancelReIndex();
        if (status != null) {
            response.addAttribute(AdminConstants.A_STATUS, STATUS_CANCELLED);
            addProgressInfo(response, status);
        } else {
            response.addAttribute(AdminConstants.A_STATUS, STATUS_IDLE);
        }
    } else {
        throw ServiceException.INVALID_REQUEST("Unknown action: " + action, null);
    }
    return response;
}
Also used : Account(com.zimbra.cs.account.Account) Set(java.util.Set) HashSet(java.util.HashSet) Element(com.zimbra.common.soap.Element) MailboxIndex(com.zimbra.cs.mailbox.MailboxIndex) ReindexMailboxInfo(com.zimbra.soap.admin.type.ReindexMailboxInfo) Provisioning(com.zimbra.cs.account.Provisioning) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) ReIndexRequest(com.zimbra.soap.admin.message.ReIndexRequest)

Aggregations

Element (com.zimbra.common.soap.Element)1 Account (com.zimbra.cs.account.Account)1 Provisioning (com.zimbra.cs.account.Provisioning)1 Mailbox (com.zimbra.cs.mailbox.Mailbox)1 MailboxIndex (com.zimbra.cs.mailbox.MailboxIndex)1 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)1 ReIndexRequest (com.zimbra.soap.admin.message.ReIndexRequest)1 ReindexMailboxInfo (com.zimbra.soap.admin.type.ReindexMailboxInfo)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1