@@ -574,14 +574,6 @@ public synchronized Database getDatabase(String name, boolean mustExist) {
574574 */
575575 @ InterfaceAudience .Private
576576 public Replication getReplicator (Map <String , Object > properties ) throws CouchbaseLiteException {
577-
578- // TODO: in the iOS equivalent of this code, there is: {@"doc_ids", _documentIDs}) - write unit test that detects this bug
579- // TODO: ditto for "headers"
580-
581- Authorizer authorizer = null ;
582- Replication repl = null ;
583- URL remote = null ;
584-
585577 Map <String , Object > remoteMap ;
586578
587579 Map <String , Object > sourceMap = parseSourceOrTarget (properties , "source" );
@@ -601,7 +593,7 @@ public Replication getReplicator(Map<String, Object> properties) throws Couchbas
601593
602594 // Map the 'source' and 'target' JSON params to a local database and remote URL:
603595 if (source == null || target == null ) {
604- throw new CouchbaseLiteException ("source and target are both null" , new Status (Status .BAD_REQUEST ));
596+ throw new CouchbaseLiteException ("Source and target are both null" , new Status (Status .BAD_REQUEST ));
605597 }
606598
607599 boolean push = false ;
@@ -623,7 +615,7 @@ public Replication getReplicator(Map<String, Object> properties) throws Couchbas
623615 db = getExistingDatabase (target );
624616 }
625617 if (db == null ) {
626- throw new CouchbaseLiteException ("database is null" , new Status (Status .NOT_FOUND ));
618+ throw new CouchbaseLiteException ("Database is null" , new Status (Status .NOT_FOUND ));
627619 }
628620 remoteMap = sourceMap ;
629621 }
@@ -633,20 +625,21 @@ public Replication getReplicator(Map<String, Object> properties) throws Couchbas
633625 throw new CouchbaseLiteException ("Can't specify both a filter and doc IDs" ,
634626 new Status (Status .BAD_REQUEST ));
635627
628+ URL remote = null ;
636629 try {
637630 remote = new URL (remoteStr );
638631 } catch (MalformedURLException e ) {
639- throw new CouchbaseLiteException ("malformed remote url: " + remoteStr ,
632+ throw new CouchbaseLiteException ("Malformed remote url: " + remoteStr ,
640633 new Status (Status .BAD_REQUEST ));
641634 }
642635 if (remote == null ) {
643- throw new CouchbaseLiteException ("remote URL is null: " + remoteStr ,
636+ throw new CouchbaseLiteException ("Remote URL is null: " + remoteStr ,
644637 new Status (Status .BAD_REQUEST ));
645638 }
646639
640+ Authorizer authorizer = null ;
647641 Map <String , Object > authMap = (Map <String , Object >) remoteMap .get ("auth" );
648642 if (authMap != null ) {
649-
650643 Map <String , Object > persona = (Map <String , Object >) authMap .get ("persona" );
651644 if (persona != null ) {
652645 String email = (String ) persona .get ("email" );
@@ -661,63 +654,51 @@ public Replication getReplicator(Map<String, Object> properties) throws Couchbas
661654 authorizer .setLocalUUID (db .publicUUID ());
662655 }
663656
657+ Replication repl = db .createReplicator (remote , push , getDefaultHttpClientFactory ());
664658
659+ repl .setContinuous (continuous );
665660
666- if (!cancel ) {
667- repl = db .getReplicator (remote , getDefaultHttpClientFactory (), push , continuous );
668- if (repl == null ) {
669- throw new CouchbaseLiteException ("unable to create replicator with remote: " + remote ,
670- new Status (Status .INTERNAL_SERVER_ERROR ));
671- }
672-
673- if (authorizer != null ) {
674- repl .setAuthenticator (authorizer );
675- }
661+ if (authorizer != null ) {
662+ repl .setAuthenticator (authorizer );
663+ }
676664
677- Map <String , Object > headers = null ;
678- if (remoteMap != null ) {
679- headers = (Map ) remoteMap .get ("headers" );
680- }
665+ Map <String , Object > headers = null ;
666+ if (remoteMap != null ) {
667+ headers = (Map ) remoteMap .get ("headers" );
668+ }
681669
682- if (headers != null && !headers .isEmpty ()) {
683- repl .setHeaders (headers );
684- }
670+ if (headers != null && !headers .isEmpty ()) {
671+ repl .setHeaders (headers );
672+ }
685673
686- String filterName = (String ) properties .get ("filter" );
687- if (filterName != null ) {
688- repl .setFilter (filterName );
689- Map <String , Object > filterParams = (Map <String , Object >) properties .get ("query_params" );
690- if (filterParams != null ) {
691- repl .setFilterParams (filterParams );
692- }
674+ String filterName = (String ) properties .get ("filter" );
675+ if (filterName != null ) {
676+ repl .setFilter (filterName );
677+ Map <String , Object > filterParams = (Map <String , Object >) properties .get ("query_params" );
678+ if (filterParams != null ) {
679+ repl .setFilterParams (filterParams );
693680 }
681+ }
694682
695- // docIDs
696- if (properties .get ("doc_ids" ) != null ) {
697- if (properties .get ("doc_ids" ) instanceof List ){
698- List <String > docIds = (List <String >)properties .get ("doc_ids" );
699- repl .setDocIds (docIds );
700- }
683+ // docIDs
684+ if (properties .get ("doc_ids" ) != null ) {
685+ if (properties .get ("doc_ids" ) instanceof List ){
686+ List <String > docIds = (List <String >)properties .get ("doc_ids" );
687+ repl .setDocIds (docIds );
701688 }
689+ }
702690
703- String remoteUUID = (String ) properties .get ("remoteUUID" );
704- if (remoteUUID != null ) {
705- repl .setRemoteUUID (remoteUUID );
706- }
691+ String remoteUUID = (String ) properties .get ("remoteUUID" );
692+ if (remoteUUID != null ) {
693+ repl .setRemoteUUID (remoteUUID );
694+ }
707695
708- if (push ) {
709- repl .setCreateTarget (createTarget );
710- }
711- } else {
712- // Cancel replication:
713- repl = db .getActiveReplicator (remote , push );
714- if (repl == null ) {
715- throw new CouchbaseLiteException ("unable to lookup replicator with remote: " + remote ,
716- new Status (Status .NOT_FOUND ));
717- }
696+ if (push ) {
697+ repl .setCreateTarget (createTarget );
718698 }
719699
720- return repl ;
700+ Replication activeReplicator = db .findActiveReplicator (repl );
701+ return activeReplicator != null ? activeReplicator : repl ;
721702 }
722703
723704 /**
0 commit comments