接收方消息处理
1.各种消息处理类
1 import java.io.InputStream; 2 import java.io.Writer; 3 import java.util.HashMap; 4 import java.util.List; 5 import java.util.Map; 6 7 import javax.servlet.http.HttpServletRequest; 8 9 import org.dom4j.Document; 10 import org.dom4j.Element; 11 import org.dom4j.io.SAXReader; 12 13 import com.eastnet.wechat.message.resp.Article; 14 import com.eastnet.wechat.message.resp.MusicMessage; 15 import com.eastnet.wechat.message.resp.NewsMessage; 16 import com.eastnet.wechat.message.resp.TextMessage; 17 import com.thoughtworks.xstream.XStream; 18 import com.thoughtworks.xstream.core.util.QuickWriter; 19 import com.thoughtworks.xstream.io.HierarchicalStreamWriter; 20 import com.thoughtworks.xstream.io.xml.PrettyPrintWriter; 21 import com.thoughtworks.xstream.io.xml.XppDriver; 22 23 /** 24 * 各种消息的处理类 25 * @author pengsong 26 * @date 2016.01.19 27 */ 28 29 public class MessageUtil { 30 /** 31 * 返回信息类型:文本 32 */ 33 public static final String RESP_MESSSAGE_TYPE_TEXT = "text"; 34 /** 35 * 返回信息类型:音乐 36 */ 37 public static final String RESP_MESSSAGE_TYPE_MUSIC = "music"; 38 /** 39 * 返回信息类型:图文 40 */ 41 public static final String RESP_MESSSAGE_TYPE_NEWS = "news"; 42 /** 43 * 请求信息类型:文本 44 */ 45 public static final String REQ_MESSSAGE_TYPE_TEXT = "text"; 46 /** 47 * 请求信息类型:图片 48 */ 49 public static final String REQ_MESSSAGE_TYPE_IMAGE = "image"; 50 /** 51 * 请求信息类型:链接 52 */ 53 public static final String REQ_MESSSAGE_TYPE_LINK = "link"; 54 /** 55 * 请求信息类型:地理位置 56 */ 57 public static final String REQ_MESSSAGE_TYPE_LOCATION = "location"; 58 /** 59 * 请求信息类型:音频 60 */ 61 public static final String REQ_MESSSAGE_TYPE_VOICE = "voice"; 62 /** 63 * 请求信息类型:推送 64 */ 65 public static final String REQ_MESSSAGE_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 /** 80 * 事件类型:view(自定义菜单点击事件,返回url) 81 */ 82 public static final String EVENT_TYPE_VIEW= "VIEW"; 83 /** 84 * 解析微信发来的请求 XML 85 */ 86 @SuppressWarnings("unchecked") 87 public static Map<String,String> pareXml(HttpServletRequest request) throws Exception { 88 89 //将解析的结果存储在HashMap中 90 Map<String,String> reqMap = new HashMap<String, String>(); 91 92 //从request中取得输入流 93 InputStream inputStream = request.getInputStream(); 94 //读取输入流 95 SAXReader reader = new SAXReader(); 96 Document document = reader.read(inputStream); 97 //得到xml根元素 98 Element root = document.getRootElement(); 99 //得到根元素的所有子节点 100 List<Element> elementList = root.elements(); 101 //遍历所有的子节点取得信息类容 102 for(Element elem:elementList){ 103 reqMap.put(elem.getName(),elem.getText()); 104 } 105 //释放资源 106 inputStream.close(); 107 inputStream = null; 108 109 return reqMap; 110 } 111 /** 112 * 响应消息转换成xml返回 113 * 文本消息对象转换成xml 114 */ 115 public static String textMessageToXml(TextMessage textMessage) { 116 xstream.alias("xml", textMessage.getClass()); 117 return xstream.toXML(textMessage); 118 } 119 /** 120 * 音乐消息的对象的转换成xml 121 * 122 */ 123 public static String musicMessageToXml(MusicMessage musicMessage) { 124 xstream.alias("xml", musicMessage.getClass()); 125 return xstream.toXML(musicMessage); 126 } 127 /** 128 * 图文消息的对象转换成xml 129 * 130 */ 131 public static String newsMessageToXml(NewsMessage newsMessage) { 132 xstream.alias("xml", newsMessage.getClass()); 133 xstream.alias("item", new Article().getClass()); 134 return xstream.toXML(newsMessage); 135 } 136 /** 137 * 拓展xstream,使得支持CDATA块 138 * 139 */ 140 private static XStream xstream = new XStream(new XppDriver(){ 141 public HierarchicalStreamWriter createWriter(Writer out){ 142 return new PrettyPrintWriter(out){ 143 //对所有的xml节点的转换都增加CDATA标记 144 boolean cdata = true; 145 146 @SuppressWarnings("unchecked") 147 public void startNode(String name,Class clazz){ 148 super.startNode(name,clazz); 149 } 150 151 protected void writeText(QuickWriter writer,String text){ 152 if(cdata){ 153 writer.write("<![CDATA["); 154 writer.write(text); 155 writer.write("]]>"); 156 }else{ 157 writer.write(text); 158 } 159 } 160 }; 161 } 162 }); 163
消息类型
/** * 消息基类 * @author pengsong * @date 2016.01.19 */ public class BaseMessage { //接收方账号(收到的openId) private String ToUserName; //开发者微信号 private String FromUserName; //消息创建时间(整型) private long CreateTime; //消息类型(text/music/news) private String MsgType; //位0x0001 被标记时,星标刚收到的消息 private int FuncFlag; public String getToUserName() { return ToUserName; } public void setToUserName(String toUserName) { ToUserName = toUserName; } public String getFromUserName() { return FromUserName; } public void setFromUserName(String fromUserName) { FromUserName = fromUserName; } public long getCreateTime() { return CreateTime; } public void setCreateTime(long createTime) { CreateTime = createTime; } public String getMsgType() { return MsgType; } public void setMsgType(String msgType) { MsgType = msgType; } public int getFuncFlag() { return FuncFlag; } public void setFuncFlag(int funcFlag) { FuncFlag = funcFlag; } }
public class NewsMessage extends BaseMessage{ //图文消息的个数,限制为10条以内 private int ArticleCount; //条条图文消息信息,默认第一个item为大图 private List<Article> Article; public int getArticleCount() { return ArticleCount; } public void setArticleCount(int articleCount) { ArticleCount = articleCount; } public List<Article> getArticle() { return Article; } public void setArticle(List<Article> article) { Article = article; } }
public class TextMessage extends BaseMessage{ //回复的消息类容 private String Content; public String getContent() { return Content; } public void setContent(String content) { Content = content; } }
/** * 图文消息中的Article类的定义 * 图文model * @author pengsong * @date 2015.01.19 * */ public class Article { //图文消息名称 private String Title; //图文消息描述 private String Description; //图文链接,支持JPG,PNG,格式,较好的效果为大图640*320,小图 //80*80,限制图片链接的域名需要与开发者填写的基本资料中的url一致 private String PicUrl; //点击图文消息跳转的链接 private String Url; public String getTitle() { return Title; } public void setTitle(String title) { Title = title; } public String getDescription() { return Description; } public void setDescription(String description) { Description = description; } public String getPicUrl() { return PicUrl; } public void setPicUrl(String picUrl) { PicUrl = picUrl; } public String getUrl() { return Url; } public void setUrl(String url) { Url = url; } }
/** * 音乐消息中的Music定义 * 音乐中的model * */ public class Music { //音乐名称 private String Title; //音乐描述 private String Description; //音乐链接 private String MusicUrl; //高质量音乐链接,WIFI环境下优先使用该链接播放音乐 private String HQMusicUrl; public String getTitle() { return Title; } public void setTitle(String title) { Title = title; } public String getDescription() { return Description; } public void setDescription(String description) { Description = description; } public String getMusicUrl() { return MusicUrl; } public void setMusicUrl(String musicUrl) { MusicUrl = musicUrl; } public String getHQMusicUrl() { return HQMusicUrl; } public void setHQMusicUrl(String hQMusicUrl) { HQMusicUrl = hQMusicUrl; } }
public class MusicMessage extends BaseMessage{ //音乐 private Music Music; public Music getMusic() { return Music; } public void setMusic(Music music) { Music = music; } }