Search in sources :

Example 11 with Policy

use of com.zimbra.soap.mail.type.Policy in project zm-mailbox by Zimbra.

the class TestPurge method testSystemRetentionPolicy.

@Test
public void testSystemRetentionPolicy() throws Exception {
    SoapProvisioning prov = TestUtil.newSoapProvisioning();
    // Test getting empty system policy.
    GetSystemRetentionPolicyRequest getReq = new GetSystemRetentionPolicyRequest();
    GetSystemRetentionPolicyResponse getRes = prov.invokeJaxb(getReq);
    RetentionPolicy rp = getRes.getRetentionPolicy();
    Assert.assertEquals(0, rp.getKeepPolicy().size());
    Assert.assertEquals(0, rp.getPurgePolicy().size());
    // Create keep policy.
    Policy keep = Policy.newSystemPolicy("keep", "60d");
    CreateSystemRetentionPolicyRequest createReq = CreateSystemRetentionPolicyRequest.newKeepRequest(keep);
    CreateSystemRetentionPolicyResponse createRes = prov.invokeJaxb(createReq);
    Policy p = createRes.getPolicy();
    Assert.assertNotNull(p.getId());
    Assert.assertEquals(keep.getName(), p.getName());
    Assert.assertEquals(keep.getLifetime(), p.getLifetime());
    keep = p;
    // Create purge policy.
    Policy purge1 = Policy.newSystemPolicy("purge1", "120d");
    createReq = CreateSystemRetentionPolicyRequest.newPurgeRequest(purge1);
    createRes = prov.invokeJaxb(createReq);
    purge1 = createRes.getPolicy();
    Policy purge2 = Policy.newSystemPolicy("purge2", "240d");
    createReq = CreateSystemRetentionPolicyRequest.newPurgeRequest(purge2);
    createRes = prov.invokeJaxb(createReq);
    purge2 = createRes.getPolicy();
    // Test getting updated system policy.
    getRes = prov.invokeJaxb(getReq);
    rp = getRes.getRetentionPolicy();
    Assert.assertEquals(1, rp.getKeepPolicy().size());
    Assert.assertEquals(keep, rp.getKeepPolicy().get(0));
    Assert.assertEquals(2, rp.getPurgePolicy().size());
    Assert.assertEquals(purge1, rp.getPolicyById(purge1.getId()));
    Assert.assertEquals(purge2, rp.getPolicyById(purge2.getId()));
    // Get system policy with the mail API.
    ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
    com.zimbra.soap.mail.message.GetSystemRetentionPolicyResponse mailRes = mbox.invokeJaxb(new com.zimbra.soap.mail.message.GetSystemRetentionPolicyRequest());
    Assert.assertEquals(rp, mailRes.getRetentionPolicy());
    // Modify lifetime.
    Policy modLifetime = Policy.newSystemPolicy(purge1.getId(), null, "121d");
    ModifySystemRetentionPolicyRequest modifyReq = new ModifySystemRetentionPolicyRequest(modLifetime);
    ModifySystemRetentionPolicyResponse modifyRes = prov.invokeJaxb(modifyReq);
    Policy newPurge1 = modifyRes.getPolicy();
    Assert.assertEquals(Policy.newSystemPolicy(purge1.getId(), "purge1", "121d"), newPurge1);
    getRes = prov.invokeJaxb(getReq);
    Assert.assertEquals(newPurge1, getRes.getRetentionPolicy().getPolicyById(newPurge1.getId()));
    // Modify name.
    Policy modName = Policy.newSystemPolicy(purge1.getId(), "purge1-new", null);
    modifyReq = new ModifySystemRetentionPolicyRequest(modName);
    modifyRes = prov.invokeJaxb(modifyReq);
    newPurge1 = modifyRes.getPolicy();
    Assert.assertEquals(Policy.newSystemPolicy(purge1.getId(), "purge1-new", "121d"), newPurge1);
    // Delete.
    DeleteSystemRetentionPolicyRequest deleteReq = new DeleteSystemRetentionPolicyRequest(purge1);
    @SuppressWarnings("unused") DeleteSystemRetentionPolicyResponse deleteRes = prov.invokeJaxb(deleteReq);
    getRes = prov.invokeJaxb(getReq);
    rp = getRes.getRetentionPolicy();
    Assert.assertEquals(1, rp.getKeepPolicy().size());
    Assert.assertEquals(keep, rp.getKeepPolicy().get(0));
    Assert.assertEquals(1, rp.getPurgePolicy().size());
    Assert.assertEquals(purge2, rp.getPurgePolicy().get(0));
}
Also used : Policy(com.zimbra.soap.mail.type.Policy) RetentionPolicy(com.zimbra.soap.mail.type.RetentionPolicy) DeleteSystemRetentionPolicyResponse(com.zimbra.soap.admin.message.DeleteSystemRetentionPolicyResponse) DeleteSystemRetentionPolicyRequest(com.zimbra.soap.admin.message.DeleteSystemRetentionPolicyRequest) CreateSystemRetentionPolicyResponse(com.zimbra.soap.admin.message.CreateSystemRetentionPolicyResponse) GetSystemRetentionPolicyResponse(com.zimbra.soap.admin.message.GetSystemRetentionPolicyResponse) RetentionPolicy(com.zimbra.soap.mail.type.RetentionPolicy) ModifySystemRetentionPolicyRequest(com.zimbra.soap.admin.message.ModifySystemRetentionPolicyRequest) ZMailbox(com.zimbra.client.ZMailbox) SoapProvisioning(com.zimbra.cs.account.soap.SoapProvisioning) CreateSystemRetentionPolicyRequest(com.zimbra.soap.admin.message.CreateSystemRetentionPolicyRequest) GetSystemRetentionPolicyRequest(com.zimbra.soap.admin.message.GetSystemRetentionPolicyRequest) ModifySystemRetentionPolicyResponse(com.zimbra.soap.admin.message.ModifySystemRetentionPolicyResponse) Test(org.junit.Test)

Example 12 with Policy

use of com.zimbra.soap.mail.type.Policy in project zm-mailbox by Zimbra.

the class RetentionPolicyManager method modifySystemPolicy.

/**
     * Updates the properties of the system policy with the given id.
     * @return {@code null} if a {@code Policy} with the given id could not be found
     */
public Policy modifySystemPolicy(Entry entry, String id, String name, String lifetime) throws ServiceException {
    validateLifetime(lifetime);
    synchronized (entry) {
        SystemPolicy sp = getCachedSystemPolicy(entry);
        if (sp.keep.containsKey(id)) {
            Policy p = Policy.newSystemPolicy(id, name, lifetime);
            sp.keep.put(id, p);
            saveSystemPolicy(entry, new RetentionPolicy(sp.keep.values(), sp.purge.values()));
            return p;
        }
        if (sp.purge.containsKey(id)) {
            Policy p = Policy.newSystemPolicy(id, name, lifetime);
            sp.purge.put(id, p);
            saveSystemPolicy(entry, new RetentionPolicy(sp.keep.values(), sp.purge.values()));
            return p;
        }
    }
    return null;
}
Also used : Policy(com.zimbra.soap.mail.type.Policy) RetentionPolicy(com.zimbra.soap.mail.type.RetentionPolicy) RetentionPolicy(com.zimbra.soap.mail.type.RetentionPolicy)

Example 13 with Policy

use of com.zimbra.soap.mail.type.Policy in project zm-mailbox by Zimbra.

the class RetentionPolicyManager method createSystemKeepPolicy.

public Policy createSystemKeepPolicy(Entry entry, String name, String lifetime) throws ServiceException {
    validateLifetime(lifetime);
    Policy p = Policy.newSystemPolicy(generateId(), name, lifetime);
    synchronized (entry) {
        SystemPolicy sp = getCachedSystemPolicy(entry);
        sp.keep.put(p.getId(), p);
        saveSystemPolicy(entry, new RetentionPolicy(sp.keep.values(), sp.purge.values()));
    }
    return p;
}
Also used : Policy(com.zimbra.soap.mail.type.Policy) RetentionPolicy(com.zimbra.soap.mail.type.RetentionPolicy) RetentionPolicy(com.zimbra.soap.mail.type.RetentionPolicy)

Example 14 with Policy

use of com.zimbra.soap.mail.type.Policy in project zm-mailbox by Zimbra.

the class RetentionPolicyManager method getCachedSystemPolicy.

private SystemPolicy getCachedSystemPolicy(Entry entry) throws ServiceException {
    SystemPolicy sp;
    synchronized (entry) {
        sp = (SystemPolicy) entry.getCachedData(SYSTEM_POLICY_KEY);
        if (sp == null) {
            String xml;
            if (entry instanceof Config)
                xml = ((Config) entry).getMailPurgeSystemPolicy();
            else if (entry instanceof Cos)
                xml = ((Cos) entry).getMailPurgeSystemPolicy();
            else
                throw ServiceException.UNSUPPORTED();
            sp = new SystemPolicy();
            if (!Strings.isNullOrEmpty(xml)) {
                ZimbraLog.purge.debug("Parsing system retention policy:\n%s", xml);
                try {
                    Element el = Element.parseXML(xml);
                    RetentionPolicy rp = JaxbUtil.elementToJaxb(el, RetentionPolicy.class);
                    for (Policy p : rp.getKeepPolicy()) {
                        assert (p.getId() != null);
                        sp.keep.put(p.getId(), p);
                    }
                    for (Policy p : rp.getPurgePolicy()) {
                        assert (p.getId() != null);
                        sp.purge.put(p.getId(), p);
                    }
                } catch (XmlParseException e) {
                    throw ServiceException.FAILURE("Unable to parse system retention policy.", e);
                }
            }
            entry.setCachedData(SYSTEM_POLICY_KEY, sp);
        }
    }
    return sp;
}
Also used : Policy(com.zimbra.soap.mail.type.Policy) RetentionPolicy(com.zimbra.soap.mail.type.RetentionPolicy) Config(com.zimbra.cs.account.Config) Cos(com.zimbra.cs.account.Cos) XMLElement(com.zimbra.common.soap.Element.XMLElement) Element(com.zimbra.common.soap.Element) XmlParseException(com.zimbra.common.soap.XmlParseException) RetentionPolicy(com.zimbra.soap.mail.type.RetentionPolicy)

Example 15 with Policy

use of com.zimbra.soap.mail.type.Policy in project zm-mailbox by Zimbra.

the class RetentionPolicyManager method toMetadata.

/**
     * Persists retention policy to {@code Metadata}. 
     * @param rp retention policy data
     * @param forMailbox {@code true} if this is mailbox retention policy,
     *     {@code false} if this is system retention policy.  For mailbox
     *     policy, only the id is persisted.
     * @return
     */
public static Metadata toMetadata(RetentionPolicy rp, boolean forMailbox) {
    MetadataList keep = new MetadataList();
    MetadataList purge = new MetadataList();
    for (Policy p : rp.getKeepPolicy()) {
        keep.add(toMetadata(p, forMailbox));
    }
    for (Policy p : rp.getPurgePolicy()) {
        purge.add(toMetadata(p, forMailbox));
    }
    Metadata m = new Metadata();
    m.put(FN_KEEP, keep);
    m.put(FN_PURGE, purge);
    return m;
}
Also used : Policy(com.zimbra.soap.mail.type.Policy) RetentionPolicy(com.zimbra.soap.mail.type.RetentionPolicy)

Aggregations

Policy (com.zimbra.soap.mail.type.Policy)28 RetentionPolicy (com.zimbra.soap.mail.type.RetentionPolicy)25 Test (org.junit.Test)12 Config (com.zimbra.cs.account.Config)6 Provisioning (com.zimbra.cs.account.Provisioning)4 ZMailbox (com.zimbra.client.ZMailbox)3 ServiceException (com.zimbra.common.service.ServiceException)3 Cos (com.zimbra.cs.account.Cos)3 Entry (com.zimbra.cs.account.Entry)3 NoSuchItemException (com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException)3 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)3 CosSelector (com.zimbra.soap.admin.type.CosSelector)3 ZFolder (com.zimbra.client.ZFolder)2 Element (com.zimbra.common.soap.Element)2 CreateSystemRetentionPolicyRequest (com.zimbra.soap.admin.message.CreateSystemRetentionPolicyRequest)2 CreateSystemRetentionPolicyResponse (com.zimbra.soap.admin.message.CreateSystemRetentionPolicyResponse)2 DeleteSystemRetentionPolicyRequest (com.zimbra.soap.admin.message.DeleteSystemRetentionPolicyRequest)2 DeleteSystemRetentionPolicyResponse (com.zimbra.soap.admin.message.DeleteSystemRetentionPolicyResponse)2 ModifySystemRetentionPolicyRequest (com.zimbra.soap.admin.message.ModifySystemRetentionPolicyRequest)2 ModifySystemRetentionPolicyResponse (com.zimbra.soap.admin.message.ModifySystemRetentionPolicyResponse)2