Add Some Logging to GAL Provider

Change-Id: Ic70d7cfe8c2090bdcfaa2504a24c2bcefedd73de
diff --git a/src/com/android/exchange/EasSyncService.java b/src/com/android/exchange/EasSyncService.java
index 6b32f9e..443e719 100644
--- a/src/com/android/exchange/EasSyncService.java
+++ b/src/com/android/exchange/EasSyncService.java
@@ -74,6 +74,7 @@
 import com.android.exchange.adapter.Tags;
 import com.android.exchange.provider.GalResult;
 import com.android.exchange.utility.CalendarUtilities;
+import com.android.exchange.utility.CurlLogger;
 import com.android.mail.utils.LogUtils;
 import com.google.common.annotations.VisibleForTesting;
 
@@ -91,6 +92,7 @@
 import org.apache.http.params.BasicHttpParams;
 import org.apache.http.params.HttpConnectionParams;
 import org.apache.http.params.HttpParams;
+import org.apache.http.protocol.BasicHttpProcessor;
 import org.xmlpull.v1.XmlPullParser;
 import org.xmlpull.v1.XmlPullParserException;
 import org.xmlpull.v1.XmlPullParserFactory;
@@ -1260,8 +1262,13 @@
         HttpConnectionParams.setConnectionTimeout(params, CONNECTION_TIMEOUT);
         HttpConnectionParams.setSoTimeout(params, timeout);
         HttpConnectionParams.setSocketBufferSize(params, 8192);
-        HttpClient client = new DefaultHttpClient(getClientConnectionManager(), params);
-        return client;
+        return new DefaultHttpClient(getClientConnectionManager(), params) {
+            protected BasicHttpProcessor createHttpProcessor() {
+                final BasicHttpProcessor processor = super.createHttpProcessor();
+                processor.addRequestInterceptor(new CurlLogger());
+                return processor;
+            }
+        };
     }
 
     public EasResponse sendHttpClientPost(String cmd, byte[] bytes) throws IOException {
diff --git a/src/com/android/exchange/provider/ExchangeDirectoryProvider.java b/src/com/android/exchange/provider/ExchangeDirectoryProvider.java
index 2b94271..8e61e9a 100644
--- a/src/com/android/exchange/provider/ExchangeDirectoryProvider.java
+++ b/src/com/android/exchange/provider/ExchangeDirectoryProvider.java
@@ -16,18 +16,6 @@
 
 package com.android.exchange.provider;
 
-import com.android.emailcommon.Configuration;
-import com.android.emailcommon.mail.PackedString;
-import com.android.emailcommon.provider.Account;
-import com.android.emailcommon.provider.EmailContent;
-import com.android.emailcommon.provider.EmailContent.AccountColumns;
-import com.android.emailcommon.service.AccountServiceProxy;
-import com.android.emailcommon.utility.Utility;
-import com.android.exchange.Eas;
-import com.android.exchange.EasSyncService;
-import com.android.exchange.R;
-import com.android.exchange.provider.GalResult.GalData;
-
 import android.accounts.AccountManager;
 import android.content.ContentProvider;
 import android.content.ContentValues;
@@ -49,6 +37,19 @@
 import android.provider.ContactsContract.RawContacts;
 import android.text.TextUtils;
 
+import com.android.emailcommon.Configuration;
+import com.android.emailcommon.mail.PackedString;
+import com.android.emailcommon.provider.Account;
+import com.android.emailcommon.provider.EmailContent;
+import com.android.emailcommon.provider.EmailContent.AccountColumns;
+import com.android.emailcommon.service.AccountServiceProxy;
+import com.android.emailcommon.utility.Utility;
+import com.android.exchange.Eas;
+import com.android.exchange.EasSyncService;
+import com.android.exchange.R;
+import com.android.exchange.provider.GalResult.GalData;
+import com.android.mail.utils.LogUtils;
+
 import java.util.HashMap;
 import java.util.List;
 
@@ -57,6 +58,8 @@
  * used solely to provide GAL (Global Address Lookup) service to email address adapters
  */
 public class ExchangeDirectoryProvider extends ContentProvider {
+    private static final String TAG = Eas.LOG_TAG;
+
     public static final String EXCHANGE_GAL_AUTHORITY =
             com.android.exchange.Configuration.EXCHANGE_GAL_AUTHORITY;
 
@@ -136,7 +139,7 @@
             if (integer != null) {
                 row[integer] = value;
             } else {
-                System.out.println("Unsupported column: " + columnName);
+                LogUtils.e(TAG, "Unsupported column: " + columnName);
             }
         }
 
@@ -198,6 +201,7 @@
     @Override
     public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
             String sortOrder) {
+        LogUtils.d(TAG, "ExchangeDirectoryProvider: query: %s", uri.toString());
         int match = sURIMatcher.match(uri);
         MatrixCursor cursor;
         Object[] row;
diff --git a/src/com/android/exchange/service/EasServerConnection.java b/src/com/android/exchange/service/EasServerConnection.java
index 6187a8a..7275e24 100644
--- a/src/com/android/exchange/service/EasServerConnection.java
+++ b/src/com/android/exchange/service/EasServerConnection.java
@@ -26,7 +26,6 @@
 import android.text.TextUtils;
 import android.text.format.DateUtils;
 import android.util.Base64;
-import android.util.Log;
 
 import com.android.emailcommon.internet.MimeUtility;
 import com.android.emailcommon.provider.Account;
@@ -39,27 +38,21 @@
 import com.android.exchange.Eas;
 import com.android.exchange.EasResponse;
 import com.android.exchange.eas.EasConnectionCache;
+import com.android.exchange.utility.CurlLogger;
 import com.android.mail.utils.LogUtils;
 
-import org.apache.http.Header;
 import org.apache.http.HttpEntity;
-import org.apache.http.HttpEntityEnclosingRequest;
-import org.apache.http.HttpRequest;
-import org.apache.http.HttpRequestInterceptor;
 import org.apache.http.client.HttpClient;
 import org.apache.http.client.methods.HttpOptions;
 import org.apache.http.client.methods.HttpPost;
 import org.apache.http.client.methods.HttpUriRequest;
 import org.apache.http.entity.ByteArrayEntity;
 import org.apache.http.impl.client.DefaultHttpClient;
-import org.apache.http.impl.client.RequestWrapper;
 import org.apache.http.params.BasicHttpParams;
 import org.apache.http.params.HttpConnectionParams;
 import org.apache.http.params.HttpParams;
 import org.apache.http.protocol.BasicHttpProcessor;
-import org.apache.http.protocol.HttpContext;
 
-import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.net.URI;
 import java.security.cert.CertificateException;
@@ -533,86 +526,4 @@
         extras.putLong(Mailbox.SYNC_EXTRA_MAILBOX_ID, mailboxId);
         ContentResolver.requestSync(amAccount, authority, extras);
     }
-
-    // Curl Logging is copied over from AndroidHttpClient. Just switching to AndroidHttpClient is
-    // not trivial so it's easier to borrow the curl logging code this way.
-
-    /**
-     * Logs cURL commands equivalent to requests.
-     */
-    private class CurlLogger implements HttpRequestInterceptor {
-        @Override
-        public void process(HttpRequest request, HttpContext context) throws IOException {
-            if (request instanceof HttpUriRequest) {
-                if ((Build.TYPE.equals("userdebug") || Build.TYPE.equals("eng"))
-                        &&  Log.isLoggable(TAG, Log.VERBOSE)) {
-                    // Allow us to log auth token on dev devices - this is not a big security risk
-                    // because dev devices have a readable account.db file where all the auth tokens
-                    // are stored.
-                    Log.d(TAG, toCurl((HttpUriRequest) request, true));
-                } else  if (Log.isLoggable(TAG, Log.DEBUG)) {
-                    Log.d(TAG, toCurl((HttpUriRequest) request, false));
-                }
-            }
-        }
-    }
-
-    /**
-     * Generates a cURL command equivalent to the given request.
-     */
-    private static String toCurl(HttpUriRequest request, boolean logAuthToken) throws IOException {
-        StringBuilder builder = new StringBuilder();
-
-        builder.append("curl ");
-
-        for (Header header: request.getAllHeaders()) {
-            builder.append("--header \"");
-            if (!logAuthToken
-                    && (header.getName().equals("Authorization") ||
-                    header.getName().equals("Cookie"))) {
-
-                builder.append(header.getName()).append(": ").append("${token}");
-            } else {
-                builder.append(header.toString().trim());
-            }
-            builder.append("\" ");
-        }
-
-        URI uri = request.getURI();
-
-        // If this is a wrapped request, use the URI from the original
-        // request instead. getURI() on the wrapper seems to return a
-        // relative URI. We want an absolute URI.
-        if (request instanceof RequestWrapper) {
-            HttpRequest original = ((RequestWrapper) request).getOriginal();
-            if (original instanceof HttpUriRequest) {
-                uri = ((HttpUriRequest) original).getURI();
-            }
-        }
-
-        builder.append("\"");
-        builder.append(uri);
-        builder.append("\"");
-
-        if (request instanceof HttpEntityEnclosingRequest) {
-            HttpEntityEnclosingRequest entityRequest =
-                    (HttpEntityEnclosingRequest) request;
-            HttpEntity entity = entityRequest.getEntity();
-            if (entity != null && entity.isRepeatable()) {
-                if (entity.getContentLength() < 1024) {
-                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
-                    entity.writeTo(stream);
-
-                    String base64 = Base64.encodeToString(stream.toByteArray(), Base64.NO_WRAP);
-                    builder.insert(0, "echo '" + base64 + "' | base64 -d > /tmp/$$.bin; ");
-                    builder.append(" --data-binary @/tmp/$$.bin");
-                } else {
-                    builder.append(" [TOO MUCH DATA TO INCLUDE]");
-                }
-            }
-        }
-
-        return builder.toString();
-    }
-
 }
diff --git a/src/com/android/exchange/utility/CurlLogger.java b/src/com/android/exchange/utility/CurlLogger.java
new file mode 100644
index 0000000..b57d9b9
--- /dev/null
+++ b/src/com/android/exchange/utility/CurlLogger.java
@@ -0,0 +1,105 @@
+// Copyright 2013 Google Inc. All Rights Reserved.
+
+package com.android.exchange.utility;
+
+import android.os.Build;
+import android.util.Base64;
+import android.util.Log;
+
+import com.android.exchange.Eas;
+
+import org.apache.http.Header;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpEntityEnclosingRequest;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpRequestInterceptor;
+import org.apache.http.client.methods.HttpUriRequest;
+import org.apache.http.impl.client.RequestWrapper;
+import org.apache.http.protocol.HttpContext;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.net.URI;
+
+/**
+ * Logs cURL commands equivalent to requests.
+ * Curl Logging is copied over from AndroidHttpClient. Just switching to AndroidHttpClient is
+ * not trivial so it's easier to borrow the curl logging code this way.
+ */
+public class CurlLogger implements HttpRequestInterceptor {
+    private static final String TAG = Eas.LOG_TAG;
+
+    @Override
+    public void process(HttpRequest request, HttpContext context) throws IOException {
+        if (request instanceof HttpUriRequest) {
+            if ((Build.TYPE.equals("userdebug") || Build.TYPE.equals("eng"))
+                    &&  Log.isLoggable(TAG, Log.VERBOSE)) {
+                // Allow us to log auth token on dev devices - this is not a big security risk
+                // because dev devices have a readable account.db file where all the auth tokens
+                // are stored.
+                Log.d(TAG, toCurl((HttpUriRequest) request, true));
+            } else  if (Log.isLoggable(TAG, Log.DEBUG)) {
+                Log.d(TAG, toCurl((HttpUriRequest) request, false));
+            }
+        }
+    }
+
+    /**
+     * Generates a cURL command equivalent to the given request.
+     */
+    private static String toCurl(HttpUriRequest request, boolean logAuthToken) throws IOException {
+        StringBuilder builder = new StringBuilder();
+
+        builder.append("curl ");
+
+        for (Header header: request.getAllHeaders()) {
+            builder.append("--header \"");
+            if (!logAuthToken
+                    && (header.getName().equals("Authorization") ||
+                    header.getName().equals("Cookie"))) {
+
+                builder.append(header.getName()).append(": ").append("${token}");
+            } else {
+                builder.append(header.toString().trim());
+            }
+            builder.append("\" ");
+        }
+
+        URI uri = request.getURI();
+
+        // If this is a wrapped request, use the URI from the original
+        // request instead. getURI() on the wrapper seems to return a
+        // relative URI. We want an absolute URI.
+        if (request instanceof RequestWrapper) {
+            HttpRequest original = ((RequestWrapper) request).getOriginal();
+            if (original instanceof HttpUriRequest) {
+                uri = ((HttpUriRequest) original).getURI();
+            }
+        }
+
+        builder.append("\"");
+        builder.append(uri);
+        builder.append("\"");
+
+        if (request instanceof HttpEntityEnclosingRequest) {
+            HttpEntityEnclosingRequest entityRequest =
+                    (HttpEntityEnclosingRequest) request;
+            HttpEntity entity = entityRequest.getEntity();
+            if (entity != null && entity.isRepeatable()) {
+                if (entity.getContentLength() < 1024) {
+                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
+                    entity.writeTo(stream);
+
+                    String base64 = Base64.encodeToString(stream.toByteArray(), Base64.NO_WRAP);
+                    builder.insert(0, "echo '" + base64 + "' | base64 -d > /tmp/$$.bin; ");
+                    builder.append(" --data-binary @/tmp/$$.bin");
+                } else {
+                    builder.append(" [TOO MUCH DATA TO INCLUDE]");
+                }
+            }
+        }
+
+        return builder.toString();
+    }
+
+}