99
1010import java .io .File ;
1111import java .io .FileInputStream ;
12- import java .io .FileNotFoundException ;
1312import java .io .IOException ;
1413import java .io .InputStream ;
1514import java .io .Serializable ;
1615import java .nio .CharBuffer ;
16+ import java .nio .charset .Charset ;
1717import java .nio .charset .StandardCharsets ;
1818import java .nio .file .Files ;
1919import java .nio .file .Path ;
2020
2121import org .apache .commons .io .FilenameUtils ;
2222import org .apache .commons .lang3 .ArrayUtils ;
23- import org .apache .http .HttpEntity ;
24- import org .apache .http .HttpResponse ;
25- import org .apache .http .client .methods .HttpPost ;
26- import org .apache .http .entity .ContentType ;
27- import org .apache .http .entity .StringEntity ;
28- import org .apache .http .entity .mime .HttpMultipartMode ;
29- import org .apache .http .entity .mime .MultipartEntityBuilder ;
30- import org .apache .http .impl .client .CloseableHttpClient ;
31- import org .apache .http .impl .client .HttpClientBuilder ;
32- import org .apache .http .util .EntityUtils ;
23+ import org .apache .hc .client5 .http .classic .methods .HttpPost ;
24+ import org .apache .hc .client5 .http .entity .mime .HttpMultipartMode ;
25+ import org .apache .hc .client5 .http .entity .mime .MultipartEntityBuilder ;
26+ import org .apache .hc .client5 .http .impl .classic .CloseableHttpClient ;
27+ import org .apache .hc .client5 .http .impl .classic .HttpClients ;
28+ import org .apache .hc .core5 .http .ContentType ;
29+ import org .apache .hc .core5 .http .io .entity .EntityUtils ;
30+ import org .apache .hc .core5 .http .io .entity .StringEntity ;
3331import org .roda .core .data .common .RodaConstants ;
3432import org .roda .core .data .common .SecureString ;
3533import org .roda .core .data .exceptions .GenericException ;
3634import org .roda .core .data .exceptions .RODAException ;
3735import org .roda .core .data .utils .JsonUtils ;
3836import org .roda .core .data .v2 .accessToken .AccessToken ;
3937
40- import com .fasterxml .jackson .databind .JsonNode ;
41-
4238/**
4339 * 20160824 hsilva: I think someone, outside RODA code base, is using this
4440 * method via maven dependency
@@ -52,168 +48,106 @@ private RESTClientUtility() {
5248 // do nothing
5349 }
5450
55- public static <T extends Serializable > T sendPostRequest (T element , Class <T > elementClass , String url ,
56- String resource , String username , String password ) throws RODAException {
57- CloseableHttpClient httpClient = HttpClientBuilder .create ().build ();
58- String basicAuthToken = new String (Base64 .encode ((username + ":" + password ).getBytes ()));
59- HttpPost httpPost = new HttpPost (url + resource );
60- httpPost .setHeader ("Authorization" , "Basic " + basicAuthToken );
61- httpPost .addHeader ("content-type" , "application/json" );
62- httpPost .addHeader ("Accept" , "application/json" );
51+ public static <T extends Serializable > T sendPostRequestWithoutBodyHttp5 (Class <T > elementClass , String url ,
52+ String resource , AccessToken accessToken ) throws GenericException {
53+ try (final CloseableHttpClient httpclient = HttpClients .createDefault ()) {
54+ HttpPost httpPost = new HttpPost (url + resource );
55+ httpPost .addHeader ("Authorization" , "Bearer " + accessToken .getToken ());
56+ httpPost .addHeader ("content-type" , "application/json" );
57+ httpPost .addHeader ("Accept" , "application/json" );
6358
64- try {
65- httpPost .setEntity (new StringEntity (JsonUtils .getJsonFromObject (element )));
66- HttpResponse response ;
67- response = httpClient .execute (httpPost );
68- HttpEntity responseEntity = response .getEntity ();
59+ Result result = httpclient .execute (httpPost , response -> new Result (response .getCode (),
60+ EntityUtils .toString (response .getEntity (), Charset .defaultCharset ())));
6961
70- int responseStatusCode = response .getStatusLine ().getStatusCode ();
71- if (responseStatusCode == 201 ) {
72- return JsonUtils .getObjectFromJson (responseEntity .getContent (), elementClass );
73- } else {
74- throw new RODAException ("POST request response status code: " + responseStatusCode );
75- }
76- } catch (IOException e ) {
77- throw new RODAException ("Error sending POST request" , e );
78- }
79- }
80-
81- public static <T extends Serializable > T sendPostRequestWithoutBody (Class <T > elementClass , String url , String resource ,
82- AccessToken accessToken ) throws GenericException {
83- CloseableHttpClient httpClient = HttpClientBuilder .create ().build ();
84- HttpPost httpPost = new HttpPost (url + resource );
85- httpPost .addHeader ("Authorization" , "Bearer " + accessToken .getToken ());
86- httpPost .addHeader ("content-type" , "application/json" );
87- httpPost .addHeader ("Accept" , "application/json" );
88-
89- try {
90- HttpResponse response ;
91- response = httpClient .execute (httpPost );
92- HttpEntity responseEntity = response .getEntity ();
93- int responseStatusCode = response .getStatusLine ().getStatusCode ();
94-
95- if (responseStatusCode == 201 ) {
62+ if (result .statusCode == 201 ) {
9663 if (elementClass != null ) {
97- return JsonUtils .getObjectFromJson (responseEntity . getContent () , elementClass );
64+ return JsonUtils .getObjectFromJson (result . content , elementClass );
9865 } else {
9966 return null ;
10067 }
10168 } else {
102- throw new GenericException ("POST request response status code: " + responseStatusCode );
69+ throw new GenericException ("POST request response status code: " + result . statusCode );
10370 }
10471 } catch (IOException e ) {
10572 throw new GenericException ("Error sending POST request" , e );
10673 }
10774 }
10875
109- public static <T extends Serializable > T sendPostRequest (Object element , Class <T > elementClass , String url ,
76+ public static <T extends Serializable > T sendPostRequestHttpClient5 (Object element , Class <T > elementClass , String url ,
11077 String resource , AccessToken accessToken ) throws GenericException {
111- CloseableHttpClient httpClient = HttpClientBuilder . create (). build ();
112- HttpPost httpPost = new HttpPost (url + resource );
113- httpPost .addHeader ("Authorization" , "Bearer " + accessToken .getToken ());
114- httpPost .addHeader ("content-type" , "application/json" );
115- httpPost .addHeader ("Accept" , "application/json" );
78+ try ( final CloseableHttpClient httpclient = HttpClients . createDefault ()) {
79+ HttpPost httpPost = new HttpPost (url + resource );
80+ httpPost .addHeader ("Authorization" , "Bearer " + accessToken .getToken ());
81+ httpPost .addHeader ("content-type" , "application/json" );
82+ httpPost .addHeader ("Accept" , "application/json" );
11683
117- try {
11884 httpPost .setEntity (new StringEntity (JsonUtils .getJsonFromObject (element )));
119- HttpResponse response ;
120- response = httpClient .execute (httpPost );
121- HttpEntity responseEntity = response .getEntity ();
12285
123- int responseStatusCode = response .getStatusLine ().getStatusCode ();
124- if (responseStatusCode == 201 ) {
86+ Result result = httpclient .execute (httpPost , response -> new Result (response .getCode (),
87+ EntityUtils .toString (response .getEntity (), Charset .defaultCharset ())));
88+
89+ if (result .statusCode == 201 ) {
12590 if (elementClass != null ) {
126- return JsonUtils .getObjectFromJson (responseEntity . getContent () , elementClass );
91+ return JsonUtils .getObjectFromJson (result . content , elementClass );
12792 } else {
12893 return null ;
12994 }
13095 } else {
131- throw new GenericException ("POST request response status code: " + responseStatusCode );
96+ throw new GenericException ("POST request response status code: " + result . statusCode );
13297 }
13398 } catch (IOException e ) {
13499 throw new GenericException ("Error sending POST request" , e );
135100 }
136101 }
137102
138- public static int sendPostRequestWithCompressedFile (String url , String resource , Path path , AccessToken accessToken )
139- throws RODAException , IOException {
140- CloseableHttpClient httpClient = HttpClientBuilder .create ().build ();
141-
142- HttpPost httpPost = new HttpPost (url + resource );
143- httpPost .addHeader ("Authorization" , "Bearer " + accessToken .getToken ());
144-
145- InputStream inputStream = Files .newInputStream (path .toFile ().toPath ());
146- MultipartEntityBuilder builder = MultipartEntityBuilder .create ();
147- builder .setMode (HttpMultipartMode .BROWSER_COMPATIBLE );
148- builder .addBinaryBody (RodaConstants .API_QUERY_KEY_FILE , inputStream , ContentType .create ("application/zip" ),
149- path .getFileName ().toString ());
103+ public static int sendPostRequestWithCompressedFileHttp5 (String url , String resource , Path path ,
104+ AccessToken accessToken ) throws RODAException {
105+ try (final CloseableHttpClient httpclient = HttpClients .createDefault ()) {
106+ HttpPost httpPost = new HttpPost (url + resource );
107+ httpPost .addHeader ("Authorization" , "Bearer " + accessToken .getToken ());
150108
151- HttpEntity entity = builder .build ();
109+ InputStream inputStream = Files .newInputStream (path .toFile ().toPath ());
110+ MultipartEntityBuilder builder = MultipartEntityBuilder .create ();
111+ builder .setMode (HttpMultipartMode .STRICT );
112+ builder .addBinaryBody (RodaConstants .API_QUERY_KEY_FILE , inputStream , ContentType .create ("application/zip" ),
113+ path .getFileName ().toString ());
114+ httpPost .setEntity (builder .build ());
152115
153- try {
154- httpPost .setEntity (entity );
155- HttpResponse response = httpClient .execute (httpPost );
156-
157- return response .getStatusLine ().getStatusCode ();
116+ Result result = httpclient .execute (httpPost , response -> new Result (response .getCode (), null ));
158117
118+ return result .statusCode ;
159119 } catch (IOException e ) {
160120 throw new RODAException ("Error sending POST request" , e );
161121 }
162122 }
163123
164- public static JsonNode sendPostRequest (String url , String resource , Object object ) throws GenericException {
165- CloseableHttpClient httpClient = HttpClientBuilder .create ().build ();
166- HttpPost httpPost = new HttpPost (url + resource );
167- httpPost .addHeader ("content-type" , "application/json" );
168- httpPost .addHeader ("Accept" , "application/json" );
169-
170- try {
171- httpPost .setEntity (new StringEntity (JsonUtils .getJsonFromObject (object )));
172- HttpResponse response ;
173- response = httpClient .execute (httpPost );
174- HttpEntity responseEntity = response .getEntity ();
175-
176- int responseStatusCode = response .getStatusLine ().getStatusCode ();
177- if (responseStatusCode == 200 ) {
178- String json = EntityUtils .toString (responseEntity , StandardCharsets .UTF_8 );
179- return JsonUtils .parseJson (json );
180- } else {
181- throw new GenericException ("POST request response status code: " + responseStatusCode );
124+ public static int sendPostRequestWithFileHttp5 (String url , String resource , String username , SecureString password ,
125+ Path file ) throws RODAException {
126+ try (final CloseableHttpClient httpclient = HttpClients .createDefault ()) {
127+ HttpPost httpPost = new HttpPost (url + resource );
128+ try (
129+ SecureString basicAuth = new SecureString (
130+ ArrayUtils .addAll ((username + ":" ).toCharArray (), password .getChars ()));
131+ SecureString basicAuthToken = new SecureString (
132+ Base64 .encode (StandardCharsets .UTF_8 .encode (CharBuffer .wrap (basicAuth )).array ()))) {
133+
134+ httpPost .setHeader ("Authorization" , "Basic " + basicAuthToken );
135+ File fileToUpload = new File (FilenameUtils .normalize (file .toString ()));
136+ InputStream inputStream = new FileInputStream (fileToUpload );
137+ MultipartEntityBuilder builder = MultipartEntityBuilder .create ();
138+ builder .addBinaryBody (RodaConstants .API_QUERY_KEY_FILE , inputStream , ContentType .DEFAULT_BINARY ,
139+ fileToUpload .getName ());
140+ httpPost .setEntity (builder .build ());
141+
142+ Result result = httpclient .execute (httpPost , response -> new Result (response .getCode (), null ));
143+
144+ return result .statusCode ;
182145 }
183146 } catch (IOException e ) {
184- throw new GenericException ("Error sending POST request" , e );
147+ throw new RODAException ("Error sending POST request" , e );
185148 }
186149 }
187150
188- public static int sendPostRequestWithFile (String url , String resource , String username , SecureString password ,
189- Path file ) throws RODAException , FileNotFoundException {
190- CloseableHttpClient httpClient = HttpClientBuilder .create ().build ();
191- HttpPost httpPost = new HttpPost (url + resource );
192- SecureString basicAuth = new SecureString (ArrayUtils .addAll ((username + ":" ).toCharArray (), password .getChars ()));
193- SecureString basicAuthToken = new SecureString (
194- Base64 .encode (StandardCharsets .UTF_8 .encode (CharBuffer .wrap (basicAuth )).array ()));
195-
196- httpPost .setHeader ("Authorization" , "Basic " + basicAuthToken .toString ());
197-
198- File fileToUpload = new File (FilenameUtils .normalize (file .toString ()));
199- InputStream inputStream = new FileInputStream (fileToUpload );
200- MultipartEntityBuilder builder = MultipartEntityBuilder .create ();
201- builder .addBinaryBody (RodaConstants .API_QUERY_KEY_FILE , inputStream , ContentType .DEFAULT_BINARY ,
202- fileToUpload .getName ());
203-
204- HttpEntity multipart = builder .build ();
205-
206- try {
207- httpPost .setEntity (multipart );
208- HttpResponse response = httpClient .execute (httpPost );
209-
210- return response .getStatusLine ().getStatusCode ();
211-
212- } catch (IOException e ) {
213- throw new RODAException ("Error sending POST request" , e );
214- } finally {
215- basicAuth .close ();
216- basicAuthToken .close ();
217- }
151+ private record Result (int statusCode , String content ) {
218152 }
219153}
0 commit comments