44import com .github .benmanes .caffeine .cache .LoadingCache ;
55import hudson .AbortException ;
66import java .io .IOException ;
7+ import java .net .URISyntaxException ;
78import java .time .Duration ;
89import java .util .HashMap ;
910import java .util .Map ;
1314import java .util .logging .Logger ;
1415import jenkins .model .Jenkins ;
1516import jenkins .plugins .slack .HttpClient ;
16- import org .apache .http .Header ;
17- import org .apache .http .HttpEntity ;
18- import org .apache .http .HttpResponse ;
19- import org .apache .http .HttpStatus ;
20- import org .apache .http .client .ResponseHandler ;
21- import org .apache .http .client .ServiceUnavailableRetryStrategy ;
22- import org .apache .http .client .methods .RequestBuilder ;
23- import org .apache .http .impl .client .CloseableHttpClient ;
24- import org .apache .http .impl .client .HttpClientBuilder ;
25- import org .apache .http .protocol .HttpContext ;
26- import org .apache .http .util .EntityUtils ;
17+ import org .apache .hc .client5 .http .HttpRequestRetryStrategy ;
18+ import org .apache .hc .client5 .http .fluent .Request ;
19+ import org .apache .hc .client5 .http .impl .classic .CloseableHttpClient ;
20+ import org .apache .hc .client5 .http .impl .classic .HttpClientBuilder ;
21+ import org .apache .hc .core5 .http .Header ;
22+ import org .apache .hc .core5 .http .HttpEntity ;
23+ import org .apache .hc .core5 .http .HttpRequest ;
24+ import org .apache .hc .core5 .http .HttpResponse ;
25+ import org .apache .hc .core5 .http .HttpStatus ;
26+ import org .apache .hc .core5 .http .io .HttpClientResponseHandler ;
27+ import org .apache .hc .core5 .http .io .entity .EntityUtils ;
28+ import org .apache .hc .core5 .http .protocol .HttpContext ;
29+ import org .apache .hc .core5 .net .URIBuilder ;
30+ import org .apache .hc .core5 .util .TimeValue ;
2731import org .json .JSONArray ;
2832import org .json .JSONObject ;
2933
@@ -41,15 +45,14 @@ public class SlackChannelIdCache {
4145
4246 private static Map <String , String > populateCache (String token ) {
4347 HttpClientBuilder closeableHttpClientBuilder = HttpClient .getCloseableHttpClientBuilder (Jenkins .get ().getProxy ())
44- .setRetryHandler ((exception , executionCount , context ) -> executionCount <= MAX_RETRIES )
45- .setServiceUnavailableRetryStrategy (new ServiceUnavailableRetryStrategy () {
48+ .setRetryStrategy (new HttpRequestRetryStrategy () {
4649
47- long retryInterval ;
50+ private long retryInterval ;
4851
4952 @ Override
5053 public boolean retryRequest (HttpResponse response , int executionCount , HttpContext context ) {
5154 boolean shouldRetry = executionCount <= MAX_RETRIES &&
52- response .getStatusLine (). getStatusCode () == HttpStatus .SC_TOO_MANY_REQUESTS ;
55+ response .getCode () == HttpStatus .SC_TOO_MANY_REQUESTS ;
5356 if (shouldRetry ) {
5457 Header firstHeader = response .getFirstHeader ("Retry-After" );
5558 if (firstHeader != null ) {
@@ -61,13 +64,18 @@ public boolean retryRequest(HttpResponse response, int executionCount, HttpConte
6164 }
6265
6366 @ Override
64- public long getRetryInterval () {
65- return retryInterval ;
67+ public boolean retryRequest (HttpRequest request , IOException exception , int execCount , HttpContext context ) {
68+ return false ;
69+ }
70+
71+ @ Override
72+ public TimeValue getRetryInterval (HttpResponse response , int execCount , HttpContext context ) {
73+ return TimeValue .ofSeconds (retryInterval );
6674 }
6775 });
6876 try (CloseableHttpClient client = closeableHttpClientBuilder .build ()) {
6977 return convertChannelNameToId (client , token , new HashMap <>(), null );
70- } catch (IOException e ) {
78+ } catch (IOException | URISyntaxException e ) {
7179 throw new RuntimeException (e );
7280 }
7381 }
@@ -110,24 +118,23 @@ private static String cleanChannelName(String channelName) {
110118 }
111119
112120
113- private static Map <String , String > convertChannelNameToId (CloseableHttpClient client , String token , Map <String , String > channels , String cursor ) throws IOException {
121+ private static Map <String , String > convertChannelNameToId (CloseableHttpClient client , String token , Map <String , String > channels , String cursor ) throws IOException , URISyntaxException {
114122 convertPublicChannelNameToId (client , token , channels , cursor );
115123 convertPrivateChannelNameToId (client , token , channels , cursor );
116124 return channels ;
117125 }
118126
119- private static Map <String , String > convertPublicChannelNameToId (CloseableHttpClient client , String token , Map <String , String > channels , String cursor ) throws IOException {
120- RequestBuilder requestBuilder = RequestBuilder .get ("https://slack.com/api/conversations.list" )
121- .addHeader ("Authorization" , "Bearer " + token )
127+ private static Map <String , String > convertPublicChannelNameToId (CloseableHttpClient client , String token , Map <String , String > channels , String cursor ) throws IOException , URISyntaxException {
128+ URIBuilder uriBuilder = new URIBuilder ("https://slack.com/api/conversations.list" )
122129 .addParameter ("exclude_archived" , "true" )
123130 .addParameter ("types" , "public_channel" )
124131 .addParameter ("limit" , "999" );
125-
126132 if (cursor != null ) {
127- requestBuilder .addParameter ("cursor" , cursor );
133+ uriBuilder .addParameter ("cursor" , cursor );
128134 }
129- ResponseHandler <JSONObject > standardResponseHandler = getStandardResponseHandler ();
130- JSONObject result = client .execute (requestBuilder .build (), standardResponseHandler );
135+ Request requestBuilder = Request .get (uriBuilder .build ())
136+ .addHeader ("Authorization" , "Bearer " + token );
137+ JSONObject result = requestBuilder .execute (client ).handleResponse (getStandardResponseHandler ());
131138
132139 if (!result .getBoolean ("ok" )) {
133140 logger .warning ("Couldn't convert channel name to ID in Slack: " + result );
@@ -152,18 +159,17 @@ private static Map<String, String> convertPublicChannelNameToId(CloseableHttpCli
152159 return channels ;
153160 }
154161
155- private static Map <String , String > convertPrivateChannelNameToId (CloseableHttpClient client , String token , Map <String , String > channels , String cursor ) throws IOException {
156- RequestBuilder requestBuilder = RequestBuilder .get ("https://slack.com/api/conversations.list" )
157- .addHeader ("Authorization" , "Bearer " + token )
162+ private static Map <String , String > convertPrivateChannelNameToId (CloseableHttpClient client , String token , Map <String , String > channels , String cursor ) throws IOException , URISyntaxException {
163+ URIBuilder uriBuilder = new URIBuilder ("https://slack.com/api/conversations.list" )
158164 .addParameter ("exclude_archived" , "true" )
159165 .addParameter ("types" , "private_channel" )
160166 .addParameter ("limit" , "999" );
161-
162167 if (cursor != null ) {
163- requestBuilder .addParameter ("cursor" , cursor );
168+ uriBuilder .addParameter ("cursor" , cursor );
164169 }
165- ResponseHandler <JSONObject > standardResponseHandler = getStandardResponseHandler ();
166- JSONObject result = client .execute (requestBuilder .build (), standardResponseHandler );
170+ Request requestBuilder = Request .get (uriBuilder .build ())
171+ .addHeader ("Authorization" , "Bearer " + token );
172+ JSONObject result = requestBuilder .execute (client ).handleResponse (getStandardResponseHandler ());
167173
168174 if (!result .getBoolean ("ok" )) {
169175 logger .warning ("Couldn't convert channel name to ID in Slack: " + result );
@@ -188,15 +194,15 @@ private static Map<String, String> convertPrivateChannelNameToId(CloseableHttpCl
188194 return channels ;
189195 }
190196
191- private static ResponseHandler <JSONObject > getStandardResponseHandler () {
197+ private static HttpClientResponseHandler <JSONObject > getStandardResponseHandler () {
192198 return response -> {
193- int status = response .getStatusLine (). getStatusCode ();
199+ int status = response .getCode ();
194200 if (status >= 200 && status < 300 ) {
195201 HttpEntity entity = response .getEntity ();
196202 return entity != null ? new JSONObject (EntityUtils .toString (entity )) : null ;
197203 } else {
198204 String errorMessage = UPLOAD_FAILED_TEMPLATE + status + " " + EntityUtils .toString (response .getEntity ());
199- throw new HttpStatusCodeException (response .getStatusLine (). getStatusCode (), errorMessage );
205+ throw new HttpStatusCodeException (response .getCode (), errorMessage );
200206 }
201207 };
202208 }
0 commit comments