Search in sources :

Example 21 with ZMailbox

use of com.zimbra.client.ZMailbox in project zm-mailbox by Zimbra.

the class ContactBackupThread method createBackup.

private void createBackup(OperationContext octxt, Mailbox mbox, Account account, Folder folder, Date startTime) {
    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
    StringBuilder filename = new StringBuilder();
    filename.append(FILE_NAME).append("-").append(sdf.format(date));
    InputStream is = null;
    ZimbraAuthToken token = null;
    try {
        token = new ZimbraAuthToken(account);
        ZMailbox.Options zoptions = new ZMailbox.Options(token.toZAuthToken(), AccountUtil.getSoapUri(account));
        zoptions.setNoSession(true);
        zoptions.setTargetAccount(account.getId());
        zoptions.setTargetAccountBy(AccountBy.id);
        ZMailbox zmbx = ZMailbox.getMailbox(zoptions);
        is = zmbx.getRESTResource(CONTACT_RES_URL);
        ParsedDocument pd = new ParsedDocument(is, filename.toString(), CT_TYPE, startTime.getTime(), OPERATION, FILE_DESC + startTime.toString());
        Document doc = mbox.createDocument(octxt, folder.getId(), pd, MailItem.Type.DOCUMENT, 0);
        ZimbraLog.contactbackup.debug("contact backup created size %d bytes", doc.getSize());
    } catch (UnsupportedOperationException | IOException | ServiceException exception) {
        success = false;
        ZimbraLog.contactbackup.warn("contact export failed, continuing to next mailbox");
        ZimbraLog.contactbackup.debug(exception);
    } catch (OutOfMemoryError e) {
        Zimbra.halt("OutOfMemoryError while creating contact backup", e);
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException ioe) {
                ZimbraLog.contactbackup.debug("IOExcepion occured while closing stream", ioe);
            }
        }
        if (token != null) {
            try {
                token.deRegister();
            } catch (AuthTokenException e) {
                ZimbraLog.contactbackup.warn("failed to deregister token");
                ZimbraLog.contactbackup.debug(e);
            }
        }
    }
}
Also used : FolderOptions(com.zimbra.cs.mailbox.Folder.FolderOptions) InputStream(java.io.InputStream) ZimbraAuthToken(com.zimbra.cs.account.ZimbraAuthToken) IOException(java.io.IOException) ParsedDocument(com.zimbra.cs.mime.ParsedDocument) Date(java.util.Date) ZMailbox(com.zimbra.client.ZMailbox) ParsedDocument(com.zimbra.cs.mime.ParsedDocument) ServiceException(com.zimbra.common.service.ServiceException) AuthTokenException(com.zimbra.cs.account.AuthTokenException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 22 with ZMailbox

use of com.zimbra.client.ZMailbox in project zm-mailbox by Zimbra.

the class TestDataSource method testBadDataSource.

/**
 * Tests {@link ZMailbox#testDataSource}.
 */
@Test
public void testBadDataSource() throws Exception {
    ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
    // Create data source
    Provisioning prov = Provisioning.getInstance();
    Map<String, Object> attrs = new HashMap<String, Object>();
    attrs.put(Provisioning.A_zimbraDataSourceEnabled, LdapConstants.LDAP_FALSE);
    attrs.put(Provisioning.A_zimbraDataSourceHost, "testhost");
    attrs.put(Provisioning.A_zimbraDataSourcePort, "0");
    attrs.put(Provisioning.A_zimbraDataSourceUsername, "testuser");
    attrs.put(Provisioning.A_zimbraDataSourcePassword, "testpass");
    attrs.put(Provisioning.A_zimbraDataSourceFolderId, "1");
    attrs.put(Provisioning.A_zimbraDataSourceConnectionType, ConnectionType.cleartext.toString());
    prov.createDataSource(account, DataSourceType.pop3, NAME_PREFIX + DS_NAME, attrs);
    ZDataSource zds = TestUtil.getDataSource(mbox, NAME_PREFIX + DS_NAME);
    String testVal = mbox.testDataSource(zds);
    assertNotNull("ZMailbox::testDataSource should return an error", testVal);
}
Also used : ZMailbox(com.zimbra.client.ZMailbox) HashMap(java.util.HashMap) Provisioning(com.zimbra.cs.account.Provisioning) ZDataSource(com.zimbra.client.ZDataSource) Test(org.junit.Test)

Example 23 with ZMailbox

use of com.zimbra.client.ZMailbox in project zm-mailbox by Zimbra.

the class TestDataSource method testErrorStatus.

/**
 * Tests the <tt>lastError</tt> element and <tt>failingSince</tt> attribute
 * for <tt>GetInfoRequest</tt> and <tt>GetDataSourcesRequest</tt>.
 */
@Test
public void testErrorStatus() throws Exception {
    // Create data source.
    Account testAccount = TestUtil.getAccount(TEST_USER_NAME);
    Provisioning prov = Provisioning.getInstance();
    Map<String, Object> attrs = new HashMap<String, Object>();
    attrs.put(Provisioning.A_zimbraDataSourceEnabled, LdapConstants.LDAP_TRUE);
    attrs.put(Provisioning.A_zimbraDataSourceHost, "localhost");
    attrs.put(Provisioning.A_zimbraDataSourcePort, TestUtil.getServerAttr(Provisioning.A_zimbraPop3BindPort));
    attrs.put(Provisioning.A_zimbraDataSourceUsername, USER_NAME_2);
    attrs.put(Provisioning.A_zimbraDataSourcePassword, TestUtil.DEFAULT_PASSWORD);
    attrs.put(Provisioning.A_zimbraDataSourceFolderId, Integer.toString(Mailbox.ID_FOLDER_INBOX));
    attrs.put(Provisioning.A_zimbraDataSourceConnectionType, ConnectionType.cleartext.toString());
    attrs.put(Provisioning.A_zimbraDataSourceLeaveOnServer, LdapConstants.LDAP_TRUE);
    DataSource ds = prov.createDataSource(testAccount, DataSourceType.pop3, DS_NAME, attrs);
    // Make sure error status is not set.
    ZMailbox mbox = TestUtil.getZMailbox(TEST_USER_NAME);
    confirmErrorStatus(mbox, null);
    // Invoke data source sync and make sure error status is not set.
    ZDataSource zds = TestUtil.getDataSource(mbox, DS_NAME);
    TestUtil.importDataSource(zds, mbox, null, true);
    confirmErrorStatus(mbox, null);
    // Change to an invalid password, make sure error status is set.
    attrs.clear();
    attrs.put(Provisioning.A_zimbraDataSourcePassword, "bogus");
    prov.modifyDataSource(testAccount, ds.getId(), attrs);
    Thread.sleep(500);
    zds = TestUtil.getDataSource(mbox, DS_NAME);
    // timestamp is returned in seconds, not millis
    long startTimestamp = System.currentTimeMillis() / 1000;
    TestUtil.importDataSource(zds, mbox, null, false);
    confirmErrorStatus(mbox, startTimestamp);
    // Fix password, make sure that error status is reset (bug 39050).
    attrs.put(Provisioning.A_zimbraDataSourcePassword, TestUtil.DEFAULT_PASSWORD);
    prov.modifyDataSource(testAccount, ds.getId(), attrs);
    Thread.sleep(500);
    confirmErrorStatus(mbox, null);
    // Do another sync, make sure error password is not set.
    zds = TestUtil.getDataSource(mbox, DS_NAME);
    startTimestamp = System.currentTimeMillis();
    TestUtil.importDataSource(zds, mbox, null, true);
    confirmErrorStatus(mbox, null);
}
Also used : Account(com.zimbra.cs.account.Account) ZMailbox(com.zimbra.client.ZMailbox) HashMap(java.util.HashMap) Provisioning(com.zimbra.cs.account.Provisioning) ZDataSource(com.zimbra.client.ZDataSource) ZDataSource(com.zimbra.client.ZDataSource) ZCalDataSource(com.zimbra.client.ZCalDataSource) ZRssDataSource(com.zimbra.client.ZRssDataSource) DataSource(com.zimbra.cs.account.DataSource) MailPop3DataSource(com.zimbra.soap.mail.type.MailPop3DataSource) Test(org.junit.Test)

Example 24 with ZMailbox

use of com.zimbra.client.ZMailbox in project zm-mailbox by Zimbra.

the class TestDataSource method testPop3.

@Test
public void testPop3() throws Exception {
    Account pop3acct = TestUtil.getAccount(TEST_USER_NAME);
    ZMailbox pop3mbox = TestUtil.getZMailbox(TEST_USER_NAME);
    ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
    String pop3DSFolder = NAME_PREFIX + " testPop3 source";
    String pop3DSFolderId = createFolderForDataSource(mbox, pop3DSFolder);
    String dsId = createPop3DataSource(mbox, pop3acct.getName(), pop3DSFolderId);
    String subj = NAME_PREFIX + " testtrashpop3";
    addMessage(pop3mbox, subj, "test");
    refreshPop3DatasourceData(mbox, dsId);
    ZSearchParams params = new ZSearchParams(String.format("subject:\"%s\"", subj));
    params.setTypes("MESSAGE");
    ZSearchResult result = mbox.search(params);
    ZSearchHit hit = result.getHits().get(0);
    String id = hit.getId();
    try {
        mbox.trashMessage(id);
    } catch (SoapFaultException sfe) {
        fail("SoapFaultException caught when deleting item from Pop3 datasource folder - " + sfe.getMessage());
    }
    params = new ZSearchParams("in:Trash");
    params.setTypes("MESSAGE");
    result = mbox.search(params);
    List<ZSearchHit> hits = result.getHits();
    assertEquals(1, hits.size());
    assertEquals(id, hits.get(0).getId());
}
Also used : ZSearchResult(com.zimbra.client.ZSearchResult) Account(com.zimbra.cs.account.Account) ZSearchHit(com.zimbra.client.ZSearchHit) ZMailbox(com.zimbra.client.ZMailbox) ZSearchParams(com.zimbra.client.ZSearchParams) SoapFaultException(com.zimbra.common.soap.SoapFaultException) Test(org.junit.Test)

Example 25 with ZMailbox

use of com.zimbra.client.ZMailbox in project zm-mailbox by Zimbra.

the class TestFilterExisting method runRules.

/**
 * Runs filter rules on existing messages.
 *
 * @param ruleNames names of rules to execute
 * @param idList comma-separated list of message id's, or <tt>null</tt> if
 * the query should be used
 * @param query a search query, or <tt>null</tt> if the id list should be used
 * @return the affected message id's
 */
private Set<String> runRules(String[] ruleNames, String idList, String query) throws Exception {
    ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
    // Assemble request.
    Element request = new XMLElement(MailConstants.APPLY_FILTER_RULES_REQUEST);
    Element rulesEl = request.addElement(MailConstants.E_FILTER_RULES);
    for (String ruleName : ruleNames) {
        rulesEl.addElement(MailConstants.E_FILTER_RULE).addAttribute(MailConstants.A_NAME, ruleName);
    }
    if (idList != null) {
        request.addElement(MailConstants.E_MSG).addAttribute(MailConstants.A_IDS, idList);
    }
    if (query != null) {
        request.addElement(MailConstants.E_QUERY).setText(query);
    }
    // Invoke and parse response.
    Element response = mbox.invoke(request);
    Set<String> affectedIds = new TreeSet<String>();
    Element msgEl = response.getOptionalElement(MailConstants.E_MSG);
    if (msgEl != null) {
        for (String id : msgEl.getAttribute(MailConstants.A_IDS).split(",")) {
            affectedIds.add(id);
        }
    }
    return affectedIds;
}
Also used : ZMailbox(com.zimbra.client.ZMailbox) TreeSet(java.util.TreeSet) Element(com.zimbra.common.soap.Element) XMLElement(com.zimbra.common.soap.Element.XMLElement) XMLElement(com.zimbra.common.soap.Element.XMLElement)

Aggregations

ZMailbox (com.zimbra.client.ZMailbox)383 Test (org.junit.Test)288 ZFolder (com.zimbra.client.ZFolder)90 ZMessage (com.zimbra.client.ZMessage)82 Mailbox (com.zimbra.cs.mailbox.Mailbox)61 Account (com.zimbra.cs.account.Account)60 ServiceException (com.zimbra.common.service.ServiceException)55 ArrayList (java.util.ArrayList)38 IOException (java.io.IOException)35 MessageData (com.zimbra.cs.mailclient.imap.MessageData)28 ZMountpoint (com.zimbra.client.ZMountpoint)26 Message (com.zimbra.cs.mailbox.Message)24 HashMap (java.util.HashMap)23 ZOutgoingMessage (com.zimbra.client.ZMailbox.ZOutgoingMessage)21 ZTag (com.zimbra.client.ZTag)21 SoapFaultException (com.zimbra.common.soap.SoapFaultException)21 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)21 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)20 HttpClient (org.apache.http.client.HttpClient)19 ZSearchParams (com.zimbra.client.ZSearchParams)18