Search in sources :

Example 21 with StoreManager

use of com.zimbra.cs.store.StoreManager in project zm-mailbox by Zimbra.

the class ZimbraMailAdapter method updateIncomingBlob.

public void updateIncomingBlob() {
    DeliveryContext ctxt = handler.getDeliveryContext();
    if (ctxt != null) {
        StoreManager sm = StoreManager.getInstance();
        InputStream in = null;
        Blob blob = ctxt.getIncomingBlob();
        try {
            ParsedMessage pm = getParsedMessage();
            pm.updateMimeMessage();
            in = pm.getRawInputStream();
            blob = sm.storeIncoming(in);
        } catch (IOException | ServiceException | MessagingException e) {
            ZimbraLog.filter.error("Unable to update MimeMessage and incomimg blob.", e);
        } finally {
            ByteUtil.closeStream(in);
        }
        ctxt.setIncomingBlob(blob);
    }
}
Also used : Blob(com.zimbra.cs.store.Blob) ServiceException(com.zimbra.common.service.ServiceException) MessagingException(javax.mail.MessagingException) InputStream(java.io.InputStream) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) IOException(java.io.IOException) DeliveryContext(com.zimbra.cs.mailbox.DeliveryContext) StoreManager(com.zimbra.cs.store.StoreManager)

Example 22 with StoreManager

use of com.zimbra.cs.store.StoreManager in project zm-mailbox by Zimbra.

the class CalendarItem method storeUpdatedBlob.

private MailboxBlob storeUpdatedBlob(MimeMessage mm) throws ServiceException, IOException {
    ParsedMessage pm = new ParsedMessage(mm, mMailbox.attachmentsIndexingEnabled());
    StoreManager sm = StoreManager.getInstance();
    InputStream is = null;
    try {
        is = pm.getRawInputStream();
        if (is != null) {
            StagedBlob sblob = sm.stage(is, mMailbox);
            return setContent(sblob, pm);
        } else {
            ZimbraLog.calendar.warn("Invalid state: updating blob with null data for calendar item " + getId() + " in mailbox " + getMailboxId());
            return setContent(null, pm);
        }
    } finally {
        ByteUtil.closeStream(is);
    }
}
Also used : StagedBlob(com.zimbra.cs.store.StagedBlob) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) InputStream(java.io.InputStream) StoreManager(com.zimbra.cs.store.StoreManager)

Example 23 with StoreManager

use of com.zimbra.cs.store.StoreManager in project zm-mailbox by Zimbra.

the class DedupeBlobs method handle.

@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    checkRight(zsc, context, null, AdminRight.PR_SYSTEM_ADMIN_ONLY);
    StoreManager sm = StoreManager.getInstance();
    if (!(sm instanceof FileBlobStore)) {
        throw ServiceException.INVALID_REQUEST(sm.getClass().getName() + " is not supported", null);
    }
    DedupeBlobsRequest req = JaxbUtil.elementToJaxb(request);
    BlobDeduper deduper = BlobDeduper.getInstance();
    DedupeBlobsResponse resp = new DedupeBlobsResponse();
    // Assemble the list of volumes.
    List<IntIdAttr> volumeList = req.getVolumes();
    List<Short> volumeIds = new ArrayList<Short>();
    if ((req.getAction() == DedupeBlobsRequest.DedupAction.start) || (req.getAction() == DedupeBlobsRequest.DedupAction.reset)) {
        if (volumeList.isEmpty()) {
            // Get all message volume id's.
            for (Volume vol : VolumeManager.getInstance().getAllVolumes()) {
                switch(vol.getType()) {
                    case Volume.TYPE_MESSAGE:
                    case Volume.TYPE_MESSAGE_SECONDARY:
                        volumeIds.add(vol.getId());
                        break;
                }
            }
        } else {
            // Read volume id's from the request.
            for (IntIdAttr attr : volumeList) {
                short volumeId = (short) attr.getId();
                Volume vol = VolumeManager.getInstance().getVolume(volumeId);
                if (vol.getType() == Volume.TYPE_INDEX) {
                    throw ServiceException.INVALID_REQUEST("Index volume " + volumeId + " is not supported", null);
                } else {
                    volumeIds.add(volumeId);
                }
            }
        }
    }
    if (req.getAction() == DedupeBlobsRequest.DedupAction.start) {
        try {
            deduper.process(volumeIds);
        } catch (IOException e) {
            throw ServiceException.FAILURE("error while deduping", e);
        }
    } else if (req.getAction() == DedupeBlobsRequest.DedupAction.stop) {
        deduper.stopProcessing();
    } else if (req.getAction() == DedupeBlobsRequest.DedupAction.reset) {
        if (volumeList.isEmpty()) {
            deduper.resetVolumeBlobs(new ArrayList<Short>());
        } else {
            deduper.resetVolumeBlobs(volumeIds);
        }
    }
    // return the stats for all actions.
    boolean isRunning = deduper.isRunning();
    if (isRunning) {
        resp.setStatus(DedupStatus.running);
    } else {
        resp.setStatus(DedupStatus.stopped);
    }
    Map<Short, String> volumeBlobsProgress = deduper.getVolumeBlobsProgress();
    VolumeIdAndProgress[] blobProgress = new VolumeIdAndProgress[volumeBlobsProgress.size()];
    int i = 0;
    for (Map.Entry<Short, String> entry : volumeBlobsProgress.entrySet()) {
        blobProgress[i++] = new VolumeIdAndProgress(String.valueOf(entry.getKey()), entry.getValue());
    }
    resp.setVolumeBlobsProgress(blobProgress);
    Map<Short, String> blobDigestsProgress = deduper.getBlobDigestsProgress();
    VolumeIdAndProgress[] digestProgress = new VolumeIdAndProgress[blobDigestsProgress.size()];
    i = 0;
    for (Map.Entry<Short, String> entry : blobDigestsProgress.entrySet()) {
        digestProgress[i++] = new VolumeIdAndProgress(String.valueOf(entry.getKey()), entry.getValue());
    }
    resp.setBlobDigestsProgress(digestProgress);
    Pair<Integer, Long> pair = deduper.getCountAndSize();
    resp.setTotalCount(pair.getFirst());
    resp.setTotalSize(pair.getSecond());
    return JaxbUtil.jaxbToElement(resp);
}
Also used : DedupeBlobsRequest(com.zimbra.soap.admin.message.DedupeBlobsRequest) BlobDeduper(com.zimbra.cs.store.file.BlobDeduper) ArrayList(java.util.ArrayList) StoreManager(com.zimbra.cs.store.StoreManager) IntIdAttr(com.zimbra.soap.admin.type.IntIdAttr) DedupeBlobsResponse(com.zimbra.soap.admin.message.DedupeBlobsResponse) FileBlobStore(com.zimbra.cs.store.file.FileBlobStore) VolumeIdAndProgress(com.zimbra.soap.admin.type.VolumeIdAndProgress) IOException(java.io.IOException) Volume(com.zimbra.cs.volume.Volume) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) Map(java.util.Map)

Example 24 with StoreManager

use of com.zimbra.cs.store.StoreManager in project zm-mailbox by Zimbra.

the class CheckBlobConsistency method handle.

@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    checkRight(zsc, context, null, AdminRight.PR_SYSTEM_ADMIN_ONLY);
    // Assemble the list of mailboxes.
    List<Integer> mailboxIds = new ArrayList<Integer>();
    List<Element> mboxElementList = request.listElements(AdminConstants.E_MAILBOX);
    if (mboxElementList.isEmpty()) {
        // Get all mailbox id's.
        for (int mboxId : MailboxManager.getInstance().getMailboxIds()) {
            mailboxIds.add(mboxId);
        }
    } else {
        // Read mailbox id's from the request.
        for (Element mboxEl : mboxElementList) {
            Mailbox mbox = MailboxManager.getInstance().getMailboxById((int) mboxEl.getAttributeLong(AdminConstants.A_ID));
            mailboxIds.add(mbox.getId());
        }
    }
    boolean checkSize = request.getAttributeBool(AdminConstants.A_CHECK_SIZE, true);
    boolean reportUsedBlobs = request.getAttributeBool(AdminConstants.A_REPORT_USED_BLOBS, false);
    // Check blobs and assemble response.
    Element response = zsc.createElement(AdminConstants.CHECK_BLOB_CONSISTENCY_RESPONSE);
    StoreManager sm = StoreManager.getInstance();
    if (sm instanceof ExternalStoreManager) {
        for (int mboxId : mailboxIds) {
            ExternalBlobConsistencyChecker checker = new ExternalBlobConsistencyChecker();
            BlobConsistencyChecker.Results results = checker.check(null, mboxId, checkSize, reportUsedBlobs);
            if (results.hasInconsistency() || reportUsedBlobs) {
                //or checking used blobs
                Element mboxEl = response.addElement(AdminConstants.E_MAILBOX).addAttribute(AdminConstants.A_ID, mboxId);
                results.toElement(mboxEl);
            }
        }
    } else if (sm instanceof FileBlobStore) {
        // Assemble the list of volumes.
        List<Short> volumeIds = new ArrayList<Short>();
        List<Element> volumeElementList = request.listElements(AdminConstants.E_VOLUME);
        if (volumeElementList.isEmpty()) {
            // Get all message volume id's.
            for (Volume vol : VolumeManager.getInstance().getAllVolumes()) {
                switch(vol.getType()) {
                    case Volume.TYPE_MESSAGE:
                    case Volume.TYPE_MESSAGE_SECONDARY:
                        volumeIds.add(vol.getId());
                        break;
                }
            }
        } else {
            // Read volume id's from the request.
            for (Element volumeEl : volumeElementList) {
                short volumeId = (short) volumeEl.getAttributeLong(AdminConstants.A_ID);
                Volume vol = VolumeManager.getInstance().getVolume(volumeId);
                if (vol.getType() == Volume.TYPE_INDEX) {
                    throw ServiceException.INVALID_REQUEST("Index volume " + volumeId + " is not supported", null);
                } else {
                    volumeIds.add(volumeId);
                }
            }
        }
        for (int mboxId : mailboxIds) {
            BlobConsistencyChecker checker = new BlobConsistencyChecker();
            BlobConsistencyChecker.Results results = checker.check(volumeIds, mboxId, checkSize, reportUsedBlobs);
            if (results.hasInconsistency() || reportUsedBlobs) {
                Element mboxEl = response.addElement(AdminConstants.E_MAILBOX).addAttribute(AdminConstants.A_ID, mboxId);
                results.toElement(mboxEl);
            }
        }
    } else {
        //neither ExternalStoreManager nor FileBlobStore..some third type we haven't coded for
        throw ServiceException.INVALID_REQUEST(sm.getClass().getName() + " is not supported", null);
    }
    return response;
}
Also used : Element(com.zimbra.common.soap.Element) ArrayList(java.util.ArrayList) ExternalBlobConsistencyChecker(com.zimbra.cs.store.external.ExternalBlobConsistencyChecker) BlobConsistencyChecker(com.zimbra.cs.store.file.BlobConsistencyChecker) ExternalBlobConsistencyChecker(com.zimbra.cs.store.external.ExternalBlobConsistencyChecker) ExternalStoreManager(com.zimbra.cs.store.external.ExternalStoreManager) StoreManager(com.zimbra.cs.store.StoreManager) Mailbox(com.zimbra.cs.mailbox.Mailbox) Volume(com.zimbra.cs.volume.Volume) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) FileBlobStore(com.zimbra.cs.store.file.FileBlobStore) ArrayList(java.util.ArrayList) List(java.util.List) ExternalStoreManager(com.zimbra.cs.store.external.ExternalStoreManager)

Example 25 with StoreManager

use of com.zimbra.cs.store.StoreManager in project zm-mailbox by Zimbra.

the class MailItem method setContent.

MailboxBlob setContent(StagedBlob staged, Object content) throws ServiceException, IOException {
    addRevision(false);
    // update the item's relevant attributes
    markItemModified(Change.CONTENT | Change.DATE | Change.IMAP_UID | Change.SIZE);
    // delete the old blob *unless* we've already rewritten it in this transaction
    if (getSavedSequence() != mMailbox.getOperationChangeID()) {
        if (!canAccess(ACL.RIGHT_WRITE)) {
            throw ServiceException.PERM_DENIED("you do not have the necessary permissions on the item");
        }
        boolean delete = true;
        // don't delete blob if last revision uses it
        if (isTagged(Flag.FlagInfo.VERSIONED)) {
            List<MailItem> revisions = loadRevisions();
            if (!revisions.isEmpty()) {
                MailItem lastRev = revisions.get(revisions.size() - 1);
                if (lastRev.getSavedSequence() == getSavedSequence()) {
                    delete = false;
                }
            }
        }
        if (delete) {
            markBlobForDeletion();
        }
    }
    // remove the content from the cache
    MessageCache.purge(this);
    // update the object to reflect its new contents
    long size = staged == null ? 0 : staged.getSize();
    if (mData.size != size) {
        mMailbox.updateSize(size - mData.size, isQuotaCheckRequired());
        mData.size = size;
    }
    getFolder().updateSize(0, 0, size - mData.size);
    mData.setBlobDigest(staged == null ? null : staged.getDigest());
    mData.date = mMailbox.getOperationTimestamp();
    mData.imapId = mMailbox.isTrackingImap() ? 0 : mData.id;
    contentChanged();
    // write the content (if any) to the store
    MailboxBlob mblob = null;
    if (staged != null) {
        StoreManager sm = StoreManager.getInstance();
        // under windows, a rename will fail if the incoming file is open
        if (SystemUtil.ON_WINDOWS)
            mblob = sm.link(staged, mMailbox, mId, getSavedSequence());
        else
            mblob = sm.renameTo(staged, mMailbox, mId, getSavedSequence());
        mMailbox.markOtherItemDirty(mblob);
    }
    mBlob = null;
    mData.locator = mblob == null ? null : mblob.getLocator();
    // rewrite the DB row to reflect our new view (MUST call saveData)
    reanalyze(content, size);
    return mblob;
}
Also used : DbMailItem(com.zimbra.cs.db.DbMailItem) MailboxBlob(com.zimbra.cs.store.MailboxBlob) StoreManager(com.zimbra.cs.store.StoreManager)

Aggregations

StoreManager (com.zimbra.cs.store.StoreManager)29 MailboxBlob (com.zimbra.cs.store.MailboxBlob)17 StagedBlob (com.zimbra.cs.store.StagedBlob)16 IOException (java.io.IOException)14 InputStream (java.io.InputStream)11 Blob (com.zimbra.cs.store.Blob)10 Mailbox (com.zimbra.cs.mailbox.Mailbox)9 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)9 Rfc822ValidationInputStream (com.zimbra.common.mime.Rfc822ValidationInputStream)6 ServiceException (com.zimbra.common.service.ServiceException)6 CopyInputStream (com.zimbra.common.util.CopyInputStream)6 TargetConstraint (com.zimbra.cs.mailbox.MailItem.TargetConstraint)5 CreateMountpoint (com.zimbra.cs.redolog.op.CreateMountpoint)5 Test (org.junit.Test)5 DbConnection (com.zimbra.cs.db.DbPool.DbConnection)4 RefreshMountpoint (com.zimbra.cs.redolog.op.RefreshMountpoint)4 ArrayList (java.util.ArrayList)4 AccountServiceException (com.zimbra.cs.account.AccountServiceException)3 DbMailItem (com.zimbra.cs.db.DbMailItem)3 ParsedMessageDataSource (com.zimbra.cs.mime.ParsedMessageDataSource)3