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);
}
}
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);
}
}
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);
}
}
}
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);
}
}
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()));
}
Aggregations