11package com .redis .enterprise ;
22
3+ import java .io .ByteArrayOutputStream ;
34import java .io .IOException ;
5+ import java .io .InputStream ;
46import java .net .URI ;
57import java .net .URISyntaxException ;
68import java .security .GeneralSecurityException ;
911
1012import javax .net .ssl .SSLContext ;
1113
14+ import org .apache .commons .io .IOUtils ;
1215import org .apache .hc .client5 .http .HttpResponseException ;
1316import org .apache .hc .client5 .http .auth .UsernamePasswordCredentials ;
1417import org .apache .hc .client5 .http .classic .methods .HttpDelete ;
@@ -134,15 +137,20 @@ private <T> T get(String path, Class<T> type) throws ParseException, IOException
134137 }
135138
136139 private <T > T get (String path , JavaType type ) throws ParseException , IOException {
137- return read (new HttpGet (uri (path )), type , HttpStatus .SC_OK );
140+ return read (header ( new HttpGet (uri (path ) )), type , HttpStatus .SC_OK );
138141 }
139142
140143 private <T > T delete (String path , Class <T > type ) throws ParseException , IOException {
141144 return delete (path , SimpleType .constructUnsafe (type ));
142145 }
143146
144147 private <T > T delete (String path , JavaType type ) throws ParseException , IOException {
145- return read (new HttpDelete (uri (path )), type , HttpStatus .SC_OK );
148+ return read (header (new HttpDelete (uri (path ))), type , HttpStatus .SC_OK );
149+ }
150+
151+ private ClassicHttpRequest header (ClassicHttpRequest request ) {
152+ request .setHeader (HttpHeaders .CONTENT_TYPE , CONTENT_TYPE_JSON );
153+ return request ;
146154 }
147155
148156 private <T > T post (String path , Object request , Class <T > responseType ) throws ParseException , IOException {
@@ -153,15 +161,10 @@ private <T> T post(String path, Object request, JavaType responseType) throws Pa
153161 HttpPost post = new HttpPost (uri (path ));
154162 String json = objectMapper .writeValueAsString (request );
155163 post .setEntity (new StringEntity (json ));
156- return read (post , responseType , HttpStatus .SC_OK );
157- }
158-
159- private <T > T read (ClassicHttpRequest request , Class <T > type , int successCode ) throws ParseException , IOException {
160- return read (request , SimpleType .constructUnsafe (type ), successCode );
164+ return read (header (post ), responseType , HttpStatus .SC_OK );
161165 }
162166
163167 private <T > T read (ClassicHttpRequest request , JavaType type , int successCode ) throws IOException , ParseException {
164- request .setHeader (HttpHeaders .CONTENT_TYPE , CONTENT_TYPE_JSON );
165168 HttpHost target = new HttpHost (protocol , host , port );
166169 HttpClientContext localContext = HttpClientContext .create ();
167170 if (credentials != null ) {
@@ -170,9 +173,11 @@ private <T> T read(ClassicHttpRequest request, JavaType type, int successCode) t
170173 localContext .resetAuthExchange (target , basicAuth );
171174 }
172175 CloseableHttpResponse response = client .execute (request , localContext );
176+ String json = EntityUtils .toString (response .getEntity ());
173177 if (response .getCode () == successCode ) {
174- return objectMapper .readValue (EntityUtils . toString ( response . getEntity ()) , type );
178+ return objectMapper .readValue (json , type );
175179 }
180+ log .error ("Error: {}" , json );
176181 throw new HttpResponseException (response .getCode (), response .getReasonPhrase ());
177182 }
178183
@@ -217,13 +222,18 @@ public void deleteDatabase(long uid) {
217222 });
218223 }
219224
220- public ModuleInstallResponse installModule (String filename , byte [] bytes ) throws ParseException , IOException {
225+ public ModuleInstallResponse installModule (String filename , InputStream inputStream )
226+ throws ParseException , IOException {
221227 HttpPost post = new HttpPost (uri (v2 (MODULES )));
222228 MultipartEntityBuilder builder = MultipartEntityBuilder .create ();
223229 builder .setMode (HttpMultipartMode .STRICT );
224- builder .addPart ("module" , new ByteArrayBody (bytes , ContentType .MULTIPART_FORM_DATA , filename ));
230+ ByteArrayOutputStream baos = new ByteArrayOutputStream ();
231+ IOUtils .copy (inputStream , baos );
232+ builder .addPart ("module" , new ByteArrayBody (baos .toByteArray (), ContentType .MULTIPART_FORM_DATA , filename ));
225233 post .setEntity (builder .build ());
226- ModuleInstallResponse response = read (post , ModuleInstallResponse .class , HttpStatus .SC_ACCEPTED );
234+ ModuleInstallResponse response = read (post , SimpleType .constructUnsafe (ModuleInstallResponse .class ),
235+ HttpStatus .SC_ACCEPTED );
236+ baos .close ();
227237 Awaitility .await ().timeout (Duration .ofSeconds (30 )).pollInterval (Duration .ofSeconds (3 )).until (() -> {
228238 log .info ("Checking status of action {}" , response .getActionUid ());
229239 Action status = getAction (response .getActionUid ());
0 commit comments