package com.example.demo;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.XML;
import com.google.common.collect.Lists;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.axis.message.SOAPHeaderElement;
import org.apache.axis.types.Schema;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.text.StringEscapeUtils;
public class WebCall {
public static JSONObject webService(String url, String namespace, String methodName, Map<String, Object> params) throws Exception {
String decodedValue = StringEscapeUtils.unescapeHtml4(call(url, namespace, methodName, params));
return convertXmlIntoJSONObject(decodedValue);
}
public static JSONObject convertXmlIntoJSONObject(String response) {
JSONObject jsonObject = XML.toJSONObject(response);
Map<String, String> keyMap = new HashMap<>();
processKeys(jsonObject, keyMap);
return analyseJson(jsonObject, keyMap);
}
public static String call(String url, String namespace, String methodName, Map<String, Object> params) throws Exception {
String soapActionURI = namespace + methodName;
Service service = new Service();
SOAPHeaderElement header = new SOAPHeaderElement(namespace, methodName);
header.setNamespaceURI(namespace);
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(url);
call.setOperationName(new QName(namespace, methodName));
List<String> parameterList = Lists.newArrayList();
if (params != null) {
Set<String> paramsKey = params.keySet();
for (String key : paramsKey) {
call.addParameter(new QName(namespace, key), XMLType.XSD_STRING, ParameterMode.IN);
String pValue = MapUtils.getString(params, key);
header.addChildElement(key).setValue(pValue);
parameterList.add(pValue);
}
}
call.setUseSOAPAction(true);
call.setSOAPActionURI(soapActionURI);
call.addHeader(header);
call.setReturnType(XMLType.XSD_SCHEMA);
Schema schemaResult = (Schema) call.invoke(parameterList.toArray());
StringBuilder result = new StringBuilder();
for (int i = 0; i < schemaResult.get_any().length; i++) {
result.append(schemaResult.get_any()[i]);
}
return result.toString();
}
public static void processKeys(JSONObject jsonObject, Map<String, String> keyMap) {
for (String key : jsonObject.keySet()) {
Object value = jsonObject.get(key);
if (key.contains("xmlns:")) {
keyMap.put(key, "del");
} else {
int i = key.indexOf(":");
if (i != -1) {
String processedKey = key.substring(i + 1);
keyMap.put(key, processedKey);
}
}
if (value instanceof JSONObject) {
processKeys((JSONObject) value, keyMap);
}
if (value instanceof JSONArray) {
JSONArray arr = (JSONArray) value;
for (Object o : arr) {
processKeys((JSONObject) o, keyMap);
}
}
}
}
public static JSONObject analyseJson(JSONObject jsonObject, Map<String, String> keyMap) {
SortedMap<String, Object> map = new TreeMap<>();
for (String key : jsonObject.keySet()) {
String resKey = keyMap.get(key) == null ? key : keyMap.get(key);
Object value = jsonObject.get(key);
if ("del".equals(resKey)) {
map.remove(resKey);
} else {
if (value instanceof JSONArray) {
JSONArray jsonArray = new JSONArray(new LinkedList<>());
JSONArray array = jsonObject.getJSONArray(key);
for (int i = 0; i < array.size(); i++) {
Object object = array.get(i);
if (object instanceof String) {
map.put(resKey, array);
} else {
JSONObject sortJson = analyseJson((JSONObject) object, keyMap);
jsonArray.add(sortJson);
map.put(resKey, jsonArray);
}
}
} else if (value instanceof JSONObject) {
JSONObject sortJson = analyseJson((JSONObject) value, keyMap);
map.put(resKey, sortJson);
} else {
map.put(resKey, value);
}
}
}
return new JSONObject(map);
}
}