diff --git a/src/java/davmail/exchange/dav/DavExchangeSession.java b/src/java/davmail/exchange/dav/DavExchangeSession.java index 05493a1c..ebdf7e62 100644 --- a/src/java/davmail/exchange/dav/DavExchangeSession.java +++ b/src/java/davmail/exchange/dav/DavExchangeSession.java @@ -856,7 +856,7 @@ public class DavExchangeSession extends ExchangeSession { LOGGER.warn("Unable to patch event to trigger activeSync push"); } else { // need to retrieve new etag - Item newItem = getItem(getHref()); + Item newItem = getItem(folderPath, itemName); itemResult.etag = newItem.etag; } } @@ -935,7 +935,6 @@ public class DavExchangeSession extends ExchangeSession { for (String attribute : FOLDER_PROPERTIES) { FOLDER_PROPERTIES_NAME_SET.add(Field.getPropertyName(attribute)); } - } /** @@ -1191,21 +1190,62 @@ public class DavExchangeSession extends ExchangeSession { httpClient, URIUtil.encodePath(folderUrl), searchRequest.toString()); } - - protected static final DavPropertyNameSet EVENT_REQUEST_PROPERTIES = new DavPropertyNameSet(); + protected static final Set EVENT_REQUEST_PROPERTIES = new HashSet(); static { - EVENT_REQUEST_PROPERTIES.add(Field.getPropertyName("permanenturl")); - EVENT_REQUEST_PROPERTIES.add(Field.getPropertyName("urlcompname")); - EVENT_REQUEST_PROPERTIES.add(Field.getPropertyName("etag")); - EVENT_REQUEST_PROPERTIES.add(Field.getPropertyName("contentclass")); - EVENT_REQUEST_PROPERTIES.add(Field.getPropertyName("displayname")); + EVENT_REQUEST_PROPERTIES.add("permanenturl"); + EVENT_REQUEST_PROPERTIES.add("urlcompname"); + EVENT_REQUEST_PROPERTIES.add("etag"); + EVENT_REQUEST_PROPERTIES.add("contentclass"); + EVENT_REQUEST_PROPERTIES.add("displayname"); + } + + protected static final DavPropertyNameSet EVENT_REQUEST_PROPERTIES_NAME_SET = new DavPropertyNameSet(); + static { + for (String attribute : EVENT_REQUEST_PROPERTIES) { + EVENT_REQUEST_PROPERTIES_NAME_SET.add(Field.getPropertyName(attribute)); + } + } @Override public Item getItem(String folderPath, String itemName) throws IOException { - String itemPath = getFolderPath(folderPath) + '/' + convertItemNameToEML(itemName); - return getItem(itemPath); + String emlItemName = convertItemNameToEML(itemName); + String itemPath = getFolderPath(folderPath) + '/' + emlItemName; + MultiStatusResponse[] responses; + try { + responses = DavGatewayHttpClientFacade.executePropFindMethod(httpClient, URIUtil.encodePath(itemPath), 0, EVENT_REQUEST_PROPERTIES_NAME_SET); + if (responses.length == 0) { + throw new HttpNotFoundException(itemPath); + } + } catch (HttpNotFoundException e) { + LOGGER.debug(itemPath +" not found, searching by urlcompname"); + // failover: try to get event by displayname + responses = searchItems(folderPath, EVENT_REQUEST_PROPERTIES, equals("urlcompname", emlItemName), FolderQueryTraversal.Shallow); + if (responses.length == 0) { + throw new HttpNotFoundException(itemPath); + } + } + // build item + String contentClass = getPropertyIfExists(responses[0].getProperties(HttpStatus.SC_OK), "contentclass"); + String urlcompname = getPropertyIfExists(responses[0].getProperties(HttpStatus.SC_OK), "urlcompname"); + if ("urn:content-classes:person".equals(contentClass)) { + // retrieve Contact properties + List contacts = searchContacts(folderPath, CONTACT_ATTRIBUTES, equals("urlcompname", urlcompname)); + if (contacts.isEmpty()) { + LOGGER.warn("Item found, but unable to build contact"); + throw new HttpNotFoundException(itemPath); + } + return contacts.get(0); + } else if ("urn:content-classes:appointment".equals(contentClass) + || "urn:content-classes:calendarmessage".equals(contentClass)) { + return new Event(responses[0]); + } else { + LOGGER.warn("wrong contentclass on item "+itemPath+": "+contentClass); + // return item anyway + return new Event(responses[0]); + } + } @Override @@ -1283,35 +1323,6 @@ public class DavExchangeSession extends ExchangeSession { DavGatewayHttpClientFacade.executeMethod(httpClient, patchMethod); } - /** - * Get item by url - * - * @param itemPath Event path - * @return event object - * @throws IOException on error - */ - public Item getItem(String itemPath) throws IOException { - MultiStatusResponse[] responses = DavGatewayHttpClientFacade.executePropFindMethod(httpClient, URIUtil.encodePath(itemPath), 0, EVENT_REQUEST_PROPERTIES); - if (responses.length == 0) { - throw new DavMailException("EXCEPTION_ITEM_NOT_FOUND"); - } - String contentClass = getPropertyIfExists(responses[0].getProperties(HttpStatus.SC_OK), "contentclass"); - String urlcompname = getPropertyIfExists(responses[0].getProperties(HttpStatus.SC_OK), "urlcompname"); - if ("urn:content-classes:person".equals(contentClass)) { - // retrieve Contact properties - List contacts = searchContacts(itemPath.substring(0, itemPath.lastIndexOf('/')), CONTACT_ATTRIBUTES, equals("urlcompname", urlcompname)); - if (contacts.isEmpty()) { - throw new DavMailException("EXCEPTION_ITEM_NOT_FOUND"); - } - return contacts.get(0); - } else if ("urn:content-classes:appointment".equals(contentClass) - || "urn:content-classes:calendarmessage".equals(contentClass)) { - return new Event(responses[0]); - } else { - throw new DavMailException("EXCEPTION_ITEM_NOT_FOUND"); - } - } - @Override public ItemResult internalCreateOrUpdateEvent(String folderPath, String itemName, String contentClass, String icsBody, String etag, String noneMatch) throws IOException { return new Event(getFolderPath(folderPath), itemName, contentClass, icsBody, etag, noneMatch).createOrUpdate(); diff --git a/src/test/davmail/AbstractDavMailTestCase.java b/src/test/davmail/AbstractDavMailTestCase.java index 7379aa7e..9139556b 100644 --- a/src/test/davmail/AbstractDavMailTestCase.java +++ b/src/test/davmail/AbstractDavMailTestCase.java @@ -22,6 +22,10 @@ import davmail.exchange.ExchangeSession; import davmail.http.DavGatewaySSLProtocolSocketFactory; import junit.framework.TestCase; +import javax.mail.MessagingException; +import javax.mail.Session; +import javax.mail.internet.MimeMessage; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; @@ -70,4 +74,18 @@ public class AbstractDavMailTestCase extends TestCase { } } + + protected MimeMessage createMimeMessage() throws MessagingException { + MimeMessage mimeMessage = new MimeMessage((Session) null); + mimeMessage.addHeader("To", "test@test.local"); + mimeMessage.setText("Test message"); + mimeMessage.setSubject("Test subject"); + return mimeMessage; + } + + protected byte[] getMimeBody(MimeMessage mimeMessage) throws IOException, MessagingException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + mimeMessage.writeTo(baos); + return baos.toByteArray(); + } } diff --git a/src/test/davmail/exchange/AbstractExchangeSessionTestCase.java b/src/test/davmail/exchange/AbstractExchangeSessionTestCase.java index b8a982ac..9c765f67 100644 --- a/src/test/davmail/exchange/AbstractExchangeSessionTestCase.java +++ b/src/test/davmail/exchange/AbstractExchangeSessionTestCase.java @@ -23,10 +23,6 @@ import davmail.Settings; import davmail.exchange.dav.DavExchangeSession; import davmail.exchange.ews.EwsExchangeSession; -import javax.mail.MessagingException; -import javax.mail.Session; -import javax.mail.internet.MimeMessage; -import java.io.ByteArrayOutputStream; import java.io.IOException; /** @@ -51,18 +47,4 @@ public class AbstractExchangeSessionTestCase extends AbstractDavMailTestCase { } } - protected MimeMessage createMimeMessage() throws MessagingException { - MimeMessage mimeMessage = new MimeMessage((Session) null); - mimeMessage.addHeader("To", "test@test.local"); - mimeMessage.setText("Test message"); - mimeMessage.setSubject("Test subject"); - return mimeMessage; - } - - protected byte[] getMimeBody(MimeMessage mimeMessage) throws IOException, MessagingException { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - mimeMessage.writeTo(baos); - return baos.toByteArray(); - } - }