1919import com .marklogic .rest .util .Fragment ;
2020import com .marklogic .rest .util .RestConfig ;
2121import com .marklogic .rest .util .RestTemplateUtil ;
22+ import org .apache .commons .lang3 .tuple .ImmutablePair ;
2223import org .springframework .core .io .ByteArrayResource ;
2324import org .springframework .core .io .Resource ;
2425import org .springframework .http .*;
@@ -80,8 +81,9 @@ public boolean execute() {
8081 HttpHeaders headers = new HttpHeaders ();
8182 headers .setContentType (MediaType .APPLICATION_JSON );
8283 HttpEntity <String > entity = new HttpEntity <>(payload , headers );
84+ ImmutablePair <Boolean , Boolean > credentialsStatus = checkCredentialsAndReplaceNulls (adminConfig );
8385 try {
84- ResponseEntity <String > response = getRestTemplate ().exchange (uri , HttpMethod .POST , entity , String .class );
86+ ResponseEntity <String > response = getRestTemplate ().exchange (uri , HttpMethod .POST , entity , String .class );
8587 logger .info ("Initialization response: " + response );
8688 // According to http://docs.marklogic.com/REST/POST/admin/v1/init, a 202 is sent back in the event a
8789 // restart is needed. A 400 or 401 will be thrown as an error by RestTemplate.
@@ -98,12 +100,14 @@ public boolean execute() {
98100 logger .error ("Caught error, response body: " + body );
99101 throw hcee ;
100102 }
101- }
103+ } finally {
104+ restoreCredentials (adminConfig , credentialsStatus );
105+ }
102106 }
103107 });
104108 }
105109
106- public void installAdmin () {
110+ public void installAdmin () {
107111 installAdmin (null , null );
108112 }
109113
@@ -130,8 +134,9 @@ public boolean execute() {
130134 HttpHeaders headers = new HttpHeaders ();
131135 headers .setContentType (MediaType .APPLICATION_JSON );
132136 HttpEntity <String > entity = new HttpEntity <>(payload , headers );
137+ ImmutablePair <Boolean , Boolean > credentialsStatus = checkCredentialsAndReplaceNulls (adminConfig );
133138 try {
134- ResponseEntity <String > response = getRestTemplate ().exchange (uri , HttpMethod .POST , entity , String .class );
139+ ResponseEntity <String > response = getRestTemplate ().exchange (uri , HttpMethod .POST , entity , String .class );
135140 logger .info ("Admin installation response: " + response );
136141 // According to http://docs.marklogic.com/REST/POST/admin/v1/init, a 202 is sent back in the event a
137142 // restart is needed. A 400 or 401 will be thrown as an error by RestTemplate.
@@ -143,6 +148,8 @@ public boolean execute() {
143148 return false ;
144149 }
145150 throw hcee ;
151+ } finally {
152+ restoreCredentials (adminConfig , credentialsStatus );
146153 }
147154 }
148155 });
@@ -323,4 +330,31 @@ public RestTemplate getRestTemplate() {
323330 }
324331 return this .restTemplate ;
325332 }
333+
334+ private ImmutablePair <Boolean , Boolean > checkCredentialsAndReplaceNulls (AdminConfig adminConfig ) {
335+ boolean setNullUsername = false ;
336+ boolean setNullPassword = false ;
337+ if (adminConfig .getUsername () == null ) {
338+ adminConfig .setUsername ("" );
339+ setNullUsername = true ;
340+ this .restTemplate = null ;
341+ }
342+ if (adminConfig .getPassword () == null ) {
343+ adminConfig .setPassword ("" );
344+ setNullPassword = true ;
345+ this .restTemplate = null ;
346+ }
347+ return new ImmutablePair <>(setNullUsername , setNullPassword );
348+ }
349+
350+ private void restoreCredentials (AdminConfig adminConfig , ImmutablePair <Boolean , Boolean > credentialsStatus ) {
351+ if (credentialsStatus .getLeft ()) {
352+ adminConfig .setUsername (null );
353+ this .restTemplate = null ;
354+ }
355+ if (credentialsStatus .getRight ()) {
356+ adminConfig .setPassword (null );
357+ this .restTemplate = null ;
358+ }
359+ }
326360}
0 commit comments