org.frameworkset.wx.common.mp.aes.XMLParse Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pdp-wx Show documentation
Show all versions of pdp-wx Show documentation
bboss public development platform base on bboss.
/**
* 对公众平台发送给公众账号的消息加解密示例代码.
*
* @copyright Copyright (c) 1998-2014 Tencent Inc.
*/
// ------------------------------------------------------------------------
package org.frameworkset.wx.common.mp.aes;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.frameworkset.wx.common.entity.WxJSAPISign;
import org.frameworkset.wx.common.entity.WxOrderMessage;
import org.frameworkset.wx.common.entity.WxServiceCompany;
import org.frameworkset.wx.common.util.WXHelper;
/**
* XMLParse class
*
* 提供提取消息格式中的密文及生成回复消息格式的接口.
*/
public class XMLParse {
private static Logger log = Logger.getLogger(XMLParse.class);
/**
* 提取出xml数据包中的加密消息
*
* @param xmltext
* 待提取的xml字符串
* @return 提取出的加密消息字符串
* @throws AesException
*/
public static Object[] extract(String xmltext) throws AesException {
Object[] result = new Object[3];
try {
Document document = DocumentHelper.parseText(xmltext);
Element root = document.getRootElement();
// NodeList nodelist1 = root.elementText("Encrypt")("Encrypt");
// NodeList nodelist2 = root.getElementsByTagName("ToUserName");
result[0] = 0;
result[1] = root.elementText("Encrypt");// nodelist1.item(0).getPrefix();//.getTextContent();
result[2] = root.elementText("ToUserName");// nodelist2.item(0).getPrefix();//.getTextContent();
return result;
} catch (Exception e) {
e.printStackTrace();
throw new AesException(AesException.ParseXmlError);
}
}
/**
* 生成xml消息
*
* @param encrypt
* 加密后的消息密文
* @param signature
* 安全签名
* @param timestamp
* 时间戳
* @param nonce
* 随机字符串
* @return 生成的xml字符串
*/
public static String generate(String encrypt, String signature, String timestamp, String nonce) {
String format = "\n" + " \n"
+ " \n" + "%3$s \n"
+ " \n" + " ";
return String.format(format, encrypt, signature, timestamp, nonce);
}
public static String generateJsAPIXML(WxJSAPISign wxJSAPISign) throws UnsupportedEncodingException {
List xmllist = new ArrayList();
StringBuffer sb = new StringBuffer();
sb.append("\n");
if (wxJSAPISign.getAppId() != null)
xmllist.add("" + wxJSAPISign.getAppId() + " \n");
if (wxJSAPISign.getTimeStamp() != null)
xmllist.add("" + wxJSAPISign.getTimeStamp() + " \n");
if (wxJSAPISign.getNonceStr() != null)
xmllist.add("" + wxJSAPISign.getNonceStr() + " \n");
if (wxJSAPISign.getPrepayId() != null)
xmllist.add("prepay_id=" + wxJSAPISign.getPrepayId() + " \n");
if (wxJSAPISign.getSignType() != null)
xmllist.add("" + wxJSAPISign.getSignType() + " \n");
Collections.sort(xmllist);
for (String str : xmllist) {
sb.append(str);
}
sb.append(" ");
return new String(sb.toString().getBytes(), "ISO8859-1");
}
/**
* @param encrypt
* @param signature
* @param timestamp
* @param nonce
* @return
* @throws UnsupportedEncodingException
*/
public static String generateUnifiedOrderXML(WxOrderMessage orderMsg) throws UnsupportedEncodingException {
List xmllist = new ArrayList();
StringBuffer sb = new StringBuffer();
sb.append("\n");
if (orderMsg.getAppid() != null)
xmllist.add("" + orderMsg.getAppid() + " \n");
if (orderMsg.getAttach() != null)
xmllist.add("" + orderMsg.getAttach() + " \n");
if (orderMsg.getBody() != null)
xmllist.add("" + orderMsg.getBody() + "\n");
if (orderMsg.getMchId() != null)
xmllist.add("" + orderMsg.getMchId() + " \n");
if (orderMsg.getDetail() != null)
xmllist.add("" + orderMsg.getDetail() + " \n");
if (orderMsg.getDeviceInfo() != null)
xmllist.add("" + orderMsg.getDeviceInfo() + " \n");
if (orderMsg.getNonceStr() != null)
xmllist.add("" + orderMsg.getNonceStr() + " \n");
if (orderMsg.getNotifyUrl() != null)
xmllist.add("" + orderMsg.getNotifyUrl() + " \n");
if (orderMsg.getOpenid() != null)
xmllist.add("" + orderMsg.getOpenid() + " \n");
if (orderMsg.getOutTradeNo() != null)
xmllist.add("" + orderMsg.getOutTradeNo() + " \n");
if (orderMsg.getSign() != null)
xmllist.add("" + orderMsg.getSign() + " \n");
if (orderMsg.getSpbillCreateIp() != null)
xmllist.add("" + orderMsg.getSpbillCreateIp() + " \n");
if (orderMsg.getTotalFee() != null)
xmllist.add("" + orderMsg.getTotalFee() + " \n");
if (orderMsg.getTradeType() != null)
xmllist.add("" + orderMsg.getTradeType() + " \n");
if (orderMsg.getFeeType() != null)
xmllist.add("" + orderMsg.getFeeType() + " \n");
if (orderMsg.getTimeStart() != null)
xmllist.add("" + orderMsg.getTimeStart() + " \n");
if (orderMsg.getTimeExpire() != null)
xmllist.add("" + orderMsg.getTimeExpire() + " \n");
if (orderMsg.getGoodsTag() != null)
xmllist.add("" + orderMsg.getGoodsTag() + " \n");
if (orderMsg.getProductId() != null)
xmllist.add("" + orderMsg.getProductId() + " \n");
if (orderMsg.getLimitPay() != null)
xmllist.add("" + orderMsg.getLimitPay() + " \n");
Collections.sort(xmllist);
for (String str : xmllist) {
sb.append(str);
}
sb.append(" ");
return new String(sb.toString().getBytes(), "ISO8859-1");
}
public static String generateServiceCompanyPayXML(WxServiceCompany wxServiceCompany, String[] columnNames) {
String xml = null;
try {
Arrays.sort(columnNames);
Class t=WxServiceCompany.class;
int i = 0;
StringBuffer sb = new StringBuffer();
sb.append("\n");
int j=0;
for (String column : WXHelper.getMethodByASCIIsort(columnNames)) {
Method m = null;
try {
m =t.getMethod(column);
} catch (NoSuchMethodException e) {
log.debug("the method: " + column + " not found in class:" +t.getName());
i++;
continue;
}
Object value = m.invoke(wxServiceCompany, null);
sb.append("<");
sb.append(columnNames[j]);
sb.append(">");
sb.append(value);
sb.append("");
sb.append(columnNames[j]);
sb.append(">\n");
j++;
}
sb.append(" ");
xml=sb.toString();
System.out.println(xml);
} catch (Exception e) {
e.printStackTrace();
}
return xml;
}
}