Search in sources :

Example 26 with UnderlyingData

use of com.zimbra.cs.mailbox.MailItem.UnderlyingData in project zm-mailbox by Zimbra.

the class DbTag method getUnreadMessages.

public static List<UnderlyingData> getUnreadMessages(Tag tag) throws ServiceException {
    Mailbox mbox = tag.getMailbox();
    ArrayList<UnderlyingData> result = new ArrayList<UnderlyingData>();
    DbConnection conn = mbox.getOperationConnection();
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        String mailboxesMatchAnd = DebugConfig.disableMailboxGroups ? "" : "mi.mailbox_id = ti.mailbox_id AND ";
        stmt = conn.prepareStatement("SELECT " + DbMailItem.DB_FIELDS + " FROM " + DbMailItem.getMailItemTableName(mbox, "mi") + " INNER JOIN " + getTaggedItemTableName(mbox, "ti") + " ON " + mailboxesMatchAnd + "ti.item_id = mi.id" + " WHERE " + inThisMailboxAnd("ti") + "type NOT IN " + DbMailItem.NON_SEARCHABLE_TYPES + " AND unread > 0 AND ti.tag_id = ?");
        if (tag.getUnreadCount() > DbMailItem.RESULTS_STREAMING_MIN_ROWS) {
            Db.getInstance().enableStreaming(stmt);
        }
        int pos = 1;
        pos = DbMailItem.setMailboxId(stmt, mbox, pos);
        stmt.setInt(pos++, tag.getId());
        rs = stmt.executeQuery();
        while (rs.next()) {
            UnderlyingData data = DbMailItem.constructItem(rs);
            if (Mailbox.isCachedType(MailItem.Type.of(data.type))) {
                throw ServiceException.INVALID_REQUEST("folders and tags must be retrieved from cache", null);
            }
            result.add(data);
        }
        return result;
    } catch (SQLException e) {
        throw ServiceException.FAILURE("fetching unread messages for tag " + tag.getName(), e);
    } finally {
        DbPool.closeResults(rs);
        DbPool.closeStatement(stmt);
    }
}
Also used : Mailbox(com.zimbra.cs.mailbox.Mailbox) SQLException(java.sql.SQLException) UnderlyingData(com.zimbra.cs.mailbox.MailItem.UnderlyingData) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) DbConnection(com.zimbra.cs.db.DbPool.DbConnection)

Example 27 with UnderlyingData

use of com.zimbra.cs.mailbox.MailItem.UnderlyingData in project zm-mailbox by Zimbra.

the class DbTag method recalculateTagCounts.

static void recalculateTagCounts(Mailbox mbox, Map<Integer, UnderlyingData> lookup) throws ServiceException {
    DbConnection conn = mbox.getOperationConnection();
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        String mailboxesMatchAnd = DebugConfig.disableMailboxGroups ? "" : "mi.mailbox_id = ti.mailbox_id AND ";
        stmt = conn.prepareStatement("SELECT ti.tag_id, COUNT(*), " + Db.clauseIFNULL("SUM(mi.unread)", "0") + " FROM " + DbMailItem.getMailItemTableName(mbox, "mi") + " INNER JOIN " + getTaggedItemTableName(mbox, "ti") + " ON " + mailboxesMatchAnd + "ti.item_id = mi.id" + " WHERE " + inThisMailboxAnd("ti") + "ti.tag_id > 0 AND type NOT IN " + DbMailItem.NON_SEARCHABLE_TYPES + " AND " + Db.getInstance().bitAND("mi.flags", "" + Flag.BITMASK_DELETED) + " = 0" + " GROUP BY ti.tag_id");
        DbMailItem.setMailboxId(stmt, mbox, 1);
        rs = stmt.executeQuery();
        while (rs.next()) {
            int tagId = rs.getInt(1);
            int count = rs.getInt(2);
            int unread = rs.getInt(3);
            UnderlyingData data = lookup.get(tagId);
            if (data != null) {
                data.unreadCount += unread;
                data.size += count;
            } else {
                ZimbraLog.mailbox.warn("inconsistent DB state: items with no corresponding tag (tag id %d)", tagId);
            }
        }
    } catch (SQLException e) {
        throw ServiceException.FAILURE("recalculating tag counts for mailbox " + mbox.getId(), e);
    } finally {
        DbPool.closeResults(rs);
        DbPool.closeStatement(stmt);
    }
}
Also used : SQLException(java.sql.SQLException) UnderlyingData(com.zimbra.cs.mailbox.MailItem.UnderlyingData) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) DbConnection(com.zimbra.cs.db.DbPool.DbConnection)

Example 28 with UnderlyingData

use of com.zimbra.cs.mailbox.MailItem.UnderlyingData in project zm-mailbox by Zimbra.

the class DbTag method asUnderlyingData.

private static UnderlyingData asUnderlyingData(ResultSet rs) throws SQLException, ServiceException {
    UnderlyingData data = new UnderlyingData();
    data.id = rs.getInt(1);
    data.type = MailItem.Type.TAG.toByte();
    data.folderId = Mailbox.ID_FOLDER_TAGS;
    data.name = rs.getString(2);
    data.size = rs.getInt(4);
    data.unreadCount = rs.getInt(5);
    boolean listed = rs.getBoolean(6);
    String policy = rs.getString(8);
    RetentionPolicy rp = policy == null ? null : RetentionPolicyManager.retentionPolicyFromMetadata(new Metadata(policy), true);
    long c = rs.getLong(3);
    Color color = rs.wasNull() ? null : Color.fromMetadata(c);
    // FIXME: if Tag weren't a subclass of MailItem, this step wouldn't be necessary
    data.metadata = Tag.encodeMetadata(color, 1, 1, rp, listed);
    data.modMetadata = rs.getInt(7);
    data.modContent = data.modMetadata;
    return data;
}
Also used : UnderlyingData(com.zimbra.cs.mailbox.MailItem.UnderlyingData) Color(com.zimbra.common.mailbox.Color) Metadata(com.zimbra.cs.mailbox.Metadata) RetentionPolicy(com.zimbra.soap.mail.type.RetentionPolicy)

Example 29 with UnderlyingData

use of com.zimbra.cs.mailbox.MailItem.UnderlyingData in project zm-mailbox by Zimbra.

the class SpamExtract method extractMessages.

private static List<String> extractMessages(HttpClientBuilder hc, HttpGet gm, String path, File outdir, boolean raw) throws HttpException, IOException {
    List<String> extractedIds = new ArrayList<String>();
    HttpClient client = hc.build();
    if (LOG.isDebugEnabled()) {
        LOG.debug("Fetching " + path);
    }
    try {
        URI uri = new URI(path);
        gm.setURI(uri);
    } catch (URISyntaxException e) {
        LOG.warn("exception occurred in URI path", e);
    }
    HttpResponse httpResp = HttpClientUtil.executeMethod(client, gm);
    if (httpResp.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
        throw new IOException("HTTP GET failed: " + gm.getRequestLine().getUri() + ": " + httpResp.getStatusLine().getStatusCode() + ": " + httpResp.getStatusLine().getReasonPhrase());
    }
    try (ArchiveInputStream tgzStream = new TarArchiveInputStream(new GZIPInputStream(httpResp.getEntity().getContent()), 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
                entry = tgzStream.getNextEntry();
                // by .eml
                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);
                    }
                }
            }
        }
    }
    return extractedIds;
}
Also used : ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) UnderlyingData(com.zimbra.cs.mailbox.MailItem.UnderlyingData) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) TarArchiveInputStream(com.zimbra.cs.service.formatter.TarArchiveInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) BufferedOutputStream(java.io.BufferedOutputStream) 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) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) IOException(java.io.IOException) 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) HttpClient(org.apache.http.client.HttpClient) FileOutputStream(java.io.FileOutputStream) File(java.io.File) ItemData(com.zimbra.cs.service.util.ItemData)

Example 30 with UnderlyingData

use of com.zimbra.cs.mailbox.MailItem.UnderlyingData in project zm-mailbox by Zimbra.

the class DbMailItem method getByImapId.

public static UnderlyingData getByImapId(Mailbox mbox, int imapId, int folderId) throws ServiceException {
    DbConnection conn = mbox.getOperationConnection();
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        stmt = conn.prepareStatement("SELECT " + DB_FIELDS + " FROM " + getMailItemTableName(mbox, "mi") + " WHERE " + IN_THIS_MAILBOX_AND + "folder_id = ? AND imap_id = ?");
        int pos = 1;
        pos = setMailboxId(stmt, mbox, pos);
        stmt.setInt(pos++, folderId);
        stmt.setInt(pos++, imapId);
        rs = stmt.executeQuery();
        if (!rs.next()) {
            throw MailServiceException.NO_SUCH_ITEM(imapId);
        }
        UnderlyingData data = constructItem(rs);
        if (data.type == MailItem.Type.CONVERSATION.toByte()) {
            throw MailServiceException.NO_SUCH_ITEM(imapId);
        }
        return data;
    } catch (SQLException e) {
        throw ServiceException.FAILURE("fetching item " + imapId, e);
    } finally {
        DbPool.closeResults(rs);
        DbPool.closeStatement(stmt);
    }
}
Also used : SQLException(java.sql.SQLException) UnderlyingData(com.zimbra.cs.mailbox.MailItem.UnderlyingData) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) DbConnection(com.zimbra.cs.db.DbPool.DbConnection)

Aggregations

UnderlyingData (com.zimbra.cs.mailbox.MailItem.UnderlyingData)30 PreparedStatement (java.sql.PreparedStatement)21 SQLException (java.sql.SQLException)21 ResultSet (java.sql.ResultSet)20 DbConnection (com.zimbra.cs.db.DbPool.DbConnection)18 ArrayList (java.util.ArrayList)13 Mailbox (com.zimbra.cs.mailbox.Mailbox)8 Tag (com.zimbra.cs.mailbox.Tag)4 ServiceException (com.zimbra.common.service.ServiceException)3 MailItem (com.zimbra.cs.mailbox.MailItem)3 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 Color (com.zimbra.common.mailbox.Color)2 BufferStream (com.zimbra.common.util.BufferStream)2 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)2 ZSharedFileInputStream (com.zimbra.common.zmime.ZSharedFileInputStream)2 DbMailItem (com.zimbra.cs.db.DbMailItem)2 Folder (com.zimbra.cs.mailbox.Folder)2 ArchiveInputEntry (com.zimbra.cs.service.formatter.ArchiveFormatter.ArchiveInputEntry)2 ArchiveInputStream (com.zimbra.cs.service.formatter.ArchiveFormatter.ArchiveInputStream)2