Search in sources :

Example 6 with BufferStream

use of com.zimbra.common.util.BufferStream 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)

Example 7 with BufferStream

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

the class SpamExtract method extractMessages.

private static List<String> extractMessages(HttpClient hc, GetMethod gm, String path, File outdir, boolean raw) throws HttpException, IOException {
    List<String> extractedIds = new ArrayList<String>();
    gm.setPath(path);
    if (LOG.isDebugEnabled()) {
        LOG.debug("Fetching " + path);
    }
    HttpClientUtil.executeMethod(hc, gm);
    if (gm.getStatusCode() != HttpStatus.SC_OK) {
        throw new IOException("HTTP GET failed: " + gm.getPath() + ": " + gm.getStatusCode() + ": " + gm.getStatusText());
    }
    ArchiveInputStream tgzStream = null;
    try {
        tgzStream = new TarArchiveInputStream(new GZIPInputStream(gm.getResponseBodyAsStream()), Charsets.UTF_8.name());
        ArchiveInputEntry entry = null;
        while ((entry = tgzStream.getNextEntry()) != null) {
            LOG.debug("got entry name %s", entry.getName());
            if (entry.getName().endsWith(".meta")) {
                ItemData itemData = new ItemData(readArchiveEntry(tgzStream, entry));
                UnderlyingData ud = itemData.ud;
                //.meta always followed by .eml
                entry = tgzStream.getNextEntry();
                if (raw) {
                    // Write the message as-is.
                    File file = new File(outdir, mOutputPrefix + "-" + mExtractIndex++);
                    OutputStream os = null;
                    try {
                        os = new BufferedOutputStream(new FileOutputStream(file));
                        byte[] data = readArchiveEntry(tgzStream, entry);
                        ByteUtil.copy(new ByteArrayInputStream(data), true, os, false);
                        if (verbose) {
                            LOG.info("Wrote: " + file);
                        }
                        extractedIds.add(ud.id + "");
                    } catch (java.io.IOException e) {
                        String fileName = outdir + "/" + mOutputPrefix + "-" + mExtractIndex;
                        LOG.error("Cannot write to " + fileName, e);
                    } finally {
                        if (os != null) {
                            os.close();
                        }
                    }
                } else {
                    // Write the attached message to the output directory.
                    BufferStream buffer = new BufferStream(entry.getSize(), MAX_BUFFER_SIZE);
                    buffer.setSequenced(false);
                    MimeMessage mm = null;
                    InputStream fis = null;
                    try {
                        byte[] data = readArchiveEntry(tgzStream, entry);
                        ByteUtil.copy(new ByteArrayInputStream(data), true, buffer, false);
                        if (buffer.isSpooled()) {
                            fis = new ZSharedFileInputStream(buffer.getFile());
                            mm = new ZMimeMessage(mJMSession, fis);
                        } else {
                            mm = new ZMimeMessage(mJMSession, buffer.getInputStream());
                        }
                        writeAttachedMessages(mm, outdir, entry.getName());
                        extractedIds.add(ud.id + "");
                    } catch (MessagingException me) {
                        LOG.warn("exception occurred fetching message", me);
                    } finally {
                        ByteUtil.closeStream(fis);
                    }
                }
            }
        }
    } finally {
        Closeables.closeQuietly(tgzStream);
    }
    return extractedIds;
}
Also used : ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MessagingException(javax.mail.MessagingException) ArchiveInputEntry(com.zimbra.cs.service.formatter.ArchiveFormatter.ArchiveInputEntry) GZIPInputStream(java.util.zip.GZIPInputStream) TarArchiveInputStream(com.zimbra.cs.service.formatter.TarArchiveInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ArchiveInputStream(com.zimbra.cs.service.formatter.ArchiveFormatter.ArchiveInputStream) ZSharedFileInputStream(com.zimbra.common.zmime.ZSharedFileInputStream) InputStream(java.io.InputStream) UnderlyingData(com.zimbra.cs.mailbox.MailItem.UnderlyingData) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) IOException(java.io.IOException) TarArchiveInputStream(com.zimbra.cs.service.formatter.TarArchiveInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) BufferStream(com.zimbra.common.util.BufferStream) ZSharedFileInputStream(com.zimbra.common.zmime.ZSharedFileInputStream) TarArchiveInputStream(com.zimbra.cs.service.formatter.TarArchiveInputStream) ArchiveInputStream(com.zimbra.cs.service.formatter.ArchiveFormatter.ArchiveInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) FileOutputStream(java.io.FileOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) ItemData(com.zimbra.cs.service.util.ItemData)

Aggregations

BufferStream (com.zimbra.common.util.BufferStream)7 ServiceException (com.zimbra.common.service.ServiceException)3 IOException (java.io.IOException)3 Rfc822ValidationInputStream (com.zimbra.common.mime.Rfc822ValidationInputStream)2 CopyInputStream (com.zimbra.common.util.CopyInputStream)2 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)2 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)2 ItemData (com.zimbra.cs.service.util.ItemData)2 Blob (com.zimbra.cs.store.Blob)2 MailboxBlob (com.zimbra.cs.store.MailboxBlob)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 MessagingException (javax.mail.MessagingException)2 MimeMessage (javax.mail.internet.MimeMessage)2 LmtpProtocolException (com.zimbra.common.lmtp.LmtpProtocolException)1 DeliveryServiceException (com.zimbra.common.service.DeliveryServiceException)1 Element (com.zimbra.common.soap.Element)1 Browser (com.zimbra.common.util.HttpUtil.Browser)1 RemoteIP (com.zimbra.common.util.RemoteIP)1 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)1