From 0d7ea29652f5556a7c545fa0a43389b839ecc8e6 Mon Sep 17 00:00:00 2001 From: Binary Wang Date: Mon, 12 Nov 2018 11:27:14 +0800 Subject: [PATCH] =?UTF-8?q?xml=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/util/xml/XStreamInitializer.java | 66 +++++++++---------- .../wxpay/bean/result/WxPayRefundResult.java | 8 ++- .../service/impl/BaseWxPayServiceImpl.java | 3 +- .../bean/result/WxPayRefundResultTest.java | 8 +-- 4 files changed, 40 insertions(+), 45 deletions(-) diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/xml/XStreamInitializer.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/xml/XStreamInitializer.java index 90b6366a..639fcf08 100644 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/xml/XStreamInitializer.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/xml/XStreamInitializer.java @@ -8,53 +8,47 @@ import com.thoughtworks.xstream.core.util.QuickWriter; import com.thoughtworks.xstream.io.HierarchicalStreamWriter; import com.thoughtworks.xstream.io.xml.PrettyPrintWriter; import com.thoughtworks.xstream.io.xml.XppDriver; -import com.thoughtworks.xstream.security.NullPermission; -import com.thoughtworks.xstream.security.PrimitiveTypePermission; public class XStreamInitializer { - - public static XStream getInstance() { - XStream xstream = new XStream(new PureJavaReflectionProvider(), new XppDriver() { - - @Override - public HierarchicalStreamWriter createWriter(Writer out) { - return new PrettyPrintWriter(out, getNameCoder()) { - protected String PREFIX_CDATA = ""; - protected String PREFIX_MEDIA_ID = ""; - protected String SUFFIX_MEDIA_ID = ""; - - @Override - protected void writeText(QuickWriter writer, String text) { - if (text.startsWith(this.PREFIX_CDATA) && text.endsWith(this.SUFFIX_CDATA)) { - writer.write(text); - } else if (text.startsWith(this.PREFIX_MEDIA_ID) && text.endsWith(this.SUFFIX_MEDIA_ID)) { - writer.write(text); - } else { - super.writeText(writer, text); - } - + private static final XppDriver XPP_DRIVER = new XppDriver() { + @Override + public HierarchicalStreamWriter createWriter(Writer out) { + return new PrettyPrintWriter(out, getNameCoder()) { + private static final String PREFIX_CDATA = ""; + private static final String PREFIX_MEDIA_ID = ""; + private static final String SUFFIX_MEDIA_ID = ""; + + @Override + protected void writeText(QuickWriter writer, String text) { + if (text.startsWith(PREFIX_CDATA) && text.endsWith(SUFFIX_CDATA)) { + writer.write(text); + } else if (text.startsWith(PREFIX_MEDIA_ID) && text.endsWith(SUFFIX_MEDIA_ID)) { + writer.write(text); + } else { + super.writeText(writer, text); } - @Override - public String encodeNode(String name) { - //防止将_转换成__ - return name; - } - }; - } - }); + } + @Override + public String encodeNode(String name) { + //防止将_转换成__ + return name; + } + }; + } + }; + + public static XStream getInstance() { + XStream xstream = new XStream(new PureJavaReflectionProvider(), XPP_DRIVER); xstream.ignoreUnknownElements(); xstream.setMode(XStream.NO_REFERENCES); - xstream.addPermission(NullPermission.NULL); - xstream.addPermission(PrimitiveTypePermission.PRIMITIVES); + XStream.setupDefaultSecurity(xstream); xstream.allowTypesByWildcard(new String[]{ "me.chanjar.weixin.**", "cn.binarywang.wx.**", "com.github.binarywang.**" }); - XStream.setupDefaultSecurity(xstream); - xstream.setClassLoader(Thread.currentThread().getContextClassLoader()); return xstream; } diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/result/WxPayRefundResult.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/result/WxPayRefundResult.java index 18ce1fc8..b60011ba 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/result/WxPayRefundResult.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/result/WxPayRefundResult.java @@ -119,7 +119,7 @@ public class WxPayRefundResult extends BaseWxPayResult implements Serializable { /** * 组装生成退款代金券信息. */ - public void composeRefundCoupons() { + private void composeRefundCoupons() { List coupons = Lists.newArrayList(); Integer refundCount = this.getCouponRefundCount(); if (refundCount == null) { @@ -139,4 +139,10 @@ public class WxPayRefundResult extends BaseWxPayResult implements Serializable { this.setRefundCoupons(coupons); } + + public static WxPayRefundResult fromXML(String xml) { + WxPayRefundResult result = BaseWxPayResult.fromXML(xml, WxPayRefundResult.class); + result.composeRefundCoupons(); + return result; + } } diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImpl.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImpl.java index 7578093a..0b9dc458 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImpl.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImpl.java @@ -144,8 +144,7 @@ public abstract class BaseWxPayServiceImpl implements WxPayService { String url = this.getPayBaseUrl() + "/secapi/pay/refund"; String responseContent = this.post(url, request.toXML(), true); - WxPayRefundResult result = BaseWxPayResult.fromXML(responseContent, WxPayRefundResult.class); - result.composeRefundCoupons(); + WxPayRefundResult result = WxPayRefundResult.fromXML(responseContent); result.checkResult(this, request.getSignType(), true); return result; } diff --git a/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/bean/result/WxPayRefundResultTest.java b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/bean/result/WxPayRefundResultTest.java index f4f87f40..730bd37a 100644 --- a/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/bean/result/WxPayRefundResultTest.java +++ b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/bean/result/WxPayRefundResultTest.java @@ -13,11 +13,8 @@ import static org.assertj.core.api.Assertions.assertThat; */ public class WxPayRefundResultTest { - /** - * Test compose refund coupons. - */ @Test - public void testComposeRefundCoupons() { + public void testFromXML() { /* 该xml字符串来自于官方文档示例,稍加改造,加上代金卷 refund_channel 是个什么鬼,官方文档只字不提 @@ -43,8 +40,7 @@ public class WxPayRefundResultTest { " 2 \n" + ""; - WxPayRefundResult result = WxPayRefundResult.fromXML(xmlString, WxPayRefundResult.class); - result.composeRefundCoupons(); + WxPayRefundResult result = WxPayRefundResult.fromXML(xmlString); assertThat(result.getRefundCoupons()).isNotEmpty(); assertThat(result.getRefundCoupons().get(0).getCouponRefundId()).isEqualTo("123");