1
1
package com .marklogic .mgmt ;
2
2
3
+ import java .net .URI ;
3
4
import java .util .ArrayList ;
4
5
import java .util .List ;
5
6
@@ -26,7 +27,6 @@ public class ManageClient extends LoggingObject {
26
27
private ManageConfig manageConfig ;
27
28
private RestTemplate restTemplate ;
28
29
private RestTemplate adminRestTemplate ;
29
- private String baseUrl ;
30
30
31
31
/**
32
32
* Can use this constructor when the default values in ManageConfig will work.
@@ -55,51 +55,46 @@ public void initialize(ManageConfig config) {
55
55
} else {
56
56
this .adminRestTemplate = restTemplate ;
57
57
}
58
-
59
- this .baseUrl = config .getBaseUrl ();
60
- if (logger .isInfoEnabled ()) {
61
- logger .info ("Initialized ManageClient with base URL of: " + baseUrl );
62
- }
63
58
}
64
59
65
60
public ResponseEntity <String > putJson (String path , String json ) {
66
61
logRequest (path , "JSON" , "PUT" );
67
- return restTemplate .exchange (baseUrl + path , HttpMethod .PUT , buildJsonEntity (json ), String .class );
62
+ return restTemplate .exchange (buildUri ( path ) , HttpMethod .PUT , buildJsonEntity (json ), String .class );
68
63
}
69
64
70
65
public ResponseEntity <String > putJsonAsAdmin (String path , String json ) {
71
66
logAdminRequest (path , "JSON" , "PUT" );
72
- return adminRestTemplate .exchange (baseUrl + path , HttpMethod .PUT , buildJsonEntity (json ), String .class );
67
+ return adminRestTemplate .exchange (buildUri ( path ) , HttpMethod .PUT , buildJsonEntity (json ), String .class );
73
68
}
74
69
75
70
public ResponseEntity <String > putXml (String path , String xml ) {
76
71
logRequest (path , "XML" , "PUT" );
77
- return restTemplate .exchange (baseUrl + path , HttpMethod .PUT , buildXmlEntity (xml ), String .class );
72
+ return restTemplate .exchange (buildUri ( path ) , HttpMethod .PUT , buildXmlEntity (xml ), String .class );
78
73
}
79
74
80
75
public ResponseEntity <String > putXmlAsAdmin (String path , String xml ) {
81
76
logAdminRequest (path , "XML" , "PUT" );
82
- return adminRestTemplate .exchange (baseUrl + path , HttpMethod .PUT , buildXmlEntity (xml ), String .class );
77
+ return adminRestTemplate .exchange (buildUri ( path ) , HttpMethod .PUT , buildXmlEntity (xml ), String .class );
83
78
}
84
79
85
80
public ResponseEntity <String > postJson (String path , String json ) {
86
81
logRequest (path , "JSON" , "POST" );
87
- return restTemplate .exchange (baseUrl + path , HttpMethod .POST , buildJsonEntity (json ), String .class );
82
+ return restTemplate .exchange (buildUri ( path ) , HttpMethod .POST , buildJsonEntity (json ), String .class );
88
83
}
89
84
90
85
public ResponseEntity <String > postJsonAsAdmin (String path , String json ) {
91
86
logAdminRequest (path , "JSON" , "POST" );
92
- return adminRestTemplate .exchange (baseUrl + path , HttpMethod .POST , buildJsonEntity (json ), String .class );
87
+ return adminRestTemplate .exchange (buildUri ( path ) , HttpMethod .POST , buildJsonEntity (json ), String .class );
93
88
}
94
89
95
90
public ResponseEntity <String > postXml (String path , String xml ) {
96
91
logRequest (path , "XML" , "POST" );
97
- return restTemplate .exchange (baseUrl + path , HttpMethod .POST , buildXmlEntity (xml ), String .class );
92
+ return restTemplate .exchange (buildUri ( path ) , HttpMethod .POST , buildXmlEntity (xml ), String .class );
98
93
}
99
94
100
95
public ResponseEntity <String > postXmlAsAdmin (String path , String xml ) {
101
96
logAdminRequest (path , "XML" , "POST" );
102
- return adminRestTemplate .exchange (baseUrl + path , HttpMethod .POST , buildXmlEntity (xml ), String .class );
97
+ return adminRestTemplate .exchange (buildUri ( path ) , HttpMethod .POST , buildXmlEntity (xml ), String .class );
103
98
}
104
99
105
100
public ResponseEntity <String > postForm (String path , String ... params ) {
@@ -111,12 +106,12 @@ public ResponseEntity<String> postForm(String path, String... params) {
111
106
map .add (params [i ], params [i + 1 ]);
112
107
}
113
108
HttpEntity <MultiValueMap <String , String >> entity = new HttpEntity <MultiValueMap <String , String >>(map , headers );
114
- return restTemplate .exchange (baseUrl + path , HttpMethod .POST , entity , String .class );
109
+ return restTemplate .exchange (buildUri ( path ) , HttpMethod .POST , entity , String .class );
115
110
}
116
111
117
112
public Fragment getXml (String path , String ... namespacePrefixesAndUris ) {
118
113
logRequest (path , "XML" , "GET" );
119
- String xml = getRestTemplate ().getForObject (getBaseUrl () + path , String .class );
114
+ String xml = getRestTemplate ().getForObject (buildUri ( path ) , String .class );
120
115
List <Namespace > list = new ArrayList <Namespace >();
121
116
for (int i = 0 ; i < namespacePrefixesAndUris .length ; i += 2 ) {
122
117
list .add (Namespace .getNamespace (namespacePrefixesAndUris [i ], namespacePrefixesAndUris [i + 1 ]));
@@ -126,7 +121,7 @@ public Fragment getXml(String path, String... namespacePrefixesAndUris) {
126
121
127
122
public Fragment getXmlAsAdmin (String path , String ... namespacePrefixesAndUris ) {
128
123
logAdminRequest (path , "XML" , "GET" );
129
- String xml = getAdminRestTemplate ().getForObject (getBaseUrl () + path , String .class );
124
+ String xml = getAdminRestTemplate ().getForObject (buildUri ( path ) , String .class );
130
125
List <Namespace > list = new ArrayList <Namespace >();
131
126
for (int i = 0 ; i < namespacePrefixesAndUris .length ; i += 2 ) {
132
127
list .add (Namespace .getNamespace (namespacePrefixesAndUris [i ], namespacePrefixesAndUris [i + 1 ]));
@@ -138,18 +133,25 @@ public String getJson(String path) {
138
133
logRequest (path , "JSON" , "GET" );
139
134
HttpHeaders headers = new HttpHeaders ();
140
135
headers .set ("Accept" , MediaType .APPLICATION_JSON_VALUE );
141
- return getRestTemplate ().exchange (getBaseUrl () + path , HttpMethod .GET , new HttpEntity <>(headers ), String .class )
136
+ return getRestTemplate ().exchange (buildUri ( path ) , HttpMethod .GET , new HttpEntity <>(headers ), String .class )
142
137
.getBody ();
143
138
}
144
139
140
+ public String getJson (URI uri ) {
141
+ logRequest (uri .toString (), "JSON" , "GET" );
142
+ HttpHeaders headers = new HttpHeaders ();
143
+ headers .set ("Accept" , MediaType .APPLICATION_JSON_VALUE );
144
+ return getRestTemplate ().exchange (uri , HttpMethod .GET , new HttpEntity <>(headers ), String .class ).getBody ();
145
+ }
146
+
145
147
public void delete (String path ) {
146
148
logRequest (path , "" , "DELETE" );
147
- restTemplate .delete (baseUrl + path );
149
+ restTemplate .delete (buildUri ( path ) );
148
150
}
149
151
150
152
public void deleteAsAdmin (String path ) {
151
153
logAdminRequest (path , "" , "DELETE" );
152
- adminRestTemplate .delete (baseUrl + path );
154
+ adminRestTemplate .delete (buildUri ( path ) );
153
155
}
154
156
155
157
public HttpEntity <String > buildJsonEntity (String json ) {
@@ -178,12 +180,12 @@ protected void logAdminRequest(String path, String contentType, String method) {
178
180
}
179
181
}
180
182
181
- public RestTemplate getRestTemplate ( ) {
182
- return restTemplate ;
183
+ public URI buildUri ( String path ) {
184
+ return manageConfig . buildUri ( path ) ;
183
185
}
184
186
185
- public String getBaseUrl () {
186
- return baseUrl ;
187
+ public RestTemplate getRestTemplate () {
188
+ return restTemplate ;
187
189
}
188
190
189
191
public RestTemplate getAdminRestTemplate () {
0 commit comments