28
28
import java .util .List ;
29
29
import java .util .Map ;
30
30
31
+ import org .apache .hc .client5 .http .utils .Base64 ;
31
32
import org .slf4j .Logger ;
32
33
import org .slf4j .LoggerFactory ;
33
34
44
45
import org .springframework .http .client .HttpComponentsClientHttpRequestFactory ;
45
46
import org .springframework .http .converter .HttpMessageNotReadableException ;
46
47
import org .springframework .http .converter .json .MappingJackson2HttpMessageConverter ;
47
- import org .springframework .util .Base64Utils ;
48
48
import org .springframework .util .StreamUtils ;
49
49
import org .springframework .util .StringUtils ;
50
50
import org .springframework .web .client .DefaultResponseErrorHandler ;
@@ -86,31 +86,24 @@ static RestTemplate createDefaultRestTemplate(String username, String password)
86
86
rest .setErrorHandler (new DefaultResponseErrorHandler () {
87
87
@ Override
88
88
public void handleError (ClientHttpResponse response ) throws IOException {
89
- if (response .getStatusCode () == HttpStatus .FORBIDDEN && response
90
- .getHeaders ().getFirst ("X-RateLimit-Remaining" ).equals ("0" )) {
91
- throw new IllegalStateException (
92
- "Rate limit exceeded. Limit will reset at "
93
- + new Date (Long
94
- .valueOf (response .getHeaders ()
95
- .getFirst ("X-RateLimit-Reset" ))
96
- * 1000 ));
89
+ if (response .getStatusCode () == HttpStatus .FORBIDDEN
90
+ && response .getHeaders ().getFirst ("X-RateLimit-Remaining" ).equals ("0" )) {
91
+ throw new IllegalStateException ("Rate limit exceeded. Limit will reset at "
92
+ + new Date (Long .valueOf (response .getHeaders ().getFirst ("X-RateLimit-Reset" )) * 1000 ));
97
93
}
98
94
}
99
95
});
100
96
BufferingClientHttpRequestFactory bufferingClient = new BufferingClientHttpRequestFactory (
101
97
new HttpComponentsClientHttpRequestFactory ());
102
98
rest .setRequestFactory (bufferingClient );
103
- rest .setInterceptors (Collections
104
- .singletonList (new BasicAuthorizationInterceptor (username , password )));
105
- rest .setMessageConverters (
106
- Arrays .asList (new ErrorLoggingMappingJackson2HttpMessageConverter ()));
99
+ rest .setInterceptors (Collections .singletonList (new BasicAuthorizationInterceptor (username , password )));
100
+ rest .setMessageConverters (Arrays .asList (new ErrorLoggingMappingJackson2HttpMessageConverter ()));
107
101
return rest ;
108
102
}
109
103
110
104
@ Override
111
105
public Page <Issue > getIssues (String organization , String repository ) {
112
- String url = "https://api.github.com/repos/" + organization + "/" + repository
113
- + "/issues" ;
106
+ String url = "https://api.github.com/repos/" + organization + "/" + repository + "/issues" ;
114
107
return getPage (url , Issue [].class );
115
108
}
116
109
@@ -129,8 +122,7 @@ private <T> Page<T> getPage(String url, Class<T[]> type) {
129
122
return null ;
130
123
}
131
124
ResponseEntity <T []> contents = this .rest .getForEntity (url , type );
132
- return new StandardPage <>(Arrays .asList (contents .getBody ()),
133
- () -> getPage (getNextUrl (contents ), type ));
125
+ return new StandardPage <>(Arrays .asList (contents .getBody ()), () -> getPage (getNextUrl (contents ), type ));
134
126
}
135
127
136
128
private String getNextUrl (ResponseEntity <?> response ) {
@@ -141,16 +133,13 @@ private String getNextUrl(ResponseEntity<?> response) {
141
133
public Issue addLabel (Issue issue , String labelName ) {
142
134
URI uri = URI .create (issue .getLabelsUrl ().replace ("{/name}" , "" ));
143
135
log .info ("Adding label {} to {}" , labelName , uri );
144
- ResponseEntity <Label []> response = this .rest .exchange (
145
- new RequestEntity <>(Arrays .asList (labelName ), HttpMethod .POST , uri ),
146
- Label [].class );
136
+ ResponseEntity <Label []> response = this .rest
137
+ .exchange (new RequestEntity <>(Arrays .asList (labelName ), HttpMethod .POST , uri ), Label [].class );
147
138
if (response .getStatusCode () != HttpStatus .OK ) {
148
- log .warn ("Failed to add label to issue. Response status: "
149
- + response .getStatusCode ());
139
+ log .warn ("Failed to add label to issue. Response status: " + response .getStatusCode ());
150
140
}
151
- return new Issue (issue .getUrl (), issue .getCommentsUrl (), issue .getEventsUrl (),
152
- issue .getLabelsUrl (), issue .getUser (), Arrays .asList (response .getBody ()),
153
- issue .getMilestone (), issue .getPullRequest ());
141
+ return new Issue (issue .getUrl (), issue .getCommentsUrl (), issue .getEventsUrl (), issue .getLabelsUrl (),
142
+ issue .getUser (), Arrays .asList (response .getBody ()), issue .getMilestone (), issue .getPullRequest ());
154
143
}
155
144
156
145
@ Override
@@ -162,42 +151,35 @@ public Issue removeLabel(Issue issue, String labelName) {
162
151
catch (URISyntaxException ex ) {
163
152
throw new RuntimeException (ex );
164
153
}
165
- ResponseEntity <Label []> response = this .rest .exchange (
166
- new RequestEntity <Void >(HttpMethod .DELETE , URI .create (
167
- issue .getLabelsUrl ().replace ("{/name}" , "/" + encodedName ))),
168
- Label [].class );
154
+ ResponseEntity <Label []> response = this .rest .exchange (new RequestEntity <Void >(HttpMethod .DELETE ,
155
+ URI .create (issue .getLabelsUrl ().replace ("{/name}" , "/" + encodedName ))), Label [].class );
169
156
List <Label > labels ;
170
157
if (response .getStatusCode () != HttpStatus .OK ) {
171
- log .warn ("Failed to remove label from issue. Response status: "
172
- + response .getStatusCode ());
158
+ log .warn ("Failed to remove label from issue. Response status: " + response .getStatusCode ());
173
159
labels = Collections .emptyList ();
174
160
}
175
161
else {
176
162
labels = Arrays .asList (response .getBody ());
177
163
}
178
- return new Issue (issue .getUrl (), issue .getCommentsUrl (), issue .getEventsUrl (),
179
- issue .getLabelsUrl (), issue .getUser (), labels , issue .getMilestone (),
180
- issue .getPullRequest ());
164
+ return new Issue (issue .getUrl (), issue .getCommentsUrl (), issue .getEventsUrl (), issue .getLabelsUrl (),
165
+ issue .getUser (), labels , issue .getMilestone (), issue .getPullRequest ());
181
166
}
182
167
183
168
@ Override
184
169
public Comment addComment (Issue issue , String comment ) {
185
170
Map <String , String > body = new HashMap <>();
186
171
body .put ("body" , comment );
187
- return this .rest .postForEntity (issue .getCommentsUrl (), body , Comment .class )
188
- .getBody ();
172
+ return this .rest .postForEntity (issue .getCommentsUrl (), body , Comment .class ).getBody ();
189
173
}
190
174
191
175
@ Override
192
176
public Issue close (Issue issue ) {
193
177
Map <String , String > body = new HashMap <>();
194
178
body .put ("state" , "closed" );
195
- ResponseEntity <Issue > response = this .rest .exchange (
196
- new RequestEntity <>(body , HttpMethod .PATCH , URI .create (issue .getUrl ())),
197
- Issue .class );
179
+ ResponseEntity <Issue > response = this .rest
180
+ .exchange (new RequestEntity <>(body , HttpMethod .PATCH , URI .create (issue .getUrl ())), Issue .class );
198
181
if (response .getStatusCode () != HttpStatus .OK ) {
199
- log .warn ("Failed to close issue. Response status: "
200
- + response .getStatusCode ());
182
+ log .warn ("Failed to close issue. Response status: " + response .getStatusCode ());
201
183
}
202
184
return response .getBody ();
203
185
}
@@ -208,8 +190,7 @@ private static final class ErrorLoggingMappingJackson2HttpMessageConverter
208
190
private static final Charset CHARSET_UTF_8 = Charset .forName ("UTF-8" );
209
191
210
192
@ Override
211
- public Object read (Type type , Class <?> contextClass ,
212
- HttpInputMessage inputMessage )
193
+ public Object read (Type type , Class <?> contextClass , HttpInputMessage inputMessage )
213
194
throws IOException , HttpMessageNotReadableException {
214
195
try {
215
196
return super .read (type , contextClass , inputMessage );
@@ -226,8 +207,7 @@ public Object read(Type type, Class<?> contextClass,
226
207
if (inputMessage instanceof ClientHttpResponse ) {
227
208
ClientHttpResponse response = (ClientHttpResponse ) inputMessage ;
228
209
if (response .getStatusCode ().is2xxSuccessful ()) {
229
- log .error ("Failed to create {} from {}" , type .getTypeName (),
230
- read (inputMessage ), ex );
210
+ log .error ("Failed to create {} from {}" , type .getTypeName (), read (inputMessage ), ex );
231
211
throw ex ;
232
212
}
233
213
}
@@ -241,8 +221,7 @@ private String read(HttpInputMessage inputMessage) throws IOException {
241
221
242
222
}
243
223
244
- private static class BasicAuthorizationInterceptor
245
- implements ClientHttpRequestInterceptor {
224
+ private static class BasicAuthorizationInterceptor implements ClientHttpRequestInterceptor {
246
225
247
226
private static final Charset UTF_8 = Charset .forName ("UTF-8" );
248
227
@@ -256,10 +235,9 @@ private static class BasicAuthorizationInterceptor
256
235
}
257
236
258
237
@ Override
259
- public ClientHttpResponse intercept (HttpRequest request , byte [] body ,
260
- ClientHttpRequestExecution execution ) throws IOException {
261
- String token = Base64Utils .encodeToString (
262
- (this .username + ":" + this .password ).getBytes (UTF_8 ));
238
+ public ClientHttpResponse intercept (HttpRequest request , byte [] body , ClientHttpRequestExecution execution )
239
+ throws IOException {
240
+ String token = Base64 .encodeBase64String ((this .username + ":" + this .password ).getBytes (UTF_8 ));
263
241
request .getHeaders ().add ("Authorization" , "Basic " + token );
264
242
return execution .execute (request , body );
265
243
}
0 commit comments