com.glaf.wechat.util.MessageUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.glaf.wechat.util.MessageUtils.java

Source

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.glaf.wechat.util;

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

/**
 * ?
 * 
 * @author jior
 */
public class MessageUtils {

    /**
     * ?
     */
    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_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_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";

    /**
     * ???XML
     * 
     * @param request
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public static Map<String, String> parseXml(HttpServletRequest request) {
        // ?HashMap
        Map<String, String> map = new java.util.concurrent.ConcurrentHashMap<String, String>();

        // request??
        InputStream inputStream = null;
        // ??
        SAXReader reader = new SAXReader();
        try {
            inputStream = request.getInputStream();

            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());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new RuntimeException(ex);
        } finally {
            try {
                // ?
                if (inputStream != null) {
                    inputStream.close();
                    inputStream = null;
                }
            } catch (IOException ex) {
            }
        }

        return map;
    }

}