253 lines
8.8 KiB
Java
253 lines
8.8 KiB
Java
package com.tenwa.httpclient.pboc;
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSONArray;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.amarsoft.are.ARE;
|
|
import com.tenwa.httpclient.resources.BigDataPropertiesUtil;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.dom4j.*;
|
|
|
|
import java.util.*;
|
|
import java.util.regex.Pattern;
|
|
|
|
public class PbocXmlUtils {
|
|
/**
|
|
* 个人单笔查询的参数组装
|
|
* @return
|
|
*/
|
|
public static String createPersonQueryParamStr(Map<String,String> params, String subjectId) {
|
|
try {
|
|
JSONObject param = new JSONObject();
|
|
JSONObject msgHead = new JSONObject();
|
|
JSONObject msgBody = new JSONObject();
|
|
msgHead.put("txCode", BigDataPropertiesUtil.get("pboc_txCode"));
|
|
msgHead.put("reqSysCode", BigDataPropertiesUtil.get("pboc_reqSysCode"));
|
|
msgHead.put("loginUserCode", BigDataPropertiesUtil.get("pboc_loginUserCode"));
|
|
msgHead.put("loginPwd", BigDataPropertiesUtil.get("pboc_loginPwd"));
|
|
msgHead.put("finDept", BigDataPropertiesUtil.get("pboc_finDept"));
|
|
param.put("msgHead", msgHead);
|
|
|
|
//查询申请类型 01-人工申请(通过前置系统发起的申请)
|
|
msgBody.put("appType", "01");
|
|
msgBody.put("rptUser", "system");//报告使用人 登录征信查询前置系统的用户账号
|
|
msgBody.put("rptUserdept", "777");//填写部门机构代码
|
|
|
|
// msgBody.put("reqId", applyId);
|
|
msgBody.put("custName", params.get("fullName"));
|
|
msgBody.put("custCertype", "10");//证件类型 10-身份证
|
|
msgBody.put("custCertno", params.get("fullcertId"));
|
|
//查询原因 23-客户准入资格审查
|
|
msgBody.put("qryReason", "23");
|
|
msgBody.put("qryType", "0");//查询类型
|
|
//"01-银行版
|
|
//02-自助查询版
|
|
//03-政府版
|
|
//04-社会版"
|
|
msgBody.put("qryFormat", "01");//查询版本
|
|
msgBody.put("authStartDt", "2021-03-25");
|
|
msgBody.put("authEndDt", "2023-03-24");
|
|
|
|
msgBody.put("authArchUrl", BigDataPropertiesUtil.get("pboc_authArchUrl"));//授权档案来源URL
|
|
msgBody.put("certSrcUrl", BigDataPropertiesUtil.get("pboc_certSrcUrl"));//证照来源URL
|
|
// msgBody.put("certFileNameList", "证照文件名称");
|
|
// msgBody.put("authArchFileNameList", "授权档案文件名称");
|
|
param.put("msgBody", msgBody);
|
|
ARE.getLog().info("xmlParamStr : " + param.toJSONString());
|
|
String xmlParamStr = toXml(param);
|
|
ARE.getLog().info("xmlParamStr : " + xmlParamStr);
|
|
return xmlParamStr;
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
// 解决 CDATA 问题
|
|
private static Pattern pattern = Pattern.compile("[<>&\"',]");
|
|
|
|
|
|
public static String toXml(JSONObject obj) {
|
|
return jsonStr2Xml(obj.toJSONString());
|
|
}
|
|
|
|
private static String escape(String string) {
|
|
return pattern.matcher(string).find() ? "<![CDATA[" + string + "]]>" : string;
|
|
}
|
|
|
|
/**
|
|
* updateXml
|
|
* @param xmlStr
|
|
*/
|
|
private static String updateXml(String xmlStr) {
|
|
xmlStr = xmlStr.trim();
|
|
if (StringUtils.isBlank(xmlStr)) {
|
|
return xmlStr;
|
|
}
|
|
|
|
// 过滤非法字符
|
|
xmlStr = xmlStr.replaceAll("[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f]", "");
|
|
|
|
StringBuilder xmlSb = new StringBuilder(xmlStr);
|
|
if (!xmlStr.startsWith("<?")) {
|
|
xmlSb.insert(0, "<?xml version=\"1.0\" encoding=\"utf-8\"?>");
|
|
}
|
|
|
|
int idx = xmlSb.indexOf("?>") + 2;
|
|
xmlSb.insert(idx, "<root_luxsun>").append("</root_luxsun>");
|
|
|
|
return xmlSb.toString();
|
|
}
|
|
|
|
/**
|
|
* xml2Json
|
|
* @param xmlStr
|
|
* @return
|
|
* @throws DocumentException
|
|
*/
|
|
public static JSONObject toJson(String xmlStr) throws DocumentException {
|
|
xmlStr = updateXml(xmlStr);
|
|
Document doc = DocumentHelper.parseText(xmlStr);
|
|
JSONObject json = new JSONObject();
|
|
dom4j2Json(doc.getRootElement(), json);
|
|
return json;
|
|
}
|
|
|
|
/**
|
|
* toJsonString
|
|
* @param xmlStr
|
|
*/
|
|
public static String toJsonString(String xmlStr) throws DocumentException {
|
|
JSONObject jsonObject = toJson(xmlStr);
|
|
String json = JSON.toJSONString(jsonObject);
|
|
return json;
|
|
}
|
|
|
|
/**
|
|
* toMap
|
|
* @param xmlStr
|
|
*/
|
|
public static Map<String, Object> toMap(String xmlStr) throws DocumentException {
|
|
String jsonStr = toJsonString(xmlStr);
|
|
Map<String, Object> xmlMap = JSON.parseObject(jsonStr, LinkedHashMap.class);
|
|
return xmlMap;
|
|
}
|
|
|
|
/**
|
|
* xml转json
|
|
* @param element
|
|
* @param json
|
|
*/
|
|
private static void dom4j2Json(Element element, JSONObject json){
|
|
// 如果是属性
|
|
for(Object o:element.attributes()){
|
|
Attribute attr=(Attribute)o;
|
|
if(!isEmpty(attr.getValue())){
|
|
json.put("@"+attr.getName(), attr.getValue());
|
|
}
|
|
}
|
|
List<Element> chdEl=element.elements();
|
|
if(chdEl.isEmpty()&&!isEmpty(element.getText())){ // 如果没有子元素,只有一个值
|
|
json.put(element.getName(), element.getText());
|
|
}
|
|
|
|
for(Element e:chdEl){ // 有子元素
|
|
if(!e.elements().isEmpty()){ // 子元素也有子元素
|
|
JSONObject chdjson=new JSONObject();
|
|
dom4j2Json(e,chdjson);
|
|
Object o=json.get(e.getName());
|
|
if(o!=null){
|
|
JSONArray jsona=null;
|
|
if(o instanceof JSONObject){ // 如果此元素已存在,则转为jsonArray
|
|
JSONObject jsono=(JSONObject)o;
|
|
json.remove(e.getName());
|
|
jsona=new JSONArray();
|
|
jsona.add(jsono);
|
|
jsona.add(chdjson);
|
|
}
|
|
if(o instanceof JSONArray){
|
|
jsona=(JSONArray)o;
|
|
jsona.add(chdjson);
|
|
}
|
|
json.put(e.getName(), jsona);
|
|
}else{
|
|
if(!chdjson.isEmpty()){
|
|
json.put(e.getName(), chdjson);
|
|
}
|
|
}
|
|
}else{ // 子元素没有子元素
|
|
for(Object o:element.attributes()){
|
|
Attribute attr=(Attribute)o;
|
|
if(!isEmpty(attr.getValue())){
|
|
json.put("@"+attr.getName(), attr.getValue());
|
|
}
|
|
}
|
|
if(!e.getText().isEmpty()){
|
|
json.put(e.getName(), e.getText());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public static boolean isEmpty(String str) {
|
|
|
|
if (str == null || str.trim().isEmpty() || "null".equals(str)) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* jsonStr2Xml
|
|
* @param json
|
|
* @return java.lang.String
|
|
*/
|
|
public static String jsonStr2Xml(String json){
|
|
try {
|
|
StringBuffer buffer = new StringBuffer();
|
|
buffer.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
|
|
JSONObject jObj = JSON.parseObject(json);
|
|
json2Xmlstr(jObj,buffer);
|
|
return buffer.toString();
|
|
}
|
|
catch (Exception e) {
|
|
e.printStackTrace();
|
|
return "";
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Json to xmlstr string
|
|
*
|
|
* @param jObj the j obj
|
|
* @param buffer the buffer
|
|
* @return the string
|
|
*/
|
|
public static String json2Xmlstr(JSONObject jObj,StringBuffer buffer){
|
|
Set<Map.Entry<String, Object>> se = jObj.entrySet();
|
|
for(Iterator<Map.Entry<String, Object>> it = se.iterator(); it.hasNext(); )
|
|
{
|
|
Map.Entry<String, Object> en = it.next();
|
|
if(en.getValue().getClass().getName().equals("com.alibaba.fastjson.JSONObject")){
|
|
buffer.append("<"+en.getKey()+">");
|
|
JSONObject jo = jObj.getJSONObject(en.getKey());
|
|
json2Xmlstr(jo,buffer);
|
|
buffer.append("</"+en.getKey()+">");
|
|
}else if(en.getValue().getClass().getName().equals("com.alibaba.fastjson.JSONArray")){
|
|
JSONArray jarray = jObj.getJSONArray(en.getKey());
|
|
for (int i = 0; i < jarray.size(); i++) {
|
|
buffer.append("<"+en.getKey()+">");
|
|
JSONObject jsonobject = jarray.getJSONObject(i);
|
|
json2Xmlstr(jsonobject,buffer);
|
|
buffer.append("</"+en.getKey()+">");
|
|
}
|
|
}else if(en.getValue().getClass().getName().equals("java.lang.String")){
|
|
buffer.append("<"+en.getKey()+">" + escape((String) en.getValue()));
|
|
buffer.append("</"+en.getKey()+">");
|
|
}
|
|
}
|
|
return buffer.toString();
|
|
}
|
|
|
|
}
|