Search in sources :

Example 11 with StagedBlob

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

the class Mailbox method createChat.

public Chat createChat(OperationContext octxt, ParsedMessage pm, int folderId, int flags, String[] tags) throws IOException, ServiceException {
    if (pm == null) {
        throw ServiceException.INVALID_REQUEST("null ParsedMessage when adding chat to mailbox " + mId, null);
    }
    // write the chat content directly to the mailbox's blob staging area
    StoreManager sm = StoreManager.getInstance();
    StagedBlob staged;
    InputStream is = pm.getRawInputStream();
    try {
        staged = sm.stage(is, this);
    } finally {
        ByteUtil.closeStream(is);
    }
    String digest = staged.getDigest();
    int size = (int) staged.getSize();
    CreateChat redoRecorder = new CreateChat(mId, digest, size, folderId, flags, tags);
    boolean success = false;
    try {
        beginTransaction("createChat", octxt, redoRecorder);
        Tag.NormalizedTags ntags = new Tag.NormalizedTags(this, tags);
        CreateChat redoPlayer = (octxt == null ? null : (CreateChat) octxt.getPlayer());
        redoRecorder.setMessageBodyInfo(new ParsedMessageDataSource(pm), size);
        int itemId = getNextItemId(redoPlayer == null ? ID_AUTO_INCREMENT : redoPlayer.getMessageId());
        Chat chat = Chat.create(itemId, getFolderById(folderId), pm, staged, false, flags, ntags);
        redoRecorder.setMessageId(chat.getId());
        MailboxBlob mblob = sm.link(staged, this, itemId, getOperationChangeID());
        markOtherItemDirty(mblob);
        // when we created the Chat, we used the staged locator/size/digest;
        //   make sure that data actually matches the final blob in the store
        chat.updateBlobData(mblob);
        index.add(chat);
        success = true;
        return chat;
    } finally {
        endTransaction(success);
        sm.quietDelete(staged);
    }
}
Also used : StagedBlob(com.zimbra.cs.store.StagedBlob) NormalizedTags(com.zimbra.cs.mailbox.Tag.NormalizedTags) MailboxBlob(com.zimbra.cs.store.MailboxBlob) Rfc822ValidationInputStream(com.zimbra.common.mime.Rfc822ValidationInputStream) CopyInputStream(com.zimbra.common.util.CopyInputStream) InputStream(java.io.InputStream) RefreshMountpoint(com.zimbra.cs.redolog.op.RefreshMountpoint) TargetConstraint(com.zimbra.cs.mailbox.MailItem.TargetConstraint) CreateMountpoint(com.zimbra.cs.redolog.op.CreateMountpoint) StoreManager(com.zimbra.cs.store.StoreManager) ParsedMessageDataSource(com.zimbra.cs.mime.ParsedMessageDataSource) CreateChat(com.zimbra.cs.redolog.op.CreateChat) NormalizedTags(com.zimbra.cs.mailbox.Tag.NormalizedTags) SaveChat(com.zimbra.cs.redolog.op.SaveChat) CreateChat(com.zimbra.cs.redolog.op.CreateChat) AlterItemTag(com.zimbra.cs.redolog.op.AlterItemTag) CreateTag(com.zimbra.cs.redolog.op.CreateTag) DbTag(com.zimbra.cs.db.DbTag)

Example 12 with StagedBlob

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

the class ExternalStoreManager method stage.

@Override
public StagedBlob stage(InputStream in, long actualSize, Mailbox mbox) throws ServiceException, IOException {
    if (actualSize < 0) {
        Blob blob = storeIncoming(in);
        try {
            return stage(blob, mbox);
        } finally {
            quietDelete(blob);
        }
    }
    MessageDigest digest;
    try {
        digest = MessageDigest.getInstance("SHA-256");
    } catch (NoSuchAlgorithmException e) {
        throw ServiceException.FAILURE("SHA-256 digest not found", e);
    }
    ByteUtil.PositionInputStream pin = new ByteUtil.PositionInputStream(new DigestInputStream(in, digest));
    try {
        String locator = writeStreamToStore(pin, actualSize, mbox);
        if (locator != null) {
            ZimbraLog.store.debug("wrote to locator %s", locator);
        } else {
            ZimbraLog.store.warn("blob staging returned null locator");
        }
        return new ExternalStagedBlob(mbox, ByteUtil.encodeFSSafeBase64(digest.digest()), pin.getPosition(), locator);
    } catch (IOException e) {
        throw ServiceException.FAILURE("unable to stage blob", e);
    }
}
Also used : Blob(com.zimbra.cs.store.Blob) MailboxBlob(com.zimbra.cs.store.MailboxBlob) StagedBlob(com.zimbra.cs.store.StagedBlob) ByteUtil(com.zimbra.common.util.ByteUtil) DigestInputStream(java.security.DigestInputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) MessageDigest(java.security.MessageDigest)

Example 13 with StagedBlob

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

the class ContentAddressableStoreManager method stage.

@Override
public StagedBlob stage(Blob blob, Mailbox mbox) throws IOException, ServiceException {
    if (supports(StoreFeature.RESUMABLE_UPLOAD) && blob instanceof ExternalUploadedBlob && blob.getRawSize() > 0) {
        ZimbraLog.store.debug("blob already uploaded, just need to commit");
        String locator = ((ExternalResumableUpload) this).finishUpload((ExternalUploadedBlob) blob);
        ZimbraLog.store.debug("staged to locator %s", locator);
        localCache.put(locator, getContent(blob));
        return new ExternalStagedBlob(mbox, blob.getDigest(), blob.getRawSize(), locator);
    } else {
        InputStream is = getContent(blob);
        String locator = getLocator(blob);
        try {
            StagedBlob staged = stage(is, blob.getRawSize(), mbox, locator);
            if (staged != null) {
                ZimbraLog.store.debug("staged to locator %s", staged.getLocator());
                localCache.put(staged.getLocator(), getContent(blob));
            }
            return staged;
        } finally {
            ByteUtil.closeStream(is);
        }
    }
}
Also used : StagedBlob(com.zimbra.cs.store.StagedBlob) DigestInputStream(java.security.DigestInputStream) InputStream(java.io.InputStream)

Example 14 with StagedBlob

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

the class Mailbox method addMessage.

private Message addMessage(OperationContext octxt, ParsedMessage pm, int folderId, boolean noICal, int flags, String[] tags, int conversationId, String rcptEmail, Message.DraftInfo dinfo, CustomMetadata customData, DeliveryContext dctxt) throws IOException, ServiceException {
    // and then actually add the message
    long start = ZimbraPerf.STOPWATCH_MBOX_ADD_MSG.start();
    //
    if (!noICal) {
        try {
            CalendarPartInfo cpi = pm.getCalendarPartInfo();
            if (cpi != null && CalendarItem.isAcceptableInvite(getAccount(), cpi)) {
                if (ICalTok.REPLY.equals(cpi.method)) {
                    processICalReplies(octxt, cpi.cal, null);
                } else if (ICalTok.COUNTER.equals(cpi.method)) {
                    processICalReplies(octxt, cpi.cal, pm.getSender());
                }
            }
        } catch (Exception e) {
            ZimbraLog.calendar.warn("Error during calendar processing.  Continuing with message add", e);
        }
    }
    // Store the incoming blob if necessary.
    if (dctxt == null) {
        dctxt = new DeliveryContext();
    }
    StoreManager sm = StoreManager.getInstance();
    Blob blob = dctxt.getIncomingBlob();
    boolean deleteIncoming = false;
    if (blob == null) {
        InputStream in = null;
        try {
            in = pm.getRawInputStream();
            blob = sm.storeIncoming(in);
        } finally {
            ByteUtil.closeStream(in);
        }
        dctxt.setIncomingBlob(blob);
        deleteIncoming = true;
    }
    StagedBlob staged = sm.stage(blob, this);
    lock.lock();
    try {
        try {
            return addMessageInternal(octxt, pm, folderId, noICal, flags, tags, conversationId, rcptEmail, dinfo, customData, dctxt, staged);
        } finally {
            if (deleteIncoming) {
                sm.quietDelete(dctxt.getIncomingBlob());
            }
            sm.quietDelete(staged);
        }
    } finally {
        lock.release();
        ZimbraPerf.STOPWATCH_MBOX_ADD_MSG.stop(start);
    }
}
Also used : StoreIncomingBlob(com.zimbra.cs.redolog.op.StoreIncomingBlob) StagedBlob(com.zimbra.cs.store.StagedBlob) MailboxBlob(com.zimbra.cs.store.MailboxBlob) Blob(com.zimbra.cs.store.Blob) StagedBlob(com.zimbra.cs.store.StagedBlob) Rfc822ValidationInputStream(com.zimbra.common.mime.Rfc822ValidationInputStream) CopyInputStream(com.zimbra.common.util.CopyInputStream) InputStream(java.io.InputStream) CalendarPartInfo(com.zimbra.cs.mime.ParsedMessage.CalendarPartInfo) AccountServiceException(com.zimbra.cs.account.AccountServiceException) IOException(java.io.IOException) NoSuchItemException(com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException) MessageChannelException(com.zimbra.cs.iochannel.MessageChannelException) ServiceException(com.zimbra.common.service.ServiceException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) StoreManager(com.zimbra.cs.store.StoreManager)

Example 15 with StagedBlob

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

the class TestStoreManager method testStore.

@Test
public void testStore() throws Exception {
    ParsedMessage pm = getMessage();
    byte[] mimeBytes = TestUtil.readInputStream(pm.getRawInputStream());
    Mailbox mbox = TestUtil.getMailbox(USER_NAME);
    StoreManager sm = StoreManager.getInstance();
    Blob blob = sm.storeIncoming(pm.getRawInputStream());
    Assert.assertEquals("blob size = message size", pm.getRawData().length, blob.getRawSize());
    Assert.assertTrue("blob content = mime content", TestUtil.bytesEqual(mimeBytes, blob.getInputStream()));
    StagedBlob staged = sm.stage(blob, mbox);
    Assert.assertEquals("staged size = blob size", blob.getRawSize(), staged.getSize());
    MailboxBlob mblob = sm.link(staged, mbox, 0, 0);
    Assert.assertEquals("link size = staged size", staged.getSize(), mblob.getSize());
    Assert.assertTrue("link content = mime content", TestUtil.bytesEqual(mimeBytes, mblob.getLocalBlob().getInputStream()));
    mblob = sm.getMailboxBlob(mbox, 0, 0, staged.getLocator());
    Assert.assertEquals("mblob size = staged size", staged.getSize(), mblob.getSize());
    Assert.assertTrue("mailboxblob content = mime content", TestUtil.bytesEqual(mimeBytes, mblob.getLocalBlob().getInputStream()));
}
Also used : Blob(com.zimbra.cs.store.Blob) MailboxBlob(com.zimbra.cs.store.MailboxBlob) StagedBlob(com.zimbra.cs.store.StagedBlob) StagedBlob(com.zimbra.cs.store.StagedBlob) Mailbox(com.zimbra.cs.mailbox.Mailbox) MailboxBlob(com.zimbra.cs.store.MailboxBlob) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) StoreManager(com.zimbra.cs.store.StoreManager) Test(org.junit.Test)

Aggregations

StagedBlob (com.zimbra.cs.store.StagedBlob)21 MailboxBlob (com.zimbra.cs.store.MailboxBlob)15 StoreManager (com.zimbra.cs.store.StoreManager)15 Blob (com.zimbra.cs.store.Blob)11 InputStream (java.io.InputStream)11 Mailbox (com.zimbra.cs.mailbox.Mailbox)8 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)8 IOException (java.io.IOException)8 Rfc822ValidationInputStream (com.zimbra.common.mime.Rfc822ValidationInputStream)6 CopyInputStream (com.zimbra.common.util.CopyInputStream)6 TargetConstraint (com.zimbra.cs.mailbox.MailItem.TargetConstraint)6 CreateMountpoint (com.zimbra.cs.redolog.op.CreateMountpoint)6 RefreshMountpoint (com.zimbra.cs.redolog.op.RefreshMountpoint)6 Test (org.junit.Test)6 ServiceException (com.zimbra.common.service.ServiceException)3 AccountServiceException (com.zimbra.cs.account.AccountServiceException)3 DbTag (com.zimbra.cs.db.DbTag)3 NormalizedTags (com.zimbra.cs.mailbox.Tag.NormalizedTags)3 ParsedMessageDataSource (com.zimbra.cs.mime.ParsedMessageDataSource)3 AlterItemTag (com.zimbra.cs.redolog.op.AlterItemTag)3