enable HTTP logging
[WhereAmI.git] / src / org / average / whereami / Oracle.java
index 157c988a3f3df4c3ea77e8b349d2713c0c653bf4..b3d992e410a437fe0e9852407aa7d830854696f5 100644 (file)
@@ -1,6 +1,13 @@
 package org.average.whereami;
 
-import org.average.whereami.CredentialStore;
+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;
@@ -14,47 +21,80 @@ import com.google.api.client.json.jackson.JacksonFactory;
 import android.content.res.Resources;
 import android.util.Log;
 
-public class Oracle {
+abstract public class Oracle {
 
        final String TAG = getClass().getName();
 
-       public HttpTransport httpTransport;
-       public JsonFactory jsonFactory;
+       static public PersistentStore store;
+       static public HttpTransport httpTransport =
+                       AndroidHttp.newCompatibleTransport();
+       static public JsonFactory jsonFactory = new JacksonFactory();
+       static public String authErrorMessage;
 
-       public GoogleAccessProtectedResource accessProtectedResource;
+       public Oracle(final Resources res, final PersistentStore store) {
+               authErrorMessage = res.getString(R.string.autherror);
+               this.store = store;
+               enableLogging();
+       }
 
-       public String authErrorMessage;
+       private class MyAccessProtectedResource
+               extends GoogleAccessProtectedResource {
 
-       public Oracle(final Resources res, final CredentialStore store) {
-               authErrorMessage = res.getString(R.string.autherror);
+               public MyAccessProtectedResource(
+                               String accessToken,
+                               HttpTransport transport,
+                               JsonFactory jsonFactory,
+                               String clientId,
+                               String clientSecret,
+                               String refreshToken) {
+                       super(accessToken, transport, jsonFactory,
+                               clientId, clientSecret, refreshToken);
+               }
 
-               httpTransport = AndroidHttp.newCompatibleTransport();
-               jsonFactory = new JacksonFactory();
-               AccessTokenResponse token = store.read();
+               @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 +
                        "\"");
-               accessProtectedResource = 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);
        }
 
-       public String getResult() {
-               return "<need to be overriden>";
+       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());
+                       }
+               });
        }
 }