use of com.zimbra.cs.dav.DavException in project zm-mailbox by Zimbra.
the class UrlNamespace method getCollectionAtUrl.
/* Returns Collection at the specified URL. */
public static Collection getCollectionAtUrl(DavContext ctxt, String url) throws DavException {
UrlComponents uc = parseUrl(url);
int lastPos = uc.path.length() - 1;
if (uc.path.endsWith("/"))
lastPos--;
int index = uc.path.lastIndexOf('/', lastPos);
String path;
if (index == -1)
path = "/";
else
path = uc.path.substring(0, index);
String user = uc.user;
if (user == null)
user = ctxt.getUser();
DavResource rsc = getResourceAt(new DavContext(ctxt, path), user, path);
if (rsc instanceof Collection)
return (Collection) rsc;
throw new DavException("invalid uri", HttpServletResponse.SC_NOT_FOUND, null);
}
use of com.zimbra.cs.dav.DavException in project zm-mailbox by Zimbra.
the class UrlNamespace method getResourceAtUrl.
/* Returns DavResource at the specified URL. */
public static DavResource getResourceAtUrl(DavContext ctxt, String url) throws DavException {
if (url.indexOf(PRINCIPALS_PATH) >= 0)
return getPrincipalAtUrl(ctxt, url);
UrlComponents uc = parseUrl(url);
if (uc.user == null || uc.path == null)
throw new DavException("invalid uri", HttpServletResponse.SC_NOT_FOUND, null);
DavResource rs = getResourceAt(ctxt, uc.user, uc.path);
if (rs != null)
rs.mUri = uc.path;
return rs;
}
use of com.zimbra.cs.dav.DavException in project zm-mailbox by Zimbra.
the class MailItemResource method getMailbox.
protected Mailbox getMailbox(DavContext ctxt) throws ServiceException, DavException {
Provisioning prov = Provisioning.getInstance();
Account account = prov.get(AccountBy.id, mOwnerId);
if (account == null) {
// Anti-account name harvesting.
ZimbraLog.dav.info("Failing GET of mailbox for item resource - no such account '%s'", mOwnerId);
throw new DavException("Request denied", HttpServletResponse.SC_NOT_FOUND, null);
}
return MailboxManager.getInstance().getMailboxByAccount(account);
}
use of com.zimbra.cs.dav.DavException 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.dav.DavException in project zm-mailbox by Zimbra.
the class CalDavClient method login.
public void login(String defaultPrincipalUrl) throws IOException, DavException {
String principalUrl = getCurrentUserPrincipal();
if (principalUrl == null) {
principalUrl = defaultPrincipalUrl;
}
DavRequest propfind = DavRequest.PROPFIND(principalUrl);
propfind.addRequestProp(DavElements.E_DISPLAYNAME);
propfind.addRequestProp(DavElements.E_CALENDAR_HOME_SET);
propfind.addRequestProp(DavElements.E_SCHEDULE_INBOX_URL);
propfind.addRequestProp(DavElements.E_SCHEDULE_OUTBOX_URL);
Collection<DavObject> response = sendMultiResponseRequest(propfind);
if (response.size() != 1) {
throw new DavException(String.format("invalid response to propfind on principal url '%s'", principalUrl), null);
}
DavObject resp = response.iterator().next();
mCalendarHomeSet = new HashSet<String>();
Element homeSet = resp.getProperty(DavElements.E_CALENDAR_HOME_SET);
if (homeSet != null) {
for (Object href : homeSet.elements(DavElements.E_HREF)) {
String hrefVal = ((Element) href).getText();
mCalendarHomeSet.add(hrefVal);
}
}
if (mCalendarHomeSet.isEmpty()) {
throw new DavException("dav response from principal url does not contain calendar-home-set", null);
}
Element elem = resp.getProperty(DavElements.E_SCHEDULE_INBOX_URL);
if (elem != null && elem.element(DavElements.E_HREF) != null) {
mScheduleInbox = elem.element(DavElements.E_HREF).getText();
}
elem = resp.getProperty(DavElements.E_SCHEDULE_OUTBOX_URL);
if (elem != null && elem.element(DavElements.E_HREF) != null) {
mScheduleOutbox = elem.element(DavElements.E_HREF).getText();
}
}
Aggregations