Skip to content

Commit 89aefa4

Browse files
committed
refactor
1 parent d4c25ce commit 89aefa4

18 files changed

Lines changed: 201 additions & 107 deletions

File tree

‎pom.xml‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,6 @@
7070
<artifactId>gson</artifactId>
7171
<version>2.2.2</version>
7272
</dependency>
73-
<dependency>
74-
<groupId>org.apache.commons</groupId>
75-
<artifactId>commons-lang3</artifactId>
76-
<version>3.1</version>
77-
</dependency>
7873
<dependency>
7974
<groupId>commons-codec</groupId>
8075
<artifactId>commons-codec</artifactId>
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package me.chanjar.weixin.common.util;
2+
3+
/**
4+
* copy from apache-commons-lang3
5+
*/
6+
public class StringUtils {
7+
8+
/**
9+
* <p>Checks if a CharSequence is whitespace, empty ("") or null.</p>
10+
*
11+
* <pre>
12+
* StringUtils.isBlank(null) = true
13+
* StringUtils.isBlank("") = true
14+
* StringUtils.isBlank(" ") = true
15+
* StringUtils.isBlank("bob") = false
16+
* StringUtils.isBlank(" bob ") = false
17+
* </pre>
18+
*
19+
* @param cs the CharSequence to check, may be null
20+
* @return {@code true} if the CharSequence is null, empty or whitespace
21+
* @since 2.0
22+
* @since 3.0 Changed signature from isBlank(String) to isBlank(CharSequence)
23+
*/
24+
public static boolean isBlank(CharSequence cs) {
25+
int strLen;
26+
if (cs == null || (strLen = cs.length()) == 0) {
27+
return true;
28+
}
29+
for (int i = 0; i < strLen; i++) {
30+
if (Character.isWhitespace(cs.charAt(i)) == false) {
31+
return false;
32+
}
33+
}
34+
return true;
35+
}
36+
37+
/**
38+
* <p>Checks if a CharSequence is not empty (""), not null and not whitespace only.</p>
39+
*
40+
* <pre>
41+
* StringUtils.isNotBlank(null) = false
42+
* StringUtils.isNotBlank("") = false
43+
* StringUtils.isNotBlank(" ") = false
44+
* StringUtils.isNotBlank("bob") = true
45+
* StringUtils.isNotBlank(" bob ") = true
46+
* </pre>
47+
*
48+
* @param cs the CharSequence to check, may be null
49+
* @return {@code true} if the CharSequence is
50+
* not empty and not null and not whitespace
51+
* @since 2.0
52+
* @since 3.0 Changed signature from isNotBlank(String) to isNotBlank(CharSequence)
53+
*/
54+
public static boolean isNotBlank(CharSequence cs) {
55+
return !StringUtils.isBlank(cs);
56+
}
57+
58+
/**
59+
* <p>Checks if a CharSequence is empty ("") or null.</p>
60+
*
61+
* <pre>
62+
* StringUtils.isEmpty(null) = true
63+
* StringUtils.isEmpty("") = true
64+
* StringUtils.isEmpty(" ") = false
65+
* StringUtils.isEmpty("bob") = false
66+
* StringUtils.isEmpty(" bob ") = false
67+
* </pre>
68+
*
69+
* <p>NOTE: This method changed in Lang version 2.0.
70+
* It no longer trims the CharSequence.
71+
* That functionality is available in isBlank().</p>
72+
*
73+
* @param cs the CharSequence to check, may be null
74+
* @return {@code true} if the CharSequence is empty or null
75+
* @since 3.0 Changed signature from isEmpty(String) to isEmpty(CharSequence)
76+
*/
77+
public static boolean isEmpty(CharSequence cs) {
78+
return cs == null || cs.length() == 0;
79+
}
80+
81+
/**
82+
* <p>Checks if a CharSequence is not empty ("") and not null.</p>
83+
*
84+
* <pre>
85+
* StringUtils.isNotEmpty(null) = false
86+
* StringUtils.isNotEmpty("") = false
87+
* StringUtils.isNotEmpty(" ") = true
88+
* StringUtils.isNotEmpty("bob") = true
89+
* StringUtils.isNotEmpty(" bob ") = true
90+
* </pre>
91+
*
92+
* @param cs the CharSequence to check, may be null
93+
* @return {@code true} if the CharSequence is not empty and not null
94+
* @since 3.0 Changed signature from isNotEmpty(String) to isNotEmpty(CharSequence)
95+
*/
96+
public static boolean isNotEmpty(CharSequence cs) {
97+
return !StringUtils.isEmpty(cs);
98+
}
99+
100+
}

‎weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/MediaDownloadRequestExecutor.java‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
package me.chanjar.weixin.common.util.http;
22

33
import me.chanjar.weixin.common.bean.result.WxError;
4-
import me.chanjar.weixin.common.util.fs.FileUtils;
54
import me.chanjar.weixin.common.exception.WxErrorException;
6-
import me.chanjar.weixin.common.util.http.InputStreamResponseHandler;
7-
import me.chanjar.weixin.common.util.http.RequestExecutor;
8-
import me.chanjar.weixin.common.util.http.Utf8ResponseHandler;
9-
import org.apache.commons.lang3.StringUtils;
5+
import me.chanjar.weixin.common.util.StringUtils;
6+
import me.chanjar.weixin.common.util.fs.FileUtils;
107
import org.apache.http.Header;
118
import org.apache.http.HttpHost;
129
import org.apache.http.client.ClientProtocolException;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ public interface WxCpMessageHandler {
1616
*
1717
* @param wxMessage
1818
* @param context 上下文,如果handler或interceptor之间有信息要传递,可以用这个
19+
* @param wxCpService
1920
* @return xml格式的消息,如果在异步规则里处理的话,可以返回null
2021
*/
21-
public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context);
22+
public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context, WxCpService wxCpService);
2223

2324
}

‎weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpMessageInterceptor.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ public interface WxCpMessageInterceptor {
1515
* 拦截微信消息
1616
* @param wxMessage
1717
* @param context 上下文,如果handler或interceptor之间有信息要传递,可以用这个
18+
* @param wxCpService
1819
* @return true代表OK,false代表不OK
1920
*/
20-
public boolean intercept(WxCpXmlMessage wxMessage, Map<String, Object> context);
21+
public boolean intercept(WxCpXmlMessage wxMessage, Map<String, Object> context, WxCpService wxCpService);
2122

2223
}

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,18 @@ public class WxCpMessageRouter {
4545

4646
private final ExecutorService es = Executors.newCachedThreadPool();
4747

48+
private final WxCpService wxCpService;
49+
50+
public WxCpMessageRouter(WxCpService wxCpService) {
51+
this.wxCpService = wxCpService;
52+
}
53+
4854
/**
4955
* 开始一个新的Route规则
5056
* @return
5157
*/
5258
public Rule rule() {
53-
return new Rule(this);
59+
return new Rule(this, wxCpService);
5460
}
5561

5662
/**
@@ -101,6 +107,8 @@ public static class Rule {
101107

102108
private final WxCpMessageRouter routerBuilder;
103109

110+
private final WxCpService wxCpService;
111+
104112
private boolean async = true;
105113

106114
private String msgType;
@@ -121,8 +129,9 @@ public static class Rule {
121129

122130
private List<WxCpMessageInterceptor> interceptors = new ArrayList<WxCpMessageInterceptor>();
123131

124-
protected Rule(WxCpMessageRouter routerBuilder) {
132+
protected Rule(WxCpMessageRouter routerBuilder, WxCpService wxCpService) {
125133
this.routerBuilder = routerBuilder;
134+
this.wxCpService = wxCpService;
126135
}
127136

128137
/**
@@ -288,7 +297,7 @@ protected WxCpXmlOutMessage service(WxCpXmlMessage wxMessage) {
288297
Map<String, Object> context = new HashMap<String, Object>();
289298
// 如果拦截器不通过
290299
for (WxCpMessageInterceptor interceptor : this.interceptors) {
291-
if (!interceptor.intercept(wxMessage, context)) {
300+
if (!interceptor.intercept(wxMessage, context, wxCpService)) {
292301
return null;
293302
}
294303
}
@@ -297,7 +306,7 @@ protected WxCpXmlOutMessage service(WxCpXmlMessage wxMessage) {
297306
WxCpXmlOutMessage res = null;
298307
for (WxCpMessageHandler handler : this.handlers) {
299308
// 返回最后handler的结果
300-
res = handler.handle(wxMessage, context);
309+
res = handler.handle(wxMessage, context, wxCpService);
301310
}
302311
return res;
303312
}

‎weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpServiceImpl.java‎

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

3-
import java.io.File;
4-
import java.io.IOException;
5-
import java.io.InputStream;
6-
import java.io.StringReader;
7-
import java.util.List;
8-
import java.util.UUID;
9-
import java.util.concurrent.atomic.AtomicBoolean;
10-
113
import com.google.gson.JsonArray;
4+
import com.google.gson.JsonElement;
125
import com.google.gson.JsonObject;
136
import com.google.gson.JsonPrimitive;
7+
import com.google.gson.internal.Streams;
8+
import com.google.gson.reflect.TypeToken;
9+
import com.google.gson.stream.JsonReader;
1410
import me.chanjar.weixin.common.bean.WxAccessToken;
1511
import me.chanjar.weixin.common.bean.WxMenu;
12+
import me.chanjar.weixin.common.bean.result.WxError;
1613
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
17-
import me.chanjar.weixin.common.util.json.GsonHelper;
18-
import me.chanjar.weixin.cp.bean.*;
19-
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
14+
import me.chanjar.weixin.common.exception.WxErrorException;
15+
import me.chanjar.weixin.common.util.StringUtils;
2016
import me.chanjar.weixin.common.util.crypto.SHA1;
17+
import me.chanjar.weixin.common.util.fs.FileUtils;
18+
import me.chanjar.weixin.common.util.http.*;
19+
import me.chanjar.weixin.common.util.json.GsonHelper;
20+
import me.chanjar.weixin.cp.bean.WxCpDepart;
21+
import me.chanjar.weixin.cp.bean.WxCpMessage;
22+
import me.chanjar.weixin.cp.bean.WxCpTag;
23+
import me.chanjar.weixin.cp.bean.WxCpUser;
2124
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
22-
import org.apache.commons.lang3.StringUtils;
2325
import org.apache.http.HttpHost;
2426
import org.apache.http.auth.AuthScope;
2527
import org.apache.http.auth.UsernamePasswordCredentials;
@@ -33,20 +35,13 @@
3335
import org.apache.http.impl.client.CloseableHttpClient;
3436
import org.apache.http.impl.client.HttpClients;
3537

36-
import me.chanjar.weixin.cp.bean.WxCpDepart;
37-
import me.chanjar.weixin.common.bean.result.WxError;
38-
import me.chanjar.weixin.cp.bean.WxCpUser;
39-
import me.chanjar.weixin.common.exception.WxErrorException;
40-
import me.chanjar.weixin.common.util.fs.FileUtils;
41-
import me.chanjar.weixin.common.util.http.MediaDownloadRequestExecutor;
42-
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
43-
import me.chanjar.weixin.common.util.http.RequestExecutor;
44-
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
45-
46-
import com.google.gson.JsonElement;
47-
import com.google.gson.internal.Streams;
48-
import com.google.gson.reflect.TypeToken;
49-
import com.google.gson.stream.JsonReader;
38+
import java.io.File;
39+
import java.io.IOException;
40+
import java.io.InputStream;
41+
import java.io.StringReader;
42+
import java.util.List;
43+
import java.util.UUID;
44+
import java.util.concurrent.atomic.AtomicBoolean;
5045

5146
public class WxCpServiceImpl implements WxCpService {
5247

‎weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/json/WxCpMessageGsonAdapter.java‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,12 @@
88
*/
99
package me.chanjar.weixin.cp.util.json;
1010

11-
import java.lang.reflect.Type;
12-
11+
import com.google.gson.*;
1312
import me.chanjar.weixin.common.api.WxConsts;
13+
import me.chanjar.weixin.common.util.StringUtils;
1414
import me.chanjar.weixin.cp.bean.WxCpMessage;
1515

16-
import com.google.gson.JsonArray;
17-
import com.google.gson.JsonElement;
18-
import com.google.gson.JsonObject;
19-
import com.google.gson.JsonSerializationContext;
20-
import com.google.gson.JsonSerializer;
21-
import org.apache.commons.lang3.StringUtils;
16+
import java.lang.reflect.Type;
2217

2318
/**
2419
*

‎weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxCpBaseAPITest.java‎

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

3-
import org.apache.commons.lang3.StringUtils;
3+
import me.chanjar.weixin.common.util.StringUtils;
44
import org.testng.Assert;
55
import org.testng.annotations.Guice;
66
import org.testng.annotations.Test;

‎weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxCpMessageRouterTest.java‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public void prepare(boolean async, StringBuffer sb, WxCpMessageRouter router) {
4747
@Test(dataProvider="messages-1")
4848
public void testSync(WxCpXmlMessage message, String expected) {
4949
StringBuffer sb = new StringBuffer();
50-
WxCpMessageRouter router = new WxCpMessageRouter();
50+
WxCpMessageRouter router = new WxCpMessageRouter(null);
5151
prepare(false, sb, router);
5252
router.route(message);
5353
Assert.assertEquals(sb.toString(), expected);
@@ -56,18 +56,18 @@ public void testSync(WxCpXmlMessage message, String expected) {
5656
@Test(dataProvider="messages-1")
5757
public void testAsync(WxCpXmlMessage message, String expected) throws InterruptedException {
5858
StringBuffer sb = new StringBuffer();
59-
WxCpMessageRouter router = new WxCpMessageRouter();
59+
WxCpMessageRouter router = new WxCpMessageRouter(null);
6060
prepare(true, sb, router);
6161
router.route(message);
6262
Thread.sleep(500l);
6363
Assert.assertEquals(sb.toString(), expected);
6464
}
6565

6666
public void testConcurrency() throws InterruptedException {
67-
final WxCpMessageRouter router = new WxCpMessageRouter();
67+
final WxCpMessageRouter router = new WxCpMessageRouter(null);
6868
router.rule().handler(new WxCpMessageHandler() {
6969
@Override
70-
public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context) {
70+
public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context, WxCpService wxCpService) {
7171
return null;
7272
}
7373
}).end();
@@ -149,7 +149,7 @@ public WxEchoCpMessageHandler(StringBuffer sb, String echoStr) {
149149
}
150150

151151
@Override
152-
public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context) {
152+
public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context, WxCpService wxCpService) {
153153
sb.append(this.echoStr).append(',');
154154
return null;
155155
}

0 commit comments

Comments
 (0)