MessageUtil.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package com.iamberry.wechat.service;
  2. import java.io.InputStream;
  3. import java.io.Writer;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import javax.servlet.http.HttpServletRequest;
  8. import org.dom4j.Document;
  9. import org.dom4j.Element;
  10. import org.dom4j.io.SAXReader;
  11. import com.iamberry.wechat.core.entity.wx.Article;
  12. import com.iamberry.wechat.core.entity.wx.MusicMessage;
  13. import com.iamberry.wechat.core.entity.wx.NewsMessage;
  14. import com.iamberry.wechat.core.entity.wx.TextMessage;
  15. import com.iamberry.wechat.core.entity.wx.TransferCustomer;
  16. import com.thoughtworks.xstream.XStream;
  17. import com.thoughtworks.xstream.core.util.QuickWriter;
  18. import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
  19. import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
  20. import com.thoughtworks.xstream.io.xml.XppDriver;
  21. /**
  22. * 消息工具类
  23. * @author fzh
  24. */
  25. public class MessageUtil {
  26. /**
  27. * 返回消息类型:文本
  28. */
  29. public static final String RESP_MESSAGE_TYPE_TEXT = "text";
  30. /**
  31. * 返回消息类型:音乐
  32. */
  33. public static final String RESP_MESSAGE_TYPE_MUSIC = "music";
  34. /**
  35. * 返回消息类型:图文
  36. */
  37. public static final String RESP_MESSAGE_TYPE_NEWS = "news";
  38. /**
  39. * 请求消息类型:文本
  40. */
  41. public static final String REQ_MESSAGE_TYPE_TEXT = "text";
  42. /**
  43. * 请求消息类型:图片
  44. */
  45. public static final String REQ_MESSAGE_TYPE_IMAGE = "image";
  46. /**
  47. * 请求消息类型:链接
  48. */
  49. public static final String REQ_MESSAGE_TYPE_LINK = "link";
  50. /**
  51. * 请求消息类型:视频
  52. */
  53. public static final String REQ_MESSAGE_TYPE_VIDEO = "video";
  54. /**
  55. * 请求消息类型:地理位置
  56. */
  57. public static final String REQ_MESSAGE_TYPE_LOCATION = "location";
  58. /**
  59. * 请求消息类型:音频
  60. */
  61. public static final String REQ_MESSAGE_TYPE_VOICE = "voice";
  62. /**
  63. * 请求消息类型:推送
  64. */
  65. public static final String REQ_MESSAGE_TYPE_EVENT = "event";
  66. /**
  67. * 事件类型:subscribe(订阅)
  68. */
  69. public static final String EVENT_TYPE_SUBSCRIBE = "subscribe";
  70. /**
  71. * 事件类型:unsubscribe(取消订阅)
  72. */
  73. public static final String EVENT_TYPE_UNSUBSCRIBE = "unsubscribe";
  74. /**
  75. * 事件类型:CLICK(自定义菜单点击事件)
  76. */
  77. public static final String EVENT_TYPE_CLICK = "CLICK";
  78. /**
  79. * 解析微信发来的请求(xml)
  80. * @param request
  81. * @return
  82. * @throws Exception
  83. */
  84. @SuppressWarnings("unchecked")
  85. public static Map<String, String> parseXml(HttpServletRequest request) throws Exception {
  86. // 将解析结果存储在HashMap中
  87. Map<String, String> map = new HashMap<String, String>();
  88. // 从request中取得输入流
  89. InputStream inputStream = request.getInputStream();
  90. // 读取输入流
  91. SAXReader reader = new SAXReader();
  92. Document document = reader.read(inputStream);
  93. // 得到xml根元素
  94. Element root = document.getRootElement();
  95. // 得到根元素的所有子节点
  96. List<Element> elementList = root.elements();
  97. // 遍历所有子节点
  98. for (Element e : elementList)
  99. map.put(e.getName(), e.getText());
  100. // 释放资源
  101. inputStream.close();
  102. inputStream = null;
  103. return map;
  104. }
  105. @SuppressWarnings("unchecked")
  106. public static Map<String, String> parse2Xml(String html) throws Exception {
  107. // 将解析结果存储在HashMap中
  108. Map<String, String> map = new HashMap<String, String>();
  109. // 读取输入流
  110. SAXReader reader = new SAXReader();
  111. Document document = reader.read(html);
  112. // 得到xml根元素
  113. Element root = document.getRootElement();
  114. // 得到根元素的所有子节点
  115. List<Element> elementList = root.elements();
  116. // 遍历所有子节点
  117. for (Element e : elementList){
  118. map.put(e.getName(), e.getText());
  119. }
  120. return map;
  121. }
  122. /**
  123. * 文本消息对象转换成xml
  124. * @param textMessage 文本消息对象
  125. * @return xml
  126. */
  127. public static String textMessageToXml(TextMessage textMessage) {
  128. xstream.alias("xml", textMessage.getClass());
  129. return xstream.toXML(textMessage);
  130. }
  131. /**
  132. * 音乐消息对象转换成xml
  133. * @param musicMessage 音乐消息对象
  134. * @return xml
  135. */
  136. public static String musicMessageToXml(MusicMessage musicMessage) {
  137. xstream.alias("xml", musicMessage.getClass());
  138. return xstream.toXML(musicMessage);
  139. }
  140. /**
  141. * 图文消息对象转换成xml
  142. * @param newsMessage 图文消息对象
  143. * @return xml
  144. */
  145. public static String newsMessageToXml(NewsMessage newsMessage) {
  146. xstream.alias("item", Article.class);
  147. xstream.alias("xml", newsMessage.getClass());
  148. return xstream.toXML(newsMessage).replaceAll("com.rat.utils.wx.event.entitys.Article", "item");
  149. }
  150. /**
  151. * 多客服消息对象转换成xml
  152. * @param transferCustomer
  153. * @return
  154. */
  155. public static String transferCustomerToXml(TransferCustomer transferCustomer) {
  156. xstream.alias("xml", transferCustomer.getClass());
  157. return xstream.toXML(transferCustomer);
  158. }
  159. /**
  160. * 扩展xstream,使其支持CDATA块
  161. */
  162. private static XStream xstream = new XStream(new XppDriver() {
  163. public HierarchicalStreamWriter createWriter(Writer out) {
  164. return new PrettyPrintWriter(out) {
  165. // 对所有xml节点的转换都增加CDATA标记
  166. boolean cdata = true;
  167. @SuppressWarnings("unchecked")
  168. public void startNode(String name, Class clazz) {
  169. super.startNode(name, clazz);
  170. }
  171. protected void writeText(QuickWriter writer, String text) {
  172. if (cdata) {
  173. writer.write("<![CDATA[");
  174. writer.write(text);
  175. writer.write("]]>");
  176. } else {
  177. writer.write(text);
  178. }
  179. }
  180. };
  181. }
  182. });
  183. }