Skip to content

Commit 939bb14

Browse files
committed
fix(channel): 修复客服上传重试与日志泄露
1 parent 094a114 commit 939bb14

5 files changed

Lines changed: 59 additions & 79 deletions

File tree

.superpowers/sdd/task-1-report.md

Lines changed: 0 additions & 22 deletions
This file was deleted.

.superpowers/sdd/task-2-report.md

Lines changed: 0 additions & 55 deletions
This file was deleted.

weixin-java-channel/src/main/java/me/chanjar/weixin/channel/api/impl/WxChannelKfServiceImpl.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
import me.chanjar.weixin.channel.bean.kf.WxChannelKfCosUploadResponse;
88
import me.chanjar.weixin.channel.bean.kf.WxChannelKfSendMsgParam;
99
import me.chanjar.weixin.channel.bean.kf.WxChannelKfSendMsgResponse;
10+
import me.chanjar.weixin.channel.util.JsonUtils;
1011
import me.chanjar.weixin.channel.util.ResponseUtils;
1112
import me.chanjar.weixin.common.bean.CommonUploadParam;
1213
import me.chanjar.weixin.common.error.WxErrorException;
14+
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
1315

1416
/** 视频号小店商家客服服务实现。 */
1517
public class WxChannelKfServiceImpl implements WxChannelKfService {
@@ -36,7 +38,8 @@ public String uploadMedia(String openId, String msgType, String fileName, byte[]
3638

3739
@Override
3840
public WxChannelKfSendMsgResponse sendMessage(WxChannelKfSendMsgParam param) throws WxErrorException {
39-
String responseJson = channelService.post(SEND_MSG_URL, param);
41+
String responseJson = channelService.executeWithoutLog(SimplePostRequestExecutor.create(channelService), SEND_MSG_URL,
42+
JsonUtils.encode(param));
4043
return ResponseUtils.decode(responseJson, WxChannelKfSendMsgResponse.class);
4144
}
4245
}

weixin-java-channel/src/test/java/me/chanjar/weixin/channel/api/impl/WxChannelKfServiceImplTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44
import static org.testng.Assert.assertNotNull;
55
import static org.testng.Assert.assertSame;
66

7+
import java.io.ByteArrayOutputStream;
8+
import java.io.IOException;
9+
import java.io.InputStream;
710
import me.chanjar.weixin.channel.bean.kf.WxChannelKfSendMsgParam;
811
import me.chanjar.weixin.channel.bean.kf.WxChannelKfSendMsgResponse;
912
import me.chanjar.weixin.channel.util.JsonUtils;
1013
import me.chanjar.weixin.common.bean.CommonUploadParam;
1114
import me.chanjar.weixin.common.error.WxErrorException;
15+
import me.chanjar.weixin.common.util.http.RequestExecutor;
1216
import org.testng.annotations.Test;
1317

1418
/** 商家客服服务离线测试。 */
@@ -49,10 +53,19 @@ public void shouldSendJsonMessageAndDecodeResponse() throws WxErrorException {
4953
assertEquals(channelService.postUrl, "https://api.weixin.qq.com/channels/ec/commkf/sendmsg");
5054
assertEquals(channelService.postJson,
5155
"{\"request_id\":\"request-id\",\"open_id\":\"open-id\",\"msg_type\":\"text\",\"text\":{\"content\":\"hello\"}}");
56+
assertEquals(channelService.executeWithoutLogCalled, true);
5257
assertEquals(response.getMsgId(), "message-id");
5358
assertEquals(response.getErrCode(), 0);
5459
}
5560

61+
@Test
62+
public void shouldProvideFreshUploadStreamForEachAttempt() throws IOException {
63+
CommonUploadParam uploadParam = CommonUploadParam.fromBytes("file", "image.png", new byte[]{1, 2, 3});
64+
65+
assertEquals(readAllBytes(uploadParam.getData().getInputStream()), new byte[]{1, 2, 3});
66+
assertEquals(readAllBytes(uploadParam.getData().getInputStream()), new byte[]{1, 2, 3});
67+
}
68+
5669
@Test
5770
public void shouldCacheKfServiceEntryPoint() {
5871
WxChannelServiceImpl channelService = new WxChannelServiceImpl();
@@ -68,6 +81,7 @@ private static class RecordingChannelService extends WxChannelServiceImpl {
6881
private CommonUploadParam uploadParam;
6982
private String postUrl;
7083
private String postJson;
84+
private boolean executeWithoutLogCalled;
7185

7286
@Override
7387
public String upload(String url, CommonUploadParam param) {
@@ -82,5 +96,25 @@ public String post(String url, Object obj) {
8296
this.postJson = JsonUtils.encode(obj);
8397
return postResult;
8498
}
99+
100+
@Override
101+
@SuppressWarnings("unchecked")
102+
public <T, E> T executeWithoutLog(RequestExecutor<T, E> executor, String uri, E data) {
103+
this.executeWithoutLogCalled = true;
104+
this.postUrl = uri;
105+
this.postJson = (String) data;
106+
return (T) postResult;
107+
}
108+
}
109+
110+
private byte[] readAllBytes(InputStream inputStream) throws IOException {
111+
try (InputStream stream = inputStream; ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
112+
byte[] buffer = new byte[16];
113+
int count;
114+
while ((count = stream.read(buffer)) != -1) {
115+
outputStream.write(buffer, 0, count);
116+
}
117+
return outputStream.toByteArray();
118+
}
85119
}
86120
}

weixin-java-common/src/main/java/me/chanjar/weixin/common/bean/CommonUploadParam.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,27 @@ public static CommonUploadParam fromFile(String name, File file) {
7878
*/
7979
@SneakyThrows
8080
public static CommonUploadParam fromBytes(String name, @Nullable String fileName, byte[] bytes) {
81-
return new CommonUploadParam(name, new CommonUploadData(fileName, new ByteArrayInputStream(bytes), bytes.length), null);
81+
return new CommonUploadParam(name, new ByteArrayUploadData(fileName, bytes), null);
82+
}
83+
84+
private static class ByteArrayUploadData extends CommonUploadData {
85+
86+
private final byte[] bytes;
87+
88+
private ByteArrayUploadData(@Nullable String fileName, byte[] bytes) {
89+
super(fileName, new ByteArrayInputStream(bytes), bytes.length);
90+
this.bytes = bytes;
91+
}
92+
93+
@Override
94+
public ByteArrayInputStream getInputStream() {
95+
return new ByteArrayInputStream(bytes);
96+
}
97+
98+
@Override
99+
public byte[] readAllBytes() {
100+
return bytes.clone();
101+
}
82102
}
83103

84104
/**

0 commit comments

Comments
 (0)