Search in sources :

Example 36 with Mailbox

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

the class UrlNamespace method getCalendarItemForMessage.

private static DavResource getCalendarItemForMessage(DavContext ctxt, Message msg) throws ServiceException {
    Mailbox mbox = msg.getMailbox();
    if (msg.isInvite() && msg.hasCalendarItemInfos()) {
        Message.CalendarItemInfo calItemInfo = msg.getCalendarItemInfo(0);
        try {
            Invite invite = calItemInfo.getInvite();
            if (invite == null && calItemInfo.calItemCreated()) {
                // Pre-6.0 data
                CalendarItem item = mbox.getCalendarItemById(ctxt.getOperationContext(), calItemInfo.getCalendarItemId());
                invite = calItemInfo.getInvite();
                int compNum = calItemInfo.getComponentNo();
                invite = item.getInvite(msg.getId(), compNum);
            }
            if (invite != null) {
                String path = CalendarObject.CalendarPath.generate(ctxt, msg.getPath(), invite.getUid(), mbox.getId(), msg.getId(), msg.getId());
                return new CalendarObject.ScheduleMessage(ctxt, path, ctxt.getUser(), invite, msg);
            }
        } catch (MailServiceException.NoSuchItemException e) {
        // the appt must have been cancelled or deleted.
        // bug 26315
        }
    }
    return null;
}
Also used : CalendarItem(com.zimbra.cs.mailbox.CalendarItem) Mailbox(com.zimbra.cs.mailbox.Mailbox) Message(com.zimbra.cs.mailbox.Message) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) Invite(com.zimbra.cs.mailbox.calendar.Invite) Mountpoint(com.zimbra.cs.mailbox.Mountpoint)

Example 37 with Mailbox

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

the class UrlNamespace method getFolders.

private static java.util.Collection<DavResource> getFolders(DavContext ctxt, String user) throws ServiceException, DavException {
    Provisioning prov = Provisioning.getInstance();
    Account account = prov.get(AccountBy.name, user);
    if (account == null) {
        // Anti-account name harvesting.
        ZimbraLog.dav.info("Failing GET of folders - no such account '%s'", user);
        throw new DavException("Request denied", HttpServletResponse.SC_NOT_FOUND, null);
    }
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
    OperationContext octxt = ctxt.getOperationContext();
    ArrayList<DavResource> rss = new ArrayList<DavResource>();
    for (Folder f : mbox.getVisibleFolders(octxt)) rss.add(getResourceFromMailItem(ctxt, f));
    return rss;
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Account(com.zimbra.cs.account.Account) Mailbox(com.zimbra.cs.mailbox.Mailbox) DavException(com.zimbra.cs.dav.DavException) ArrayList(java.util.ArrayList) Folder(com.zimbra.cs.mailbox.Folder) Provisioning(com.zimbra.cs.account.Provisioning)

Example 38 with Mailbox

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

the class UrlNamespace method getMailItemResource.

/**
     *
     * @param ctxt
     * @param user
     * @param path - May contain parameters
     * @return
     */
private static DavResource getMailItemResource(DavContext ctxt, String user, String path) throws ServiceException, DavException {
    Provisioning prov = Provisioning.getInstance();
    Account account = prov.get(AccountBy.name, user);
    if (account == null) {
        // Anti-account name harvesting.
        ZimbraLog.dav.info("Failing GET of mail item resource - no such account '%s' path '%s'", user, path);
        throw new DavException("Request denied", HttpServletResponse.SC_NOT_FOUND, null);
    }
    if (ctxt.getUser().compareTo(user) != 0 || !Provisioning.onLocalServer(account)) {
        try {
            return new RemoteCollection(ctxt, path, account);
        } catch (MailServiceException.NoSuchItemException e) {
            return null;
        }
    }
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
    int id = 0;
    int index = path.indexOf('?');
    if (index > 0) {
        Map<String, String> params = HttpUtil.getURIParams(path.substring(index + 1));
        path = path.substring(0, index);
        if (params.containsKey("id")) {
            try {
                id = Integer.parseInt(params.get("id"));
            } catch (NumberFormatException e) {
            }
        }
    }
    // At this point, path will have had any parameters stripped from it
    OperationContext octxt = ctxt.getOperationContext();
    MailItem item = null;
    // simple case.  root folder or if id is specified.
    if (path.equals("/")) {
        item = mbox.getFolderByPath(octxt, "/");
    } else if (id > 0) {
        item = mbox.getItemById(octxt, id, MailItem.Type.UNKNOWN);
    }
    if (item != null) {
        return getResourceFromMailItem(ctxt, item);
    }
    // check for named items (folders, documents)
    try {
        return getResourceFromMailItem(ctxt, mbox.getItemByPath(octxt, path));
    } catch (MailServiceException.NoSuchItemException e) {
    }
    // check if the this is renamed folder.
    DavResource rs = checkRenamedResource(user, path);
    if (rs != null)
        return rs;
    // look up the item from path
    if (path.endsWith("/")) {
        path = path.substring(0, path.length() - 1);
    }
    index = path.lastIndexOf('/');
    String folderPath = path.substring(0, index);
    String baseName = path.substring(index + 1);
    Folder f = null;
    if (index != -1) {
        try {
            f = mbox.getFolderByPath(octxt, folderPath);
        } catch (MailServiceException.NoSuchItemException e) {
        }
    }
    if (f != null) {
        /* First check whether the default name has been over-ridden - perhaps because the name was
             * chosen by a DAV client via PUT to a URL when creating the item. */
        DavNames.DavName davName = null;
        if (DebugConfig.enableDAVclientCanChooseResourceBaseName) {
            davName = DavNames.DavName.create(mbox.getId(), f.getId(), baseName);
        }
        if (davName != null) {
            Integer itemId = DavNames.get(davName);
            if (itemId != null) {
                item = mbox.getItemById(octxt, itemId, MailItem.Type.UNKNOWN);
                if ((item != null) && (f.getId() == item.getFolderId())) {
                    return getResourceFromMailItem(ctxt, item);
                }
                item = null;
            }
        }
        if (baseName.toLowerCase().endsWith(CalendarObject.CAL_EXTENSION)) {
            String uid = baseName.substring(0, baseName.length() - CalendarObject.CAL_EXTENSION.length());
            // Unescape the name (It was encoded in DavContext intentionally)
            uid = HttpUtil.urlUnescape(uid);
            index = uid.indexOf(',');
            if (index > 0) {
                try {
                    id = Integer.parseInt(uid.substring(index + 1));
                } catch (NumberFormatException e) {
                }
            }
            if (id > 0) {
                item = mbox.getItemById(octxt, id, MailItem.Type.UNKNOWN);
            } else {
                item = mbox.getCalendarItemByUid(octxt, uid);
            }
            if ((item != null) && (f.getId() != item.getFolderId())) {
                item = null;
            }
        } else if (baseName.toLowerCase().endsWith(AddressObject.VCARD_EXTENSION)) {
            rs = AddressObject.getAddressObjectByUID(ctxt, baseName, account, f);
            if (rs != null) {
                return rs;
            }
        } else if (f.getId() == Mailbox.ID_FOLDER_INBOX || f.getId() == Mailbox.ID_FOLDER_SENT) {
            ctxt.setActingAsDelegateFor(baseName);
            // delegated scheduling and notification handling
            return getResourceFromMailItem(ctxt, f);
        }
    }
    return getResourceFromMailItem(ctxt, item);
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Account(com.zimbra.cs.account.Account) DavException(com.zimbra.cs.dav.DavException) Folder(com.zimbra.cs.mailbox.Folder) Provisioning(com.zimbra.cs.account.Provisioning) Mountpoint(com.zimbra.cs.mailbox.Mountpoint) DavNames(com.zimbra.cs.mailbox.DavNames) MailItem(com.zimbra.cs.mailbox.MailItem) Mailbox(com.zimbra.cs.mailbox.Mailbox) MailServiceException(com.zimbra.cs.mailbox.MailServiceException)

Example 39 with Mailbox

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

the class DbMailItem method persistCounts.

public static void persistCounts(MailItem item, Metadata metadata) throws ServiceException {
    Mailbox mbox = item.getMailbox();
    DbConnection conn = mbox.getOperationConnection();
    PreparedStatement stmt = null;
    try {
        stmt = conn.prepareStatement("UPDATE " + getMailItemTableName(item) + " SET size = ?, unread = ?, metadata = ?, mod_metadata = ?, change_date = ?, mod_content = ?" + " WHERE " + IN_THIS_MAILBOX_AND + "id = ?");
        int pos = 1;
        stmt.setLong(pos++, item.getSize());
        stmt.setInt(pos++, item.getUnreadCount());
        stmt.setString(pos++, checkMetadataLength(metadata.toString()));
        stmt.setInt(pos++, item.getModifiedSequence());
        if (item.getChangeDate() > 0) {
            stmt.setInt(pos++, (int) (item.getChangeDate() / 1000));
        } else {
            stmt.setNull(pos++, Types.INTEGER);
        }
        stmt.setInt(pos++, item.getSavedSequence());
        pos = setMailboxId(stmt, mbox, pos);
        stmt.setInt(pos++, item.getId());
        stmt.executeUpdate();
    } catch (SQLException e) {
        throw ServiceException.FAILURE("writing metadata for mailbox " + item.getMailboxId() + ", item " + item.getId(), e);
    } finally {
        DbPool.closeStatement(stmt);
    }
}
Also used : Mailbox(com.zimbra.cs.mailbox.Mailbox) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) DbConnection(com.zimbra.cs.db.DbPool.DbConnection)

Example 40 with Mailbox

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

the class DbMailItem method getUnreadMessages.

public static List<UnderlyingData> getUnreadMessages(MailItem relativeTo) throws ServiceException {
    if (relativeTo instanceof Tag) {
        return DbTag.getUnreadMessages((Tag) relativeTo);
    }
    Mailbox mbox = relativeTo.getMailbox();
    ArrayList<UnderlyingData> result = new ArrayList<UnderlyingData>();
    DbConnection conn = mbox.getOperationConnection();
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        String relation;
        if (relativeTo instanceof VirtualConversation) {
            relation = "id = ?";
        } else if (relativeTo instanceof Conversation) {
            relation = "parent_id = ?";
        } else if (relativeTo instanceof Folder) {
            relation = "folder_id = ?";
        } else {
            relation = "id = ?";
        }
        stmt = conn.prepareStatement("SELECT " + DB_FIELDS + " FROM " + getMailItemTableName(relativeTo.getMailbox(), " mi") + " WHERE " + IN_THIS_MAILBOX_AND + "unread > 0 AND " + relation + " AND type NOT IN " + NON_SEARCHABLE_TYPES);
        if (relativeTo.getUnreadCount() > RESULTS_STREAMING_MIN_ROWS) {
            Db.getInstance().enableStreaming(stmt);
        }
        int pos = 1;
        pos = setMailboxId(stmt, mbox, pos);
        if (relativeTo instanceof VirtualConversation) {
            stmt.setInt(pos++, ((VirtualConversation) relativeTo).getMessageId());
        } else {
            stmt.setInt(pos++, relativeTo.getId());
        }
        rs = stmt.executeQuery();
        while (rs.next()) {
            UnderlyingData data = 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 item " + relativeTo.getId(), e);
    } finally {
        DbPool.closeResults(rs);
        DbPool.closeStatement(stmt);
    }
}
Also used : SQLException(java.sql.SQLException) UnderlyingData(com.zimbra.cs.mailbox.MailItem.UnderlyingData) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) VirtualConversation(com.zimbra.cs.mailbox.VirtualConversation) Conversation(com.zimbra.cs.mailbox.Conversation) Folder(com.zimbra.cs.mailbox.Folder) DbConnection(com.zimbra.cs.db.DbPool.DbConnection) Mailbox(com.zimbra.cs.mailbox.Mailbox) ResultSet(java.sql.ResultSet) Tag(com.zimbra.cs.mailbox.Tag) VirtualConversation(com.zimbra.cs.mailbox.VirtualConversation)

Aggregations

Mailbox (com.zimbra.cs.mailbox.Mailbox)817 Account (com.zimbra.cs.account.Account)389 Test (org.junit.Test)376 OperationContext (com.zimbra.cs.mailbox.OperationContext)365 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)306 Message (com.zimbra.cs.mailbox.Message)303 DeliveryContext (com.zimbra.cs.mailbox.DeliveryContext)243 ItemId (com.zimbra.cs.service.util.ItemId)243 Element (com.zimbra.common.soap.Element)138 ServiceException (com.zimbra.common.service.ServiceException)127 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)127 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)106 MimeMessage (javax.mail.internet.MimeMessage)104 Folder (com.zimbra.cs.mailbox.Folder)82 ArrayList (java.util.ArrayList)81 ZMailbox (com.zimbra.client.ZMailbox)73 DbConnection (com.zimbra.cs.db.DbPool.DbConnection)71 PreparedStatement (java.sql.PreparedStatement)67 SQLException (java.sql.SQLException)67 Header (javax.mail.Header)66