|
|
@@ -0,0 +1,122 @@
|
|
|
+package com.mrxu.framework.common.xcx;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.mrxu.framework.common.util.BusinessException;
|
|
|
+import com.mrxu.framework.common.util.MD5Util;
|
|
|
+
|
|
|
+import org.jdom.Document;
|
|
|
+import org.jdom.Element;
|
|
|
+import org.jdom.JDOMException;
|
|
|
+import org.jdom.input.SAXBuilder;
|
|
|
+
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+public class WeixinUtil {
|
|
|
+
|
|
|
+ public static String createSign(SortedMap<String, Object> parameters, String key) {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ Set<Map.Entry<String, Object>> es = parameters.entrySet();
|
|
|
+ Iterator<Map.Entry<String, Object>> it = es.iterator();
|
|
|
+ while (it.hasNext()) {
|
|
|
+ Map.Entry<String, Object> entry = it.next();
|
|
|
+ String k = entry.getKey();
|
|
|
+ Object v = entry.getValue();
|
|
|
+ if (null != v && !"".equals(v) && !"sign".equals(k) && !"key".equals(k)) {
|
|
|
+ sb.append(k + "=" + v + "&");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ sb.append("key=" + key);
|
|
|
+ String sign = MD5Util.MD5Encode(sb.toString()).toUpperCase();
|
|
|
+ return sign;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getRequestXml(Map<String,Object> parameters) {
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
+ sb.append("<xml>");
|
|
|
+ Set<Map.Entry<String, Object>> es = parameters.entrySet();
|
|
|
+ Iterator<Map.Entry<String, Object>> it = es.iterator();
|
|
|
+ while (it.hasNext()) {
|
|
|
+ Map.Entry<String, Object> entry = it.next();
|
|
|
+ String k = entry.getKey();
|
|
|
+ Object v = entry.getValue();
|
|
|
+ if ("attach".equalsIgnoreCase(k) || "body".equalsIgnoreCase(k) || "sign".equalsIgnoreCase(k)) {
|
|
|
+ sb.append("<" + k + ">" + "<![CDATA[" + v + "]]></" + k + ">");
|
|
|
+ } else {
|
|
|
+ sb.append("<" + k + ">" + v + "</" + k + ">");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ sb.append("</xml>");
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static JSONObject doXMLParse(String strxml) {
|
|
|
+ strxml = strxml.replaceFirst("encoding=\".*\"", "encoding=\"UTF-8\"");
|
|
|
+ if (null == strxml || "".equals(strxml)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ JSONObject m = new JSONObject();
|
|
|
+ InputStream in = null;
|
|
|
+ Document doc = null;
|
|
|
+ try {
|
|
|
+ in = new ByteArrayInputStream(strxml.getBytes("UTF-8"));
|
|
|
+ SAXBuilder builder = new SAXBuilder();
|
|
|
+ doc = builder.build(in);
|
|
|
+ Element root = doc.getRootElement();
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ List<Element> list = root.getChildren();
|
|
|
+ Iterator<Element> it = list.iterator();
|
|
|
+ while (it.hasNext()) {
|
|
|
+ Element e = (Element) it.next();
|
|
|
+ String k = e.getName();
|
|
|
+ String v = "";
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ List<Element> children = e.getChildren();
|
|
|
+ if (children.isEmpty()) {
|
|
|
+ v = e.getTextNormalize();
|
|
|
+ } else {
|
|
|
+ v = getChildrenText(children);
|
|
|
+ }
|
|
|
+ m.put(k, v);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (JDOMException | IOException e1) {
|
|
|
+ e1.printStackTrace();
|
|
|
+ throw new BusinessException("非法xml格式");
|
|
|
+ }
|
|
|
+ finally {
|
|
|
+ try {
|
|
|
+ if(in != null) {
|
|
|
+ in.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (IOException e) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return m;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getChildrenText(List<Element> children) {
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
+ if (!children.isEmpty()) {
|
|
|
+ Iterator<Element> it = children.iterator();
|
|
|
+ while (it.hasNext()) {
|
|
|
+ Element e = it.next();
|
|
|
+ String name = e.getName();
|
|
|
+ String value = e.getTextNormalize();
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ List<Element> list = e.getChildren();
|
|
|
+ sb.append("<" + name + ">");
|
|
|
+ if (!list.isEmpty()) {
|
|
|
+ sb.append(getChildrenText(list));
|
|
|
+ }
|
|
|
+ sb.append(value);
|
|
|
+ sb.append("</" + name + ">");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+}
|