|
@@ -1,11 +1,21 @@
|
|
|
package org.springblade.school.util;
|
|
package org.springblade.school.util;
|
|
|
|
|
|
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
import org.w3c.dom.Document;
|
|
import org.w3c.dom.Document;
|
|
|
|
|
+import org.w3c.dom.Node;
|
|
|
|
|
+import org.w3c.dom.NodeList;
|
|
|
|
|
|
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.xml.XMLConstants;
|
|
import javax.xml.XMLConstants;
|
|
|
import javax.xml.parsers.DocumentBuilder;
|
|
import javax.xml.parsers.DocumentBuilder;
|
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
|
|
import javax.xml.parsers.ParserConfigurationException;
|
|
import javax.xml.parsers.ParserConfigurationException;
|
|
|
|
|
+import java.io.BufferedReader;
|
|
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
|
|
+import java.io.InputStream;
|
|
|
|
|
+import java.util.HashMap;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
|
|
|
public class WXPayXmlUtil {
|
|
public class WXPayXmlUtil {
|
|
|
|
|
|
|
@@ -25,4 +35,68 @@ public class WXPayXmlUtil {
|
|
|
public static Document newDocument() throws ParserConfigurationException {
|
|
public static Document newDocument() throws ParserConfigurationException {
|
|
|
return newDocumentBuilder().newDocument();
|
|
return newDocumentBuilder().newDocument();
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ public static Map<String, String> analysis(HttpServletRequest request) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ BufferedReader reader = null;
|
|
|
|
|
+ String line = "";
|
|
|
|
|
+ String xmlString = null;
|
|
|
|
|
+ reader = request.getReader();
|
|
|
|
|
+ StringBuffer inputString = new StringBuffer();
|
|
|
|
|
+
|
|
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
|
|
|
+ inputString.append(line);
|
|
|
|
|
+ }
|
|
|
|
|
+ xmlString = inputString.toString();
|
|
|
|
|
+ Map<String, String> dataMap = xmlToMap(xmlString);
|
|
|
|
|
+ return dataMap;
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * XML格式字符串转换为Map
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param strXML XML字符串
|
|
|
|
|
+ * @return XML数据转换后的Map
|
|
|
|
|
+ * @throws Exception
|
|
|
|
|
+ */
|
|
|
|
|
+ public static Map<String, String> xmlToMap(String strXML) throws Exception {
|
|
|
|
|
+ try {
|
|
|
|
|
+ Map<String, String> data = new HashMap<String, String>();
|
|
|
|
|
+ DocumentBuilder documentBuilder = WXPayXmlUtil.newDocumentBuilder();
|
|
|
|
|
+ InputStream stream = new ByteArrayInputStream(strXML.getBytes("UTF-8"));
|
|
|
|
|
+ org.w3c.dom.Document doc = documentBuilder.parse(stream);
|
|
|
|
|
+ doc.getDocumentElement().normalize();
|
|
|
|
|
+ NodeList nodeList = doc.getDocumentElement().getChildNodes();
|
|
|
|
|
+ for (int idx = 0; idx < nodeList.getLength(); ++idx) {
|
|
|
|
|
+ Node node = nodeList.item(idx);
|
|
|
|
|
+ if (node.getNodeType() == Node.ELEMENT_NODE) {
|
|
|
|
|
+ org.w3c.dom.Element element = (org.w3c.dom.Element) node;
|
|
|
|
|
+ data.put(element.getNodeName(), element.getTextContent());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ try {
|
|
|
|
|
+ stream.close();
|
|
|
|
|
+ } catch (Exception ex) {
|
|
|
|
|
+ // do nothing
|
|
|
|
|
+ }
|
|
|
|
|
+ return data;
|
|
|
|
|
+ } catch (Exception ex) {
|
|
|
|
|
+ getLogger().warn("Invalid XML, can not convert to map. Error message: {}. XML content: {}", ex.getMessage(), strXML);
|
|
|
|
|
+ throw ex;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 日志
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ public static Logger getLogger() {
|
|
|
|
|
+ Logger logger = LoggerFactory.getLogger("wxpay java sdk");
|
|
|
|
|
+ return logger;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|