|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 package org.mozilla.gecko.sync; |
|
6 |
|
7 import java.net.URI; |
|
8 |
|
9 import org.mozilla.gecko.background.common.log.Logger; |
|
10 import org.mozilla.gecko.sync.crypto.KeyBundle; |
|
11 import org.mozilla.gecko.sync.net.AuthHeaderProvider; |
|
12 |
|
13 import android.content.SharedPreferences; |
|
14 import android.content.SharedPreferences.Editor; |
|
15 |
|
16 /** |
|
17 * Override SyncConfiguration to restore the old behavior of clusterURL -- |
|
18 * that is, a URL without the protocol version etc. |
|
19 * |
|
20 */ |
|
21 public class Sync11Configuration extends SyncConfiguration { |
|
22 private static final String LOG_TAG = "Sync11Configuration"; |
|
23 private static final String API_VERSION = "1.1"; |
|
24 |
|
25 public Sync11Configuration(String username, |
|
26 AuthHeaderProvider authHeaderProvider, |
|
27 SharedPreferences prefs) { |
|
28 super(username, authHeaderProvider, prefs); |
|
29 } |
|
30 |
|
31 public Sync11Configuration(String username, |
|
32 AuthHeaderProvider authHeaderProvider, |
|
33 SharedPreferences prefs, |
|
34 KeyBundle keyBundle) { |
|
35 super(username, authHeaderProvider, prefs, keyBundle); |
|
36 } |
|
37 |
|
38 @Override |
|
39 public String getAPIVersion() { |
|
40 return API_VERSION; |
|
41 } |
|
42 |
|
43 @Override |
|
44 public String storageURL() { |
|
45 return clusterURL + API_VERSION + "/" + username + "/storage"; |
|
46 } |
|
47 |
|
48 @Override |
|
49 protected String infoBaseURL() { |
|
50 return clusterURL + API_VERSION + "/" + username + "/info/"; |
|
51 } |
|
52 |
|
53 protected void setAndPersistClusterURL(URI u, SharedPreferences prefs) { |
|
54 boolean shouldPersist = (prefs != null) && (clusterURL == null); |
|
55 |
|
56 Logger.trace(LOG_TAG, "Setting cluster URL to " + u.toASCIIString() + |
|
57 (shouldPersist ? ". Persisting." : ". Not persisting.")); |
|
58 clusterURL = u; |
|
59 if (shouldPersist) { |
|
60 Editor edit = prefs.edit(); |
|
61 edit.putString(PREF_CLUSTER_URL, clusterURL.toASCIIString()); |
|
62 edit.commit(); |
|
63 } |
|
64 } |
|
65 |
|
66 protected void setClusterURL(URI u, SharedPreferences prefs) { |
|
67 if (u == null) { |
|
68 Logger.warn(LOG_TAG, "Refusing to set cluster URL to null."); |
|
69 return; |
|
70 } |
|
71 URI uri = u.normalize(); |
|
72 if (uri.toASCIIString().endsWith("/")) { |
|
73 setAndPersistClusterURL(u, prefs); |
|
74 return; |
|
75 } |
|
76 setAndPersistClusterURL(uri.resolve("/"), prefs); |
|
77 Logger.trace(LOG_TAG, "Set cluster URL to " + clusterURL.toASCIIString() + ", given input " + u.toASCIIString()); |
|
78 } |
|
79 |
|
80 @Override |
|
81 public void setClusterURL(URI u) { |
|
82 setClusterURL(u, this.getPrefs()); |
|
83 } |
|
84 } |