mirror of
https://github.com/moparisthebest/davmail
synced 2025-02-28 09:21:49 -05:00
CardDav: use new ExchangePropPatchMethod to create haspicture boolean property
git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1260 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
parent
dd2aa81d1e
commit
5ad16b3b65
@ -41,6 +41,7 @@ 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.w3c.dom.Node;
|
||||
@ -652,71 +653,90 @@ public class DavExchangeSession extends ExchangeSession {
|
||||
}
|
||||
itemResult.status = status;
|
||||
|
||||
String contactPictureUrl = URIUtil.encodePath(getHref() + "/ContactPicture.jpg");
|
||||
String photo = get("photo");
|
||||
if (photo != null) {
|
||||
// need to update photo
|
||||
byte[] resizedImageBytes = IOUtil.resizeImage(Base64.decodeBase64(photo.getBytes()), 90);
|
||||
if (status == HttpStatus.SC_OK || status == HttpStatus.SC_CREATED) {
|
||||
String contactPictureUrl = URIUtil.encodePath(getHref() + "/ContactPicture.jpg");
|
||||
String photo = get("photo");
|
||||
if (photo != null) {
|
||||
// need to update photo
|
||||
byte[] resizedImageBytes = IOUtil.resizeImage(Base64.decodeBase64(photo.getBytes()), 90);
|
||||
|
||||
final PutMethod putmethod = new PutMethod(contactPictureUrl);
|
||||
putmethod.setRequestHeader("Overwrite", "t");
|
||||
putmethod.setRequestHeader("Content-Type", "image/jpeg");
|
||||
putmethod.setRequestEntity(new ByteArrayRequestEntity(resizedImageBytes, "image/jpeg"));
|
||||
try {
|
||||
status = httpClient.executeMethod(putmethod);
|
||||
if (status != HttpStatus.SC_OK && status != HttpStatus.SC_CREATED) {
|
||||
throw new IOException("Unable to update contact picture: "+status+ ' ' +putmethod.getStatusLine());
|
||||
final PutMethod putmethod = new PutMethod(contactPictureUrl);
|
||||
putmethod.setRequestHeader("Overwrite", "t");
|
||||
putmethod.setRequestHeader("Content-Type", "image/jpeg");
|
||||
putmethod.setRequestEntity(new ByteArrayRequestEntity(resizedImageBytes, "image/jpeg"));
|
||||
try {
|
||||
status = httpClient.executeMethod(putmethod);
|
||||
if (status != HttpStatus.SC_OK && status != HttpStatus.SC_CREATED) {
|
||||
throw new IOException("Unable to update contact picture: " + status + ' ' + putmethod.getStatusLine());
|
||||
}
|
||||
} catch (IOException e) {
|
||||
LOGGER.error("Error in contact photo create or update", e);
|
||||
throw e;
|
||||
} finally {
|
||||
putmethod.releaseConnection();
|
||||
}
|
||||
|
||||
ArrayList<DavConstants> changeList = new ArrayList<DavConstants>();
|
||||
changeList.add(Field.createDavProperty("attachmentContactPhoto", "true"));
|
||||
//changeList.add(Field.createDavProperty("renderingPosition", "-1"));
|
||||
changeList.add(Field.createDavProperty("attachExtension", ".jpg"));
|
||||
|
||||
final PropPatchMethod attachmentPropPatchMethod = new PropPatchMethod(contactPictureUrl, changeList);
|
||||
try {
|
||||
status = httpClient.executeMethod(attachmentPropPatchMethod);
|
||||
if (status != HttpStatus.SC_MULTI_STATUS) {
|
||||
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<PropertyValue> propertyValues = new HashSet<PropertyValue>();
|
||||
propertyValues.add(new PropertyValue(hasPicturePropertyName.getNamespace(),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) {
|
||||
throw new IOException("Unable to delete contact picture");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
LOGGER.error("Error in contact photo delete", e);
|
||||
throw e;
|
||||
} finally {
|
||||
deleteMethod.releaseConnection();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
LOGGER.error("Error in contact photo create or update", e);
|
||||
throw e;
|
||||
} finally {
|
||||
putmethod.releaseConnection();
|
||||
}
|
||||
|
||||
ArrayList<DavConstants> changeList = new ArrayList<DavConstants>();
|
||||
changeList.add(Field.createDavProperty("attachmentContactPhoto", "true"));
|
||||
changeList.add(Field.createDavProperty("renderingPosition", "-1"));
|
||||
|
||||
final PropPatchMethod attachmentPropPatchMethod = new PropPatchMethod(contactPictureUrl, changeList);
|
||||
// need to retrieve new etag
|
||||
HeadMethod headMethod = new HeadMethod(URIUtil.encodePath(getHref()));
|
||||
try {
|
||||
status = httpClient.executeMethod(attachmentPropPatchMethod);
|
||||
if (status != HttpStatus.SC_MULTI_STATUS) {
|
||||
throw new IOException("Unable to update contact picture");
|
||||
httpClient.executeMethod(headMethod);
|
||||
if (headMethod.getResponseHeader("ETag") != null) {
|
||||
itemResult.etag = headMethod.getResponseHeader("ETag").getValue();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
LOGGER.error("Error in contact photo create or update", e);
|
||||
throw e;
|
||||
} finally {
|
||||
attachmentPropPatchMethod.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) {
|
||||
throw new IOException("Unable to delete contact picture");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
LOGGER.error("Error in contact photo delete", e);
|
||||
throw e;
|
||||
} finally {
|
||||
deleteMethod.releaseConnection();
|
||||
headMethod.releaseConnection();
|
||||
}
|
||||
}
|
||||
// need to retrieve new etag
|
||||
HeadMethod headMethod = new HeadMethod(URIUtil.encodePath(getHref()));
|
||||
try {
|
||||
httpClient.executeMethod(headMethod);
|
||||
if (headMethod.getResponseHeader("ETag") != null) {
|
||||
itemResult.etag = headMethod.getResponseHeader("ETag").getValue();
|
||||
}
|
||||
} finally {
|
||||
headMethod.releaseConnection();
|
||||
}
|
||||
|
||||
return itemResult;
|
||||
|
||||
}
|
||||
|
@ -44,6 +44,12 @@ public class ExchangePropPatchMethod extends EntityEnclosingMethod {
|
||||
static final Namespace TYPE_NAMESPACE = Namespace.getNamespace("urn:schemas-microsoft-com:datatypes");
|
||||
final Set<PropertyValue> propertyValues;
|
||||
|
||||
/**
|
||||
* Create PROPPATCH method.
|
||||
*
|
||||
* @param path path
|
||||
* @param propertyValues property values
|
||||
*/
|
||||
public ExchangePropPatchMethod(String path, Set<PropertyValue> propertyValues) {
|
||||
super(path);
|
||||
this.propertyValues = propertyValues;
|
||||
@ -74,7 +80,7 @@ public class ExchangePropPatchMethod extends EntityEnclosingMethod {
|
||||
});
|
||||
}
|
||||
|
||||
public byte[] generateRequestContent() {
|
||||
protected byte[] generateRequestContent() {
|
||||
try {
|
||||
// build namespace map
|
||||
int currentChar = 'e';
|
||||
@ -135,7 +141,7 @@ public class ExchangePropPatchMethod extends EntityEnclosingMethod {
|
||||
writer.write("</D:prop></D:set>");
|
||||
}
|
||||
if (!deletePropertyValues.isEmpty()) {
|
||||
writer.write("<D:delete><D:prop>");
|
||||
writer.write("<D:remove><D:prop>");
|
||||
for (PropertyValue propertyValue : deletePropertyValues) {
|
||||
char nameSpaceChar = (char) nameSpaceMap.get(propertyValue.getNamespace()).intValue();
|
||||
writer.write('<');
|
||||
@ -144,7 +150,7 @@ public class ExchangePropPatchMethod extends EntityEnclosingMethod {
|
||||
writer.write(propertyValue.getName());
|
||||
writer.write("/>");
|
||||
}
|
||||
writer.write("</D:prop></D:delete>");
|
||||
writer.write("</D:prop></D:remove>");
|
||||
}
|
||||
writer.write("</D:propertyupdate>");
|
||||
writer.close();
|
||||
@ -193,7 +199,7 @@ public class ExchangePropPatchMethod extends EntityEnclosingMethod {
|
||||
reader = xmlInputFactory.createXMLStreamReader(new FilterInputStream(getResponseBodyAsStream()) {
|
||||
@Override
|
||||
public int read() throws IOException {
|
||||
return in.read();
|
||||
return in.read();
|
||||
}
|
||||
|
||||
});
|
||||
@ -225,23 +231,23 @@ public class ExchangePropPatchMethod extends EntityEnclosingMethod {
|
||||
if ("HTTP/1.1 200 OK".equals(reader.getElementText())) {
|
||||
currentStatus = HttpStatus.SC_OK;
|
||||
}
|
||||
} else if (tagLocalName.equals("prop")) {
|
||||
handleProperty(reader, currentStatus, multiStatusResponse);
|
||||
} else if ("prop".equals(tagLocalName) && currentStatus == HttpStatus.SC_OK) {
|
||||
handleProperty(reader, multiStatusResponse);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected void handleProperty(XMLStreamReader reader, int currentStatus, MultiStatusResponse multiStatusResponse) throws XMLStreamException {
|
||||
protected void handleProperty(XMLStreamReader reader, MultiStatusResponse multiStatusResponse) throws XMLStreamException {
|
||||
while (reader.hasNext() && !isEndTag(reader, "prop")) {
|
||||
try {
|
||||
int event = reader.next();
|
||||
if (event == XMLStreamConstants.START_ELEMENT) {
|
||||
String tagLocalName = reader.getLocalName();
|
||||
Namespace namespace = Namespace.getNamespace(reader.getNamespaceURI());
|
||||
multiStatusResponse.add(new DefaultDavProperty(tagLocalName, reader.getElementText(), namespace));
|
||||
}
|
||||
int event = reader.next();
|
||||
if (event == XMLStreamConstants.START_ELEMENT) {
|
||||
String tagLocalName = reader.getLocalName();
|
||||
Namespace namespace = Namespace.getNamespace(reader.getNamespaceURI());
|
||||
multiStatusResponse.add(new DefaultDavProperty(tagLocalName, reader.getElementText(), namespace));
|
||||
}
|
||||
} catch (XMLStreamException e) {
|
||||
// ignore, exchange invalid response
|
||||
logger.debug("Ignore invalid response tag name");
|
||||
@ -249,6 +255,12 @@ public class ExchangePropPatchMethod extends EntityEnclosingMethod {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Multistatus responses.
|
||||
*
|
||||
* @return responses
|
||||
* @throws HttpException on error
|
||||
*/
|
||||
public List<MultiStatusResponse> getResponses() throws HttpException {
|
||||
if (responses == null) {
|
||||
throw new HttpException(getStatusLine().toString());
|
||||
|
@ -308,11 +308,7 @@ public class Field {
|
||||
// Common namespace expects hex names
|
||||
name = "0x" + toHexString(propertyTag);
|
||||
}
|
||||
if ("haspicture".equals(alias)) {
|
||||
updateAlias = "_x0030_x" + toHexString(propertyTag);
|
||||
} else {
|
||||
updateAlias = "_x0030_x" + toHexString(propertyTag);
|
||||
}
|
||||
updateAlias = "_x0030_x" + toHexString(propertyTag);
|
||||
Field field = new Field(alias, Namespace.getNamespace(SCHEMAS_MAPI_ID.getURI() +
|
||||
'{' + distinguishedPropertySetMap.get(propertySetType) + "}/"), name, propertyType, responseAlias, null, updateAlias);
|
||||
fieldMap.put(field.alias, field);
|
||||
|
Loading…
x
Reference in New Issue
Block a user