Carddav: handle multiple values on a single line and add new properties

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1137 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2010-07-06 10:54:04 +00:00
parent e6ef48c90e
commit 96b816b42b
3 changed files with 63 additions and 37 deletions

View File

@ -22,7 +22,6 @@ import davmail.BundleMessage;
import davmail.Settings;
import davmail.exception.DavMailAuthenticationException;
import davmail.exception.DavMailException;
import davmail.exchange.dav.Field;
import davmail.http.DavGatewayHttpClientFacade;
import davmail.http.DavGatewayOTPPrompt;
import davmail.util.StringUtil;
@ -31,7 +30,6 @@ import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.util.URIUtil;
import org.apache.jackrabbit.webdav.DavConstants;
import org.apache.log4j.Logger;
import org.htmlcleaner.CommentToken;
import org.htmlcleaner.HtmlCleaner;
@ -2626,6 +2624,8 @@ public abstract class ExchangeSession {
}
}
protected static final String[] VCARD_N_PROPERTIES = {"sn", "givenName", "middlename", "personaltitle", "namesuffix"};
protected ItemResult createOrUpdateContact(String folderPath, String itemName, String itemBody, String etag, String noneMatch) throws IOException {
// parse VCARD body to build contact property map
Map<String, String> properties = new HashMap<String, String>();
@ -2640,13 +2640,12 @@ public abstract class ExchangeSession {
properties.put("fileas", property.getValue());
} else if ("N".equals(property.getKey())) {
String[] values = property.getValues();
if (values.length > 0) {
properties.put("sn", values[0]);
}
if (values.length > 1) {
properties.put("givenName", values[1]);
List<String> values = property.getValues();
for (int i = 0; i < values.size() && i < VCARD_N_PROPERTIES.length; i++) {
properties.put(VCARD_N_PROPERTIES[i], values.get(i));
}
} else if ("NICKNAME".equals(property.getKey())) {
properties.put("nickname", property.getValue());
} else if ("TEL".equals(property.getKey())) {
if (property.hasParam("TYPE", "cell")) {
properties.put("mobile", property.getValue());

View File

@ -20,10 +20,7 @@ package davmail.exchange;
import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.*;
/**
* VCARD reader.
@ -35,7 +32,7 @@ public class VCardReader extends ICSBufferedReader {
public class Property {
protected String key;
protected Map<String, Set<String>> params;
protected String value;
protected List<String> values;
/**
* Property key, without optional parameters (e.g. TEL).
@ -52,17 +49,20 @@ public class VCardReader extends ICSBufferedReader {
* @return value
*/
public String getValue() {
return value;
if (values == null || values.size() == 0) {
return null;
} else {
return values.get(0);
}
}
/**
* Property values, split on ;.
* Property values.
*
* @return values
*/
public String[] getValues() {
// TODO: handle protected characters
return value.split(";");
public List<String> getValues() {
return values;
}
public boolean hasParam(String paramName, String paramValue) {
@ -75,6 +75,13 @@ public class VCardReader extends ICSBufferedReader {
}
params.put(paramName, paramValues);
}
protected void addValue(String value) {
if (values == null) {
values = new ArrayList<String>();
}
values.add(value);
}
}
/**
@ -87,12 +94,12 @@ public class VCardReader extends ICSBufferedReader {
super(in);
String firstLine = readLine();
if (firstLine == null || !"BEGIN:VCARD".equals(firstLine)) {
throw new IOException("Invalid VCard body: "+firstLine);
throw new IOException("Invalid VCard body: " + firstLine);
}
}
protected static enum State {
KEY, PARAM_NAME, PARAM_VALUE, VALUE
KEY, PARAM_NAME, PARAM_VALUE, VALUE, BACKSLASH
}
public Property readProperty() throws IOException {
@ -138,9 +145,19 @@ public class VCardReader extends ICSBufferedReader {
paramValues.add(line.substring(startIndex, i));
startIndex = i + 1;
}
} else if (state == State.VALUE) {
if (currentChar == ';') {
property.addValue(line.substring(startIndex, i));
startIndex = i + 1;
} else if (currentChar == '\\') {
state = State.BACKSLASH;
}
// state == State.BACKSLASH
} else {
state = State.VALUE;
}
}
property.value = line.substring(startIndex);
property.addValue(line.substring(startIndex));
}
return property;
}

View File

@ -18,26 +18,16 @@
*/
package davmail.exchange;
import davmail.exchange.dav.DavExchangeSession;
import java.io.IOException;
import java.util.*;
import java.util.UUID;
/**
* Test ExchangeSession contact features.
*/
@SuppressWarnings({"UseOfSystemOutOrSystemErr"})
public class TestExchangeSessionContact extends AbstractExchangeSessionTestCase {
@Override
public void setUp() throws IOException {
super.setUp();
// recreate empty folder
session.deleteFolder("testcontactfolder");
session.createContactFolder("testcontactfolder");
}
static String itemName;
/*
public void testSearchContacts() throws IOException {
List<ExchangeSession.Contact> contacts = session.searchContacts(ExchangeSession.CONTACTS, ExchangeSession.CONTACT_ATTRIBUTES, null);
for (ExchangeSession.Contact contact : contacts) {
@ -61,16 +51,36 @@ public class TestExchangeSessionContact extends AbstractExchangeSessionTestCase
for (ExchangeSession.Contact contact : contacts) {
System.out.println(session.searchContacts(ExchangeSession.CONTACTS, attributes, session.equals("uid", contact.get("uid"))));
}
} */
public void testCreateFolder() throws IOException {
// recreate empty folder
session.deleteFolder("testcontactfolder");
session.createContactFolder("testcontactfolder");
}
public void testCreateContact() throws IOException {
itemName = UUID.randomUUID().toString() + ".vcf";
VCardWriter vCardWriter = new VCardWriter();
vCardWriter.startCard();
vCardWriter.appendProperty("N", "surname", "given name", "honorific prefix", "honorific suffix");
vCardWriter.appendProperty("FN", "test name");
vCardWriter.appendProperty("N", "sn", "givenName", "middlename", "personaltitle", "namesuffix");
vCardWriter.appendProperty("FN", "common name");
vCardWriter.appendProperty("NICKNAME", "nickname");
vCardWriter.endCard();
session.createOrUpdateContact("testcontactfolder", UUID.randomUUID().toString() + ".vcf", vCardWriter.toString(), null, null);
session.createOrUpdateContact("testcontactfolder", itemName, vCardWriter.toString(), null, null);
}
public void testGetContact() throws IOException {
ExchangeSession.Contact contact = (ExchangeSession.Contact) session.getItem("testcontactfolder", itemName);
assertEquals("common name", contact.get("cn"));
assertEquals("sn", contact.get("sn"));
assertEquals("givenName", contact.get("givenName"));
assertEquals("middlename", contact.get("middlename"));
assertEquals("personaltitle", contact.get("personaltitle"));
assertEquals("namesuffix", contact.get("namesuffix"));
assertNotNull("lastmodified");
assertEquals("nickname", contact.get("nickname"));
}
}