From 06bd2e9c7d3d937fff06c1b8a6dc01bb18f26a3a Mon Sep 17 00:00:00 2001 From: mguessan Date: Sat, 24 Jul 2010 13:11:22 +0000 Subject: [PATCH] Dav: more property update fixes git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1263 3d1905a2-6b24-0410-a738-b14d5a86fcbd --- .../davmail/exchange/ExchangeSession.java | 2 +- .../exchange/dav/DavExchangeSession.java | 61 +++++-------------- .../exchange/dav/ExchangePropPatchMethod.java | 29 +++++++-- src/java/davmail/exchange/dav/Field.java | 4 +- 4 files changed, 44 insertions(+), 52 deletions(-) diff --git a/src/java/davmail/exchange/ExchangeSession.java b/src/java/davmail/exchange/ExchangeSession.java index 2ea29640..d27d7a39 100644 --- a/src/java/davmail/exchange/ExchangeSession.java +++ b/src/java/davmail/exchange/ExchangeSession.java @@ -2871,7 +2871,7 @@ public abstract class ExchangeSession { // reset missing properties to null for (String key : CONTACT_ATTRIBUTES) { if (!"imapUid".equals(key) && !"etag".equals(key) && !"urlcompname".equals(key) - && !"lastmodified".equals(key) && + && !"lastmodified".equals(key) && !"sensitivity".equals(key) && !properties.containsKey(key)) { properties.put(key, null); } diff --git a/src/java/davmail/exchange/dav/DavExchangeSession.java b/src/java/davmail/exchange/dav/DavExchangeSession.java index d71b6d9d..e0b7cba7 100644 --- a/src/java/davmail/exchange/dav/DavExchangeSession.java +++ b/src/java/davmail/exchange/dav/DavExchangeSession.java @@ -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.PropPatchMethod; 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.DavPropertySet; -import org.apache.log4j.Level; -import org.apache.log4j.Logger; import org.w3c.dom.Node; import javax.mail.MessagingException; @@ -599,15 +596,15 @@ public class DavExchangeSession extends ExchangeSession { } protected Set buildProperties() { - Set list = new HashSet(); + Set propertyValues = new HashSet(); for (Map.Entry entry : entrySet()) { String key = entry.getKey(); 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 { status = httpClient.executeMethod(propPatchMethod); if (status == HttpStatus.SC_MULTI_STATUS) { - List responses = propPatchMethod.getResponses(); + status = propPatchMethod.getResponseStatusCode(); //noinspection VariableNotUsedInsideIf - if (etag == null) { - status = HttpStatus.SC_CREATED; + if (status == HttpStatus.SC_CREATED) { LOGGER.debug("Created contact " + getHref()); } else { - status = HttpStatus.SC_OK; LOGGER.debug("Updated contact " + getHref()); } } else { @@ -674,52 +669,31 @@ public class DavExchangeSession extends ExchangeSession { putmethod.releaseConnection(); } - ArrayList changeList = new ArrayList(); - changeList.add(Field.createDavProperty("attachmentContactPhoto", "true")); - //changeList.add(Field.createDavProperty("renderingPosition", "-1")); - changeList.add(Field.createDavProperty("attachExtension", ".jpg")); + Set picturePropertyValues = new HashSet(); + picturePropertyValues.add(Field.createPropertyValue("attachmentContactPhoto", "true")); + // picturePropertyValues.add(Field.createPropertyValue("renderingPosition", "-1")); + picturePropertyValues.add(Field.createPropertyValue("attachExtension", ".jpg")); - final PropPatchMethod attachmentPropPatchMethod = new PropPatchMethod(contactPictureUrl, changeList); + final ExchangePropPatchMethod attachmentPropPatchMethod = new ExchangePropPatchMethod(contactPictureUrl, picturePropertyValues); try { status = httpClient.executeMethod(attachmentPropPatchMethod); 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"); } - } catch (IOException e) { - LOGGER.error("Error in contact photo create or update", e); - throw e; } finally { attachmentPropPatchMethod.releaseConnection(); } - // update haspicture flag to boolean property - DavPropertyName hasPicturePropertyName = Field.getPropertyName("haspicture"); - Set propertyValues = new HashSet(); - 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 { // try to delete picture DeleteMethod deleteMethod = new DeleteMethod(contactPictureUrl); try { status = httpClient.executeMethod(deleteMethod); 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"); } - } catch (IOException e) { - LOGGER.error("Error in contact photo delete", e); - throw e; } finally { deleteMethod.releaseConnection(); } @@ -845,13 +819,10 @@ public class DavExchangeSession extends ExchangeSession { try { status = httpClient.executeMethod(putmethod); if (status == HttpURLConnection.HTTP_OK) { - //noinspection VariableNotUsedInsideIf - if (etag != null) { - LOGGER.debug("Updated event " + getHref()); - } else { - LOGGER.warn("Overwritten event " + getHref()); - } - } else if (status != HttpURLConnection.HTTP_CREATED) { + LOGGER.debug("Updated event " + getHref()); + } else if (status == HttpURLConnection.HTTP_CREATED) { + LOGGER.warn("Overwritten event " + getHref()); + } else { LOGGER.warn("Unable to create or update message " + status + ' ' + putmethod.getStatusLine()); } } finally { diff --git a/src/java/davmail/exchange/dav/ExchangePropPatchMethod.java b/src/java/davmail/exchange/dav/ExchangePropPatchMethod.java index a9e272de..1de9a6c8 100644 --- a/src/java/davmail/exchange/dav/ExchangePropPatchMethod.java +++ b/src/java/davmail/exchange/dav/ExchangePropPatchMethod.java @@ -199,14 +199,14 @@ public class ExchangePropPatchMethod extends PostMethod { final byte[] lastbytes = new byte[3]; @Override - public int read(byte b[], int off, int len) throws IOException { - int count = in.read(b, off, len); + public int read(byte[] bytes, int off, int len) throws IOException { + int count = in.read(bytes, off, len); // patch invalid element name for (int i = 0; i < count; i++) { - byte currentByte = b[off + i]; + byte currentByte = bytes[off + i]; if ((lastbytes[0] == '<') && (currentByte >= '0' && currentByte <= '9')) { // 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[1] = lastbytes[2]; @@ -297,4 +297,25 @@ public class ExchangePropPatchMethod extends PostMethod { 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; + } + } } diff --git a/src/java/davmail/exchange/dav/Field.java b/src/java/davmail/exchange/dav/Field.java index 64e51f5e..7825dab3 100644 --- a/src/java/davmail/exchange/dav/Field.java +++ b/src/java/davmail/exchange/dav/Field.java @@ -497,9 +497,9 @@ public class Field { return new PropertyValue(davPropertyName.getNamespace().getURI(), davPropertyName.getName(), buffer.toString()); } else if (field.isBooleanValue) { 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)) { - return new PropertyValue(davPropertyName.getNamespace().getURI(), davPropertyName.getName(), "0"); + return new PropertyValue(davPropertyName.getNamespace().getURI(), davPropertyName.getName(), "0", PropertyType.Boolean); } else { throw new RuntimeException("Invalid value for " + field.alias + ": " + value); }