278 lines
10 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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.amarsoft.dict.als.manage.CodeManager;
import com.tenwa.flow.util.CalculateUtil;
import com.tenwa.httpclient.resources.BigDataPropertiesUtil;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.*;
import java.text.SimpleDateFormat;
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", CodeManager.getItemName("pboc_query_strategy", "txCode"));
msgHead.put("reqSysCode", CodeManager.getItemName("pboc_query_strategy", "reqSysCode"));
msgHead.put("loginUserCode", CodeManager.getItemName("pboc_query_strategy", "loginUserCode"));
msgHead.put("loginPwd", CodeManager.getItemName("pboc_query_strategy", "loginPwd"));
msgHead.put("finDept", CodeManager.getItemName("pboc_query_strategy", "finDept"));
param.put("msgHead", msgHead);
//查询申请类型 01-人工申请(通过前置系统发起的申请)
msgBody.put("appType", "01");
msgBody.put("rptUser", CodeManager.getItemName("pboc_query_strategy", "finDept"));//报告使用人 登录征信查询前置系统的用户账号
msgBody.put("rptUserdept", CodeManager.getItemName("pboc_query_strategy", "finDept"));//填写部门机构代码
// msgBody.put("reqId", applyId);
msgBody.put("custName", params.get("fullName"));
msgBody.put("custCertype", "10");//证件类型 10-身份证
msgBody.put("custCertno", params.get("fullCertId"));
//查询原因 24-融资审批
String fullRelation = params.get("fullRelation");
if("共同申请人".equals(fullRelation)){
msgBody.put("qryReason", CodeManager.getItemName("pboc_query_strategy", "qryReason_jointApplicant"));
}else if("担保人".equals(fullRelation)){
msgBody.put("qryReason", CodeManager.getItemName("pboc_query_strategy", "qryReason_guarantor"));
}else if("申请人".equals(fullRelation)){
msgBody.put("qryReason", CodeManager.getItemName("pboc_query_strategy", "qryReason_lessee"));
}
msgBody.put("qryType", CodeManager.getItemName("pboc_query_strategy", "qryType"));//查询类型
msgBody.put("rptQryType", CodeManager.getItemName("pboc_query_strategy", "rptQryType"));//信用报告查询方式1本地优先2仅查询本地3仅查询征信系统。
//"01-银行版
//02-自助查询版
//03-政府版
//04-社会版"
msgBody.put("qryFormat", CodeManager.getItemName("pboc_query_strategy", "qryFormat"));//查询版本
/**
* a-基本信息
* b-信息概要
* e-公共信息明细
*/
// msgBody.put("rptRstSegment", "a|b|e");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
String startTime = sdf.format(date);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.YEAR, 1);
String endTime = sdf.format(cal.getTime());
msgBody.put("authStartDt", startTime);
msgBody.put("authEndDt", endTime);
msgBody.put("authArchUrl", CodeManager.getItemName("pboc_query_strategy", "authArchUrl"));//授权档案来源URL
msgBody.put("certSrcUrl", CodeManager.getItemName("pboc_query_strategy", "certSrcUrl"));//证照来源URL
msgBody.put("certFileNameList", CodeManager.getItemName("pboc_query_strategy", "certFileNameList"));
msgBody.put("authArchFileNameList", CodeManager.getItemName("pboc_query_strategy", "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();
}
}