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