michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.sync.config; michael@0: michael@0: import java.net.URI; michael@0: michael@0: import org.mozilla.gecko.background.common.log.Logger; michael@0: import org.mozilla.gecko.sync.SyncConfiguration; michael@0: import org.mozilla.gecko.sync.net.AuthHeaderProvider; michael@0: import org.mozilla.gecko.sync.net.BaseResource; michael@0: import org.mozilla.gecko.sync.net.SyncStorageRecordRequest; michael@0: import org.mozilla.gecko.sync.net.SyncStorageRequestDelegate; michael@0: import org.mozilla.gecko.sync.net.SyncStorageResponse; michael@0: michael@0: /** michael@0: * Bug 770785: when an Android Account is deleted, we need to (try to) delete michael@0: * the associated client GUID from the server's clients table. michael@0: *
michael@0: * This class provides a static method to do that. michael@0: */ michael@0: public class ClientRecordTerminator { michael@0: public static final String LOG_TAG = "ClientRecTerminator"; michael@0: michael@0: protected ClientRecordTerminator() { michael@0: super(); // Stop this class from being instantiated. michael@0: } michael@0: michael@0: public static void deleteClientRecord(final SyncConfiguration config, final String clientGUID) michael@0: throws Exception { michael@0: michael@0: final String collection = "clients"; michael@0: final URI wboURI = config.wboURI(collection, clientGUID); michael@0: michael@0: // Would prefer to break this out into a self-contained client library. michael@0: final SyncStorageRecordRequest r = new SyncStorageRecordRequest(wboURI); michael@0: r.delegate = new SyncStorageRequestDelegate() { michael@0: @Override michael@0: public AuthHeaderProvider getAuthHeaderProvider() { michael@0: return config.getAuthHeaderProvider(); michael@0: } michael@0: michael@0: @Override michael@0: public String ifUnmodifiedSince() { michael@0: return null; michael@0: } michael@0: michael@0: @Override michael@0: public void handleRequestSuccess(SyncStorageResponse response) { michael@0: Logger.info(LOG_TAG, "Deleted client record with GUID " + clientGUID + " from server."); michael@0: BaseResource.consumeEntity(response); michael@0: } michael@0: michael@0: @Override michael@0: public void handleRequestFailure(SyncStorageResponse response) { michael@0: Logger.warn(LOG_TAG, "Failed to delete client record with GUID " + clientGUID + " from server."); michael@0: try { michael@0: Logger.warn(LOG_TAG, "Server error message was: " + response.getErrorMessage()); michael@0: } catch (Exception e) { michael@0: // Do nothing. michael@0: } michael@0: BaseResource.consumeEntity(response); michael@0: } michael@0: michael@0: @Override michael@0: public void handleRequestError(Exception ex) { michael@0: // It could be that we don't have network access when trying michael@0: // to remove an Account; not much to be done in this situation. michael@0: Logger.error(LOG_TAG, "Got exception trying to delete client record with GUID " + clientGUID + " from server; ignoring.", ex); michael@0: } michael@0: }; michael@0: michael@0: r.delete(); michael@0: } michael@0: }