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