Commit c8d7ade9 by chengzhuoshen

处理无效xml,提取AppHdr Document组有效xml

parent 32395c3b
...@@ -124,6 +124,7 @@ public class SwiftTransfer { ...@@ -124,6 +124,7 @@ public class SwiftTransfer {
*/ */
public static Map<String, String> mx2MtMap(String xmlStr, Map<String, Object> extraMap) throws SwiftException { public static Map<String, String> mx2MtMap(String xmlStr, Map<String, Object> extraMap) throws SwiftException {
try { try {
xmlStr = MxMessageReader.handingXml(xmlStr);
SwiftTranslationReport str = mx2MtPlus(xmlStr, null, extraMap); SwiftTranslationReport str = mx2MtPlus(xmlStr, null, extraMap);
Map<String, String> maps = MxMessageReader.getSwfMsgInfoViaMsg(str); Map<String, String> maps = MxMessageReader.getSwfMsgInfoViaMsg(str);
AbstractMX abstractMx = AbstractMX.parse(xmlStr); AbstractMX abstractMx = AbstractMX.parse(xmlStr);
...@@ -158,7 +159,7 @@ public class SwiftTransfer { ...@@ -158,7 +159,7 @@ public class SwiftTransfer {
} }
return maps; return maps;
} catch (DocumentException e) { } catch (DocumentException e) {
throw new SwiftException("ERROR", e.getMessage()); throw new SwiftException(e.getMessage());
} }
} }
......
...@@ -21,6 +21,11 @@ public abstract class AbstractMx2MapCreator implements Mx2MapCreator{ ...@@ -21,6 +21,11 @@ public abstract class AbstractMx2MapCreator implements Mx2MapCreator{
protected Map<String, Object> maps = new LinkedHashMap<>(); protected Map<String, Object> maps = new LinkedHashMap<>();
/**
* MX103报文是否是单双报逻辑
*/
protected String doubleMessageFlag = "N";
public String getXmlStr() { public String getXmlStr() {
return xmlStr; return xmlStr;
} }
...@@ -37,6 +42,14 @@ public abstract class AbstractMx2MapCreator implements Mx2MapCreator{ ...@@ -37,6 +42,14 @@ public abstract class AbstractMx2MapCreator implements Mx2MapCreator{
this.localCode = localCode; this.localCode = localCode;
} }
public String getDoubleMessageFlag() {
return doubleMessageFlag;
}
public void setDoubleMessageFlag(String doubleMessageFlag) {
this.doubleMessageFlag = doubleMessageFlag;
}
protected String getPropertyValue(String key) { protected String getPropertyValue(String key) {
return MessageUtil.getPropertyValue(localCode, key); return MessageUtil.getPropertyValue(localCode, key);
} }
...@@ -104,6 +117,9 @@ public abstract class AbstractMx2MapCreator implements Mx2MapCreator{ ...@@ -104,6 +117,9 @@ public abstract class AbstractMx2MapCreator implements Mx2MapCreator{
} }
} }
if (existFlag) { if (existFlag) {
if ("instgRmbrsmntAgt".equals(name) || "instdRmbrsmntAgt".equals(name)) {
doubleMessageFlag = "Y";
}
Map<String, Object> newMaps = new LinkedHashMap<>(); Map<String, Object> newMaps = new LinkedHashMap<>();
targetMaps.put(Mx2MtConstants.NEW_LINE + getPropertyValue(name+"Info"), newMaps); targetMaps.put(Mx2MtConstants.NEW_LINE + getPropertyValue(name+"Info"), newMaps);
for (String key : keys) { for (String key : keys) {
...@@ -141,6 +157,9 @@ public abstract class AbstractMx2MapCreator implements Mx2MapCreator{ ...@@ -141,6 +157,9 @@ public abstract class AbstractMx2MapCreator implements Mx2MapCreator{
} }
} }
if (existFlag) { if (existFlag) {
if ("instgRmbrsmntAgt".equals(name) || "instdRmbrsmntAgt".equals(name)) {
doubleMessageFlag = "Y";
}
Map<String, Object> newMaps = new LinkedHashMap<>(); Map<String, Object> newMaps = new LinkedHashMap<>();
targetMaps.put(Mx2MtConstants.NEW_LINE + getPropertyValue(subMapName), newMaps); targetMaps.put(Mx2MtConstants.NEW_LINE + getPropertyValue(subMapName), newMaps);
for (String key : keys) { for (String key : keys) {
...@@ -195,6 +214,7 @@ public abstract class AbstractMx2MapCreator implements Mx2MapCreator{ ...@@ -195,6 +214,7 @@ public abstract class AbstractMx2MapCreator implements Mx2MapCreator{
protected void buildInstgRmbrsmntAgtInfo(Map<String, Object> targetMaps, AbstractMT abstractMT) { protected void buildInstgRmbrsmntAgtInfo(Map<String, Object> targetMaps, AbstractMT abstractMT) {
Tag tag = abstractMT.getSwiftMessage().getBlock4().getTagByName("53A"); Tag tag = abstractMT.getSwiftMessage().getBlock4().getTagByName("53A");
if (tag != null) { if (tag != null) {
doubleMessageFlag = "Y";
Map<String, Object> newMaps = new LinkedHashMap<>(); Map<String, Object> newMaps = new LinkedHashMap<>();
targetMaps.put(Mx2MtConstants.NEW_LINE + getPropertyValue("instgRmbrsmntAgtInfo"), newMaps); targetMaps.put(Mx2MtConstants.NEW_LINE + getPropertyValue("instgRmbrsmntAgtInfo"), newMaps);
Field53A field53A = (Field53A)tag.asField(); Field53A field53A = (Field53A)tag.asField();
...@@ -209,6 +229,7 @@ public abstract class AbstractMx2MapCreator implements Mx2MapCreator{ ...@@ -209,6 +229,7 @@ public abstract class AbstractMx2MapCreator implements Mx2MapCreator{
} }
tag = abstractMT.getSwiftMessage().getBlock4().getTagByName("53B"); tag = abstractMT.getSwiftMessage().getBlock4().getTagByName("53B");
if (tag != null) { if (tag != null) {
doubleMessageFlag = "Y";
Map<String, Object> newMaps = new LinkedHashMap<>(); Map<String, Object> newMaps = new LinkedHashMap<>();
targetMaps.put(Mx2MtConstants.NEW_LINE + getPropertyValue("instgRmbrsmntAgtInfo"), newMaps); targetMaps.put(Mx2MtConstants.NEW_LINE + getPropertyValue("instgRmbrsmntAgtInfo"), newMaps);
Field53B field53B = (Field53B)tag.asField(); Field53B field53B = (Field53B)tag.asField();
......
...@@ -42,12 +42,7 @@ public class Mx2MapPacs008Creator extends AbstractMx2MapCreator { ...@@ -42,12 +42,7 @@ public class Mx2MapPacs008Creator extends AbstractMx2MapCreator {
maps.put(getPropertyValue("uetr"), elementMaps.get("uetr")); maps.put(getPropertyValue("uetr"), elementMaps.get("uetr"));
} }
if (elementMaps.get("sttlmMtd") != null) { if (elementMaps.get("sttlmMtd") != null) {
String settlementMethod = (String) elementMaps.get("sttlmMtd"); maps.put(getPropertyValue("settlement.method"), elementMaps.get("sttlmMtd"));
if (SettlementMethodCode.COVE.value().equals(settlementMethod)) {
maps.put(getPropertyValue("settlement.method"), elementMaps.get("sttlmMtd") + "("+getPropertyValue("doubleMessage")+")");
} else {
maps.put(getPropertyValue("settlement.method"), elementMaps.get("sttlmMtd") + "("+getPropertyValue("simpleMessage")+")");
}
} }
if (elementMaps.get("sttlmAcctIdIban") != null) { if (elementMaps.get("sttlmAcctIdIban") != null) {
maps.put(getPropertyValue("settlement.acct"), elementMaps.get("sttlmAcctIdIban")); maps.put(getPropertyValue("settlement.acct"), elementMaps.get("sttlmAcctIdIban"));
...@@ -130,6 +125,12 @@ public class Mx2MapPacs008Creator extends AbstractMx2MapCreator { ...@@ -130,6 +125,12 @@ public class Mx2MapPacs008Creator extends AbstractMx2MapCreator {
if (tag != null) { if (tag != null) {
maps.put(getPropertyValue("regulatoryReporting"), tag.getValue()); maps.put(getPropertyValue("regulatoryReporting"), tag.getValue());
} }
//判断MX103 是单报还是双报
if ("Y".equals(doubleMessageFlag)) {
maps.put(getPropertyValue("settlement.method"), elementMaps.get("sttlmMtd") + "("+getPropertyValue("doubleMessage")+")");
} else {
maps.put(getPropertyValue("settlement.method"), elementMaps.get("sttlmMtd") + "("+getPropertyValue("simpleMessage")+")");
}
return maps; return maps;
} }
} }
package com.brilliance.swift.util; package com.brilliance.swift.util;
import com.brilliance.swift.SwiftTransfer;
import com.brilliance.swift.constants.ERROR; import com.brilliance.swift.constants.ERROR;
import com.brilliance.swift.constants.Mx2MtConstants; import com.brilliance.swift.constants.Mx2MtConstants;
import com.brilliance.swift.exception.SwiftException; import com.brilliance.swift.exception.SwiftException;
...@@ -10,6 +11,8 @@ import com.prowidesoftware.swift.model.*; ...@@ -10,6 +11,8 @@ import com.prowidesoftware.swift.model.*;
import com.prowidesoftware.swift.model.field.*; import com.prowidesoftware.swift.model.field.*;
import com.prowidesoftware.swift.model.mt.AbstractMT; import com.prowidesoftware.swift.model.mt.AbstractMT;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
...@@ -437,4 +440,24 @@ public class MxMessageReader { ...@@ -437,4 +440,24 @@ public class MxMessageReader {
throw new SwiftException(e.getMessage()); throw new SwiftException(e.getMessage());
} }
} }
/**
* 如果传入的xml 没有报文头,导致dom4j无法解析
* 提取xml里面的AppHdr和Document,重新组装成有效的xml报文
*/
public static String handingXml(String xmlStr) {
boolean flag = true;
try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
documentBuilder.parse(xmlStr);
} catch (Exception e) {
flag = false;
}
if (flag) { //如果是有效xml报文,直接返回
return xmlStr;
} else {
return SwiftTransfer.fetchXmlFromMixMessage(xmlStr);
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment