7ba6fa12069f05f58e9023424ca891a59dde2e70.svn-base 3.6 KB
package com.thinkgem.jeesite.modules.reg.web.wechat;

import java.io.IOException;
import java.io.InputStream;
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;

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 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_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";  
    /** 
     * 事件类型:CLICK(自定义菜单扫一扫事件) 
     */  
    public static final String EVENT_TYPE_SCANWAIT = "scancode_waitmsg";  
    
    /** 
     * 文本消息对象转换成xml 
     *  
     * @param textMessage 文本消息对象 
     * @return xml 
     */ 
    public static String textMessageToXml(TextMessage textMessage){
        XStream xstream = new XStream();
        xstream.alias("xml", textMessage.getClass());
        return xstream.toXML(textMessage);
    }
    
    /**
     * xml转换为map
     * @param request
     * @return
     * @throws IOException
     */
    @SuppressWarnings("unchecked")
    public static Map<String, String> xmlToMap(HttpServletRequest request) throws IOException{
    	 Map<String, String> map = new HashMap<String,String>();
         SAXReader reader = new SAXReader();
         try {
             //从request中获取输入流
             InputStream inputStream = request.getInputStream();
             Document document = reader.read(inputStream);
             //获取xml的根节点
             Element root = document.getRootElement();
             //获取根元素的所有子节点
             List<Element> list = root.elements();
             //遍历list
             for(Element element:list){
                 //遍历的结果放到集合中
                 map.put(element.getName(), element.getText());
                 List<Element> elementsSon = element.elements();
                 for(Element elementSon:elementsSon){
                     //遍历的结果放到集合中
                     map.put(elementSon.getName(), elementSon.getText());
                 }
             }
             //关闭流
             inputStream.close();
         } catch (Exception e) {
             e.printStackTrace();
         }
         //返回map集合
         return map;
    }
}