Beispiel #1
0
  /**
   * Upload new crypto/keys.
   *
   * @param keys new keys.
   * @param keyUploadDelegate a delegate.
   */
  public void uploadKeys(final CollectionKeys keys, final KeyUploadDelegate keyUploadDelegate) {
    SyncStorageRecordRequest request;
    try {
      request = new SyncStorageRecordRequest(this.config.keysURI());
    } catch (URISyntaxException e) {
      keyUploadDelegate.onKeyUploadFailed(e);
      return;
    }

    request.delegate =
        new SyncStorageRequestDelegate() {

          @Override
          public String ifUnmodifiedSince() {
            return null;
          }

          @Override
          public void handleRequestSuccess(SyncStorageResponse response) {
            Logger.debug(LOG_TAG, "Keys uploaded.");
            BaseResource.consumeEntity(response); // We don't need the response at all.
            keyUploadDelegate.onKeysUploaded();
          }

          @Override
          public void handleRequestFailure(SyncStorageResponse response) {
            Logger.debug(LOG_TAG, "Failed to upload keys.");
            GlobalSession.this.interpretHTTPFailure(response.httpResponse());
            BaseResource.consumeEntity(
                response); // The exception thrown should not need the body of the response.
            keyUploadDelegate.onKeyUploadFailed(new HTTPFailureException(response));
          }

          @Override
          public void handleRequestError(Exception ex) {
            Logger.warn(LOG_TAG, "Got exception trying to upload keys", ex);
            keyUploadDelegate.onKeyUploadFailed(ex);
          }

          @Override
          public AuthHeaderProvider getAuthHeaderProvider() {
            return GlobalSession.this.getAuthHeaderProvider();
          }
        };

    // Convert keys to an encrypted crypto record.
    CryptoRecord keysRecord;
    try {
      keysRecord = keys.asCryptoRecord();
      keysRecord.setKeyBundle(config.syncKeyBundle);
      keysRecord.encrypt();
    } catch (Exception e) {
      Logger.warn(LOG_TAG, "Got exception trying creating crypto record from keys", e);
      keyUploadDelegate.onKeyUploadFailed(e);
      return;
    }

    request.put(keysRecord);
  }