This repository was archived by the owner on Apr 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathWeixinAbstractHandler.java
More file actions
202 lines (165 loc) · 4.57 KB
/
WeixinAbstractHandler.java
File metadata and controls
202 lines (165 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package com.github.x0001.weixin;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import com.github.x0001.weixin.vo.recv.*;
import com.github.x0001.weixin.vo.send.WxSendMsg;
import com.github.x0001.weixin.vo.send.WxSendMusicMsg;
import com.github.x0001.weixin.vo.send.WxSendNewsMsg;
import com.github.x0001.weixin.vo.send.WxSendTextMsg;
import org.jdom.JDOMException;
/**
* 非线程安全 (应该每个请求创建一个)
* Created by 0x0001 on 14-9-28.
*/
public abstract class WeixinAbstractHandler {
private final HttpServletRequest req;
private final HttpServletResponse res;
private final String TOKEN;
private WxRecvMsg recvMsg;
public WeixinAbstractHandler(HttpServletRequest req, HttpServletResponse res, String token) {
this.req = req;
this.res = res;
this.TOKEN = token;
}
public void handler() {
if (!isFromWeiXin()) {
onNotFromWeixin();
return;
}
if (isWeixinVerifyURL()) {
renderText(getPara("echostr"));
return;
}
try {
WxRecvMsg msg = WeiXin.recv(req.getInputStream());
handler(msg);
} catch (Exception e) {
e.printStackTrace();
}
}
// ------------------------------------------------------------------- protected Methods
protected void sendText(String content) {
WxSendMsg send = WeiXin.builderSendByRecv(this.recvMsg);
send(new WxSendTextMsg(send, content));
}
protected void sendNewsMsg(WxSendNewsMsg wxSendNewsMsg) {
send(wxSendNewsMsg);
}
protected void sendMusicMsg(String title, String desc, String music, String hqMusic) {
WxSendMsg send = WeiXin.builderSendByRecv(this.recvMsg);
send(new WxSendMusicMsg(send, title, desc, music, hqMusic));
}
protected WxSendNewsMsg buildNewsMsg() {
return new WxSendNewsMsg(WeiXin.builderSendByRecv(this.recvMsg));
}
protected void send(WxSendMsg send) {
try {
WeiXin.send(send, res.getOutputStream());
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
protected boolean isFromWeiXin() {
if (verifySignature()) {
return isWeixinVerifyURL() || isPost();
}
return false;
}
// ------------------------------------------------------------------- Abstract Methods
/**
* 收到图片消息
* @param msg
*/
protected abstract void onPic(WxRecvPicMsg msg);
/**
* 收到链接消息
* @param msg
*/
protected abstract void onLink(WxRecvLinkMsg msg);
/**
* 收到地址消息
* @param msg
*/
protected abstract void onGeo(WxRecvGeoMsg msg);
/**
* 收到文本消息
* @param msg
*/
protected abstract void onText(WxRecvTextMsg msg);
/**
* 收到事件消息
* @param msg
*/
protected abstract void onEvent(WxRecvEventMsg msg);
/**
* 语音消息
* @param msg
*/
protected abstract void onVoice(WxRecvVoiceMsg msg);
/**
* 视频消息
* @param msg
*/
protected abstract void onVideo(WxRecvVideoMsg msg);
/**
* 请求可能不是来自微信
*/
protected abstract void onNotFromWeixin();
// -------------------------------------------------------- Get Set
public HttpServletRequest getRequest() {
return req;
}
public HttpServletResponse getResponse() {
return res;
}
// -------------------------------------------------------- Private Methods
private boolean isPost() {
return "POST".equalsIgnoreCase(req.getMethod());
}
private boolean isWeixinVerifyURL() {
return getPara("echostr") != null;
}
private boolean verifySignature() {
String signature = getPara("signature");
String timestamp = getPara("timestamp");
String nonce = getPara("nonce");
if (null != timestamp && null != nonce && null != signature) {
return WeiXin.access(TOKEN, signature, timestamp, nonce);
}
return false;
}
private void handler(WxRecvMsg msg) {
this.recvMsg = msg;
if (msg instanceof WxRecvTextMsg) {
onText((WxRecvTextMsg) msg);
} else if (msg instanceof WxRecvEventMsg) {
onEvent((WxRecvEventMsg) msg);
} else if (msg instanceof WxRecvGeoMsg) {
onGeo((WxRecvGeoMsg) msg);
} else if (msg instanceof WxRecvLinkMsg) {
onLink((WxRecvLinkMsg) msg);
} else if (msg instanceof WxRecvPicMsg) {
onPic((WxRecvPicMsg) msg);
} else if (msg instanceof WxRecvVoiceMsg) {
onVoice((WxRecvVoiceMsg) msg);
} else if (msg instanceof WxRecvVideoMsg) {
onVideo((WxRecvVideoMsg) msg);
}
}
private String getPara(String key) {
return req.getParameter(key);
}
private void renderText(String text) {
try {
PrintWriter pw = res.getWriter();
pw.write(text);
pw.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}