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

View File

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

View File

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