WxMessageUtil.java
11.0 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package org.theyeasy.weixin.util;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.theyeasy.weixin.model.BaseMessage;
import org.theyeasy.weixin.model.TextMessage;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.core.util.QuickWriter;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
import com.thoughtworks.xstream.io.xml.XppDriver;
public class WxMessageUtil {
/**
* 返回消息类型:文本
*/
public static final String RESP_MESSAGE_TYPE_TEXT = "text";
/**
* 返回消息类型:音乐
*/
public static final String RESP_MESSAGE_TYPE_MUSIC = "music";
/**
* 返回消息类型:图文
*/
public static final String RESP_MESSAGE_TYPE_NEWS = "news";
/**
* 返回消息类型:图片
*/
public static final String RESP_MESSAGE_TYPE_IMAGE = "image";
/**
* 返回消息类型:语音
*/
public static final String RESP_MESSAGE_TYPE_VOICE = "voice";
/**
* 返回消息类型:视频
*/
public static final String RESP_MESSAGE_TYPE_VIDEO = "video";
/**
* 请求消息类型:文本
*/
public static final String REQ_MESSAGE_TYPE_TEXT = "text";
/**
* 请求消息类型:图片
*/
public static final String REQ_MESSAGE_TYPE_IMAGE = "image";
/**
* 请求消息类型:链接
*/
public static final String REQ_MESSAGE_TYPE_LINK = "link";
/**
* 请求消息类型:地理位置
*/
public static final String REQ_MESSAGE_TYPE_LOCATION = "location";
/**
* 请求消息类型:音频
*/
public static final String REQ_MESSAGE_TYPE_VOICE = "voice";
/**
* 请求消息类型:视频
*/
public static final String REQ_MESSAGE_TYPE_VIDEO = "video";
/**
* 请求消息类型:推送
*/
public static final String REQ_MESSAGE_TYPE_EVENT = "event";
/**
* 请求消息类型:转发至多客服
*/
public static final String REQ_MESSAGE_TYPE_CUSTOMERSERVICE = "transfer_customer_service";
/**
* 事件类型:subscribe(订阅)
*/
public static final String EVENT_TYPE_SUBSCRIBE = "subscribe";
/**
* 事件类型:unsubscribe(取消订阅)
*/
public static final String EVENT_TYPE_UNSUBSCRIBE = "unsubscribe";
/**
* 事件类型:扫描二维码
*/
public static final String EVENT_TYPE_SCAN="SCAN";
/**
* 事件类型:CLICK(自定义菜单点击事件)
*/
public static final String EVENT_TYPE_CLICK = "click";
/**
* 事件类型:VIEW(自定义菜单点击事件)
*/
public static final String EVENT_TYPE_VIEW = "view";
/**
* 地理位置通知事件
*/
public static final String EVENT_TYPE_LOCATION = "LOCATION";
/**
* 礼券:审核事件推送
*/
public static final String EVENT_CARD_PASS_CHECK = "card_pass_check";
/**
* 礼券:领取事件推送
*/
public static final String EVENT_USER_GET_CARD = "user_get_card";
/**
* 礼券:删除事件推送
*/
public static final String EVENT_USER_DEL_CARD = "user_del_card";
/**
* 礼券:核销事件推送
*/
public static final String EVENT_USER_CONSUME_CARD = "user_consume_card";
/**
* 礼券:进入会员卡事件推送
*/
public static final String EVENT_USER_VIEW_CARD = "user_view_card";
/**
* 礼券:从卡券进入公众号会话事件推送
*/
public static final String EVENT_USER_ENTER_SESSION_FROM_CARD = "user_enter_session_from_card";
public static String readStrFromInputStream(HttpServletRequest request){
// 从request中取得输入流
StringBuffer sbf = new StringBuffer();
try {
InputStream inputStream = request.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));
char[] bufferChar = new char[1024];
int index = 0;
while((index=in.read(bufferChar))!=-1){
sbf.append(bufferChar,0,index);
}
inputStream.close();
inputStream =null;
return sbf.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 解析微信发来的请求(XML)
*
* @param request
* @return
* @throws Exception
*/
@SuppressWarnings("unchecked")
public static Map<String, String> parseXml(String resultXml) throws Exception {
// 将解析结果存储在HashMap中
Map<String, String> map = new HashMap<String, String>();
// 读取输入流
SAXReader reader = new SAXReader();
Document document = reader.read(new ByteArrayInputStream(resultXml.getBytes("UTF-8")));
// 得到xml根元素
Element root = document.getRootElement();
// 得到根元素的所有子节点
List<Element> elementList = root.elements();
// 遍历所有子节点
for (Element e : elementList){
// System.out.println("....name..."+e.getName());
map.put(e.getName(), e.getText());
}
return map;
}
/**
* 解析微信发来的请求(XML)
*
* @param request
* @return
* @throws Exception
*/
@SuppressWarnings("unchecked")
public static Map<String, String> parseXml(HttpServletRequest request) throws Exception {
// 将解析结果存储在HashMap中
Map<String, String> map = new HashMap<String, String>();
// 从request中取得输入流
InputStream inputStream = request.getInputStream();
// 读取输入流
SAXReader reader = new SAXReader();
Document document = reader.read(inputStream);
// 得到xml根元素
Element root = document.getRootElement();
// 得到根元素的所有子节点
List<Element> elementList = root.elements();
// 遍历所有子节点
for (Element e : elementList)
map.put(e.getName(), e.getText());
// 释放资源
inputStream.close();
inputStream = null;
return map;
}
/**
* 消息对象转换成xml
*
* @param 消息对象
* @return xml
*/
public static String messageToXml(Object obj) {
xstream.alias("xml", obj.getClass());
return xstream.toXML(obj);
}
/**
* 扩展xstream,使其支持CDATA块
*
* @date 2013-05-19
*/
private static XStream xstream = new XStream(new XppDriver() {
public HierarchicalStreamWriter createWriter(Writer out) {
return new PrettyPrintWriter(out) {
// 对所有xml节点的转换都增加CDATA标记
boolean cdata = true;
public void startNode(String name, @SuppressWarnings("rawtypes") Class clazz) {
super.startNode(name, clazz);
}
protected void writeText(QuickWriter writer, String text) {
if (cdata) {
writer.write("<![CDATA[");
writer.write(text);
writer.write("]]>");
} else {
writer.write(text);
}
}
};
}
});
public static String textMessageToXml(TextMessage textMessage)
{
xstream.alias("xml", textMessage.getClass());
return xstream.toXML(textMessage);
}
// //音乐消息对象转换成xml
// public static String musicMessageToXml(MusicMessage musicMessage) {
// xstream.alias("xml", musicMessage.getClass());
// return xstream.toXML(musicMessage);
// }
// // 图文消息对象转换成xml
// public static String newsMessageToXml(NewsMessage newsMessage) {
// xstream.alias("xml", newsMessage.getClass());
// xstream.alias("item", new Article().getClass());
// return xstream.toXML(newsMessage);
// }
/**
* 判断是否是QQ表情
* @param content
* @return
*/
public static boolean isQqFace(String content) {
boolean result = false;
// 判断QQ表情的正则表达式
String qqfaceRegex = "/::\\)|/::~|/::B|/::\\||/:8-\\)|/::<|/::$|/::X|/::Z|/::'\\(|/::-\\||/::@|/::P|/::D|/::O|/::\\(|/::\\+|/:--b|/::Q|/::T|/:,@P|/:,@-D|/::d|/:,@o|/::g|/:\\|-\\)|/::!|/::L|/::>|/::,@|/:,@f|/::-S|/:\\?|/:,@x|/:,@@|/::8|/:,@!|/:!!!|/:xx|/:bye|/:wipe|/:dig|/:handclap|/:&-\\(|/:B-\\)|/:<@|/:@>|/::-O|/:>-\\||/:P-\\(|/::'\\||/:X-\\)|/::\\*|/:@x|/:8\\*|/:pd|/:<W>|/:beer|/:basketb|/:oo|/:coffee|/:eat|/:pig|/:rose|/:fade|/:showlove|/:heart|/:break|/:cake|/:li|/:bome|/:kn|/:footb|/:ladybug|/:shit|/:moon|/:sun|/:gift|/:hug|/:strong|/:weak|/:share|/:v|/:@\\)|/:jj|/:@@|/:bad|/:lvu|/:no|/:ok|/:love|/:<L>|/:jump|/:shake|/:<O>|/:circle|/:kotow|/:turn|/:skip|/:oY|/:#-0|/:hiphot|/:kiss|/:<&|/:&>";
Pattern p = Pattern.compile(qqfaceRegex);
Matcher m = p.matcher(content);
if (m.matches()) {
result = true;
}
return result;
}
/**
* 计算采用utf-8编码方式时字符串所占字节数
* @param content
* @return
*/
public static int getByteSize(String content) {
int size = 0;
if (null != content) {
try {
// 汉字采用utf-8编码时占3个字节
size = content.getBytes("utf-8").length;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return size;
}
public static void main(String[] arg)
{
try {
String response="<xml><return_code><![CDATA[SUCCESS]]></return_code> <return_msg><![CDATA[]]></return_msg><mch_appid><![CDATA[wxec38b8ff840bd989]]></mch_appid> <mchid><![CDATA[10013274]]></mchid><device_info><![CDATA[]]></device_info> <nonce_str><![CDATA[lxuDzMnRjpcXzxLx0q]]></nonce_str> <result_code><![CDATA[SUCCESS]]></result_code> <partner_trade_no><![CDATA[10013574201505191526582441]]></partner_trade_no> <payment_no><![CDATA[1000018301201505190181489473]]></payment_no> <payment_time><![CDATA[2015-05-19 15:26:59]]></payment_time> </xml>";
Map<String, String> requestMap = WxMessageUtil.parseXml(response);
String code = requestMap.get("return_code");
System.out.println(code);
} catch (Exception e) {
e.printStackTrace();
}
}
}