Skip to content

Commit d05da57

Browse files
committed
code clean up for cp module
1 parent f7a64a7 commit d05da57

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+966
-924
lines changed

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpConfigStorage.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
/**
99
* 微信客户端配置存储
10-
* @author Daniel Qian
1110
*
11+
* @author Daniel Qian
1212
*/
1313
public interface WxCpConfigStorage {
1414

@@ -36,12 +36,13 @@ public interface WxCpConfigStorage {
3636

3737
/**
3838
* 应该是线程安全的
39+
*
3940
* @param jsapiTicket
4041
*/
4142
void updateJsapiTicket(String jsapiTicket, int expiresInSeconds);
4243

4344
String getCorpId();
44-
45+
4546
String getCorpSecret();
4647

4748
String getAgentId();
@@ -61,11 +62,12 @@ public interface WxCpConfigStorage {
6162
String getHttp_proxy_username();
6263

6364
String getHttp_proxy_password();
64-
65+
6566
File getTmpDirFile();
6667

6768
/**
6869
* http client builder
70+
*
6971
* @return ApacheHttpClientBuilder
7072
*/
7173
ApacheHttpClientBuilder getApacheHttpClientBuilder();

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpInMemoryConfigStorage.java

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
/**
99
* 基于内存的微信配置provider,在实际生产环境中应该将这些配置持久化
10-
* @author Daniel Qian
1110
*
11+
* @author Daniel Qian
1212
*/
1313
public class WxCpInMemoryConfigStorage implements WxCpConfigStorage {
1414

@@ -39,6 +39,10 @@ public String getAccessToken() {
3939
return this.accessToken;
4040
}
4141

42+
public void setAccessToken(String accessToken) {
43+
this.accessToken = accessToken;
44+
}
45+
4246
public boolean isAccessTokenExpired() {
4347
return System.currentTimeMillis() > this.expiresTime;
4448
}
@@ -50,7 +54,7 @@ public void expireAccessToken() {
5054
public synchronized void updateAccessToken(WxAccessToken accessToken) {
5155
updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
5256
}
53-
57+
5458
public synchronized void updateAccessToken(String accessToken, int expiresInSeconds) {
5559
this.accessToken = accessToken;
5660
this.expiresTime = System.currentTimeMillis() + (expiresInSeconds - 200) * 1000l;
@@ -91,28 +95,32 @@ public String getCorpId() {
9195
return this.corpId;
9296
}
9397

98+
public void setCorpId(String corpId) {
99+
this.corpId = corpId;
100+
}
101+
94102
public String getCorpSecret() {
95103
return this.corpSecret;
96104
}
97105

98-
public String getToken() {
99-
return this.token;
106+
public void setCorpSecret(String corpSecret) {
107+
this.corpSecret = corpSecret;
100108
}
101109

102-
public long getExpiresTime() {
103-
return this.expiresTime;
110+
public String getToken() {
111+
return this.token;
104112
}
105113

106-
public void setCorpId(String corpId) {
107-
this.corpId = corpId;
114+
public void setToken(String token) {
115+
this.token = token;
108116
}
109117

110-
public void setCorpSecret(String corpSecret) {
111-
this.corpSecret = corpSecret;
118+
public long getExpiresTime() {
119+
return this.expiresTime;
112120
}
113121

114-
public void setToken(String token) {
115-
this.token = token;
122+
public void setExpiresTime(long expiresTime) {
123+
this.expiresTime = expiresTime;
116124
}
117125

118126
public String getAesKey() {
@@ -123,14 +131,6 @@ public void setAesKey(String aesKey) {
123131
this.aesKey = aesKey;
124132
}
125133

126-
public void setAccessToken(String accessToken) {
127-
this.accessToken = accessToken;
128-
}
129-
130-
public void setExpiresTime(long expiresTime) {
131-
this.expiresTime = expiresTime;
132-
}
133-
134134
public String getAgentId() {
135135
return agentId;
136136
}
@@ -183,21 +183,21 @@ public void setHttp_proxy_password(String http_proxy_password) {
183183
@Override
184184
public String toString() {
185185
return "WxCpInMemoryConfigStorage{" +
186-
"corpId='" + corpId + '\'' +
187-
", corpSecret='" + corpSecret + '\'' +
188-
", token='" + token + '\'' +
189-
", accessToken='" + accessToken + '\'' +
190-
", aesKey='" + aesKey + '\'' +
191-
", agentId='" + agentId + '\'' +
192-
", expiresTime=" + expiresTime +
193-
", http_proxy_host='" + http_proxy_host + '\'' +
194-
", http_proxy_port=" + http_proxy_port +
195-
", http_proxy_username='" + http_proxy_username + '\'' +
196-
", http_proxy_password='" + http_proxy_password + '\'' +
197-
", jsapiTicket='" + jsapiTicket + '\'' +
198-
", jsapiTicketExpiresTime='" + jsapiTicketExpiresTime + '\'' +
199-
", tmpDirFile='" + tmpDirFile + '\'' +
200-
'}';
186+
"corpId='" + corpId + '\'' +
187+
", corpSecret='" + corpSecret + '\'' +
188+
", token='" + token + '\'' +
189+
", accessToken='" + accessToken + '\'' +
190+
", aesKey='" + aesKey + '\'' +
191+
", agentId='" + agentId + '\'' +
192+
", expiresTime=" + expiresTime +
193+
", http_proxy_host='" + http_proxy_host + '\'' +
194+
", http_proxy_port=" + http_proxy_port +
195+
", http_proxy_username='" + http_proxy_username + '\'' +
196+
", http_proxy_password='" + http_proxy_password + '\'' +
197+
", jsapiTicket='" + jsapiTicket + '\'' +
198+
", jsapiTicketExpiresTime='" + jsapiTicketExpiresTime + '\'' +
199+
", tmpDirFile='" + tmpDirFile + '\'' +
200+
'}';
201201
}
202202

203203
public File getTmpDirFile() {

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpMessageHandler.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package me.chanjar.weixin.cp.api;
22

33
import me.chanjar.weixin.common.exception.WxErrorException;
4-
import me.chanjar.weixin.common.session.WxSession;
54
import me.chanjar.weixin.common.session.WxSessionManager;
65
import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
76
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpMessageRouter.java

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package me.chanjar.weixin.cp.api;
22

3+
import me.chanjar.weixin.common.api.WxErrorExceptionHandler;
4+
import me.chanjar.weixin.common.api.WxMessageDuplicateChecker;
5+
import me.chanjar.weixin.common.api.WxMessageInMemoryDuplicateChecker;
36
import me.chanjar.weixin.common.session.InternalSession;
47
import me.chanjar.weixin.common.session.InternalSessionManager;
58
import me.chanjar.weixin.common.session.StandardSessionManager;
69
import me.chanjar.weixin.common.session.WxSessionManager;
710
import me.chanjar.weixin.common.util.LogExceptionHandler;
8-
import me.chanjar.weixin.common.api.WxErrorExceptionHandler;
9-
import me.chanjar.weixin.common.api.WxMessageDuplicateChecker;
10-
import me.chanjar.weixin.common.api.WxMessageInMemoryDuplicateChecker;
1111
import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
1212
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;
1313
import org.slf4j.Logger;
@@ -45,15 +45,13 @@
4545
* router.route(message);
4646
*
4747
* </pre>
48-
* @author Daniel Qian
4948
*
49+
* @author Daniel Qian
5050
*/
5151
public class WxCpMessageRouter {
5252

53-
protected final Logger log = LoggerFactory.getLogger(WxCpMessageRouter.class);
54-
5553
private static final int DEFAULT_THREAD_POOL_SIZE = 100;
56-
54+
protected final Logger log = LoggerFactory.getLogger(WxCpMessageRouter.class);
5755
private final List<WxCpMessageRouterRule> rules = new ArrayList<WxCpMessageRouterRule>();
5856

5957
private final WxCpService wxCpService;
@@ -79,6 +77,7 @@ public WxCpMessageRouter(WxCpService wxCpService) {
7977
* 设置自定义的 {@link ExecutorService}
8078
* 如果不调用该方法,默认使用 Executors.newFixedThreadPool(100)
8179
* </pre>
80+
*
8281
* @param executorService
8382
*/
8483
public void setExecutorService(ExecutorService executorService) {
@@ -90,6 +89,7 @@ public void setExecutorService(ExecutorService executorService) {
9089
* 设置自定义的 {@link me.chanjar.weixin.common.api.WxMessageDuplicateChecker}
9190
* 如果不调用该方法,默认使用 {@link me.chanjar.weixin.common.api.WxMessageInMemoryDuplicateChecker}
9291
* </pre>
92+
*
9393
* @param messageDuplicateChecker
9494
*/
9595
public void setMessageDuplicateChecker(WxMessageDuplicateChecker messageDuplicateChecker) {
@@ -101,6 +101,7 @@ public void setMessageDuplicateChecker(WxMessageDuplicateChecker messageDuplicat
101101
* 设置自定义的{@link me.chanjar.weixin.common.session.WxSessionManager}
102102
* 如果不调用该方法,默认使用 {@link me.chanjar.weixin.common.session.StandardSessionManager}
103103
* </pre>
104+
*
104105
* @param sessionManager
105106
*/
106107
public void setSessionManager(WxSessionManager sessionManager) {
@@ -112,6 +113,7 @@ public void setSessionManager(WxSessionManager sessionManager) {
112113
* 设置自定义的{@link me.chanjar.weixin.common.api.WxErrorExceptionHandler}
113114
* 如果不调用该方法,默认使用 {@link me.chanjar.weixin.common.util.LogExceptionHandler}
114115
* </pre>
116+
*
115117
* @param exceptionHandler
116118
*/
117119
public void setExceptionHandler(WxErrorExceptionHandler exceptionHandler) {
@@ -131,6 +133,7 @@ public WxCpMessageRouterRule rule() {
131133

132134
/**
133135
* 处理微信消息
136+
*
134137
* @param wxMessage
135138
*/
136139
public WxCpXmlOutMessage route(final WxCpXmlMessage wxMessage) {
@@ -144,7 +147,7 @@ public WxCpXmlOutMessage route(final WxCpXmlMessage wxMessage) {
144147
for (final WxCpMessageRouterRule rule : rules) {
145148
if (rule.test(wxMessage)) {
146149
matchRules.add(rule);
147-
if(!rule.isReEnter()) {
150+
if (!rule.isReEnter()) {
148151
break;
149152
}
150153
}
@@ -158,13 +161,13 @@ public WxCpXmlOutMessage route(final WxCpXmlMessage wxMessage) {
158161
final List<Future> futures = new ArrayList<Future>();
159162
for (final WxCpMessageRouterRule rule : matchRules) {
160163
// 返回最后一个非异步的rule的执行结果
161-
if(rule.isAsync()) {
164+
if (rule.isAsync()) {
162165
futures.add(
163-
executorService.submit(new Runnable() {
164-
public void run() {
165-
rule.service(wxMessage, wxCpService, sessionManager, exceptionHandler);
166-
}
167-
})
166+
executorService.submit(new Runnable() {
167+
public void run() {
168+
rule.service(wxMessage, wxCpService, sessionManager, exceptionHandler);
169+
}
170+
})
168171
);
169172
} else {
170173
res = rule.service(wxMessage, wxCpService, sessionManager, exceptionHandler);
@@ -201,10 +204,10 @@ protected boolean isDuplicateMessage(WxCpXmlMessage wxMessage) {
201204
String messageId = "";
202205
if (wxMessage.getMsgId() == null) {
203206
messageId = String.valueOf(wxMessage.getCreateTime())
204-
+ "-" +String.valueOf(wxMessage.getAgentId() == null ? "" : wxMessage.getAgentId())
205-
+ "-" + wxMessage.getFromUserName()
206-
+ "-" + String.valueOf(wxMessage.getEventKey() == null ? "" : wxMessage.getEventKey())
207-
+ "-" + String.valueOf(wxMessage.getEvent() == null ? "" : wxMessage.getEvent())
207+
+ "-" + String.valueOf(wxMessage.getAgentId() == null ? "" : wxMessage.getAgentId())
208+
+ "-" + wxMessage.getFromUserName()
209+
+ "-" + String.valueOf(wxMessage.getEventKey() == null ? "" : wxMessage.getEventKey())
210+
+ "-" + String.valueOf(wxMessage.getEvent() == null ? "" : wxMessage.getEvent())
208211
;
209212
} else {
210213
messageId = String.valueOf(wxMessage.getMsgId());
@@ -216,11 +219,12 @@ protected boolean isDuplicateMessage(WxCpXmlMessage wxMessage) {
216219

217220
/**
218221
* 对session的访问结束
222+
*
219223
* @param wxMessage
220224
*/
221225
protected void sessionEndAccess(WxCpXmlMessage wxMessage) {
222226

223-
InternalSession session = ((InternalSessionManager)sessionManager).findSession(wxMessage.getFromUserName());
227+
InternalSession session = ((InternalSessionManager) sessionManager).findSession(wxMessage.getFromUserName());
224228
if (session != null) {
225229
session.endAccess();
226230
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpMessageRouterRule.java

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package me.chanjar.weixin.cp.api;
22

3+
import me.chanjar.weixin.common.api.WxErrorExceptionHandler;
34
import me.chanjar.weixin.common.exception.WxErrorException;
45
import me.chanjar.weixin.common.session.WxSessionManager;
5-
import me.chanjar.weixin.common.api.WxErrorExceptionHandler;
66
import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
77
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;
88

@@ -186,7 +186,6 @@ public WxCpMessageRouterRule handler(WxCpMessageHandler handler, WxCpMessageHand
186186

187187
/**
188188
* 规则结束,代表如果一个消息匹配该规则,那么它将不再会进入其他规则
189-
*
190189
*/
191190
public WxCpMessageRouter end() {
192191
this.routerBuilder.getRules().add(this);
@@ -195,7 +194,6 @@ public WxCpMessageRouter end() {
195194

196195
/**
197196
* 规则结束,但是消息还会进入其他规则
198-
*
199197
*/
200198
public WxCpMessageRouter next() {
201199
this.reEnter = true;
@@ -204,24 +202,24 @@ public WxCpMessageRouter next() {
204202

205203
protected boolean test(WxCpXmlMessage wxMessage) {
206204
return
207-
(this.fromUser == null || this.fromUser.equals(wxMessage.getFromUserName()))
208-
&&
209-
(this.agentId == null || this.agentId.equals(wxMessage.getAgentId()))
210-
&&
211-
(this.msgType == null || this.msgType.equals(wxMessage.getMsgType()))
212-
&&
213-
(this.event == null || this.event.equals(wxMessage.getEvent()))
214-
&&
215-
(this.eventKey == null || this.eventKey.equals(wxMessage.getEventKey()))
216-
&&
217-
(this.content == null || this.content
218-
.equals(wxMessage.getContent() == null ? null : wxMessage.getContent().trim()))
219-
&&
220-
(this.rContent == null || Pattern
221-
.matches(this.rContent, wxMessage.getContent() == null ? "" : wxMessage.getContent().trim()))
222-
&&
223-
(this.matcher == null || this.matcher.match(wxMessage))
224-
;
205+
(this.fromUser == null || this.fromUser.equals(wxMessage.getFromUserName()))
206+
&&
207+
(this.agentId == null || this.agentId.equals(wxMessage.getAgentId()))
208+
&&
209+
(this.msgType == null || this.msgType.equals(wxMessage.getMsgType()))
210+
&&
211+
(this.event == null || this.event.equals(wxMessage.getEvent()))
212+
&&
213+
(this.eventKey == null || this.eventKey.equals(wxMessage.getEventKey()))
214+
&&
215+
(this.content == null || this.content
216+
.equals(wxMessage.getContent() == null ? null : wxMessage.getContent().trim()))
217+
&&
218+
(this.rContent == null || Pattern
219+
.matches(this.rContent, wxMessage.getContent() == null ? "" : wxMessage.getContent().trim()))
220+
&&
221+
(this.matcher == null || this.matcher.match(wxMessage))
222+
;
225223
}
226224

227225
/**
@@ -231,9 +229,9 @@ protected boolean test(WxCpXmlMessage wxMessage) {
231229
* @return true 代表继续执行别的router,false 代表停止执行别的router
232230
*/
233231
protected WxCpXmlOutMessage service(WxCpXmlMessage wxMessage,
234-
WxCpService wxCpService,
235-
WxSessionManager sessionManager,
236-
WxErrorExceptionHandler exceptionHandler) {
232+
WxCpService wxCpService,
233+
WxSessionManager sessionManager,
234+
WxErrorExceptionHandler exceptionHandler) {
237235

238236
try {
239237

0 commit comments

Comments
 (0)