Java Code Examples for com.google.api.client.util.store.DataStore#set()

The following examples show how to use com.google.api.client.util.store.DataStore#set() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ApigeeDataClient.java    From apigee-android-sdk with Apache License 2.0 6 votes vote down vote up
/**
 * Stores the given OAuth 2 token response within a file data store.
 * The stored token response can then retrieved using the getOAuth2TokenDataFromStore method.
 *
 * @param storageId a string object that is used to store the token response
 * @param tokenResponse the token response containing the OAuth 2 token information.
 * @return If the token response was stored or not.
 */
public Boolean storeOAuth2TokenData(String storageId, TokenResponse tokenResponse) {
    Boolean wasStored = false;
    try {
        File oauth2StorageFolder = new File(this.context.getFilesDir(),"oauth2StorageFolder");
        oauth2StorageFolder.mkdirs();
        FileDataStoreFactory fileDataStoreFactory = new FileDataStoreFactory(oauth2StorageFolder);
        DataStore<StoredCredential> storedCredentialDataStore = fileDataStoreFactory.getDataStore(storageId);
        Credential oauth2Credential = new Credential(BearerToken.authorizationHeaderAccessMethod()).setFromTokenResponse(
                tokenResponse);
        StoredCredential storedOAuth2Credential = new StoredCredential(oauth2Credential);
        storedCredentialDataStore.set(storageId,storedOAuth2Credential);
        wasStored = true;
    } catch ( Exception exception ) {
        logInfo("Exception storing OAuth2TokenData :" + exception.getLocalizedMessage());
    }
    return wasStored;
}
 
Example 2
Source File: FileDataStoreFactoryTest.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
public void testSave() throws IOException {
  FileDataStoreFactory factory = newDataStoreFactory();
  DataStore<String> store = factory.getDataStore("foo");
  store.set("k", "v");
  assertEquals(
      ImmutableSet.of("k"),
      new FileDataStoreFactory(factory.getDataDirectory()).getDataStore("foo").keySet());
  store.clear();
  assertTrue(new FileDataStoreFactory(factory.getDataDirectory()).getDataStore("foo").isEmpty());
}
 
Example 3
Source File: AppEngineCredentialStore.java    From google-oauth-java-client with Apache License 2.0 5 votes vote down vote up
/**
 * Migrates to the new format using {@link DataStore} of {@link StoredCredential}.
 *
 * @param credentialDataStore credential data store
 * @since 1.16
 */
public final void migrateTo(DataStore<StoredCredential> credentialDataStore) throws IOException {
  DatastoreService service = DatastoreServiceFactory.getDatastoreService();
  PreparedQuery queryResult = service.prepare(new Query(KIND));
  for (Entity entity : queryResult.asIterable()) {
    StoredCredential storedCredential = new StoredCredential().setAccessToken(
        (String) entity.getProperty("accessToken"))
        .setRefreshToken((String) entity.getProperty("refreshToken"))
        .setExpirationTimeMilliseconds((Long) entity.getProperty("expirationTimeMillis"));
    credentialDataStore.set(entity.getKey().getName(), storedCredential);
  }
}
 
Example 4
Source File: FilePersistedCredentials.java    From google-oauth-java-client with Apache License 2.0 4 votes vote down vote up
void migrateTo(DataStore<StoredCredential> typedDataStore) throws IOException {
  for (Map.Entry<String, FilePersistedCredential> entry : credentials.entrySet()) {
    typedDataStore.set(entry.getKey(), entry.getValue().toStoredCredential());
  }
}
 
Example 5
Source File: StoredChannel.java    From google-api-java-client with Apache License 2.0 3 votes vote down vote up
/**
 * Stores this notification channel in the given notification channel data store.
 *
 * <p>
 * It is important that this method be called before the watch HTTP request is made in case the
 * notification is received before the watch HTTP response is received.
 * </p>
 *
 * @param dataStore notification channel data store
 */
public StoredChannel store(DataStore<StoredChannel> dataStore) throws IOException {
  lock.lock();
  try {
    dataStore.set(getId(), this);
    return this;
  } finally {
    lock.unlock();
  }
}