1
0
mirror of https://github.com/moparisthebest/davmail synced 2024-12-13 19:22:22 -05:00

Caldav: Refactor getItem

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1288 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2010-07-26 20:55:51 +00:00
parent f043b80973
commit f8eb0adda7
3 changed files with 69 additions and 58 deletions

View File

@ -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<String> EVENT_REQUEST_PROPERTIES = new HashSet<String>();
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<ExchangeSession.Contact> 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<ExchangeSession.Contact> 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();

View File

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

View File

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