LAPTOP-SB56SG4Q\86185
3 years ago
10 changed files with 503 additions and 1 deletions
Binary file not shown.
Binary file not shown.
@ -1,3 +1,6 @@
|
||||
# open-JSD-8350 |
||||
|
||||
JSD-8350 开源任务材料 |
||||
JSD-8350 基于数据工厂的saop webservice调用数据集\ |
||||
免责说明:该源码为第三方爱好者提供,不保证源码和方案的可靠性,也不提供任何形式的源码教学指导和协助!\ |
||||
仅作为开发者学习参考使用!禁止用于任何商业用途!\ |
||||
为保护开发者隐私,开发者信息已隐去!若原开发者希望公开自己的信息,可联系hugh处理。 |
@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?><plugin> |
||||
<id>com.fr.plugin.ws.factory</id> |
||||
<name><![CDATA[ws数据工厂]]></name> |
||||
<active>yes</active> |
||||
<version>1.2</version> |
||||
<env-version>10.0</env-version> |
||||
<jartime>2018-07-31</jartime> |
||||
<vendor>.fr.open</vendor> |
||||
<description><![CDATA[ws数据工厂]]></description> |
||||
<change-notes><![CDATA[ |
||||
[2021-09-02]【1.0】初始化插件。<br/> |
||||
[2021-09-10]【1.1】修改初始化配置,增加打印日志。<br/> |
||||
[2021-09-10]【1.2】修改获取数据逻辑。<br/> |
||||
]]></change-notes> |
||||
<extra-core> |
||||
<TableDataLoader class="com.fr.plugin.ws.factory.loader.SoapLoader"/> |
||||
<TableDataResolver class="com.fr.plugin.ws.factory.resolver.BasicSoapResolver"/> |
||||
</extra-core> |
||||
<function-recorder class="com.fr.plugin.ws.factory.loader.SoapLoader"/> |
||||
<dependence> |
||||
<Item key="com.tptj.plugin.hg.tabledata.factory.v10" type="plugin"/> |
||||
</dependence> |
||||
</plugin> |
@ -0,0 +1,287 @@
|
||||
package com.fr.plugin.ws.factory.loader; |
||||
|
||||
import com.eviware.soapui.impl.wsdl.WsdlInterface; |
||||
import com.eviware.soapui.impl.wsdl.WsdlOperation; |
||||
import com.eviware.soapui.impl.wsdl.WsdlProject; |
||||
import com.eviware.soapui.impl.wsdl.support.wsdl.WsdlContext; |
||||
import com.eviware.soapui.impl.wsdl.support.wsdl.WsdlImporter; |
||||
import com.fr.json.JSONArray; |
||||
import com.fr.json.JSONObject; |
||||
import com.fr.log.FineLoggerFactory; |
||||
import com.fr.plugin.transform.ExecuteFunctionRecord; |
||||
import com.fr.plugin.transform.FunctionRecorder; |
||||
import com.fr.plugin.ws.factory.loader.soap.ObjectSoapParameter; |
||||
import com.fr.plugin.ws.factory.resolver.BasicSoapResolver; |
||||
import com.fr.script.Calculator; |
||||
import com.fr.stable.ParameterProvider; |
||||
import com.fr.stable.StringUtils; |
||||
import com.ibm.wsdl.PortImpl; |
||||
import com.ibm.wsdl.ServiceImpl; |
||||
import com.ibm.wsdl.extensions.soap.SOAPAddressImpl; |
||||
import com.tptj.plugin.hg.impl.AbstractLoader; |
||||
import com.tptj.plugin.hg.stable.SimpleDataModel; |
||||
import org.dom4j.DocumentHelper; |
||||
import org.dom4j.Element; |
||||
import org.dom4j.Node; |
||||
|
||||
import javax.wsdl.Definition; |
||||
import javax.wsdl.Port; |
||||
import javax.xml.namespace.QName; |
||||
import java.io.*; |
||||
import java.net.HttpURLConnection; |
||||
import java.net.URL; |
||||
import java.nio.charset.StandardCharsets; |
||||
import java.util.*; |
||||
|
||||
@FunctionRecorder |
||||
public class SoapLoader extends AbstractLoader { |
||||
|
||||
private static WsdlProject WSDL_PROJECT = null; |
||||
|
||||
private static Map<String, WsdlInterface> servicesmap = new HashMap<String, WsdlInterface>(); |
||||
|
||||
public SoapLoader() { |
||||
|
||||
} |
||||
|
||||
public static void main(String[] args) { |
||||
//代码涉密已删除!
|
||||
} |
||||
|
||||
@ExecuteFunctionRecord |
||||
@Override |
||||
public Object load(Calculator cal, ParameterProvider[] params, String others) { |
||||
try { |
||||
try { |
||||
if (null == WSDL_PROJECT) { |
||||
Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); |
||||
WSDL_PROJECT = new WsdlProject(); |
||||
} |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(),e); |
||||
} |
||||
JSONObject conf = new JSONObject(others); |
||||
String wsdl = conf.optString("wsdl"); |
||||
String operationName = conf.optString("operation"); |
||||
String username = conf.optString("username"); |
||||
String password = conf.optString("password"); |
||||
JSONObject parameters = conf.optJSONObject("parameters"); |
||||
boolean cache = conf.optBoolean("cache", true); |
||||
WsdlInterface service = loadService(wsdl, cache, username, password); |
||||
WsdlOperation operation = service.getOperationByName(operationName); |
||||
String soapAction = operation.getAction(); |
||||
String host = getHost(service); |
||||
String soapxml = buildSoapRequest(operation, parameters).replaceAll("\\?"," "); |
||||
FineLoggerFactory.getLogger().info("host is {}",host); |
||||
FineLoggerFactory.getLogger().info("soapxml is {}",soapxml); |
||||
Element resutlt = string2XML(load(host, soapAction, soapxml, username, password)); |
||||
Element resbody = resutlt.element("Body"); |
||||
resbody = resbody.element(operationName + "Response"); |
||||
return resbody.asXML(); |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(),e); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
private String buildSoapRequest(WsdlOperation operation, JSONObject parameters) { |
||||
String soapxml = operation.createRequest(true); |
||||
Element root = string2XML(soapxml); |
||||
Element emethod = (Element) root.element("Body").elements().get(0); |
||||
new ObjectSoapParameter(emethod, parameters).getXML(); |
||||
return root.asXML(); |
||||
} |
||||
|
||||
public static Element string2XML(String xmlStr) { |
||||
try { |
||||
Element ele = DocumentHelper.parseText(xmlStr).getRootElement(); |
||||
deleteXmlNotation(ele); |
||||
return ele; |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(),e); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
public static String XML2String(Element doc) { |
||||
return doc.asXML(); |
||||
} |
||||
|
||||
private static void deleteXmlNotation(Element ele) { |
||||
Iterator<Node> nodes = ele.nodeIterator(); |
||||
List<Node> rmNodes = new ArrayList<Node>(); |
||||
//循环收集可以删除的节点
|
||||
while (nodes.hasNext()) { |
||||
Node subNode = nodes.next(); |
||||
if (subNode.getNodeType() == Node.COMMENT_NODE) { |
||||
rmNodes.add(subNode); |
||||
rmNodes.add(nodes.next()); |
||||
} |
||||
} |
||||
//删除收集到的节点
|
||||
for (Node node : rmNodes) { |
||||
ele.remove(node); |
||||
} |
||||
//递归,处理下级节点
|
||||
Iterator<Element> eleIt = ele.elementIterator(); |
||||
while (eleIt.hasNext()) { |
||||
deleteXmlNotation(eleIt.next()); |
||||
} |
||||
} |
||||
|
||||
private String getSoapHost(ServiceImpl service) { |
||||
Set<Map.Entry<String, Port>> set = service.getPorts().entrySet(); |
||||
for (Map.Entry<String, Port> entry : set) { |
||||
PortImpl port = (PortImpl) entry.getValue(); |
||||
List extensibilityElements = port.getExtensibilityElements(); |
||||
for (Object ele : extensibilityElements) { |
||||
if (ele instanceof SOAPAddressImpl) { |
||||
SOAPAddressImpl addr = (SOAPAddressImpl) ele; |
||||
return addr.getLocationURI(); |
||||
} |
||||
} |
||||
} |
||||
return StringUtils.EMPTY; |
||||
} |
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes"}) |
||||
private String getHost(WsdlInterface wsdlInterface) { |
||||
WsdlContext wsdlContext = wsdlInterface.getWsdlContext(); |
||||
try { |
||||
Definition definition = wsdlContext.getDefinition(); |
||||
Map<QName, ServiceImpl> allServices = definition.getAllServices(); |
||||
Set<QName> qNames = allServices.keySet(); |
||||
for (QName qname : qNames) { |
||||
ServiceImpl service = allServices.get(qname); |
||||
String host = getSoapHost(service); |
||||
if (StringUtils.isNotEmpty(host)) { |
||||
return host.replace("eccappqas.sphkdl.shaphar.net", "10.1.151.128"); |
||||
} |
||||
} |
||||
} catch (Exception e) { |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
private WsdlInterface loadService(String wsdl, boolean cache, String username, String password) { |
||||
try { |
||||
if (cache && servicesmap.containsKey(wsdl)) { |
||||
return servicesmap.get(wsdl); |
||||
} |
||||
if (null == WSDL_PROJECT) { |
||||
WSDL_PROJECT = new WsdlProject(); |
||||
} |
||||
String TMP_PATH = System.getProperty("java.io.tmpdir"); |
||||
File file = new File(TMP_PATH + "/" + UUID.randomUUID()); |
||||
file.createNewFile(); |
||||
String body = loadWsdl(wsdl, username, password); |
||||
FileOutputStream outputStream = new FileOutputStream(file); |
||||
outputStream.write(body.getBytes(StandardCharsets.UTF_8)); |
||||
outputStream.flush(); |
||||
outputStream.close(); |
||||
WsdlInterface[] wsdlInterfaces = WsdlImporter.importWsdl(WSDL_PROJECT, file.getAbsolutePath()); |
||||
for (WsdlInterface service : wsdlInterfaces) { |
||||
if ("SOAP 1.1".equals(service.getSoapVersion().getName())) { |
||||
servicesmap.put(wsdl, service); |
||||
return service; |
||||
} |
||||
} |
||||
file.delete(); |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(),e); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
public static final String KEY = "Plugin-Factory_Data_Set_Loader_Type_Soap"; |
||||
|
||||
@Override |
||||
public String getName() { |
||||
return KEY; |
||||
} |
||||
|
||||
@Override |
||||
public String getDefaultConfig() { |
||||
StringBuilder sb = new StringBuilder(); |
||||
sb.append("{\r\n") |
||||
.append(" wsdl:\"\",\r\n") |
||||
.append(" operation:\"\",\r\n") |
||||
.append(" parameters:{},\r\n") |
||||
.append(" cache:true,\r\n") |
||||
.append(" username:\"\",\r\n") |
||||
.append(" password:\"\",\r\n") |
||||
.append("}\r\n"); |
||||
return sb.toString(); |
||||
} |
||||
|
||||
public String load(String host, String soapAction, String soapxml, String username, String password) { |
||||
StringBuffer sb = new StringBuffer(""); |
||||
HttpURLConnection httpConn = null; |
||||
|
||||
try { |
||||
URL url = new URL(host); |
||||
httpConn = (HttpURLConnection) url.openConnection(); |
||||
byte[] buf = (soapxml).getBytes("utf-8"); |
||||
//httpConn.setRequestProperty("Content-Length",String.valueOf( buf.length ));
|
||||
httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8"); |
||||
httpConn.setRequestProperty("SOAPAction", soapAction); |
||||
httpConn.setRequestProperty("Authorization", "Basic " + getEncode(username, password)); |
||||
//httpConn.setRequestProperty("Connection", "Keep-Alive");
|
||||
//httpConn.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
||||
httpConn.setRequestMethod("POST"); |
||||
httpConn.setDoOutput(true); |
||||
httpConn.setDoInput(true); |
||||
OutputStream out = httpConn.getOutputStream(); |
||||
out.write(buf); |
||||
out.close(); |
||||
InputStreamReader isr; |
||||
httpConn.connect(); |
||||
isr = new InputStreamReader(httpConn.getInputStream(), "utf-8"); |
||||
BufferedReader in = new BufferedReader(isr); |
||||
String inputLine; |
||||
while ((inputLine = in.readLine()) != null) { |
||||
sb.append(inputLine + "\r\n"); |
||||
} |
||||
in.close(); |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(),e); |
||||
} |
||||
FineLoggerFactory.getLogger().info("get soap res is {}",sb.toString()); |
||||
return sb.toString(); |
||||
} |
||||
|
||||
public String loadWsdl(String wsdl, String username, String password) { |
||||
StringBuffer sb = new StringBuffer(""); |
||||
HttpURLConnection httpConn = null; |
||||
|
||||
try { |
||||
URL url = new URL(wsdl); |
||||
httpConn = (HttpURLConnection) url.openConnection(); |
||||
//httpConn.setRequestProperty("Content-Length",String.valueOf( buf.length ));
|
||||
httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8"); |
||||
httpConn.setRequestProperty("Authorization", "Basic " + getEncode(username, password)); |
||||
//httpConn.setRequestProperty("Connection", "Keep-Alive");
|
||||
//httpConn.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
||||
httpConn.setRequestMethod("POST"); |
||||
httpConn.setDoOutput(true); |
||||
httpConn.setDoInput(true); |
||||
OutputStream out = httpConn.getOutputStream(); |
||||
out.close(); |
||||
InputStreamReader isr; |
||||
isr = new InputStreamReader(httpConn.getInputStream(), "utf-8"); |
||||
BufferedReader in = new BufferedReader(isr); |
||||
String inputLine; |
||||
while ((inputLine = in.readLine()) != null) { |
||||
sb.append(inputLine + "\r\n"); |
||||
} |
||||
in.close(); |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(),e); |
||||
} |
||||
return sb.toString(); |
||||
} |
||||
|
||||
private String getEncode(String username, String password) throws UnsupportedEncodingException { |
||||
return new String(Base64.getEncoder().encode((username + ":" + password).getBytes("UTF-8"))); |
||||
} |
||||
} |
@ -0,0 +1,16 @@
|
||||
package com.fr.plugin.ws.factory.loader.soap; |
||||
|
||||
import org.dom4j.Element; |
||||
|
||||
public abstract class AbstractSoapParameter<T> implements SoapParameter { |
||||
|
||||
protected Element el = null; |
||||
|
||||
protected T param = null; |
||||
|
||||
public AbstractSoapParameter( Element el, T param ){ |
||||
this.el = el; |
||||
this.param = param; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,19 @@
|
||||
package com.fr.plugin.ws.factory.loader.soap; |
||||
|
||||
import org.dom4j.Element; |
||||
|
||||
import com.fr.general.GeneralUtils; |
||||
|
||||
public class BasicSoapParameter extends AbstractSoapParameter<Object> { |
||||
|
||||
public BasicSoapParameter(Element el, Object param) { |
||||
super(el, param); |
||||
} |
||||
|
||||
@Override |
||||
public Element getXML() { |
||||
el.setText(GeneralUtils.objectToString(param)); |
||||
return el; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,64 @@
|
||||
package com.fr.plugin.ws.factory.loader.soap; |
||||
|
||||
import com.fr.base.FRContext; |
||||
import com.fr.json.JSONArray; |
||||
import com.fr.json.JSONObject; |
||||
import org.dom4j.DocumentHelper; |
||||
import org.dom4j.Element; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
import java.util.Set; |
||||
|
||||
public class ObjectSoapParameter extends AbstractSoapParameter<JSONObject> { |
||||
|
||||
public ObjectSoapParameter(Element el, JSONObject param) { |
||||
super(el, param); |
||||
} |
||||
|
||||
@SuppressWarnings("unchecked") |
||||
@Override |
||||
public Element getXML() { |
||||
Map<String,Object> pmap = param.toMap(); |
||||
Set<String> ks = pmap.keySet(); |
||||
for( String key : ks ){ |
||||
Object val = pmap.get(key); |
||||
Element ele = el.element(key); |
||||
if( val instanceof JSONObject ){ |
||||
new ObjectSoapParameter(ele,(JSONObject)val).getXML(); |
||||
}else if( val instanceof JSONArray ){ |
||||
FRContext.getLogger().error("SOAP取数目前尚不支持数组型参数!若需要请到帆软需求中心提供相应的测试环境后,将免费进行适配!"); |
||||
}else{ |
||||
new BasicSoapParameter(ele,val).getXML(); |
||||
} |
||||
} |
||||
//hugh:有的参数为空时是不允许传参的,这类节点就需要删除掉
|
||||
List<Element> rmv = new ArrayList<Element>(); |
||||
List<Element> els = el.elements(); |
||||
for(Element sel : els){ |
||||
if(sel.isTextOnly()){ |
||||
if( "?".equals( sel.getText() ) ){ |
||||
rmv.add(sel); |
||||
} |
||||
} |
||||
} |
||||
|
||||
return el; |
||||
} |
||||
|
||||
@SuppressWarnings("unchecked") |
||||
public static void main(String [] args){ |
||||
try{ |
||||
String xmlStr = "<p1><p2><p2>1</p2><p2>2</p2></p2><p2><p2>3</p2><p2>4</p2></p2></p1>"; |
||||
Element ele = DocumentHelper.parseText(xmlStr).getRootElement(); |
||||
List<Element> s = ele.elements("p2"); |
||||
for( Element i : s ){ |
||||
System.out.println(i.asXML()); |
||||
} |
||||
|
||||
}catch(Exception e){ |
||||
e.printStackTrace(); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,7 @@
|
||||
package com.fr.plugin.ws.factory.loader.soap; |
||||
|
||||
import org.dom4j.Element; |
||||
|
||||
public interface SoapParameter { |
||||
public Element getXML(); |
||||
} |
@ -0,0 +1,83 @@
|
||||
package com.fr.plugin.ws.factory.resolver; |
||||
|
||||
import com.fr.general.GeneralUtils; |
||||
import com.fr.json.JSONObject; |
||||
import com.fr.plugin.ws.factory.loader.SoapLoader; |
||||
import com.fr.stable.StringUtils; |
||||
import com.tptj.plugin.hg.impl.AbstractResolver; |
||||
import com.tptj.plugin.hg.stable.SimpleDataModel; |
||||
import org.dom4j.Element; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
public class BasicSoapResolver<main> extends AbstractResolver { |
||||
public static String[] split(String src) { |
||||
String[] arr = new String[]{src}; |
||||
if (src.indexOf(".") != -1) { |
||||
arr = src.split("\\."); |
||||
} |
||||
return arr; |
||||
} |
||||
|
||||
@Override |
||||
public SimpleDataModel parse(Object data, String others) { |
||||
if(StringUtils.isBlank(others)){ |
||||
return new SimpleDataModel(); |
||||
} |
||||
ArrayList<String> cols = new ArrayList<String>(); |
||||
SimpleDataModel dm = new SimpleDataModel(cols); |
||||
try { |
||||
Element result = SoapLoader.string2XML((String) data); |
||||
JSONObject conf = new JSONObject(others); |
||||
String[] paths = split(conf.optString("tablepath")); |
||||
List numkeys = conf.optJSONArray("numkeys").toUnmodifiableList(); |
||||
List transarray = conf.optJSONArray("transarray").toUnmodifiableList(); |
||||
//hugh:目前只支持单节点路径
|
||||
Element table = result; |
||||
for (String path : paths) { |
||||
if (StringUtils.isNotEmpty(path)) { |
||||
table = table.element(path); |
||||
} |
||||
} |
||||
List<Element> rows = table.elements(); |
||||
for (Element row : rows) { |
||||
List<Element> datas = row.elements(); |
||||
dm.addRow(datas.size()); |
||||
for (Element val : datas) { |
||||
String colName = val.getName(); |
||||
int pos = transarray.indexOf(colName); |
||||
if (pos != -1 && pos % 2 == 0) { |
||||
colName = (String) transarray.get(pos + 1); |
||||
} |
||||
dm.addColumn(colName); |
||||
String value = val.getText(); |
||||
dm.setValueAtLastRow(colName, numkeys.indexOf(colName) == -1 ? value : GeneralUtils.objectToNumber(value)); |
||||
} |
||||
} |
||||
} catch (Exception e) { |
||||
|
||||
} |
||||
return dm; |
||||
} |
||||
|
||||
public static final String KEY = "Plugin-Factory_Data_Set_Resolver_Type_Soap"; |
||||
|
||||
@Override |
||||
public String getName() { |
||||
return KEY; |
||||
} |
||||
|
||||
@Override |
||||
public String getDefaultConfig() { |
||||
StringBuilder sb = new StringBuilder(); |
||||
sb.append("{\r\n") |
||||
.append(" tablepath:\"OT_RETURN\",\r\n")//表格节点 diffgram.Airlines
|
||||
.append(" numkeys:[],\r\n")//数字列
|
||||
.append(" transarray:[],\r\n")//转义组
|
||||
.append(" customResolver:\"\",\r\n")//自定义解析器,没有就不传
|
||||
.append("}\r\n"); |
||||
return sb.toString(); |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue