Improve contact picture error handling

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@2240 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2014-03-09 10:13:22 +00:00
parent a16ae1203b
commit c3dabcdfe4
2 changed files with 47 additions and 48 deletions

View File

@ -2279,41 +2279,39 @@ public class DavExchangeSession extends ExchangeSession {
@Override
public ExchangeSession.ContactPhoto getContactPhoto(ExchangeSession.Contact contact) throws IOException {
ContactPhoto contactPhoto = null;
if ("true".equals(contact.get("haspicture"))) {
final GetMethod method = new GetMethod(URIUtil.encodePath(contact.getHref()) + "/ContactPicture.jpg");
method.setRequestHeader("Translate", "f");
method.setRequestHeader("Accept-Encoding", "gzip");
final GetMethod method = new GetMethod(URIUtil.encodePath(contact.getHref()) + "/ContactPicture.jpg");
method.setRequestHeader("Translate", "f");
method.setRequestHeader("Accept-Encoding", "gzip");
InputStream inputStream = null;
try {
DavGatewayHttpClientFacade.executeGetMethod(httpClient, method, true);
if (DavGatewayHttpClientFacade.isGzipEncoded(method)) {
inputStream = (new GZIPInputStream(method.getResponseBodyAsStream()));
} else {
inputStream = method.getResponseBodyAsStream();
}
contactPhoto = new ContactPhoto();
contactPhoto.contentType = "image/jpeg";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream partInputStream = inputStream;
byte[] bytes = new byte[8192];
int length;
while ((length = partInputStream.read(bytes)) > 0) {
baos.write(bytes, 0, length);
}
contactPhoto.content = new String(Base64.encodeBase64(baos.toByteArray()));
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
LOGGER.debug(e);
}
}
method.releaseConnection();
InputStream inputStream = null;
try {
DavGatewayHttpClientFacade.executeGetMethod(httpClient, method, true);
if (DavGatewayHttpClientFacade.isGzipEncoded(method)) {
inputStream = (new GZIPInputStream(method.getResponseBodyAsStream()));
} else {
inputStream = method.getResponseBodyAsStream();
}
contactPhoto = new ContactPhoto();
contactPhoto.contentType = "image/jpeg";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream partInputStream = inputStream;
byte[] bytes = new byte[8192];
int length;
while ((length = partInputStream.read(bytes)) > 0) {
baos.write(bytes, 0, length);
}
contactPhoto.content = new String(Base64.encodeBase64(baos.toByteArray()));
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
LOGGER.debug(e);
}
}
method.releaseConnection();
}
return contactPhoto;
}

View File

@ -2011,22 +2011,23 @@ public class EwsExchangeSession extends ExchangeSession {
getItemMethod.addAdditionalProperty(Field.get("attachments"));
executeMethod(getItemMethod);
EWSMethod.Item item = getItemMethod.getResponseItem();
if (item != null) {
FileAttachment attachment = item.getAttachmentByName("ContactPicture.jpg");
if (attachment == null) {
throw new IOException("Missing contact picture");
}
// get attachment content
GetAttachmentMethod getAttachmentMethod = new GetAttachmentMethod(attachment.attachmentId);
executeMethod(getAttachmentMethod);
if (item == null) {
throw new IOException("Missing contact picture");
}
FileAttachment attachment = item.getAttachmentByName("ContactPicture.jpg");
if (attachment == null) {
throw new IOException("Missing contact picture");
}
// get attachment content
GetAttachmentMethod getAttachmentMethod = new GetAttachmentMethod(attachment.attachmentId);
executeMethod(getAttachmentMethod);
contactPhoto = new ContactPhoto();
contactPhoto.content = getAttachmentMethod.getResponseItem().get("Content");
if (attachment.contentType == null) {
contactPhoto.contentType = "image/jpeg";
} else {
contactPhoto.contentType = attachment.contentType;
}
contactPhoto = new ContactPhoto();
contactPhoto.content = getAttachmentMethod.getResponseItem().get("Content");
if (attachment.contentType == null) {
contactPhoto.contentType = "image/jpeg";
} else {
contactPhoto.contentType = attachment.contentType;
}
return contactPhoto;