summaryrefslogtreecommitdiffstats
path: root/mobile/android/services/src/main/java/org/mozilla/gecko/sync/Sync11Configuration.java
blob: 4b2280895589da0befb62b45e769140270bed99a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.gecko.sync;

import java.net.URI;

import org.mozilla.gecko.background.common.log.Logger;
import org.mozilla.gecko.sync.crypto.KeyBundle;
import org.mozilla.gecko.sync.net.AuthHeaderProvider;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * Override SyncConfiguration to restore the old behavior of clusterURL --
 * that is, a URL without the protocol version etc.
 *
 */
public class Sync11Configuration extends SyncConfiguration {
  private static final String LOG_TAG = "Sync11Configuration";
  private static final String API_VERSION = "1.1";

  public Sync11Configuration(String username,
                             AuthHeaderProvider authHeaderProvider,
                             SharedPreferences prefs) {
    super(username, authHeaderProvider, prefs);
  }

  public Sync11Configuration(String username,
                             AuthHeaderProvider authHeaderProvider,
                             SharedPreferences prefs,
                             KeyBundle keyBundle) {
    super(username, authHeaderProvider, prefs, keyBundle);
  }

  @Override
  public String getAPIVersion() {
    return API_VERSION;
  }

  @Override
  public String storageURL() {
    return clusterURL + API_VERSION + "/" + username + "/storage";
  }

  @Override
  protected String infoBaseURL() {
    return clusterURL + API_VERSION + "/" + username + "/info/";
  }

  protected void setAndPersistClusterURL(URI u, SharedPreferences prefs) {
    boolean shouldPersist = (prefs != null) && (clusterURL == null);

    Logger.trace(LOG_TAG, "Setting cluster URL to " + u.toASCIIString() +
                          (shouldPersist ? ". Persisting." : ". Not persisting."));
    clusterURL = u;
    if (shouldPersist) {
      Editor edit = prefs.edit();
      edit.putString(PREF_CLUSTER_URL, clusterURL.toASCIIString());
      edit.commit();
    }
  }

  protected void setClusterURL(URI u, SharedPreferences prefs) {
    if (u == null) {
      Logger.warn(LOG_TAG, "Refusing to set cluster URL to null.");
      return;
    }
    URI uri = u.normalize();
    if (uri.toASCIIString().endsWith("/")) {
      setAndPersistClusterURL(u, prefs);
      return;
    }
    setAndPersistClusterURL(uri.resolve("/"), prefs);
    Logger.trace(LOG_TAG, "Set cluster URL to " + clusterURL.toASCIIString() + ", given input " + u.toASCIIString());
  }

  @Override
  public void setClusterURL(URI u) {
    setClusterURL(u, this.getPrefs());
  }
}