mirror of
https://github.com/moparisthebest/davmail
synced 2025-01-05 18:58:02 -05:00
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:
parent
a16ae1203b
commit
c3dabcdfe4
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user