88import com .crowdin .client .core .http .exceptions .HttpException ;
99import com .crowdin .client .core .model .ClientConfig ;
1010import com .crowdin .client .core .model .Credentials ;
11- import org .apache .http .HttpEntity ;
12- import org .apache .http .HttpHost ;
13- import org .apache .http .auth .AuthScope ;
14- import org .apache .http .auth .UsernamePasswordCredentials ;
15- import org .apache .http .client .config .CookieSpecs ;
16- import org .apache .http .client .config .RequestConfig ;
17- import org .apache .http .client .methods .CloseableHttpResponse ;
18- import org .apache .http .client .methods .HttpDelete ;
19- import org .apache .http .client .methods .HttpGet ;
20- import org .apache .http .client .methods .HttpHead ;
21- import org .apache .http .client .methods .HttpPatch ;
22- import org .apache .http .client .methods .HttpPost ;
23- import org .apache .http .client .methods .HttpPut ;
24- import org .apache .http .client .methods .HttpUriRequest ;
25- import org .apache .http .client .methods .RequestBuilder ;
26- import org .apache .http .entity .ContentType ;
27- import org .apache .http .entity .InputStreamEntity ;
28- import org .apache .http .entity .StringEntity ;
29- import org .apache .http .impl .client .BasicCredentialsProvider ;
30- import org .apache .http .impl .client .CloseableHttpClient ;
31- import org .apache .http .impl .client .HttpClientBuilder ;
11+ import org .apache .hc .client5 .http .classic .methods .*;
12+ import org .apache .hc .core5 .http .HttpEntity ;
13+ import org .apache .hc .core5 .http .HttpHost ;
14+ import org .apache .hc .client5 .http .auth .AuthScope ;
15+ import org .apache .hc .client5 .http .auth .UsernamePasswordCredentials ;
16+ import org .apache .hc .client5 .http .cookie .StandardCookieSpec ;
17+ import org .apache .hc .client5 .http .config .RequestConfig ;
18+ //import org.apache.http.client.methods.HttpUriRequest;
19+ //import org.apache.http.client.methods.RequestBuilder;
20+ //import org.apache.http.entity.ContentType;
21+ //import org.apache.http.entity.InputStreamEntity;
22+ //import org.apache.http.entity.StringEntity;
23+ //import org.apache.http.impl.client.BasicCredentialsProvider;
24+ //import org.apache.http.impl.client.CloseableHttpClient;
25+ //import org.apache.http.impl.client.HttpClientBuilder;
26+
27+ import org .apache .hc .client5 .http .classic .methods .HttpUriRequestBase ;
28+ import org .apache .hc .client5 .http .impl .classic .CloseableHttpResponse ;
29+ import org .apache .hc .core5 .http .ContentType ;
30+ import org .apache .hc .core5 .http .io .entity .InputStreamEntity ;
31+ import org .apache .hc .core5 .http .io .entity .StringEntity ;
32+
33+ import org .apache .hc .client5 .http .impl .auth .BasicCredentialsProvider ;
34+
35+ import org .apache .hc .client5 .http .impl .classic .CloseableHttpClient ;
36+ import org .apache .hc .client5 .http .impl .classic .HttpClientBuilder ;
37+ import org .apache .hc .core5 .util .Timeout ;
38+
39+ import org .apache .hc .client5 .http .classic .methods .HttpUriRequestBase ;
40+ import org .apache .hc .client5 .http .classic .methods .HttpPost ;
41+
42+ import org .apache .hc .core5 .http .HttpEntity ;
43+ import org .apache .hc .core5 .http .ContentType ;
44+ import org .apache .hc .core5 .http .io .entity .InputStreamEntity ;
45+ import org .apache .hc .core5 .http .io .entity .StringEntity ;
3246
3347import java .io .IOException ;
3448import java .io .InputStream ;
3852import java .nio .charset .StandardCharsets ;
3953import java .util .HashMap ;
4054import java .util .Map ;
55+ import java .util .concurrent .TimeUnit ;
4156
4257public class ApacheHttpClient implements HttpClient {
4358
@@ -62,19 +77,20 @@ public ApacheHttpClient(Credentials credentials, JsonTransformer jsonTransformer
6277 this .defaultHeaders = defaultHeaders ;
6378 this .proxy = proxy ;
6479 this .proxyCreds = proxyCreds ;
65- RequestConfig .Builder requestConfig = RequestConfig .custom ().setCookieSpec (CookieSpecs . STANDARD );
80+ RequestConfig .Builder requestConfig = RequestConfig .custom ().setCookieSpec (StandardCookieSpec . RELAXED );
6681 if (timeoutMs != null ) {
67- requestConfig .setConnectionRequestTimeout (timeoutMs );
68- requestConfig .setConnectTimeout (timeoutMs );
69- requestConfig .setSocketTimeout (timeoutMs );
82+ Timeout timeout = Timeout .of (timeoutMs , TimeUnit .MILLISECONDS );
83+ requestConfig .setConnectionRequestTimeout (timeout );
84+ requestConfig .setConnectTimeout (timeout );
85+ requestConfig .setResponseTimeout (timeout );
7086 }
7187 this .httpClient = (proxy != null )
7288 ? HttpClientBuilder .create ()
7389 .setProxy (new HttpHost (proxy .getHost (), proxy .getPort ()))
7490 .setDefaultRequestConfig (requestConfig .build ())
7591 .setDefaultCredentialsProvider ((proxyCreds != null )
7692 ? new BasicCredentialsProvider () {{
77- setCredentials (new AuthScope (proxy .getHost (), proxy .getPort ()), new UsernamePasswordCredentials (proxyCreds .getUsername (), proxyCreds .getPassword ()));
93+ setCredentials (new AuthScope (proxy .getHost (), proxy .getPort ()), new UsernamePasswordCredentials (proxyCreds .getUsername (), proxyCreds .getPassword (). toCharArray () ));
7894 }}
7995 : new BasicCredentialsProvider ())
8096 .build ()
@@ -119,10 +135,10 @@ private <T, V> T request(String url,
119135 HttpRequestConfig config ,
120136 Class <T > clazz ,
121137 String method ) throws HttpException , HttpBadRequestException {
122- HttpUriRequest request = this .buildRequest (method , url , data , config );
138+ HttpUriRequestBase request = this .buildRequest (method , url , data , config );
123139 String httpResponse = null ;
124140 try (CloseableHttpResponse response = httpClient .execute (request )) {
125- int statusCode = response .getStatusLine (). getStatusCode ();
141+ int statusCode = response .getCode ();
126142 if (statusCode < 200 || statusCode >= 300 ) {
127143 String error = this .toString (response .getEntity ());
128144 throw this .jsonTransformer .parse (error , CrowdinApiException .class );
@@ -142,30 +158,32 @@ private <T, V> T request(String url,
142158 }
143159 }
144160
145- private <V > HttpUriRequest buildRequest (String httpMethod , String url , V data , HttpRequestConfig config ) {
146- RequestBuilder requestBuilder = RequestBuilder .create (httpMethod );
147- requestBuilder .setUri (URI .create (this .appendUrlParams (url , config .getUrlParams ())));
148- requestBuilder .addHeader ("Authorization" , "Bearer " + this .credentials .getToken ());
161+ private <V > HttpUriRequestBase buildRequest (String httpMethod , String url , V data , HttpRequestConfig config ) {
162+ //RequestBuilder requestBuilder = RequestBuilder.create(httpMethod);
163+ HttpUriRequestBase request = new HttpUriRequestBase (httpMethod , URI .create (this .appendUrlParams (url , config .getUrlParams ())));
164+
165+ //requestBuilder.setUri(URI.create(this.appendUrlParams(url, config.getUrlParams())));
166+ request .addHeader ("Authorization" , "Bearer " + this .credentials .getToken ());
149167 if (data != null ) {
150168 HttpEntity entity ;
151169 if (data instanceof InputStream ) {
152- entity = new InputStreamEntity ((InputStream ) data );
170+ entity = new InputStreamEntity ((InputStream ) data , ContentType . APPLICATION_OCTET_STREAM );
153171 } else if (data instanceof String ) {
154172 entity = new StringEntity ((String ) data , ContentType .APPLICATION_OCTET_STREAM );
155173 } else {
156174 entity = new StringEntity (this .jsonTransformer .convert (data ), ContentType .APPLICATION_JSON );
157175 }
158- requestBuilder .setEntity (entity );
176+ request .setEntity (entity );
159177 } else if (HttpPost .METHOD_NAME .equals (httpMethod )) {
160- requestBuilder .setEntity (new StringEntity ("" , ContentType .APPLICATION_JSON ));
178+ request .setEntity (new StringEntity ("" , ContentType .APPLICATION_JSON ));
161179 }
162180 Map <String , Object > headers = new HashMap <>();
163181 headers .putAll (config .getHeaders ());
164182 headers .putAll (this .defaultHeaders );
165183 for (Map .Entry <String , ?> entry : headers .entrySet ()) {
166- requestBuilder = requestBuilder .addHeader (entry .getKey (), entry .getValue ().toString ());
184+ request .addHeader (entry .getKey (), entry .getValue ().toString ());
167185 }
168- return requestBuilder . build () ;
186+ return request ;
169187 }
170188
171189 private String toString (HttpEntity entity ) throws IOException {
0 commit comments