From c3dabcdfe481a65eaa849ef6ae9f2a7d7996a112 Mon Sep 17 00:00:00 2001 From: mguessan Date: Sun, 9 Mar 2014 10:13:22 +0000 Subject: [PATCH] Improve contact picture error handling git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@2240 3d1905a2-6b24-0410-a738-b14d5a86fcbd --- .../exchange/dav/DavExchangeSession.java | 64 +++++++++---------- .../exchange/ews/EwsExchangeSession.java | 31 ++++----- 2 files changed, 47 insertions(+), 48 deletions(-) diff --git a/src/java/davmail/exchange/dav/DavExchangeSession.java b/src/java/davmail/exchange/dav/DavExchangeSession.java index bf76af19..b8fb37de 100644 --- a/src/java/davmail/exchange/dav/DavExchangeSession.java +++ b/src/java/davmail/exchange/dav/DavExchangeSession.java @@ -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; } diff --git a/src/java/davmail/exchange/ews/EwsExchangeSession.java b/src/java/davmail/exchange/ews/EwsExchangeSession.java index 1bedae07..1caa9dd6 100644 --- a/src/java/davmail/exchange/ews/EwsExchangeSession.java +++ b/src/java/davmail/exchange/ews/EwsExchangeSession.java @@ -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;