Exemplo n.º 1
0
        public virtual Replication GetReplicator(IDictionary <string, object> properties)
        {
            // TODO: in the iOS equivalent of this code, there is: {@"doc_ids", _documentIDs}) - write unit test that detects this bug
            // TODO: ditto for "headers"
            Authorizer  authorizer = null;
            Replication repl       = null;
            Uri         remote     = null;
            IDictionary <string, object> remoteMap;
            IDictionary <string, object> sourceMap = ParseSourceOrTarget(properties, "source");
            IDictionary <string, object> targetMap = ParseSourceOrTarget(properties, "target");
            string source = (string)sourceMap.Get("url");
            string target = (string)targetMap.Get("url");
            bool   createTargetBoolean = (bool)properties.Get("create_target");
            bool   createTarget        = (createTargetBoolean != null && createTargetBoolean);
            bool   continuousBoolean   = (bool)properties.Get("continuous");
            bool   continuous          = (continuousBoolean != null && continuousBoolean);
            bool   cancelBoolean       = (bool)properties.Get("cancel");
            bool   cancel = (cancelBoolean != null && cancelBoolean);

            // Map the 'source' and 'target' JSON params to a local database and remote URL:
            if (source == null || target == null)
            {
                throw new CouchbaseLiteException("source and target are both null", new Status(Status
                                                                                               .BadRequest));
            }
            bool     push      = false;
            Database db        = null;
            string   remoteStr = null;

            if (Couchbase.Lite.Manager.IsValidDatabaseName(source))
            {
                db        = GetExistingDatabase(source);
                remoteStr = target;
                push      = true;
                remoteMap = targetMap;
            }
            else
            {
                remoteStr = source;
                if (createTarget && !cancel)
                {
                    bool mustExist = false;
                    db = GetDatabaseWithoutOpening(target, mustExist);
                    if (!db.Open())
                    {
                        throw new CouchbaseLiteException("cannot open database: " + db, new Status(Status
                                                                                                   .InternalServerError));
                    }
                }
                else
                {
                    db = GetExistingDatabase(target);
                }
                if (db == null)
                {
                    throw new CouchbaseLiteException("database is null", new Status(Status.NotFound));
                }
                remoteMap = sourceMap;
            }
            IDictionary <string, object> authMap = (IDictionary <string, object>)remoteMap.Get(
                "auth");

            if (authMap != null)
            {
                IDictionary <string, object> persona = (IDictionary <string, object>)authMap.Get("persona"
                                                                                                 );
                if (persona != null)
                {
                    string email = (string)persona.Get("email");
                    authorizer = new PersonaAuthorizer(email);
                }
                IDictionary <string, object> facebook = (IDictionary <string, object>)authMap.Get("facebook"
                                                                                                  );
                if (facebook != null)
                {
                    string email = (string)facebook.Get("email");
                    authorizer = new FacebookAuthorizer(email);
                }
            }
            try
            {
                remote = new Uri(remoteStr);
            }
            catch (UriFormatException)
            {
                throw new CouchbaseLiteException("malformed remote url: " + remoteStr, new Status
                                                     (Status.BadRequest));
            }
            if (remote == null || !remote.Scheme.StartsWith("http"))
            {
                throw new CouchbaseLiteException("remote URL is null or non-http: " + remoteStr,
                                                 new Status(Status.BadRequest));
            }
            if (!cancel)
            {
                repl = db.GetReplicator(remote, GetDefaultHttpClientFactory(), push, continuous,
                                        GetWorkExecutor());
                if (repl == null)
                {
                    throw new CouchbaseLiteException("unable to create replicator with remote: " + remote
                                                     , new Status(Status.InternalServerError));
                }
                if (authorizer != null)
                {
                    repl.SetAuthorizer(authorizer);
                }
                string filterName = (string)properties.Get("filter");
                if (filterName != null)
                {
                    repl.SetFilter(filterName);
                    IDictionary <string, object> filterParams = (IDictionary <string, object>)properties
                                                                .Get("query_params");
                    if (filterParams != null)
                    {
                        repl.SetFilterParams(filterParams);
                    }
                }
                if (push)
                {
                    ((Pusher)repl).SetCreateTarget(createTarget);
                }
            }
            else
            {
                // Cancel replication:
                repl = db.GetActiveReplicator(remote, push);
                if (repl == null)
                {
                    throw new CouchbaseLiteException("unable to lookup replicator with remote: " + remote
                                                     , new Status(Status.NotFound));
                }
            }
            return(repl);
        }