44import com .marklogic .client .ext .helper .LoggingObject ;
55import com .marklogic .mgmt .util .ObjectMapperFactory ;
66import com .marklogic .rest .util .Fragment ;
7+ import com .marklogic .rest .util .RestConfig ;
78import com .marklogic .rest .util .RestTemplateUtil ;
89import org .jdom2 .Namespace ;
910import org .springframework .http .*;
2324 */
2425public class ManageClient extends LoggingObject {
2526
26- private ManageConfig manageConfig ;
27- private RestTemplate restTemplate ;
27+ private ManageConfig manageConfig ;
28+ private RestTemplate restTemplate ;
29+ private RestTemplate adminRestTemplate ;
2830 private PayloadParser payloadParser ;
2931
3032 /**
@@ -44,6 +46,21 @@ public void initialize(ManageConfig config) {
4446 logger .info ("Initializing ManageClient with manage config of: " + config );
4547 }
4648 this .restTemplate = RestTemplateUtil .newRestTemplate (config );
49+
50+ if (!config .getUsername ().equals (config .getAdminUsername ())) {
51+ if (logger .isInfoEnabled ()) {
52+ logger .info ("Initializing ManageClient with admin config, admin user: " + config .getAdminUsername ());
53+ }
54+
55+ RestConfig rc = new RestConfig (config .getHost (), config .getPort (), config .getAdminUsername (), config .getAdminPassword ());
56+ rc .setScheme (config .getScheme ());
57+ rc .setConfigureSimpleSsl (config .isConfigureSimpleSsl ());
58+ rc .setHostnameVerifier (config .getHostnameVerifier ());
59+ rc .setSslContext (config .getSslContext ());
60+ this .adminRestTemplate = RestTemplateUtil .newRestTemplate (rc );
61+ } else {
62+ this .adminRestTemplate = restTemplate ;
63+ }
4764 }
4865
4966 /**
@@ -53,29 +70,61 @@ public void initialize(ManageConfig config) {
5370 * @param restTemplate
5471 */
5572 public ManageClient (RestTemplate restTemplate ) {
56- this .restTemplate = restTemplate ;
73+ this (restTemplate , restTemplate );
74+ }
75+
76+ /**
77+ * Use this when you want to provide your own RestTemplate as opposed to using the one that's constructed via a
78+ * ManageConfig instance.
79+ *
80+ * @param restTemplate
81+ * @param adminRestTemplate
82+ */
83+ public ManageClient (RestTemplate restTemplate , RestTemplate adminRestTemplate ) {
84+ this .restTemplate = restTemplate ;
85+ this .adminRestTemplate = adminRestTemplate ;
5786 }
5887
5988 public ResponseEntity <String > putJson (String path , String json ) {
6089 logRequest (path , "JSON" , "PUT" );
6190 return restTemplate .exchange (buildUri (path ), HttpMethod .PUT , buildJsonEntity (json ), String .class );
6291 }
6392
93+ public ResponseEntity <String > putJsonAsAdmin (String path , String json ) {
94+ logAdminRequest (path , "JSON" , "PUT" );
95+ return adminRestTemplate .exchange (buildUri (path ), HttpMethod .PUT , buildJsonEntity (json ), String .class );
96+ }
97+
6498 public ResponseEntity <String > putXml (String path , String xml ) {
6599 logRequest (path , "XML" , "PUT" );
66100 return restTemplate .exchange (buildUri (path ), HttpMethod .PUT , buildXmlEntity (xml ), String .class );
67101 }
68102
103+ public ResponseEntity <String > putXmlAsAdmin (String path , String xml ) {
104+ logAdminRequest (path , "XML" , "PUT" );
105+ return adminRestTemplate .exchange (buildUri (path ), HttpMethod .PUT , buildXmlEntity (xml ), String .class );
106+ }
107+
69108 public ResponseEntity <String > postJson (String path , String json ) {
70109 logRequest (path , "JSON" , "POST" );
71110 return restTemplate .exchange (buildUri (path ), HttpMethod .POST , buildJsonEntity (json ), String .class );
72111 }
73112
113+ public ResponseEntity <String > postJsonAsAdmin (String path , String json ) {
114+ logAdminRequest (path , "JSON" , "POST" );
115+ return adminRestTemplate .exchange (buildUri (path ), HttpMethod .POST , buildJsonEntity (json ), String .class );
116+ }
117+
74118 public ResponseEntity <String > postXml (String path , String xml ) {
75119 logRequest (path , "XML" , "POST" );
76120 return restTemplate .exchange (buildUri (path ), HttpMethod .POST , buildXmlEntity (xml ), String .class );
77121 }
78122
123+ public ResponseEntity <String > postXmlAsAdmin (String path , String xml ) {
124+ logAdminRequest (path , "XML" , "POST" );
125+ return adminRestTemplate .exchange (buildUri (path ), HttpMethod .POST , buildXmlEntity (xml ), String .class );
126+ }
127+
79128 public ResponseEntity <String > postForm (String path , String ... params ) {
80129 logRequest (path , "form" , "POST" );
81130 HttpHeaders headers = new HttpHeaders ();
@@ -102,6 +151,20 @@ public Fragment getXml(String path, String... namespacePrefixesAndUris) {
102151 return new Fragment (xml , list .toArray (new Namespace [] {}));
103152 }
104153
154+ public String getXmlStringAsAdmin (String path ) {
155+ logAdminRequest (path , "XML" , "GET" );
156+ return getAdminRestTemplate ().getForObject (buildUri (path ), String .class );
157+ }
158+
159+ public Fragment getXmlAsAdmin (String path , String ... namespacePrefixesAndUris ) {
160+ String xml = getXmlStringAsAdmin (path );
161+ List <Namespace > list = new ArrayList <Namespace >();
162+ for (int i = 0 ; i < namespacePrefixesAndUris .length ; i += 2 ) {
163+ list .add (Namespace .getNamespace (namespacePrefixesAndUris [i ], namespacePrefixesAndUris [i + 1 ]));
164+ }
165+ return new Fragment (xml , list .toArray (new Namespace [] {}));
166+ }
167+
105168 public String getJson (String path ) {
106169 logRequest (path , "JSON" , "GET" );
107170 HttpHeaders headers = new HttpHeaders ();
@@ -117,11 +180,24 @@ public String getJson(URI uri) {
117180 return getRestTemplate ().exchange (uri , HttpMethod .GET , new HttpEntity <>(headers ), String .class ).getBody ();
118181 }
119182
183+ public String getJsonAsAdmin (String path ) {
184+ logAdminRequest (path , "JSON" , "GET" );
185+ HttpHeaders headers = new HttpHeaders ();
186+ headers .set ("Accept" , MediaType .APPLICATION_JSON_VALUE );
187+ return getAdminRestTemplate ().exchange (buildUri (path ), HttpMethod .GET , new HttpEntity <>(headers ), String .class )
188+ .getBody ();
189+ }
190+
120191 public void delete (String path ) {
121192 logRequest (path , "" , "DELETE" );
122193 restTemplate .delete (buildUri (path ));
123194 }
124195
196+ public void deleteAsAdmin (String path ) {
197+ logAdminRequest (path , "" , "DELETE" );
198+ adminRestTemplate .delete (buildUri (path ));
199+ }
200+
125201 /**
126202 * Per #187 and version 3.1.0, when an HttpEntity is constructed with a JSON payload, this method will check to see
127203 * if it should "clean" the JSON via the Jackson library, which is primarily intended for removing comments from
@@ -187,11 +263,19 @@ public RestTemplate getRestTemplate() {
187263 return restTemplate ;
188264 }
189265
266+ public RestTemplate getAdminRestTemplate () {
267+ return adminRestTemplate ;
268+ }
269+
190270 public ManageConfig getManageConfig () {
191271 return manageConfig ;
192272 }
193273
194274 public void setRestTemplate (RestTemplate restTemplate ) {
195275 this .restTemplate = restTemplate ;
196276 }
277+
278+ public void setAdminRestTemplate (RestTemplate adminRestTemplate ) {
279+ this .adminRestTemplate = adminRestTemplate ;
280+ }
197281}
0 commit comments