Search in sources :

Example 1 with CopyInputStream

use of com.zimbra.common.util.CopyInputStream in project zm-mailbox by Zimbra.

the class Mailbox method addMessage.

public Message addMessage(OperationContext octxt, InputStream in, long sizeHint, Long receivedDate, DeliveryOptions dopt, DeliveryContext dctxt, ItemData id) throws IOException, ServiceException {
    int bufLen = Provisioning.getInstance().getLocalServer().getMailDiskStreamingThreshold();
    CopyInputStream cs = new CopyInputStream(in, sizeHint, bufLen, bufLen);
    in = cs;
    Blob blob = null;
    try {
        BufferStream bs = cs.getBufferStream();
        ParsedMessage pm = null;
        Rfc822ValidationInputStream validator = null;
        if (LC.zimbra_lmtp_validate_messages.booleanValue()) {
            validator = new Rfc822ValidationInputStream(cs, LC.zimbra_lmtp_max_line_length.longValue());
            in = validator;
        }
        blob = StoreManager.getInstance().storeIncoming(in);
        if (id != null && id.ud != null && id.ud.getBlobDigest() != null && !id.ud.getBlobDigest().isEmpty()) {
            blob.setDigest(id.ud.getBlobDigest());
        }
        if (validator != null && !validator.isValid()) {
            StoreManager.getInstance().delete(blob);
            throw ServiceException.INVALID_REQUEST("Message content is invalid.", null);
        }
        pm = new ParsedMessage(new ParsedMessageOptions(blob, bs.isPartial() ? null : bs.getBuffer(), receivedDate, attachmentsIndexingEnabled()));
        cs.release();
        if (dctxt == null) {
            dctxt = new DeliveryContext();
        }
        dctxt.setIncomingBlob(blob);
        return addMessage(octxt, pm, dopt, dctxt);
    } finally {
        cs.release();
        StoreManager.getInstance().quietDelete(blob);
    }
}
Also used : BufferStream(com.zimbra.common.util.BufferStream) 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) ParsedMessageOptions(com.zimbra.cs.mime.ParsedMessageOptions) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) CopyInputStream(com.zimbra.common.util.CopyInputStream) Rfc822ValidationInputStream(com.zimbra.common.mime.Rfc822ValidationInputStream) RefreshMountpoint(com.zimbra.cs.redolog.op.RefreshMountpoint) TargetConstraint(com.zimbra.cs.mailbox.MailItem.TargetConstraint) CreateMountpoint(com.zimbra.cs.redolog.op.CreateMountpoint)

Example 2 with CopyInputStream

use of com.zimbra.common.util.CopyInputStream in project zm-mailbox by Zimbra.

the class ZimbraLmtpBackend method deliver.

@Override
public void deliver(LmtpEnvelope env, InputStream in, int sizeHint) throws UnrecoverableLmtpException {
    CopyInputStream cis = null;
    Blob blob = null;
    try {
        int bufLen = Provisioning.getInstance().getLocalServer().getMailDiskStreamingThreshold();
        cis = new CopyInputStream(in, sizeHint, bufLen, bufLen);
        in = cis;
        // MimeParserInputStream mpis = null;
        // if (ZMimeMessage.usingZimbraParser()) {
        // mpis = new MimeParserInputStream(in);
        // in = mpis;
        // }
        Rfc822ValidationInputStream validator = null;
        if (LC.zimbra_lmtp_validate_messages.booleanValue()) {
            validator = new Rfc822ValidationInputStream(in, LC.zimbra_lmtp_max_line_length.longValue());
            in = validator;
        }
        try {
            blob = StoreManager.getInstance().storeIncoming(in);
        } catch (IOException ioe) {
            throw new UnrecoverableLmtpException("Error in storing incoming message", ioe);
        }
        if (validator != null && !validator.isValid()) {
            try {
                StoreManager.getInstance().delete(blob);
            } catch (IOException e) {
                ZimbraLog.lmtp.warn("Error in deleting blob %s", blob, e);
            }
            setDeliveryStatuses(env.getRecipients(), LmtpReply.INVALID_BODY_PARAMETER);
            return;
        }
        BufferStream bs = cis.getBufferStream();
        byte[] data = bs.isPartial() ? null : bs.getBuffer();
        BlobInputStream bis = null;
        MimeMessage mm = null;
        try {
            deliverMessageToLocalMailboxes(blob, bis, data, mm, env);
        } catch (Exception e) {
            ZimbraLog.lmtp.warn("Exception delivering mail (temporary failure)", e);
            setDeliveryStatuses(env.getLocalRecipients(), LmtpReply.TEMPORARY_FAILURE);
        }
        try {
            deliverMessageToRemoteMailboxes(blob, data, env);
        } catch (Exception e) {
            ZimbraLog.lmtp.warn("Exception delivering remote mail", e);
            setDeliveryStatuses(env.getRemoteRecipients(), LmtpReply.TEMPORARY_FAILURE);
        }
    } catch (ServiceException e) {
        ZimbraLog.lmtp.warn("Exception delivering mail (temporary failure)", e);
        setDeliveryStatuses(env.getRecipients(), LmtpReply.TEMPORARY_FAILURE);
    } finally {
        if (cis != null) {
            cis.release();
        }
        if (blob != null) {
            try {
                // clean up the incoming blob
                StoreManager.getInstance().delete(blob);
            } catch (IOException e) {
                ZimbraLog.lmtp.warn("Error in deleting blob %s", blob, e);
            }
        }
    }
}
Also used : BufferStream(com.zimbra.common.util.BufferStream) Blob(com.zimbra.cs.store.Blob) MailboxBlob(com.zimbra.cs.store.MailboxBlob) ServiceException(com.zimbra.common.service.ServiceException) DeliveryServiceException(com.zimbra.common.service.DeliveryServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) MimeMessage(javax.mail.internet.MimeMessage) CopyInputStream(com.zimbra.common.util.CopyInputStream) Rfc822ValidationInputStream(com.zimbra.common.mime.Rfc822ValidationInputStream) IOException(java.io.IOException) BlobInputStream(com.zimbra.cs.store.BlobInputStream) MessagingException(javax.mail.MessagingException) LmtpProtocolException(com.zimbra.common.lmtp.LmtpProtocolException) ServiceException(com.zimbra.common.service.ServiceException) IOException(java.io.IOException) DeliveryServiceException(com.zimbra.common.service.DeliveryServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException)

Aggregations

Rfc822ValidationInputStream (com.zimbra.common.mime.Rfc822ValidationInputStream)2 BufferStream (com.zimbra.common.util.BufferStream)2 CopyInputStream (com.zimbra.common.util.CopyInputStream)2 Blob (com.zimbra.cs.store.Blob)2 MailboxBlob (com.zimbra.cs.store.MailboxBlob)2 LmtpProtocolException (com.zimbra.common.lmtp.LmtpProtocolException)1 DeliveryServiceException (com.zimbra.common.service.DeliveryServiceException)1 ServiceException (com.zimbra.common.service.ServiceException)1 TargetConstraint (com.zimbra.cs.mailbox.MailItem.TargetConstraint)1 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)1 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)1 ParsedMessageOptions (com.zimbra.cs.mime.ParsedMessageOptions)1 CreateMountpoint (com.zimbra.cs.redolog.op.CreateMountpoint)1 RefreshMountpoint (com.zimbra.cs.redolog.op.RefreshMountpoint)1 StoreIncomingBlob (com.zimbra.cs.redolog.op.StoreIncomingBlob)1 BlobInputStream (com.zimbra.cs.store.BlobInputStream)1 StagedBlob (com.zimbra.cs.store.StagedBlob)1 IOException (java.io.IOException)1 MessagingException (javax.mail.MessagingException)1 MimeMessage (javax.mail.internet.MimeMessage)1