Search in sources :

Example 16 with DataSource

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

the class PlainTextSignature method preModify.

@Override
public void preModify(CallbackContext context, String attrName, Object value, Map attrsToModify, Entry entry) throws ServiceException {
    SingleValueMod mod = singleValueMod(attrName, value);
    if (mod.unsetting())
        return;
    Account account;
    if (entry instanceof Account) {
        account = (Account) entry;
    } else if (entry instanceof Identity) {
        account = ((Identity) entry).getAccount();
    } else if (entry instanceof DataSource) {
        account = ((DataSource) entry).getAccount();
    } else {
        return;
    }
    Signature sig = Provisioning.getInstance().get(account, Key.SignatureBy.id, mod.value());
    if (sig == null) {
        throw ServiceException.INVALID_REQUEST("No such signature " + mod.value() + " for account " + account.getName(), null);
    }
    String sigAttr = SignatureUtil.mimeTypeToAttrName(MimeConstants.CT_TEXT_PLAIN);
    String plainSig = sig.getAttr(sigAttr, null);
    if (StringUtil.isNullOrEmpty(plainSig)) {
        throw ServiceException.INVALID_REQUEST("Signature " + mod.value() + " must have plain text content", null);
    }
}
Also used : Account(com.zimbra.cs.account.Account) Signature(com.zimbra.cs.account.Signature) Identity(com.zimbra.cs.account.Identity) DataSource(com.zimbra.cs.account.DataSource)

Example 17 with DataSource

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

the class Mailbox method updateRssDataSource.

/**
 * Updates the data source for an RSS folder.  If the folder URL is set,
 * checks or creates a data source that updates the folder.  If the URL
 * is not set, deletes the data source if necessary.
 */
protected void updateRssDataSource(Folder folder) {
    try {
        Provisioning prov = Provisioning.getInstance();
        Account account = getAccount();
        DataSource ds = null;
        List<DataSource> dataSources = prov.getAllDataSources(account);
        for (DataSource i : dataSources) {
            if (i.getFolderId() == folder.getId() && (i.getType() == DataSourceType.rss || i.getType() == DataSourceType.cal)) {
                ds = i;
                break;
            }
        }
        if (StringUtil.isNullOrEmpty(folder.getUrl())) {
            if (ds != null) {
                // URL removed from folder.
                String dsid = ds.getId();
                prov.deleteDataSource(account, dsid);
                DataSourceManager.cancelSchedule(account, dsid);
            }
            return;
        }
        // URL is not null or empty.  Create data source if necessary.
        if (ds == null) {
            Map<String, Object> attrs = new HashMap<String, Object>();
            attrs.put(Provisioning.A_zimbraDataSourceEnabled, LdapConstants.LDAP_TRUE);
            attrs.put(Provisioning.A_zimbraDataSourceFolderId, Integer.toString(folder.getId()));
            DataSourceType type;
            String name;
            if (folder.getDefaultView() == MailItem.Type.APPOINTMENT) {
                type = DataSourceType.cal;
                name = "CAL-" + folder.getId();
            } else {
                type = DataSourceType.rss;
                name = "RSS-" + folder.getId();
            }
            ds = prov.createDataSource(account, type, name, attrs);
            DataSourceManager.updateSchedule(account, ds);
        }
    } catch (ServiceException e) {
        ZimbraLog.mailbox.warn("Unable to update data source for folder %s.", folder.getPath(), e);
    }
}
Also used : Account(com.zimbra.cs.account.Account) AccountServiceException(com.zimbra.cs.account.AccountServiceException) ServiceException(com.zimbra.common.service.ServiceException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentLinkedHashMap(com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap) HashMap(java.util.HashMap) DataSourceType(com.zimbra.soap.admin.type.DataSourceType) Provisioning(com.zimbra.cs.account.Provisioning) MailboxBlobDataSource(com.zimbra.cs.store.MailboxBlobDataSource) DataSource(com.zimbra.cs.account.DataSource) DbDataSource(com.zimbra.cs.db.DbDataSource) ParsedMessageDataSource(com.zimbra.cs.mime.ParsedMessageDataSource)

Example 18 with DataSource

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

the class SoapProvisioning method getAllDataSources.

@Override
public List<DataSource> getAllDataSources(Account account) throws ServiceException {
    List<DataSource> result = new ArrayList<DataSource>();
    XMLElement req = new XMLElement(AdminConstants.GET_DATA_SOURCES_REQUEST);
    req.addElement(AdminConstants.E_ID).setText(account.getId());
    Element resp = invoke(req);
    for (Element dataSource : resp.listElements(AccountConstants.E_DATA_SOURCE)) {
        result.add(new SoapDataSource(account, dataSource, this));
    }
    return result;
}
Also used : Element(com.zimbra.common.soap.Element) XMLElement(com.zimbra.common.soap.Element.XMLElement) ArrayList(java.util.ArrayList) XMLElement(com.zimbra.common.soap.Element.XMLElement) DataSource(com.zimbra.cs.account.DataSource)

Example 19 with DataSource

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

the class DbDataSource method getOldestConversationsUpToSize.

public static List<PurgeableConv> getOldestConversationsUpToSize(List<DataSource> dataSources, long targetSize, long startDate) throws ServiceException {
    Mailbox mbox = DataSourceManager.getInstance().getMailbox(dataSources.get(0));
    DbConnection conn = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    long totalSize = 0;
    try {
        conn = DbPool.getConnection();
        StringBuilder sb = new StringBuilder();
        sb.append("SELECT DISTINCT COALESCE(msgs.parent_id, msgs.msg_id) AS item_id," + " msgs.data_source_id AS data_source," + " COALESCE(convs.latest_date, msgs.mdate) AS newest_msg_date," + " COALESCE(convs.num_msgs, 1) AS num_msgs," + " COALESCE(convs.conv_size, msgs.msize) AS size");
        sb.append(" FROM ");
        sb.append("(SELECT di.data_source_id AS data_source_id, " + " mi.id AS msg_id," + " mi.parent_id AS parent_id," + " mi.size AS msize," + " mi.date AS mdate FROM ");
        sb.append(getTableName(mbox)).append(" di ");
        sb.append(" INNER JOIN ");
        sb.append(DbMailItem.getMailItemTableName(mbox)).append(" mi ");
        sb.append(" ON di.mailbox_id = mi.mailbox_id AND di.item_id = mi.id");
        sb.append(" WHERE di.mailbox_id = ? AND mi.type = ? AND ");
        sb.append(DbUtil.whereIn("di.data_source_id", dataSources.size()));
        sb.append(") AS msgs");
        sb.append(" LEFT JOIN ");
        sb.append("(SELECT parent_id, max(date) AS latest_date, count(date) AS num_msgs, sum(size) AS conv_size");
        sb.append(" FROM ").append(DbMailItem.getMailItemTableName(mbox));
        sb.append(" WHERE mailbox_id = ? AND type = ? GROUP BY parent_id) AS convs");
        sb.append(" ON msgs.parent_id = convs.parent_id");
        if (startDate > 0L) {
            sb.append(" WHERE COALESCE(convs.latest_date, msgs.mdate) > ?");
        }
        sb.append(" ORDER BY COALESCE(convs.latest_date, msgs.mdate) ASC");
        stmt = conn.prepareStatement(sb.toString());
        int pos = 1;
        pos = DbMailItem.setMailboxId(stmt, mbox, pos);
        stmt.setByte(pos++, MailItem.Type.MESSAGE.toByte());
        for (DataSource ds : dataSources) {
            stmt.setString(pos++, ds.getId());
        }
        pos = DbMailItem.setMailboxId(stmt, mbox, pos);
        stmt.setByte(pos++, MailItem.Type.MESSAGE.toByte());
        if (startDate > 0L) {
            stmt.setLong(pos++, startDate);
        }
        rs = stmt.executeQuery();
        List<PurgeableConv> convs = new LinkedList<PurgeableConv>();
        while (rs.next() && totalSize < targetSize) {
            long convSize = rs.getLong("size");
            int itemId = rs.getInt("item_id");
            int numMsgs = rs.getInt("num_msgs");
            long convDate = rs.getLong("newest_msg_date");
            String dataSourceId = rs.getString("data_source");
            convs.add(new PurgeableConv(itemId, convSize, convDate, dataSourceId, numMsgs));
            totalSize += convSize;
        }
        rs.close();
        stmt.close();
        return convs;
    } catch (SQLException e) {
        throw ServiceException.FAILURE("Unable to get oldest conversations for data sources", e);
    } finally {
        DbPool.closeResults(rs);
        DbPool.closeStatement(stmt);
        DbPool.quietClose(conn);
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) DbConnection(com.zimbra.cs.db.DbPool.DbConnection) LinkedList(java.util.LinkedList) DataSource(com.zimbra.cs.account.DataSource) PurgeableConv(com.zimbra.cs.purge.DataSourcePurge.PurgeableConv) Mailbox(com.zimbra.cs.mailbox.Mailbox) ResultSet(java.sql.ResultSet)

Example 20 with DataSource

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

the class DeleteDataSource method handle.

@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException, SoapFaultException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Provisioning prov = Provisioning.getInstance();
    Account account = getRequestedAccount(zsc);
    if (!canModifyOptions(zsc, account))
        throw ServiceException.PERM_DENIED("can not modify options");
    Mailbox mbox = getRequestedMailbox(zsc);
    for (Element eDsrc : request.listElements()) {
        DataSource dsrc = null;
        String name, id = eDsrc.getAttribute(MailConstants.A_ID, null);
        if (id != null)
            dsrc = prov.get(account, Key.DataSourceBy.id, id);
        else if ((name = eDsrc.getAttribute(MailConstants.A_NAME, null)) != null)
            dsrc = prov.get(account, Key.DataSourceBy.name, name);
        else
            throw ServiceException.INVALID_REQUEST("must specify either 'id' or 'name'", null);
        // note that we're not checking the element name against the actual data source's type
        if (dsrc == null)
            continue;
        String dataSourceId = dsrc.getId();
        DataSourceType dstype = dsrc.getType();
        prov.deleteDataSource(account, dataSourceId);
        DbDataSource.deletePurgedDataForDataSource(mbox, dataSourceId);
        if (dstype == DataSourceType.pop3)
            DbPop3Message.deleteUids(mbox, dataSourceId);
        else if (dstype == DataSourceType.imap) {
            DbImapFolder.deleteImapData(mbox, dataSourceId);
            DataSourceListner.deleteDataSource(account, dsrc);
        }
        DbDataSource.deleteAllMappings(dsrc);
        DataSourceManager.cancelSchedule(account, dataSourceId);
    }
    Element response = zsc.createElement(MailConstants.DELETE_DATA_SOURCE_RESPONSE);
    return response;
}
Also used : Account(com.zimbra.cs.account.Account) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) Element(com.zimbra.common.soap.Element) DataSourceType(com.zimbra.soap.admin.type.DataSourceType) Provisioning(com.zimbra.cs.account.Provisioning) DataSource(com.zimbra.cs.account.DataSource) DbDataSource(com.zimbra.cs.db.DbDataSource)

Aggregations

DataSource (com.zimbra.cs.account.DataSource)71 Account (com.zimbra.cs.account.Account)29 HashMap (java.util.HashMap)21 Provisioning (com.zimbra.cs.account.Provisioning)18 DbDataSource (com.zimbra.cs.db.DbDataSource)18 ServiceException (com.zimbra.common.service.ServiceException)15 Element (com.zimbra.common.soap.Element)14 Mailbox (com.zimbra.cs.mailbox.Mailbox)11 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)11 ArrayList (java.util.ArrayList)11 ZDataSource (com.zimbra.client.ZDataSource)8 AccountServiceException (com.zimbra.cs.account.AccountServiceException)7 Folder (com.zimbra.cs.mailbox.Folder)7 DataSourceType (com.zimbra.soap.admin.type.DataSourceType)7 HashSet (java.util.HashSet)7 Test (org.junit.Test)7 DataSourceItem (com.zimbra.cs.db.DbDataSource.DataSourceItem)6 ZMailbox (com.zimbra.client.ZMailbox)5 LdapDataSource (com.zimbra.cs.account.ldap.entry.LdapDataSource)5 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)5