23
23
import me .chanjar .weixin .cp .util .json .WxCpGsonBuilder ;
24
24
import org .apache .commons .lang3 .StringUtils ;
25
25
import org .apache .http .HttpHost ;
26
- import org .apache .http .client .ClientProtocolException ;
27
26
import org .apache .http .client .config .RequestConfig ;
28
27
import org .apache .http .client .methods .CloseableHttpResponse ;
29
28
import org .apache .http .client .methods .HttpGet ;
@@ -69,7 +68,7 @@ public class WxCpServiceImpl implements WxCpService {
69
68
public boolean checkSignature (String msgSignature , String timestamp , String nonce , String data ) {
70
69
try {
71
70
return SHA1 .gen (this .configStorage .getToken (), timestamp , nonce , data )
72
- .equals (msgSignature );
71
+ .equals (msgSignature );
73
72
} catch (Exception e ) {
74
73
return false ;
75
74
}
@@ -95,18 +94,18 @@ public String getAccessToken(boolean forceRefresh) throws WxErrorException {
95
94
synchronized (this .globalAccessTokenRefreshLock ) {
96
95
if (this .configStorage .isAccessTokenExpired ()) {
97
96
String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?"
98
- + "&corpid=" + this .configStorage .getCorpId ()
99
- + "&corpsecret=" + this .configStorage .getCorpSecret ();
97
+ + "&corpid=" + this .configStorage .getCorpId ()
98
+ + "&corpsecret=" + this .configStorage .getCorpSecret ();
100
99
try {
101
100
HttpGet httpGet = new HttpGet (url );
102
101
if (this .httpProxy != null ) {
103
102
RequestConfig config = RequestConfig .custom ()
104
- .setProxy (this .httpProxy ).build ();
103
+ .setProxy (this .httpProxy ).build ();
105
104
httpGet .setConfig (config );
106
105
}
107
106
String resultContent = null ;
108
107
try (CloseableHttpClient httpclient = getHttpclient ();
109
- CloseableHttpResponse response = httpclient .execute (httpGet )) {
108
+ CloseableHttpResponse response = httpclient .execute (httpGet )) {
110
109
resultContent = new BasicResponseHandler ().handleResponse (response );
111
110
} finally {
112
111
httpGet .releaseConnection ();
@@ -117,9 +116,7 @@ public String getAccessToken(boolean forceRefresh) throws WxErrorException {
117
116
}
118
117
WxAccessToken accessToken = WxAccessToken .fromJson (resultContent );
119
118
this .configStorage .updateAccessToken (
120
- accessToken .getAccessToken (), accessToken .getExpiresIn ());
121
- } catch (ClientProtocolException e ) {
122
- throw new RuntimeException (e );
119
+ accessToken .getAccessToken (), accessToken .getExpiresIn ());
123
120
} catch (IOException e ) {
124
121
throw new RuntimeException (e );
125
122
}
@@ -149,7 +146,7 @@ public String getJsapiTicket(boolean forceRefresh) throws WxErrorException {
149
146
String jsapiTicket = tmpJsonObject .get ("ticket" ).getAsString ();
150
147
int expiresInSeconds = tmpJsonObject .get ("expires_in" ).getAsInt ();
151
148
this .configStorage .updateJsapiTicket (jsapiTicket ,
152
- expiresInSeconds );
149
+ expiresInSeconds );
153
150
}
154
151
}
155
152
}
@@ -162,10 +159,10 @@ public WxJsapiSignature createJsapiSignature(String url) throws WxErrorException
162
159
String noncestr = RandomUtils .getRandomStr ();
163
160
String jsapiTicket = getJsapiTicket (false );
164
161
String signature = SHA1 .genWithAmple (
165
- "jsapi_ticket=" + jsapiTicket ,
166
- "noncestr=" + noncestr ,
167
- "timestamp=" + timestamp ,
168
- "url=" + url
162
+ "jsapi_ticket=" + jsapiTicket ,
163
+ "noncestr=" + noncestr ,
164
+ "timestamp=" + timestamp ,
165
+ "url=" + url
169
166
);
170
167
WxJsapiSignature jsapiSignature = new WxJsapiSignature ();
171
168
jsapiSignature .setTimestamp (timestamp );
@@ -193,7 +190,7 @@ public void menuCreate(WxMenu menu) throws WxErrorException {
193
190
@ Override
194
191
public void menuCreate (Integer agentId , WxMenu menu ) throws WxErrorException {
195
192
String url = "https://qyapi.weixin.qq.com/cgi-bin/menu/create?agentid="
196
- + this .configStorage .getAgentId ();
193
+ + this .configStorage .getAgentId ();
197
194
post (url , menu .toJson ());
198
195
}
199
196
@@ -230,7 +227,7 @@ public WxMenu menuGet(Integer agentId) throws WxErrorException {
230
227
231
228
@ Override
232
229
public WxMediaUploadResult mediaUpload (String mediaType , String fileType , InputStream inputStream )
233
- throws WxErrorException , IOException {
230
+ throws WxErrorException , IOException {
234
231
return mediaUpload (mediaType , FileUtils .createTmpFile (inputStream , UUID .randomUUID ().toString (), fileType ));
235
232
}
236
233
@@ -244,19 +241,19 @@ public WxMediaUploadResult mediaUpload(String mediaType, File file) throws WxErr
244
241
public File mediaDownload (String media_id ) throws WxErrorException {
245
242
String url = "https://qyapi.weixin.qq.com/cgi-bin/media/get" ;
246
243
return execute (
247
- new MediaDownloadRequestExecutor (
248
- this .configStorage .getTmpDirFile ()),
249
- url , "media_id=" + media_id );
244
+ new MediaDownloadRequestExecutor (
245
+ this .configStorage .getTmpDirFile ()),
246
+ url , "media_id=" + media_id );
250
247
}
251
248
252
249
253
250
@ Override
254
251
public Integer departCreate (WxCpDepart depart ) throws WxErrorException {
255
252
String url = "https://qyapi.weixin.qq.com/cgi-bin/department/create" ;
256
253
String responseContent = execute (
257
- new SimplePostRequestExecutor (),
258
- url ,
259
- depart .toJson ());
254
+ new SimplePostRequestExecutor (),
255
+ url ,
256
+ depart .toJson ());
260
257
JsonElement tmpJsonElement = new JsonParser ().parse (responseContent );
261
258
return GsonHelper .getAsInteger (tmpJsonElement .getAsJsonObject ().get ("id" ));
262
259
}
@@ -283,11 +280,11 @@ public List<WxCpDepart> departGet() throws WxErrorException {
283
280
*/
284
281
JsonElement tmpJsonElement = new JsonParser ().parse (responseContent );
285
282
return WxCpGsonBuilder .INSTANCE .create ()
286
- .fromJson (
287
- tmpJsonElement .getAsJsonObject ().get ("department" ),
288
- new TypeToken <List <WxCpDepart >>() {
289
- }.getType ()
290
- );
283
+ .fromJson (
284
+ tmpJsonElement .getAsJsonObject ().get ("department" ),
285
+ new TypeToken <List <WxCpDepart >>() {
286
+ }.getType ()
287
+ );
291
288
}
292
289
293
290
@ Override
@@ -313,8 +310,8 @@ public void userDelete(String[] userids) throws WxErrorException {
313
310
String url = "https://qyapi.weixin.qq.com/cgi-bin/user/batchdelete" ;
314
311
JsonObject jsonObject = new JsonObject ();
315
312
JsonArray jsonArray = new JsonArray ();
316
- for (int i = 0 ; i < userids . length ; i ++ ) {
317
- jsonArray .add (new JsonPrimitive (userids [ i ] ));
313
+ for (String userid : userids ) {
314
+ jsonArray .add (new JsonPrimitive (userid ));
318
315
}
319
316
jsonObject .add ("useridlist" , jsonArray );
320
317
post (url , jsonObject .toString ());
@@ -343,11 +340,11 @@ public List<WxCpUser> userList(Integer departId, Boolean fetchChild, Integer sta
343
340
String responseContent = get (url , params );
344
341
JsonElement tmpJsonElement = new JsonParser ().parse (responseContent );
345
342
return WxCpGsonBuilder .INSTANCE .create ()
346
- .fromJson (
347
- tmpJsonElement .getAsJsonObject ().get ("userlist" ),
348
- new TypeToken <List <WxCpUser >>() {
349
- }.getType ()
350
- );
343
+ .fromJson (
344
+ tmpJsonElement .getAsJsonObject ().get ("userlist" ),
345
+ new TypeToken <List <WxCpUser >>() {
346
+ }.getType ()
347
+ );
351
348
}
352
349
353
350
@ Override
@@ -366,11 +363,11 @@ public List<WxCpUser> departGetUsers(Integer departId, Boolean fetchChild, Integ
366
363
String responseContent = get (url , params );
367
364
JsonElement tmpJsonElement = new JsonParser ().parse (responseContent );
368
365
return WxCpGsonBuilder .INSTANCE .create ()
369
- .fromJson (
370
- tmpJsonElement .getAsJsonObject ().get ("userlist" ),
371
- new TypeToken <List <WxCpUser >>() {
372
- }.getType ()
373
- );
366
+ .fromJson (
367
+ tmpJsonElement .getAsJsonObject ().get ("userlist" ),
368
+ new TypeToken <List <WxCpUser >>() {
369
+ }.getType ()
370
+ );
374
371
}
375
372
376
373
@ Override
@@ -404,11 +401,11 @@ public List<WxCpTag> tagGet() throws WxErrorException {
404
401
String responseContent = get (url , null );
405
402
JsonElement tmpJsonElement = new JsonParser ().parse (responseContent );
406
403
return WxCpGsonBuilder .INSTANCE .create ()
407
- .fromJson (
408
- tmpJsonElement .getAsJsonObject ().get ("taglist" ),
409
- new TypeToken <List <WxCpTag >>() {
410
- }.getType ()
411
- );
404
+ .fromJson (
405
+ tmpJsonElement .getAsJsonObject ().get ("taglist" ),
406
+ new TypeToken <List <WxCpTag >>() {
407
+ }.getType ()
408
+ );
412
409
}
413
410
414
411
@ Override
@@ -417,11 +414,11 @@ public List<WxCpUser> tagGetUsers(String tagId) throws WxErrorException {
417
414
String responseContent = get (url , null );
418
415
JsonElement tmpJsonElement = new JsonParser ().parse (responseContent );
419
416
return WxCpGsonBuilder .INSTANCE .create ()
420
- .fromJson (
421
- tmpJsonElement .getAsJsonObject ().get ("userlist" ),
422
- new TypeToken <List <WxCpUser >>() {
423
- }.getType ()
424
- );
417
+ .fromJson (
418
+ tmpJsonElement .getAsJsonObject ().get ("userlist" ),
419
+ new TypeToken <List <WxCpUser >>() {
420
+ }.getType ()
421
+ );
425
422
}
426
423
427
424
@ Override
@@ -460,14 +457,14 @@ public void tagRemoveUsers(String tagId, List<String> userIds) throws WxErrorExc
460
457
}
461
458
462
459
@ Override
463
- public String oauth2buildAuthorizationUrl (String state ) {
464
- return this .oauth2buildAuthorizationUrl (
465
- this .configStorage .getOauth2redirectUri (),
466
- state
467
- );
468
- }
460
+ public String oauth2buildAuthorizationUrl (String state ) {
461
+ return this .oauth2buildAuthorizationUrl (
462
+ this .configStorage .getOauth2redirectUri (),
463
+ state
464
+ );
465
+ }
469
466
470
- @ Override
467
+ @ Override
471
468
public String oauth2buildAuthorizationUrl (String redirectUri , String state ) {
472
469
String url = "https://open.weixin.qq.com/connect/oauth2/authorize?" ;
473
470
url += "appid=" + this .configStorage .getCorpId ();
@@ -489,8 +486,8 @@ public String[] oauth2getUserInfo(String code) throws WxErrorException {
489
486
@ Override
490
487
public String [] oauth2getUserInfo (Integer agentId , String code ) throws WxErrorException {
491
488
String url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?"
492
- + "code=" + code
493
- + "&agentid=" + agentId ;
489
+ + "code=" + code
490
+ + "&agentid=" + agentId ;
494
491
String responseText = get (url , null );
495
492
JsonElement je = new JsonParser ().parse (responseText );
496
493
JsonObject jo = je .getAsJsonObject ();
@@ -544,14 +541,14 @@ public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) thro
544
541
return executeInternal (executor , uri , data );
545
542
} catch (WxErrorException e ) {
546
543
WxError error = e .getError ();
547
- /**
544
+ /*
548
545
* -1 系统繁忙, 1000ms后重试
549
546
*/
550
547
if (error .getErrorCode () == -1 ) {
551
548
int sleepMillis = this .retrySleepMillis * (1 << retryTimes );
552
549
try {
553
550
this .log .debug ("微信系统繁忙,{}ms 后重试(第{}次)" , sleepMillis ,
554
- retryTimes + 1 );
551
+ retryTimes + 1 );
555
552
Thread .sleep (sleepMillis );
556
553
} catch (InterruptedException e1 ) {
557
554
throw new RuntimeException (e1 );
@@ -566,7 +563,7 @@ public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) thro
566
563
}
567
564
568
565
protected synchronized <T , E > T executeInternal (RequestExecutor <T , E > executor , String uri , E data ) throws WxErrorException {
569
- if (uri .indexOf ("access_token=" ) != - 1 ) {
566
+ if (uri .contains ("access_token=" )) {
570
567
throw new IllegalArgumentException ("uri参数中不允许有access_token: " + uri );
571
568
}
572
569
String accessToken = getAccessToken (false );
@@ -576,7 +573,7 @@ protected synchronized <T, E> T executeInternal(RequestExecutor<T, E> executor,
576
573
577
574
try {
578
575
return executor .execute (getHttpclient (), this .httpProxy ,
579
- uriWithAccessToken , data );
576
+ uriWithAccessToken , data );
580
577
} catch (WxErrorException e ) {
581
578
WxError error = e .getError ();
582
579
/*
@@ -593,8 +590,6 @@ protected synchronized <T, E> T executeInternal(RequestExecutor<T, E> executor,
593
590
throw new WxErrorException (error );
594
591
}
595
592
return null ;
596
- } catch (ClientProtocolException e ) {
597
- throw new RuntimeException (e );
598
593
} catch (IOException e ) {
599
594
throw new RuntimeException (e );
600
595
}
@@ -608,15 +603,15 @@ protected CloseableHttpClient getHttpclient() {
608
603
public void setWxCpConfigStorage (WxCpConfigStorage wxConfigProvider ) {
609
604
this .configStorage = wxConfigProvider ;
610
605
ApacheHttpClientBuilder apacheHttpClientBuilder = this .configStorage
611
- .getApacheHttpClientBuilder ();
606
+ .getApacheHttpClientBuilder ();
612
607
if (null == apacheHttpClientBuilder ) {
613
608
apacheHttpClientBuilder = DefaultApacheHttpClientBuilder .get ();
614
609
}
615
610
616
611
apacheHttpClientBuilder .httpProxyHost (this .configStorage .getHttpProxyHost ())
617
- .httpProxyPort (this .configStorage .getHttpProxyPort ())
618
- .httpProxyUsername (this .configStorage .getHttpProxyUsername ())
619
- .httpProxyPassword (this .configStorage .getHttpProxyPassword ());
612
+ .httpProxyPort (this .configStorage .getHttpProxyPort ())
613
+ .httpProxyUsername (this .configStorage .getHttpProxyUsername ())
614
+ .httpProxyPassword (this .configStorage .getHttpProxyPassword ());
620
615
621
616
if (this .configStorage .getHttpProxyHost () != null && this .configStorage .getHttpProxyPort () > 0 ) {
622
617
this .httpProxy = new HttpHost (this .configStorage .getHttpProxyHost (), this .configStorage .getHttpProxyPort ());
0 commit comments