use of com.zimbra.cs.account.DataSource in project zm-mailbox by Zimbra.
the class CalDavDataImport method pushModify.
private void pushModify(MailItem mitem) throws ServiceException, IOException, DavException {
int itemId = mitem.getId();
DataSource ds = getDataSource();
DataSourceItem item = DbDataSource.getMapping(ds, itemId);
boolean isCreate = false;
if (item.remoteId == null) {
// new item
item.md = new Metadata();
item.md.put(METADATA_KEY_TYPE, METADATA_TYPE_APPOINTMENT);
item.remoteId = createTargetUrl(mitem);
item.folderId = mitem.getFolderId();
isCreate = true;
}
String type = item.md.get(METADATA_KEY_TYPE);
if (METADATA_TYPE_FOLDER.equals(type)) {
if (mitem.getType() != MailItem.Type.FOLDER) {
ZimbraLog.datasource.warn("pushModify: item type doesn't match in metadata for item %d", itemId);
return;
}
// detect and push rename
} else if (METADATA_TYPE_APPOINTMENT.equals(type)) {
if (mitem.getType() != MailItem.Type.APPOINTMENT) {
ZimbraLog.datasource.warn("pushModify: item type doesn't match in metadata for item %d", itemId);
return;
}
// push modified appt
ZimbraLog.datasource.debug("pushModify: sending appointment %s", item.remoteId);
String etag = putAppointment((CalendarItem) mitem, item);
if (etag == null) {
Appointment appt = mClient.getEtag(item.remoteId);
etag = appt.etag;
}
item.md.put(METADATA_KEY_ETAG, etag);
if (isCreate) {
DbDataSource.addMapping(ds, item);
} else {
DbDataSource.updateMapping(ds, item);
}
} else {
ZimbraLog.datasource.warn("pushModify: unrecognized item type for %d: %s", itemId, type);
return;
}
}
use of com.zimbra.cs.account.DataSource in project zm-mailbox by Zimbra.
the class CalDavDataImport method applyRemoteItem.
private MailItem applyRemoteItem(RemoteItem remoteItem, Folder where) throws ServiceException, IOException {
if (!(remoteItem instanceof RemoteCalendarItem)) {
ZimbraLog.datasource.warn("applyRemoteItem: not a calendar item: %s", remoteItem);
return null;
}
RemoteCalendarItem item = (RemoteCalendarItem) remoteItem;
DataSource ds = getDataSource();
DataSourceItem dsItem = DbDataSource.getReverseMapping(ds, item.href);
OperationContext octxt = new OperationContext(mbox);
MailItem mi = null;
boolean isStale = false;
boolean isCreate = false;
if (dsItem.md == null && item.status != Status.deleted) {
dsItem.md = new Metadata();
dsItem.md.put(METADATA_KEY_TYPE, METADATA_TYPE_APPOINTMENT);
}
if (dsItem.itemId == 0) {
isStale = true;
isCreate = true;
} else {
String etag = dsItem.md.get(METADATA_KEY_ETAG, null);
try {
mi = mbox.getItemById(octxt, dsItem.itemId, MailItem.Type.UNKNOWN);
} catch (MailServiceException.NoSuchItemException se) {
ZimbraLog.datasource.warn("applyRemoteItem: calendar item not found: ", remoteItem);
}
if (item.etag == null) {
ZimbraLog.datasource.warn("No Etag returned for item %s", item.href);
isStale = true;
} else if (etag == null) {
ZimbraLog.datasource.warn("Empty etag for item %d", dsItem.itemId);
isStale = true;
} else {
isStale = !item.etag.equals(etag);
}
if (mi == null)
isStale = true;
}
if (item.status == Status.deleted) {
ZimbraLog.datasource.debug("Deleting appointment %s", item.href);
try {
mi = mbox.getItemById(octxt, item.itemId, MailItem.Type.UNKNOWN);
} catch (NoSuchItemException se) {
mi = null;
}
try {
mbox.delete(octxt, item.itemId, MailItem.Type.UNKNOWN);
} catch (ServiceException se) {
ZimbraLog.datasource.warn("Error deleting remotely deleted item %d (%s)", item.itemId, dsItem.remoteId);
}
} else if (isStale) {
ZimbraLog.datasource.debug("Updating stale appointment %s", item.href);
ZCalendar.ZVCalendar vcalendar;
SetCalendarItemData main = new SetCalendarItemData();
SetCalendarItemData[] exceptions = null;
CalDavClient client = null;
try {
client = getClient();
} catch (DavException e) {
throw ServiceException.FAILURE("error creating CalDAV client", e);
}
Appointment appt = client.getCalendarData(new Appointment(item.href, item.etag));
if (appt.data == null) {
ZimbraLog.datasource.warn("No appointment found at " + item.href);
return null;
}
dsItem.md.put(METADATA_KEY_ETAG, appt.etag);
try {
vcalendar = ZCalendar.ZCalendarBuilder.build(appt.data);
List<Invite> invites = Invite.createFromCalendar(mbox.getAccount(), null, vcalendar, true);
if (invites.size() > 1)
exceptions = new SetCalendarItemData[invites.size() - 1];
int pos = 0;
boolean first = true;
for (Invite i : invites) {
if (first) {
main.invite = i;
first = false;
} else {
SetCalendarItemData scid = new SetCalendarItemData();
scid.invite = i;
exceptions[pos++] = scid;
}
}
} catch (Exception e) {
ZimbraLog.datasource.warn("Error parsing appointment ", e);
return null;
}
mi = mbox.setCalendarItem(octxt, where.getId(), 0, null, main, exceptions, null, CalendarItem.NEXT_ALARM_KEEP_CURRENT);
dsItem.itemId = mi.getId();
dsItem.folderId = mi.getFolderId();
if (isCreate) {
DbDataSource.addMapping(ds, dsItem);
} else {
DbDataSource.updateMapping(ds, dsItem);
}
} else {
ZimbraLog.datasource.debug("Appointment up to date %s", item.href);
try {
mi = mbox.getItemById(octxt, dsItem.itemId, MailItem.Type.UNKNOWN);
} catch (NoSuchItemException se) {
// item not found. delete the mapping so it can be downloaded again if needed.
ArrayList<Integer> deletedIds = new ArrayList<Integer>();
deletedIds.add(dsItem.itemId);
DbDataSource.deleteMappings(ds, deletedIds);
}
}
return mi;
}
use of com.zimbra.cs.account.DataSource in project zm-mailbox by Zimbra.
the class ModifyDataSource method handle.
@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException, SoapFaultException {
ZimbraSoapContext zsc = getZimbraSoapContext(context);
Provisioning prov = Provisioning.getInstance();
ModifyDataSourceRequest req = JaxbUtil.elementToJaxb(request);
String id = req.getId();
if (null == id) {
throw ServiceException.INVALID_REQUEST("missing required attribute: " + AdminConstants.E_ID, null);
}
Account account = prov.get(AccountBy.id, id, zsc.getAuthToken());
defendAgainstAccountOrCalendarResourceHarvesting(account, AccountBy.id, id, zsc, Admin.R_adminLoginAs, Admin.R_adminLoginCalendarResourceAs);
DataSourceInfo dataSource = req.getDataSource();
Map<String, Object> attrs = dataSource.getAttrsAsOldMultimap();
String dsId = dataSource.getId();
DataSource ds = prov.get(account, Key.DataSourceBy.id, dsId);
if (ds == null) {
throw ServiceException.INVALID_REQUEST("Cannot find data source with id=" + dsId, null);
}
DataSourceType type = ds.getType();
// Note: isDomainAdminOnly *always* returns false for pure ACL based AccessManager
if (isDomainAdminOnly(zsc)) {
// yuck, can't really integrate into AdminDocumentHandler methods
// have to check separately here
AttributeClass klass = ModifyDataSource.getAttributeClassFromType(type);
checkModifyAttrs(zsc, klass, attrs);
}
ZimbraLog.addDataSourceNameToContext(ds.getName());
prov.modifyDataSource(account, dsId, attrs);
Element response = zsc.createElement(AdminConstants.MODIFY_DATA_SOURCE_RESPONSE);
return response;
}
use of com.zimbra.cs.account.DataSource in project zm-mailbox by Zimbra.
the class ModifyDataSource method handle.
@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
ZimbraSoapContext zsc = getZimbraSoapContext(context);
Provisioning prov = Provisioning.getInstance();
Account account = getRequestedAccount(zsc);
if (!canModifyOptions(zsc, account))
throw ServiceException.PERM_DENIED("can not modify options");
//boolean wipeOutOldData = false;
Element eDataSource = CreateDataSource.getDataSourceElement(request);
DataSourceType type = DataSourceType.fromString(eDataSource.getName());
String id = eDataSource.getAttribute(MailConstants.A_ID);
DataSource ds = prov.get(account, Key.DataSourceBy.id, id);
if (ds == null) {
throw ServiceException.INVALID_REQUEST("Unable to find data source with id=" + id, null);
}
Map<String, Object> dsAttrs = new HashMap<String, Object>();
String value = eDataSource.getAttribute(MailConstants.A_NAME, null);
if (value != null) {
dsAttrs.put(Provisioning.A_zimbraDataSourceName, value);
}
value = eDataSource.getAttribute(MailConstants.A_DS_IS_ENABLED, null);
if (value != null) {
dsAttrs.put(Provisioning.A_zimbraDataSourceEnabled, LdapUtil.getLdapBooleanString(eDataSource.getAttributeBool(MailConstants.A_DS_IS_ENABLED)));
}
value = eDataSource.getAttribute(MailConstants.A_FOLDER, null);
if (value != null) {
Mailbox mbox = getRequestedMailbox(zsc);
CreateDataSource.validateFolderId(account, mbox, eDataSource, type);
dsAttrs.put(Provisioning.A_zimbraDataSourceFolderId, value);
}
value = eDataSource.getAttribute(MailConstants.A_DS_HOST, null);
if (value != null && !value.equals(ds.getHost())) {
dsAttrs.put(Provisioning.A_zimbraDataSourceHost, value);
}
value = eDataSource.getAttribute(MailConstants.A_DS_PORT, null);
if (value != null)
dsAttrs.put(Provisioning.A_zimbraDataSourcePort, value);
value = eDataSource.getAttribute(MailConstants.A_DS_CONNECTION_TYPE, null);
if (value != null)
dsAttrs.put(Provisioning.A_zimbraDataSourceConnectionType, value);
value = eDataSource.getAttribute(MailConstants.A_DS_USERNAME, null);
if (value != null && !value.equals(ds.getUsername())) {
dsAttrs.put(Provisioning.A_zimbraDataSourceUsername, value);
}
value = eDataSource.getAttribute(MailConstants.A_DS_PASSWORD, null);
if (value != null) {
dsAttrs.put(Provisioning.A_zimbraDataSourcePassword, value);
}
value = eDataSource.getAttribute(MailConstants.A_DS_OAUTH_TOKEN, null);
if (value != null) {
dsAttrs.put(Provisioning.A_zimbraDataSourceOAuthToken, value);
dsAttrs.put(Provisioning.A_zimbraDataSourceAuthMechanism, ZAttrProvisioning.DataSourceAuthMechanism.XOAUTH2.name());
}
value = eDataSource.getAttribute(MailConstants.A_DS_CLIENT_ID, null);
if (value != null) {
dsAttrs.put(Provisioning.A_zimbraDataSourceOAuthClientId, value);
}
value = eDataSource.getAttribute(MailConstants.A_DS_CLIENT_SECRET, null);
if (value != null) {
dsAttrs.put(Provisioning.A_zimbraDataSourceOAuthClientSecret, value);
}
value = eDataSource.getAttribute(MailConstants.A_DS_REFRESH_TOKEN, null);
if (value != null) {
dsAttrs.put(Provisioning.A_zimbraDataSourceOAuthRefreshToken, value);
}
value = eDataSource.getAttribute(MailConstants.A_DS_REFRESH_TOKEN_URL, null);
if (value != null) {
dsAttrs.put(Provisioning.A_zimbraDataSourceOAuthRefreshTokenUrl, value);
}
value = eDataSource.getAttribute(MailConstants.A_DS_LEAVE_ON_SERVER, null);
if (value != null) {
if (type != DataSourceType.pop3) {
String msg = String.format("%s only allowed for %s data sources", MailConstants.A_DS_LEAVE_ON_SERVER, MailConstants.E_DS_POP3);
throw ServiceException.INVALID_REQUEST(msg, null);
}
boolean newValue = eDataSource.getAttributeBool(MailConstants.A_DS_LEAVE_ON_SERVER);
if (newValue != ds.leaveOnServer()) {
dsAttrs.put(Provisioning.A_zimbraDataSourceLeaveOnServer, LdapUtil.getLdapBooleanString(newValue));
Mailbox mbox = getRequestedMailbox(zsc);
DbPop3Message.deleteUids(mbox, ds.getId());
DbDataSource.deleteAllMappings(ds);
}
}
value = eDataSource.getAttribute(MailConstants.A_DS_POLLING_INTERVAL, null);
if (value != null) {
dsAttrs.put(Provisioning.A_zimbraDataSourcePollingInterval, value);
}
// import class
String importClass = eDataSource.getAttribute(MailConstants.A_DS_IMPORT_CLASS, DataSourceManager.getDefaultImportClass(type));
if (importClass != null) {
dsAttrs.put(Provisioning.A_zimbraDataSourceImportClassName, importClass);
}
value = eDataSource.getAttribute(MailConstants.A_DS_SMTP_ENABLED, null);
if (value != null) {
dsAttrs.put(Provisioning.A_zimbraDataSourceSmtpEnabled, LdapUtil.getLdapBooleanString(eDataSource.getAttributeBool(MailConstants.A_DS_SMTP_ENABLED)));
}
value = eDataSource.getAttribute(MailConstants.A_DS_SMTP_HOST, null);
if (value != null) {
dsAttrs.put(Provisioning.A_zimbraDataSourceSmtpHost, value);
}
value = eDataSource.getAttribute(MailConstants.A_DS_SMTP_PORT, null);
if (value != null) {
dsAttrs.put(Provisioning.A_zimbraDataSourceSmtpPort, value);
}
value = eDataSource.getAttribute(MailConstants.A_DS_SMTP_CONNECTION_TYPE, null);
if (value != null) {
dsAttrs.put(Provisioning.A_zimbraDataSourceSmtpConnectionType, value);
}
value = eDataSource.getAttribute(MailConstants.A_DS_SMTP_AUTH_REQUIRED, null);
if (value != null) {
dsAttrs.put(Provisioning.A_zimbraDataSourceSmtpAuthRequired, LdapUtil.getLdapBooleanString(eDataSource.getAttributeBool(MailConstants.A_DS_SMTP_AUTH_REQUIRED)));
}
value = eDataSource.getAttribute(MailConstants.A_DS_SMTP_USERNAME, null);
if (value != null) {
dsAttrs.put(Provisioning.A_zimbraDataSourceSmtpAuthUsername, value);
}
value = eDataSource.getAttribute(MailConstants.A_DS_SMTP_PASSWORD, null);
if (value != null) {
dsAttrs.put(Provisioning.A_zimbraDataSourceSmtpAuthPassword, value);
}
if (eDataSource.getAttributeBool(MailConstants.A_DS_TEST, false)) {
TestDataSource.testDataSourceConnection(prov, eDataSource, type, account);
}
processCommonOptionalAttrs(dsAttrs, eDataSource);
prov.modifyDataSource(account, id, dsAttrs);
Element response = zsc.createElement(MailConstants.MODIFY_DATA_SOURCE_RESPONSE);
return response;
}
use of com.zimbra.cs.account.DataSource in project zm-mailbox by Zimbra.
the class ImportData method handle.
@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
ZimbraSoapContext zsc = getZimbraSoapContext(context);
Provisioning prov = Provisioning.getInstance();
Account account = getRequestedAccount(zsc);
for (Element elem : request.listElements()) {
DataSource ds;
String name, id = elem.getAttribute(MailConstants.A_ID, null);
if (id != null) {
ds = prov.get(account, Key.DataSourceBy.id, id);
if (ds == null) {
throw ServiceException.INVALID_REQUEST("Could not find Data Source with id " + id, null);
}
} else if ((name = elem.getAttribute(MailConstants.A_NAME, null)) != null) {
ds = prov.get(account, Key.DataSourceBy.name, name);
if (ds == null) {
throw ServiceException.INVALID_REQUEST("Could not find Data Source with name " + name, null);
}
} else {
throw ServiceException.INVALID_REQUEST("must specify either 'id' or 'name'", null);
}
ZimbraLog.addDataSourceNameToContext(ds.getName());
DataSourceManager.asyncImportData(ds);
}
Element response = zsc.createElement(MailConstants.IMPORT_DATA_RESPONSE);
return response;
}
Aggregations