4
4
import com .github .benmanes .caffeine .cache .LoadingCache ;
5
5
import hudson .AbortException ;
6
6
import java .io .IOException ;
7
+ import java .net .URISyntaxException ;
7
8
import java .time .Duration ;
8
9
import java .util .HashMap ;
9
10
import java .util .Map ;
13
14
import java .util .logging .Logger ;
14
15
import jenkins .model .Jenkins ;
15
16
import 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 ;
27
31
import org .json .JSONArray ;
28
32
import org .json .JSONObject ;
29
33
@@ -41,15 +45,14 @@ public class SlackChannelIdCache {
41
45
42
46
private static Map <String , String > populateCache (String token ) {
43
47
HttpClientBuilder closeableHttpClientBuilder = HttpClient .getCloseableHttpClientBuilder (Jenkins .get ().getProxy ())
44
- .setRetryHandler ((exception , executionCount , context ) -> executionCount <= MAX_RETRIES )
45
- .setServiceUnavailableRetryStrategy (new ServiceUnavailableRetryStrategy () {
48
+ .setRetryStrategy (new HttpRequestRetryStrategy () {
46
49
47
- long retryInterval ;
50
+ private long retryInterval ;
48
51
49
52
@ Override
50
53
public boolean retryRequest (HttpResponse response , int executionCount , HttpContext context ) {
51
54
boolean shouldRetry = executionCount <= MAX_RETRIES &&
52
- response .getStatusLine (). getStatusCode () == HttpStatus .SC_TOO_MANY_REQUESTS ;
55
+ response .getCode () == HttpStatus .SC_TOO_MANY_REQUESTS ;
53
56
if (shouldRetry ) {
54
57
Header firstHeader = response .getFirstHeader ("Retry-After" );
55
58
if (firstHeader != null ) {
@@ -61,13 +64,18 @@ public boolean retryRequest(HttpResponse response, int executionCount, HttpConte
61
64
}
62
65
63
66
@ 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 );
66
74
}
67
75
});
68
76
try (CloseableHttpClient client = closeableHttpClientBuilder .build ()) {
69
77
return convertChannelNameToId (client , token , new HashMap <>(), null );
70
- } catch (IOException e ) {
78
+ } catch (IOException | URISyntaxException e ) {
71
79
throw new RuntimeException (e );
72
80
}
73
81
}
@@ -110,24 +118,23 @@ private static String cleanChannelName(String channelName) {
110
118
}
111
119
112
120
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 {
114
122
convertPublicChannelNameToId (client , token , channels , cursor );
115
123
convertPrivateChannelNameToId (client , token , channels , cursor );
116
124
return channels ;
117
125
}
118
126
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" )
122
129
.addParameter ("exclude_archived" , "true" )
123
130
.addParameter ("types" , "public_channel" )
124
131
.addParameter ("limit" , "999" );
125
-
126
132
if (cursor != null ) {
127
- requestBuilder .addParameter ("cursor" , cursor );
133
+ uriBuilder .addParameter ("cursor" , cursor );
128
134
}
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 ());
131
138
132
139
if (!result .getBoolean ("ok" )) {
133
140
logger .warning ("Couldn't convert channel name to ID in Slack: " + result );
@@ -152,18 +159,17 @@ private static Map<String, String> convertPublicChannelNameToId(CloseableHttpCli
152
159
return channels ;
153
160
}
154
161
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" )
158
164
.addParameter ("exclude_archived" , "true" )
159
165
.addParameter ("types" , "private_channel" )
160
166
.addParameter ("limit" , "999" );
161
-
162
167
if (cursor != null ) {
163
- requestBuilder .addParameter ("cursor" , cursor );
168
+ uriBuilder .addParameter ("cursor" , cursor );
164
169
}
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 ());
167
173
168
174
if (!result .getBoolean ("ok" )) {
169
175
logger .warning ("Couldn't convert channel name to ID in Slack: " + result );
@@ -188,15 +194,15 @@ private static Map<String, String> convertPrivateChannelNameToId(CloseableHttpCl
188
194
return channels ;
189
195
}
190
196
191
- private static ResponseHandler <JSONObject > getStandardResponseHandler () {
197
+ private static HttpClientResponseHandler <JSONObject > getStandardResponseHandler () {
192
198
return response -> {
193
- int status = response .getStatusLine (). getStatusCode ();
199
+ int status = response .getCode ();
194
200
if (status >= 200 && status < 300 ) {
195
201
HttpEntity entity = response .getEntity ();
196
202
return entity != null ? new JSONObject (EntityUtils .toString (entity )) : null ;
197
203
} else {
198
204
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 );
200
206
}
201
207
};
202
208
}
0 commit comments