enable HTTP logging
[WhereAmI.git] / src / org / average / whereami / Oracle.java
index 7ba16a5ec2dfde72e118e4b339783a6979895760..b3d992e410a437fe0e9852407aa7d830854696f5 100644 (file)
@@ -2,6 +2,13 @@ package org.average.whereami;
 
 import org.average.whereami.PersistentStore;
 
+import java.io.IOException;
+import com.google.api.client.http.HttpTransport;
+import java.util.logging.Handler;
+import java.util.logging.Level;
+import java.util.logging.LogRecord;
+import java.util.logging.Logger;
+
 import com.google.api.client.extensions.android2.AndroidHttp;
 import com.google.api.client.auth.oauth2.draft10.AccessTokenResponse;
 import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessProtectedResource;
@@ -18,45 +25,76 @@ abstract public class Oracle {
 
        final String TAG = getClass().getName();
 
-       public HttpTransport httpTransport;
-       public JsonFactory jsonFactory;
-       public PersistentStore store;
-
-
-       public String authErrorMessage;
+       static public PersistentStore store;
+       static public HttpTransport httpTransport =
+                       AndroidHttp.newCompatibleTransport();
+       static public JsonFactory jsonFactory = new JacksonFactory();
+       static public String authErrorMessage;
 
        public Oracle(final Resources res, final PersistentStore store) {
                authErrorMessage = res.getString(R.string.autherror);
                this.store = store;
+               enableLogging();
+       }
 
-               httpTransport = AndroidHttp.newCompatibleTransport();
-               jsonFactory = new JacksonFactory();
-}
+       private class MyAccessProtectedResource
+               extends GoogleAccessProtectedResource {
 
-       public GoogleAccessProtectedResource accessProtectedResource() {
+               public MyAccessProtectedResource(
+                               String accessToken,
+                               HttpTransport transport,
+                               JsonFactory jsonFactory,
+                               String clientId,
+                               String clientSecret,
+                               String refreshToken) {
+                       super(accessToken, transport, jsonFactory,
+                               clientId, clientSecret, refreshToken);
+               }
+
+               @Override
+               public void onAccessToken(String accessToken) {
+                       Log.v(TAG, "Update access token to \""
+                               + accessToken + "\"");
+                       store.updateAccessToken( accessToken);
+               }
+
+               @Override
+               protected boolean executeRefreshToken() throws IOException {
+                       Log.v(TAG, "executeRefreshToken was called");
+                       return super.executeRefreshToken();
+               }
+       }
+
+       public MyAccessProtectedResource
+                               newMyAccessProtectedResource() {
                AccessTokenResponse token = store.readTokens();
                Log.v(TAG, "tokens - access: \"" + token.accessToken +
                        "\", refresh: \"" + token.refreshToken +
                        "\", client_id: \"" + ClientCredentials.CLIENT_ID +
                        "\", client_secret: \"" +
-                                       ClientCredentials.CLIENT_SECRET +
+                               ClientCredentials.CLIENT_SECRET +
                        "\"");
-               return new GoogleAccessProtectedResource(
+               return new MyAccessProtectedResource(
                        token.accessToken, httpTransport, jsonFactory,
                        ClientCredentials.CLIENT_ID,
                        ClientCredentials.CLIENT_SECRET,
-                       token.refreshToken)
-                       {
-                               @Override
-                               public void onAccessToken(
-                                               String accessToken) {
-                                       Log.v(TAG, "Update access token to \""
-                                               + accessToken + "\"");
-                                       store.updateAccessToken(
-                                               accessToken);
-                               }
-                       };
+                       token.refreshToken);
        }
 
        abstract public String getResult();
+
+       public static void enableLogging() {
+               Logger logger = Logger.getLogger(HttpTransport.class.getName());
+               logger.setLevel(Level.CONFIG);
+               logger.addHandler(new Handler() {
+                       @Override
+                       public void close() throws SecurityException {}
+                       @Override
+                       public void flush() {}
+                       @Override
+                       public void publish(LogRecord record) {
+                               Log.v("HttpTransport", record.getMessage());
+                       }
+               });
+       }
 }