Keyboard Shortcuts

File
u :up to issue
m :publish + mail comments
M :edit review message
j / k :jump to file after / before current file
J / K :jump to next file with a comment after / before current file
Side-by-side diff
i :toggle intra-line diffs
e :expand all comments
c :collapse all comments
s :toggle showing all comments
n / p :next / previous diff chunk or comment
N / P :next / previous comment
<Up> / <Down> :next / previous line
<Enter> :respond to / edit current comment
d :mark current comment as done
Issue
u :up to list of issues
m :publish + mail comments
j / k :jump to patch after / before current patch
o / <Enter> :open current patch in side-by-side view
i :open current patch in unified diff view
Issue List
j / k :jump to issue after / before current issue
o / <Enter> :open current issue
# : close issue
Comment/message editing
<Ctrl> + s or <Ctrl> + Enter :save comment
<Esc> :cancel edit
Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(46)
Issues Repositories Search
Open Issues | Closed Issues | All Issues | Sign in with your Google Account to create issues and add comments

Unified Diff: bigquery-appengine-sample/src/main/java/com/google/api/client/sample/bigquery/appengine/dashboard/AuthUtils.java

Issue 5414043: Uploading Bigquery example App Engine dashboard Base URL: https://google-api-java-client.googlecode.com/hg/
Patch Set: Removing unnecessary dependency. Created 14 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « bigquery-appengine-sample/src/main/java/com/google/api/client/sample/bigquery/appengine/dashboard/AuthServlet.java ('k') | bigquery-appengine-sample/src/main/java/com/google/api/client/sample/bigquery/appengine/dashboard/BigqueryUtils.java » ('j') | no next file with comments »
('e') | ('c') | ('s')
Index: bigquery-appengine-sample/src/main/java/com/google/api/client/sample/bigquery/appengine/dashboard/AuthUtils.java
===================================================================
new file mode 100755
--- /dev/null
+++ b/bigquery-appengine-sample/src/main/java/com/google/api/client/sample/bigquery/appengine/dashboard/AuthUtils.java
@@ -0,0 +1,126 @@
+// Copyright 2011 Google Inc. All Rights Reserved.
+
+package com.google.api.client.sample.bigquery.appengine.dashboard;
+
+import com.google.api.client.extensions.auth.helpers.ThreeLeggedFlow;
+import com.google.api.client.extensions.auth.helpers.oauth2.draft10.OAuth2Credential;
+import com.google.api.client.googleapis.extensions.auth.helpers.oauth2.draft10.GoogleOAuth2ThreeLeggedFlow;
+import com.google.api.client.http.HttpTransport;
+import com.google.api.client.json.JsonFactory;
+import com.google.api.services.samples.shared.appengine.AppEngineUtils;
+import com.google.api.services.samples.shared.appengine.OAuth2ClientCredentials;
+
+import java.io.IOException;
+import java.util.logging.Logger;
+
+import javax.jdo.PersistenceManager;
+import javax.jdo.PersistenceManagerFactory;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * @author lparkinson@google.com (Laura Parkinson)
+ *
+ * Contains authorization variables and helper functions that multiple servlets need to access.
+ * Also supervises the OAuth tokens, refreshing them when asked and tracking the most
+ * recent access token.
+ */
+public class AuthUtils {
+
+ private static final Logger log = Logger.getLogger(AuthUtils.class.getName());
+
+ static final String BIGQUERY_SCOPE = "https://www.googleapis.com/auth/bigquery";
+ static final PersistenceManagerFactory PM_FACTORY =
+ AppEngineUtils.getPersistenceManagerFactory();
+
+ private String userId;
+ private HttpTransport transport;
+ private JsonFactory factory;
+
+ public AuthUtils(String userId, HttpTransport transport, JsonFactory factory) {
+ this.userId = userId;
+ this.transport = transport;
+ this.factory = factory;
+ }
+
+ public ThreeLeggedFlow newFlow(String callbackUrl) throws SampleDashboardException {
+ try {
+ return new GoogleOAuth2ThreeLeggedFlow(userId, OAuth2ClientCredentials.getClientId(),
+ OAuth2ClientCredentials.getClientSecret(), BIGQUERY_SCOPE, callbackUrl);
+ } catch (IOException ex) {
+ throw new SampleDashboardException(ex);
+ }
+ }
+
+ /**
+ * Removes the credentials for the given user from the datastore.
+ * @throws SampleDashboardException
+ */
+ public void clearTokens() throws SampleDashboardException {
+ PersistenceManager persistenceManager = PM_FACTORY.getPersistenceManager();
+ try {
+ OAuth2Credential credential = getCredential(persistenceManager);
+ persistenceManager.deletePersistent(credential);
+ } finally {
+ persistenceManager.close();
+ }
+ }
+
+ /**
+ * Uses the refresh token in the datastore to get a new access token. Stores and returns it.
+ */
+ public String refreshAccessToken() {
+ String access = null;
+ PersistenceManager persistenceManager = PM_FACTORY.getPersistenceManager();
+ try {
+ OAuth2Credential credential = getCredential(persistenceManager);
+ credential.refresh(transport, factory);
+ access = credential.getAccessToken();
+ } catch (IOException e) {
+ log.warning("Unable to refresh access token.");
+ access = null;
+ } finally {
+ persistenceManager.close();
+ }
+ return access;
+ }
+
+ /**
+ * Retrieves the access token from the datastore.
+ * @throws SampleDashboardException
+ */
+ public String getAccessToken() throws SampleDashboardException {
+ String access = null;
+ PersistenceManager persistenceManager = PM_FACTORY.getPersistenceManager();
+ try {
+ OAuth2Credential credential = getCredential(persistenceManager);
+ access = credential.getAccessToken();
+ } finally {
+ persistenceManager.close();
+ }
+ return access;
+ }
+
+ private OAuth2Credential getCredential(PersistenceManager persistenceManager)
+ throws SampleDashboardException {
+ ThreeLeggedFlow oauthFlow = newFlow(null);
+ oauthFlow.setJsonFactory(factory);
+ oauthFlow.setHttpTransport(transport);
+ return (OAuth2Credential) oauthFlow.loadCredential(persistenceManager);
+ }
+
+ /**
+ * Clears user credentials if a given exception is an unauthorized exception.
+ */
+ public boolean handleUnauthorizedException(SampleDashboardException ex) {
+ if (ex.getStatusCode() == HttpServletResponse.SC_UNAUTHORIZED) {
+ log.warning("User credentials didn't work, so they were deleted.");
+ try {
+ clearTokens();
+ return true;
+ } catch (SampleDashboardException ex2) {
+ return false;
+ }
+ }
+ return false;
+ }
+}
« no previous file with comments | « bigquery-appengine-sample/src/main/java/com/google/api/client/sample/bigquery/appengine/dashboard/AuthServlet.java ('k') | bigquery-appengine-sample/src/main/java/com/google/api/client/sample/bigquery/appengine/dashboard/BigqueryUtils.java » ('j') | no next file with comments »
Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b

AltStyle によって変換されたページ (->オリジナル) /