package com.xiaojiu.wechat.util;
import java.io.InputStream;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
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;
import com.xiaojiu.wechat.message.res.Article;
import com.xiaojiu.wechat.message.res.ImageMessage;
import com.xiaojiu.wechat.message.res.MusicMessage;
import com.xiaojiu.wechat.message.res.NewsMessage;
import com.xiaojiu.wechat.message.res.TextMessage;
import com.xiaojiu.wechat.message.res.VideoMessage;
import com.xiaojiu.wechat.message.res.VoiceMessage;
/**
* ClassName: MessageUtil
* @Description: 消息工具类
* @author XiaoJiu
* @date 2017-10-17
*/
public class MessageUtil {
/**
* 返回消息类型:文本
*/
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";
/**
* 事件类型:subscribe(订阅)
*/
public static final String EVENT_TYPE_SUBSCRIBE = "subscribe";
/**
* 事件类型:unsubscribe(取消订阅)
*/
public static final String EVENT_TYPE_UNSUBSCRIBE = "unsubscribe";
/**
* 事件类型:CLICK(自定义菜单点击事件)
*/
public static final String EVENT_TYPE_CLICK = "CLICK";
/**
* 事件类型:VIEW(自定义菜单 URl 视图)
*/
public static final String EVENT_TYPE_VIEW = "VIEW";
/**
* 事件类型:LOCATION(上报地理位置事件)
*/
public static final String EVENT_TYPE_LOCATION = "LOCATION";
/**
* 事件类型:LOCATION(上报地理位置事件)
*/
public static final String EVENT_TYPE_SCAN = "SCAN";
/**
* @Description: 解析微信发来的请求(XML)
* @param @param request
* @param @throws Exception
* @return Map<String,String>
* @author XiaoJiu
* @date 2017-10-18
*/
@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;
}
/**
* @Description: 文本消息对象转换成 xml
* @param @param textMessage
* @return String
* @author XiaoJiu
* @date 2017-10-18
*/
public static String textMessageToXml(TextMessage textMessage) {
xstream.alias("xml", textMessage.getClass());
return xstream.toXML(textMessage);
}
/**
*
* @Description: 图文消息对象转换成 xml
* @param @param newsMessage
* @param @return
* @return String
* @author XiaoJiu
* @date 2017-10-18
*/
public static String newsMessageToXml(NewsMessage newsMessage) {
xstream.alias("xml", newsMessage.getClass());
xstream.alias("item", new Article().getClass());
return xstream.toXML(newsMessage);
}
/**
* @Description: 图片消息对象转换成 xml
* @param @param imageMessage
* @param @return
* @return String
* @author XiaoJiu
* @date 2017-10-18
*/
public static String imageMessageToXml(ImageMessage imageMessage) {
xstream.alias("xml", imageMessage.getClass());
return xstream.toXML(imageMessage);
}
/**
* @Description: 语音消息对象转换成 xml
* @param @param voiceMessage
* @param @return
* @return String
* @author XiaoJiu
* @date 2017-10-18
*/
public static String voiceMessageToXml(VoiceMessage voiceMessage) {
xstream.alias("xml", voiceMessage.getClass());
return xstream.toXML(voiceMessage);
}
/**
* @Description: 视频消息对象转换成 xml
* @param @param videoMessage
* @param @return
* @return String
* @author XiaoJiu
* @date 2017-10-18
*/
public static String videoMessageToXml(VideoMessage videoMessage) {
xstream.alias("xml", videoMessage.getClass());
return xstream.toXML(videoMessage);
}
/**
* @Description: 音乐消息对象转换成 xml
* @param @param musicMessage
* @param @return
* @return String
* @author XiaoJiu
* @date 2017-10-18
*/
public static String musicMessageToXml(MusicMessage musicMessage) {
xstream.alias("xml", musicMessage.getClass());
return xstream.toXML(musicMessage);
}
/**
* 对象到 xml的处理
*/
private static XStream xstream = new XStream(new XppDriver() {
public HierarchicalStreamWriter createWriter(Writer out) {
return new PrettyPrintWriter(out) {
// 对所有 xml节点的转换都增加 CDATA 标记
boolean cdata = true;
@SuppressWarnings("rawtypes")
public void startNode(String name, 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);
}
}
};
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
wechat.zip (84个子文件)
.project 2KB
.mymetadata 299B
src
main
webapp
WEB-INF
view
index.jsp 52B
classes
com
xiaojiu
wechat
service
controller
WechatController.class 3KB
util
SignUtil.class 2KB
MessageUtil$1$1.class 1KB
MessageUtil$1.class 754B
MessageUtil.class 5KB
message
req
ImageMessage.class 614B
VoiceMessage.class 815B
TextMessage.class 615B
BaseMessage.class 1KB
LocationMessage.class 1KB
LinkMessage.class 1009B
VideoMessage.class 839B
res
ImageMessage.class 670B
VoiceMessage.class 670B
TextMessage.class 615B
Voice.class 571B
MusicMessage.class 670B
Music.class 1KB
BaseMessage.class 1KB
NewsMessage.class 1KB
Image.class 571B
Video.class 981B
Article.class 1KB
VideoMessage.class 670B
dispatcher
EventDispatcher.class 1KB
MsgDispatcher.class 2KB
mapping
lib
dom4j-1.6.1.jar 307KB
xstream-1.4.10.jar 576KB
xmlpull-1.1.3.1.jar 7KB
web.xml 3KB
META-INF
MANIFEST.MF 39B
resources
config.properties 184B
spring-mybatis.xml 5KB
spring-mvc.xml 1KB
log4j.properties 797B
web.xml 3KB
spring.xml 696B
java
com
xiaojiu
wechat
service
controller
WechatController.java 3KB
util
MessageUtil.java 8KB
SignUtil.java 3KB
message
req
LinkMessage.java 704B
VideoMessage.java 718B
BaseMessage.java 1KB
LocationMessage.java 964B
VoiceMessage.java 548B
TextMessage.java 380B
ImageMessage.java 371B
res
Voice.java 332B
NewsMessage.java 714B
Image.java 333B
VideoMessage.java 340B
Video.java 769B
BaseMessage.java 1KB
Music.java 1KB
VoiceMessage.java 230B
MusicMessage.java 360B
Article.java 972B
TextMessage.java 405B
ImageMessage.java 340B
dispatcher
EventDispatcher.java 1KB
MsgDispatcher.java 2KB
mapping
target
m2e-jee
web-resources
META-INF
MANIFEST.MF 103B
maven
wechat.xiaojiu.com
wechat
pom.properties 219B
pom.xml 3KB
classes
config.properties 184B
spring-mybatis.xml 5KB
spring-mvc.xml 1KB
log4j.properties 797B
web.xml 3KB
spring.xml 696B
test-classes
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 252B
org.eclipse.m2e.core.prefs 90B
org.eclipse.jdt.core.prefs 430B
org.eclipse.wst.jsdt.ui.superType.name 6B
com.genuitec.eclipse.j2eedt.core.prefs 56B
org.eclipse.core.resources.prefs 57B
org.eclipse.wst.common.component 571B
.jsdtscope 508B
pom.xml 3KB
.classpath 648B
共 84 条
- 1
资源评论
小小程序员丶
- 粉丝: 3
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功