enable HTTP logging
[WhereAmI.git] / src / org / average / whereami / Oracle.java
1 package org.average.whereami;
2
3 import org.average.whereami.PersistentStore;
4
5 import java.io.IOException;
6 import com.google.api.client.http.HttpTransport;
7 import java.util.logging.Handler;
8 import java.util.logging.Level;
9 import java.util.logging.LogRecord;
10 import java.util.logging.Logger;
11
12 import com.google.api.client.extensions.android2.AndroidHttp;
13 import com.google.api.client.auth.oauth2.draft10.AccessTokenResponse;
14 import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessProtectedResource;
15 import com.google.api.client.http.HttpResponseException;
16 import com.google.api.client.http.HttpTransport;
17 import com.google.api.client.http.javanet.NetHttpTransport;
18 import com.google.api.client.json.JsonFactory;
19 import com.google.api.client.json.jackson.JacksonFactory;
20
21 import android.content.res.Resources;
22 import android.util.Log;
23
24 abstract public class Oracle {
25
26         final String TAG = getClass().getName();
27
28         static public PersistentStore store;
29         static public HttpTransport httpTransport =
30                         AndroidHttp.newCompatibleTransport();
31         static public JsonFactory jsonFactory = new JacksonFactory();
32         static public String authErrorMessage;
33
34         public Oracle(final Resources res, final PersistentStore store) {
35                 authErrorMessage = res.getString(R.string.autherror);
36                 this.store = store;
37                 enableLogging();
38         }
39
40         private class MyAccessProtectedResource
41                 extends GoogleAccessProtectedResource {
42
43                 public MyAccessProtectedResource(
44                                 String accessToken,
45                                 HttpTransport transport,
46                                 JsonFactory jsonFactory,
47                                 String clientId,
48                                 String clientSecret,
49                                 String refreshToken) {
50                         super(accessToken, transport, jsonFactory,
51                                 clientId, clientSecret, refreshToken);
52                 }
53
54                 @Override
55                 public void onAccessToken(String accessToken) {
56                         Log.v(TAG, "Update access token to \""
57                                 + accessToken + "\"");
58                         store.updateAccessToken( accessToken);
59                 }
60
61                 @Override
62                 protected boolean executeRefreshToken() throws IOException {
63                         Log.v(TAG, "executeRefreshToken was called");
64                         return super.executeRefreshToken();
65                 }
66         }
67
68         public MyAccessProtectedResource
69                                 newMyAccessProtectedResource() {
70                 AccessTokenResponse token = store.readTokens();
71                 Log.v(TAG, "tokens - access: \"" + token.accessToken +
72                         "\", refresh: \"" + token.refreshToken +
73                         "\", client_id: \"" + ClientCredentials.CLIENT_ID +
74                         "\", client_secret: \"" +
75                                 ClientCredentials.CLIENT_SECRET +
76                         "\"");
77                 return new MyAccessProtectedResource(
78                         token.accessToken, httpTransport, jsonFactory,
79                         ClientCredentials.CLIENT_ID,
80                         ClientCredentials.CLIENT_SECRET,
81                         token.refreshToken);
82         }
83
84         abstract public String getResult();
85
86         public static void enableLogging() {
87                 Logger logger = Logger.getLogger(HttpTransport.class.getName());
88                 logger.setLevel(Level.CONFIG);
89                 logger.addHandler(new Handler() {
90                         @Override
91                         public void close() throws SecurityException {}
92                         @Override
93                         public void flush() {}
94                         @Override
95                         public void publish(LogRecord record) {
96                                 Log.v("HttpTransport", record.getMessage());
97                         }
98                 });
99         }
100 }