mobile/android/base/sync/config/ClientRecordTerminator.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/sync/config/ClientRecordTerminator.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,76 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +package org.mozilla.gecko.sync.config;
     1.9 +
    1.10 +import java.net.URI;
    1.11 +
    1.12 +import org.mozilla.gecko.background.common.log.Logger;
    1.13 +import org.mozilla.gecko.sync.SyncConfiguration;
    1.14 +import org.mozilla.gecko.sync.net.AuthHeaderProvider;
    1.15 +import org.mozilla.gecko.sync.net.BaseResource;
    1.16 +import org.mozilla.gecko.sync.net.SyncStorageRecordRequest;
    1.17 +import org.mozilla.gecko.sync.net.SyncStorageRequestDelegate;
    1.18 +import org.mozilla.gecko.sync.net.SyncStorageResponse;
    1.19 +
    1.20 +/**
    1.21 + * Bug 770785: when an Android Account is deleted, we need to (try to) delete
    1.22 + * the associated client GUID from the server's clients table.
    1.23 + * <p>
    1.24 + * This class provides a static method to do that.
    1.25 + */
    1.26 +public class ClientRecordTerminator {
    1.27 +  public static final String LOG_TAG = "ClientRecTerminator";
    1.28 +
    1.29 +  protected ClientRecordTerminator() {
    1.30 +    super(); // Stop this class from being instantiated.
    1.31 +  }
    1.32 +
    1.33 +  public static void deleteClientRecord(final SyncConfiguration config, final String clientGUID)
    1.34 +    throws Exception {
    1.35 +
    1.36 +    final String collection = "clients";
    1.37 +    final URI wboURI = config.wboURI(collection, clientGUID);
    1.38 +
    1.39 +    // Would prefer to break this out into a self-contained client library.
    1.40 +    final SyncStorageRecordRequest r = new SyncStorageRecordRequest(wboURI);
    1.41 +    r.delegate = new SyncStorageRequestDelegate() {
    1.42 +      @Override
    1.43 +      public AuthHeaderProvider getAuthHeaderProvider() {
    1.44 +        return config.getAuthHeaderProvider();
    1.45 +      }
    1.46 +
    1.47 +      @Override
    1.48 +      public String ifUnmodifiedSince() {
    1.49 +        return null;
    1.50 +      }
    1.51 +
    1.52 +      @Override
    1.53 +      public void handleRequestSuccess(SyncStorageResponse response) {
    1.54 +        Logger.info(LOG_TAG, "Deleted client record with GUID " + clientGUID + " from server.");
    1.55 +        BaseResource.consumeEntity(response);
    1.56 +      }
    1.57 +
    1.58 +      @Override
    1.59 +      public void handleRequestFailure(SyncStorageResponse response) {
    1.60 +        Logger.warn(LOG_TAG, "Failed to delete client record with GUID " + clientGUID + " from server.");
    1.61 +        try {
    1.62 +          Logger.warn(LOG_TAG, "Server error message was: " + response.getErrorMessage());
    1.63 +        } catch (Exception e) {
    1.64 +          // Do nothing.
    1.65 +        }
    1.66 +        BaseResource.consumeEntity(response);
    1.67 +      }
    1.68 +
    1.69 +      @Override
    1.70 +      public void handleRequestError(Exception ex) {
    1.71 +        // It could be that we don't have network access when trying
    1.72 +        // to remove an Account; not much to be done in this situation.
    1.73 +        Logger.error(LOG_TAG, "Got exception trying to delete client record with GUID " + clientGUID + " from server; ignoring.", ex);
    1.74 +      }
    1.75 +    };
    1.76 +
    1.77 +    r.delete();
    1.78 +  }
    1.79 +}

mercurial