1515 */
1616package com .marklogic .client .datamovement ;
1717
18+ import com .marklogic .client .datamovement .Forest .HostType ;
19+ import com .marklogic .client .datamovement .impl .ForestConfigurationImpl ;
1820import com .marklogic .client .datamovement .impl .ForestImpl ;
21+
22+ import java .util .ArrayList ;
23+ import java .util .Arrays ;
1924import java .util .HashMap ;
2025import java .util .HashSet ;
26+ import java .util .List ;
2127import java .util .Map ;
2228import java .util .Set ;
29+ import java .util .stream .Collectors ;
2330import java .util .stream .Stream ;
2431
2532/**
@@ -71,6 +78,11 @@ public Forest[] listForests() {
7178 if ( renames .containsKey (openReplicaHost ) ) {
7279 openReplicaHost = renames .get (openReplicaHost );
7380 }
81+ String requestHost = forest .getRequestHost ();
82+ if ( requestHost != null ) requestHost = requestHost .toLowerCase ();
83+ if ( renames .containsKey (requestHost ) ) {
84+ requestHost = renames .get (requestHost );
85+ }
7486 String alternateHost = forest .getAlternateHost ();
7587 if ( alternateHost != null ) alternateHost = alternateHost .toLowerCase ();
7688 if ( renames .containsKey (alternateHost ) ) {
@@ -81,12 +93,19 @@ public Forest[] listForests() {
8193 if ( renames .containsKey (host ) ) {
8294 host = renames .get (host );
8395 }
84- return new ForestImpl (host , openReplicaHost , alternateHost , forest .getDatabaseName (),
96+ return new ForestImpl (host , openReplicaHost , requestHost , alternateHost , forest .getDatabaseName (),
8597 forest .getForestName (), forest .getForestId (), forest .isUpdateable (), false );
8698 }).toArray (Forest []::new );
8799
88- String [] validHosts = Stream .of (renamedForests ).flatMap ( forest ->
89- Stream .of (new String [] {forest .getHost (), forest .getOpenReplicaHost (), forest .getAlternateHost ()})
100+ String [] validHosts = Stream .of (renamedForests ).flatMap ( forest -> {
101+ String [] hostArray ;
102+ if ((blackList .contains (forest .getPreferredHost ()) || blackList .contains (forest .getHost ()))
103+ && forest .getPreferredHostType () == HostType .REQUEST_HOST ) {
104+ hostArray = new String [] {forest .getOpenReplicaHost (), forest .getAlternateHost ()};
105+ } else {
106+ hostArray = new String [] {forest .getHost (), forest .getOpenReplicaHost (), forest .getAlternateHost (), forest .getRequestHost ()};
107+ }
108+ return Stream .of (hostArray )
90109 .filter ( host -> host != null )
91110 .map ( host -> host .toLowerCase () )
92111 .filter ( host -> {
@@ -100,13 +119,14 @@ public Forest[] listForests() {
100119 }
101120 // this host is valid, add it to the list
102121 return true ;
103- })
104- ).distinct ().toArray (String []::new );
122+ });
123+ } ).distinct ().toArray (String []::new );
105124
106125 Stream <Forest > replaced = Stream .of (renamedForests );
107126 if ( blackList .size () > 0 ) {
108127 replaced = replaced .map ( forest -> {
109- if ( blackList .contains (forest .getPreferredHost ()) ) {
128+ if ( blackList .contains (forest .getPreferredHost ()) ||
129+ (forest .getPreferredHostType () == HostType .REQUEST_HOST && blackList .contains (forest .getHost ()))) {
110130 if ( validHosts .length == 0 ) {
111131 throw new IllegalStateException ("White list or black list rules are too restrictive:" +
112132 " no valid hosts are left" );
@@ -117,15 +137,21 @@ public Forest[] listForests() {
117137 if ( blackList .contains (openReplicaHost ) ) {
118138 openReplicaHost = replaceHost (openReplicaHost , validHosts );
119139 }
140+ String requestHost = forest .getRequestHost ();
141+ String host = forest .getHost ();
142+ if ( blackList .contains (requestHost ) ) {
143+ requestHost = replaceHost (requestHost , validHosts );
144+ host = requestHost ;
145+ }
120146 String alternateHost = forest .getAlternateHost ();
121147 if ( blackList .contains (alternateHost ) ) {
122148 alternateHost = replaceHost (alternateHost , validHosts );
123149 }
124- String host = forest .getHost ();
125150 if ( blackList .contains (host ) ) {
126151 host = replaceHost (host , validHosts );
152+ if (requestHost != null ) requestHost = host ;
127153 }
128- return new ForestImpl (host , openReplicaHost , alternateHost , forest .getDatabaseName (),
154+ return new ForestImpl (host , openReplicaHost , requestHost , alternateHost , forest .getDatabaseName (),
129155 forest .getForestName (), forest .getForestId (), forest .isUpdateable (), false );
130156 } else {
131157 return forest ;
@@ -145,6 +171,10 @@ public Forest[] listForests() {
145171 if ( ! whiteList .contains (openReplicaHost ) ) {
146172 openReplicaHost = replaceHost (openReplicaHost , validHosts );
147173 }
174+ String requestHost = forest .getRequestHost ();
175+ if ( ! whiteList .contains (requestHost ) ) {
176+ requestHost = replaceHost (requestHost , validHosts );
177+ }
148178 String alternateHost = forest .getAlternateHost ();
149179 if ( ! whiteList .contains (alternateHost ) ) {
150180 alternateHost = replaceHost (alternateHost , validHosts );
@@ -153,7 +183,7 @@ public Forest[] listForests() {
153183 if ( ! whiteList .contains (host ) ) {
154184 host = replaceHost (host , validHosts );
155185 }
156- return new ForestImpl (host , openReplicaHost , alternateHost , forest .getDatabaseName (),
186+ return new ForestImpl (host , openReplicaHost , requestHost , alternateHost , forest .getDatabaseName (),
157187 forest .getForestName (), forest .getForestId (), forest .isUpdateable (), false );
158188 } else {
159189 return forest ;
@@ -182,6 +212,7 @@ private String replaceHost(String invalidHost, String[] validHosts) {
182212 */
183213 public FilteredForestConfiguration withBlackList (String ... hostNames ) {
184214 if ( whiteList .size () > 0 ) throw new IllegalStateException ("whiteList already initialized" );
215+ if ( hostNames == null ) throw new IllegalArgumentException ("hostNames must not be null" );
185216 for ( String hostName : hostNames ) {
186217 if ( hostName != null ) blackList .add (hostName .toLowerCase ());
187218 }
@@ -198,6 +229,7 @@ public FilteredForestConfiguration withBlackList(String... hostNames) {
198229 */
199230 public FilteredForestConfiguration withWhiteList (String ... hostNames ) {
200231 if ( blackList .size () > 0 ) throw new IllegalStateException ("blackList already initialized" );
232+ if ( hostNames == null ) throw new IllegalArgumentException ("hostNames must not be null" );
201233 for ( String hostName : hostNames ) {
202234 if ( hostName != null ) whiteList .add (hostName .toLowerCase ());
203235 }
0 commit comments