gecko/mobile/android/base/sync/SyncConfiguration.java

157 lines
5.0 KiB
Java

/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Android Sync Client.
*
* The Initial Developer of the Original Code is
* the Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2011
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Richard Newman <rnewman@mozilla.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
package org.mozilla.gecko.sync;
import java.net.URI;
import java.net.URISyntaxException;
import org.mozilla.gecko.sync.crypto.KeyBundle;
import android.util.Log;
public class SyncConfiguration implements CredentialsSource {
public static final String DEFAULT_USER_API = "https://auth.services.mozilla.com/user/1.0/";
private static final String LOG_TAG = "SyncConfiguration";
// These must be set in GlobalSession's constructor.
public String userAPI;
public URI serverURL;
public URI clusterURL;
public String username;
public KeyBundle syncKeyBundle;
public CollectionKeys collectionKeys;
public InfoCollections infoCollections;
public MetaGlobal metaGlobal;
public String password;
public String syncID;
public SyncConfiguration() {
}
@Override
public String credentials() {
return username + ":" + password;
}
@Override
public CollectionKeys getCollectionKeys() {
return collectionKeys;
}
@Override
public KeyBundle keyForCollection(String collection) throws NoCollectionKeysSetException {
return getCollectionKeys().keyBundleForCollection(collection);
}
public void setCollectionKeys(CollectionKeys k) {
collectionKeys = k;
}
public String nodeWeaveURL() {
return this.nodeWeaveURL((this.serverURL == null) ? null : this.serverURL.toASCIIString());
}
public String nodeWeaveURL(String serverURL) {
String userPart = username + "/node/weave";
if (serverURL == null) {
return DEFAULT_USER_API + userPart;
}
if (!serverURL.endsWith("/")) {
serverURL = serverURL + "/";
}
return serverURL + "user/1.0/" + userPart;
}
public String infoURL() {
return clusterURL + GlobalSession.API_VERSION + "/" + username + "/info/collections";
}
public String metaURL() {
return clusterURL + GlobalSession.API_VERSION + "/" + username + "/storage/meta/global";
}
public String storageURL(boolean trailingSlash) {
return clusterURL + GlobalSession.API_VERSION + "/" + username +
(trailingSlash ? "/storage/" : "/storage");
}
public URI collectionURI(String collection, boolean full) throws URISyntaxException {
// Do it this way to make it easier to add more params later.
// It's pretty ugly, I'll grant.
boolean anyParams = full;
String uriParams = "";
if (anyParams) {
StringBuilder params = new StringBuilder("?");
if (full) {
params.append("full=1");
}
uriParams = params.toString();
}
String uri = storageURL(true) + collection + uriParams;
return new URI(uri);
}
public URI wboURI(String collection, String id) throws URISyntaxException {
return new URI(storageURL(true) + collection + "/" + id);
}
public URI keysURI() throws URISyntaxException {
return wboURI("crypto", "keys");
}
public void setClusterURL(URI u) {
if (u == null) {
Log.w(LOG_TAG, "Refusing to set cluster URL to null.");
return;
}
URI uri = u.normalize();
if (uri.toASCIIString().endsWith("/")) {
this.clusterURL = u;
return;
}
this.clusterURL = uri.resolve("/");
Log.i(LOG_TAG, "Set cluster URL to " + this.clusterURL.toASCIIString() + ", given input " + u.toASCIIString());
}
public void setClusterURL(String url) throws URISyntaxException {
this.setClusterURL(new URI(url));
}
}