Dav: more property update fixes

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1263 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2010-07-24 13:11:22 +00:00
parent 722be0aeb1
commit 06bd2e9c7d
4 changed files with 44 additions and 52 deletions

View File

@ -2871,7 +2871,7 @@ public abstract class ExchangeSession {
// reset missing properties to null // reset missing properties to null
for (String key : CONTACT_ATTRIBUTES) { for (String key : CONTACT_ATTRIBUTES) {
if (!"imapUid".equals(key) && !"etag".equals(key) && !"urlcompname".equals(key) if (!"imapUid".equals(key) && !"etag".equals(key) && !"urlcompname".equals(key)
&& !"lastmodified".equals(key) && && !"lastmodified".equals(key) && !"sensitivity".equals(key) &&
!properties.containsKey(key)) { !properties.containsKey(key)) {
properties.put(key, null); properties.put(key, null);
} }

View File

@ -41,11 +41,8 @@ import org.apache.jackrabbit.webdav.client.methods.MoveMethod;
import org.apache.jackrabbit.webdav.client.methods.PropFindMethod; import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;
import org.apache.jackrabbit.webdav.client.methods.PropPatchMethod; import org.apache.jackrabbit.webdav.client.methods.PropPatchMethod;
import org.apache.jackrabbit.webdav.property.DavProperty; import org.apache.jackrabbit.webdav.property.DavProperty;
import org.apache.jackrabbit.webdav.property.DavPropertyName;
import org.apache.jackrabbit.webdav.property.DavPropertyNameSet; import org.apache.jackrabbit.webdav.property.DavPropertyNameSet;
import org.apache.jackrabbit.webdav.property.DavPropertySet; import org.apache.jackrabbit.webdav.property.DavPropertySet;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.w3c.dom.Node; import org.w3c.dom.Node;
import javax.mail.MessagingException; import javax.mail.MessagingException;
@ -599,15 +596,15 @@ public class DavExchangeSession extends ExchangeSession {
} }
protected Set<PropertyValue> buildProperties() { protected Set<PropertyValue> buildProperties() {
Set<PropertyValue> list = new HashSet<PropertyValue>(); Set<PropertyValue> propertyValues = new HashSet<PropertyValue>();
for (Map.Entry<String, String> entry : entrySet()) { for (Map.Entry<String, String> entry : entrySet()) {
String key = entry.getKey(); String key = entry.getKey();
if (!"photo".equals(key)) { if (!"photo".equals(key)) {
list.add(Field.createPropertyValue(key, entry.getValue())); propertyValues.add(Field.createPropertyValue(key, entry.getValue()));
} }
} }
return list; return propertyValues;
} }
/** /**
@ -629,13 +626,11 @@ public class DavExchangeSession extends ExchangeSession {
try { try {
status = httpClient.executeMethod(propPatchMethod); status = httpClient.executeMethod(propPatchMethod);
if (status == HttpStatus.SC_MULTI_STATUS) { if (status == HttpStatus.SC_MULTI_STATUS) {
List<MultiStatusResponse> responses = propPatchMethod.getResponses(); status = propPatchMethod.getResponseStatusCode();
//noinspection VariableNotUsedInsideIf //noinspection VariableNotUsedInsideIf
if (etag == null) { if (status == HttpStatus.SC_CREATED) {
status = HttpStatus.SC_CREATED;
LOGGER.debug("Created contact " + getHref()); LOGGER.debug("Created contact " + getHref());
} else { } else {
status = HttpStatus.SC_OK;
LOGGER.debug("Updated contact " + getHref()); LOGGER.debug("Updated contact " + getHref());
} }
} else { } else {
@ -674,52 +669,31 @@ public class DavExchangeSession extends ExchangeSession {
putmethod.releaseConnection(); putmethod.releaseConnection();
} }
ArrayList<DavConstants> changeList = new ArrayList<DavConstants>(); Set<PropertyValue> picturePropertyValues = new HashSet<PropertyValue>();
changeList.add(Field.createDavProperty("attachmentContactPhoto", "true")); picturePropertyValues.add(Field.createPropertyValue("attachmentContactPhoto", "true"));
//changeList.add(Field.createDavProperty("renderingPosition", "-1")); // picturePropertyValues.add(Field.createPropertyValue("renderingPosition", "-1"));
changeList.add(Field.createDavProperty("attachExtension", ".jpg")); picturePropertyValues.add(Field.createPropertyValue("attachExtension", ".jpg"));
final PropPatchMethod attachmentPropPatchMethod = new PropPatchMethod(contactPictureUrl, changeList); final ExchangePropPatchMethod attachmentPropPatchMethod = new ExchangePropPatchMethod(contactPictureUrl, picturePropertyValues);
try { try {
status = httpClient.executeMethod(attachmentPropPatchMethod); status = httpClient.executeMethod(attachmentPropPatchMethod);
if (status != HttpStatus.SC_MULTI_STATUS) { if (status != HttpStatus.SC_MULTI_STATUS) {
LOGGER.error("Error in contact photo create or update: "+attachmentPropPatchMethod.getStatusCode());
throw new IOException("Unable to update contact picture"); throw new IOException("Unable to update contact picture");
} }
} catch (IOException e) {
LOGGER.error("Error in contact photo create or update", e);
throw e;
} finally { } finally {
attachmentPropPatchMethod.releaseConnection(); attachmentPropPatchMethod.releaseConnection();
} }
// update haspicture flag to boolean property
DavPropertyName hasPicturePropertyName = Field.getPropertyName("haspicture");
Set<PropertyValue> propertyValues = new HashSet<PropertyValue>();
propertyValues.add(new PropertyValue(hasPicturePropertyName.getNamespace().getURI(),hasPicturePropertyName.getName(),"1", PropertyType.Boolean));
ExchangePropPatchMethod exchangePropPatchMethod = new ExchangePropPatchMethod(URIUtil.encodePath(getHref()), propertyValues);
try {
status = httpClient.executeMethod(exchangePropPatchMethod);
if (status != HttpStatus.SC_MULTI_STATUS) {
throw new IOException("Unable to update haspicture flag");
}
} catch (IOException e) {
LOGGER.error("Error in haspicture flag update", e);
throw e;
} finally {
exchangePropPatchMethod.releaseConnection();
}
} else { } else {
// try to delete picture // try to delete picture
DeleteMethod deleteMethod = new DeleteMethod(contactPictureUrl); DeleteMethod deleteMethod = new DeleteMethod(contactPictureUrl);
try { try {
status = httpClient.executeMethod(deleteMethod); status = httpClient.executeMethod(deleteMethod);
if (status != HttpStatus.SC_OK && status != HttpStatus.SC_NOT_FOUND) { if (status != HttpStatus.SC_OK && status != HttpStatus.SC_NOT_FOUND) {
LOGGER.error("Error in contact photo delete: "+status);
throw new IOException("Unable to delete contact picture"); throw new IOException("Unable to delete contact picture");
} }
} catch (IOException e) {
LOGGER.error("Error in contact photo delete", e);
throw e;
} finally { } finally {
deleteMethod.releaseConnection(); deleteMethod.releaseConnection();
} }
@ -845,13 +819,10 @@ public class DavExchangeSession extends ExchangeSession {
try { try {
status = httpClient.executeMethod(putmethod); status = httpClient.executeMethod(putmethod);
if (status == HttpURLConnection.HTTP_OK) { if (status == HttpURLConnection.HTTP_OK) {
//noinspection VariableNotUsedInsideIf LOGGER.debug("Updated event " + getHref());
if (etag != null) { } else if (status == HttpURLConnection.HTTP_CREATED) {
LOGGER.debug("Updated event " + getHref()); LOGGER.warn("Overwritten event " + getHref());
} else { } else {
LOGGER.warn("Overwritten event " + getHref());
}
} else if (status != HttpURLConnection.HTTP_CREATED) {
LOGGER.warn("Unable to create or update message " + status + ' ' + putmethod.getStatusLine()); LOGGER.warn("Unable to create or update message " + status + ' ' + putmethod.getStatusLine());
} }
} finally { } finally {

View File

@ -199,14 +199,14 @@ public class ExchangePropPatchMethod extends PostMethod {
final byte[] lastbytes = new byte[3]; final byte[] lastbytes = new byte[3];
@Override @Override
public int read(byte b[], int off, int len) throws IOException { public int read(byte[] bytes, int off, int len) throws IOException {
int count = in.read(b, off, len); int count = in.read(bytes, off, len);
// patch invalid element name // patch invalid element name
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
byte currentByte = b[off + i]; byte currentByte = bytes[off + i];
if ((lastbytes[0] == '<') && (currentByte >= '0' && currentByte <= '9')) { if ((lastbytes[0] == '<') && (currentByte >= '0' && currentByte <= '9')) {
// move invalid first tag char to valid range // move invalid first tag char to valid range
b[off + i] = (byte) (currentByte + 49); bytes[off + i] = (byte) (currentByte + 49);
} }
lastbytes[0] = lastbytes[1]; lastbytes[0] = lastbytes[1];
lastbytes[1] = lastbytes[2]; lastbytes[1] = lastbytes[2];
@ -297,4 +297,25 @@ public class ExchangePropPatchMethod extends PostMethod {
return responses; return responses;
} }
/**
* Get single Multistatus response.
*
* @return response
* @throws HttpException on error
*/
public MultiStatusResponse getResponse() throws HttpException {
if (responses == null || responses.size() != 1) {
throw new HttpException(getStatusLine().toString());
}
return responses.get(0);
}
public int getResponseStatusCode() throws HttpException {
String responseDescription = getResponse().getResponseDescription();
if ("HTTP/1.1 201 Created".equals(responseDescription)) {
return HttpStatus.SC_CREATED;
} else {
return HttpStatus.SC_OK;
}
}
} }

View File

@ -497,9 +497,9 @@ public class Field {
return new PropertyValue(davPropertyName.getNamespace().getURI(), davPropertyName.getName(), buffer.toString()); return new PropertyValue(davPropertyName.getNamespace().getURI(), davPropertyName.getName(), buffer.toString());
} else if (field.isBooleanValue) { } else if (field.isBooleanValue) {
if ("true".equals(value)) { if ("true".equals(value)) {
return new PropertyValue(davPropertyName.getNamespace().getURI(), davPropertyName.getName(), "1"); return new PropertyValue(davPropertyName.getNamespace().getURI(), davPropertyName.getName(), "1", PropertyType.Boolean);
} else if ("false".equals(value)) { } else if ("false".equals(value)) {
return new PropertyValue(davPropertyName.getNamespace().getURI(), davPropertyName.getName(), "0"); return new PropertyValue(davPropertyName.getNamespace().getURI(), davPropertyName.getName(), "0", PropertyType.Boolean);
} else { } else {
throw new RuntimeException("Invalid value for " + field.alias + ": " + value); throw new RuntimeException("Invalid value for " + field.alias + ": " + value);
} }