From 63fdfb111c59c150cb0217b37c3f398923d63770 Mon Sep 17 00:00:00 2001 From: mguessan Date: Fri, 17 Jun 2011 09:17:31 +0000 Subject: [PATCH] EWS: implement gzip encoding on response git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1705 3d1905a2-6b24-0410-a738-b14d5a86fcbd --- .../exchange/dav/DavExchangeSession.java | 16 ++-------------- src/java/davmail/exchange/ews/EWSMethod.java | 9 ++++++++- .../http/DavGatewayHttpClientFacade.java | 17 +++++++++++++++++ 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/java/davmail/exchange/dav/DavExchangeSession.java b/src/java/davmail/exchange/dav/DavExchangeSession.java index 2c6cfc53..b9b81689 100644 --- a/src/java/davmail/exchange/dav/DavExchangeSession.java +++ b/src/java/davmail/exchange/dav/DavExchangeSession.java @@ -1997,7 +1997,7 @@ public class DavExchangeSession extends ExchangeSession { InputStream inputStream = null; try { DavGatewayHttpClientFacade.executeGetMethod(httpClient, method, true); - if (isGzipEncoded(method)) { + if (DavGatewayHttpClientFacade.isGzipEncoded(method)) { inputStream = (new GZIPInputStream(method.getResponseBodyAsStream())); } else { inputStream = method.getResponseBodyAsStream(); @@ -2425,18 +2425,6 @@ public class DavExchangeSession extends ExchangeSession { } } - protected boolean isGzipEncoded(HttpMethod method) { - Header[] contentEncodingHeaders = method.getResponseHeaders("Content-Encoding"); - if (contentEncodingHeaders != null) { - for (Header header : contentEncodingHeaders) { - if ("gzip".equals(header.getValue())) { - return true; - } - } - } - return false; - } - // wrong hostname fix flag protected boolean restoreHostName; @@ -2568,7 +2556,7 @@ public class DavExchangeSession extends ExchangeSession { InputStream inputStream; try { DavGatewayHttpClientFacade.executeGetMethod(httpClient, method, true); - if (isGzipEncoded(method)) { + if (DavGatewayHttpClientFacade.isGzipEncoded(method)) { inputStream = new GZIPInputStream(method.getResponseBodyAsStream()); } else { inputStream = method.getResponseBodyAsStream(); diff --git a/src/java/davmail/exchange/ews/EWSMethod.java b/src/java/davmail/exchange/ews/EWSMethod.java index e216c72c..b9aac2f3 100644 --- a/src/java/davmail/exchange/ews/EWSMethod.java +++ b/src/java/davmail/exchange/ews/EWSMethod.java @@ -19,6 +19,7 @@ package davmail.exchange.ews; import davmail.exchange.XMLStreamUtil; +import davmail.http.DavGatewayHttpClientFacade; import davmail.util.StringUtil; import org.apache.commons.codec.binary.Base64; import org.apache.commons.httpclient.Header; @@ -35,6 +36,7 @@ import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; import java.io.*; import java.util.*; +import java.util.zip.GZIPInputStream; /** * EWS SOAP method. @@ -102,6 +104,7 @@ public abstract class EWSMethod extends PostMethod { this.itemType = itemType; this.methodName = methodName; this.responseCollectionName = responseCollectionName; + setRequestHeader("Accept-Encoding", "gzip"); setRequestEntity(new RequestEntity() { byte[] content; @@ -988,7 +991,11 @@ public abstract class EWSMethod extends PostMethod { Header contentTypeHeader = getResponseHeader("Content-Type"); if (contentTypeHeader != null && "text/xml; charset=utf-8".equals(contentTypeHeader.getValue())) { try { - processResponseStream(getResponseBodyAsStream()); + if (DavGatewayHttpClientFacade.isGzipEncoded(this)) { + processResponseStream(new GZIPInputStream(getResponseBodyAsStream())); + } else { + processResponseStream(getResponseBodyAsStream()); + } } catch (IOException e) { LOGGER.error("Error while parsing soap response: " + e, e); } diff --git a/src/java/davmail/http/DavGatewayHttpClientFacade.java b/src/java/davmail/http/DavGatewayHttpClientFacade.java index ebe1f936..f48f1c2b 100644 --- a/src/java/davmail/http/DavGatewayHttpClientFacade.java +++ b/src/java/davmail/http/DavGatewayHttpClientFacade.java @@ -663,6 +663,23 @@ public final class DavGatewayHttpClientFacade { } } + /** + * Test if the method response is gzip encoded + * @param method http method + * @return true if response is gzip encoded + */ + public static boolean isGzipEncoded(HttpMethod method) { + Header[] contentEncodingHeaders = method.getResponseHeaders("Content-Encoding"); + if (contentEncodingHeaders != null) { + for (Header header : contentEncodingHeaders) { + if ("gzip".equals(header.getValue())) { + return true; + } + } + } + return false; + } + /** * Stop HttpConnectionManager. */