2
13693261870
2022-09-16 653761a31dfeb50dd3d007e892d69c90bf0cdafc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package com.landtool.lanbase.common.utils;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
 
import javax.wsdl.Binding;
import javax.wsdl.Definition;
import javax.wsdl.Import;
import javax.wsdl.Operation;
import javax.wsdl.Part;
import javax.wsdl.Types;
import javax.wsdl.WSDLException;
import javax.wsdl.extensions.ExtensibilityElement;
import javax.wsdl.extensions.schema.Schema;
import javax.wsdl.factory.WSDLFactory;
import javax.wsdl.xml.WSDLReader;
import javax.wsdl.xml.WSDLWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
 
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
 
import com.landtool.lanbase.modules.api.utils.DOMUtil;
import com.landtool.lanbase.modules.res.entity.WebService.ParameterInfo;
 
 
/**
 * 第一步,使用wsdl4j解析wsdl,wsdl文档结构推荐参考http://blog.csdn.net/wudouguerwxx/article/details/2036821
 *
 * @author wangleai
 */
public class WAWsdlUtil {
 
    private static Logger log = Logger.getLogger(WAWsdlUtil.class);
 
    private static WSDLFactory wsdlFactory;
    private static WSDLReader wsdlReader;
 
    private static DocumentBuilder documentBuilder;
 
    private static XPath xPath;
 
    /**
     * @return
     * @throws WSDLException
     */
    private static WSDLFactory getWsdlFactory() throws WSDLException {
        if (wsdlFactory == null) {
            wsdlFactory = WSDLFactory.newInstance();
        }
        return wsdlFactory;
    }
 
    /**
     * @return
     * @throws WSDLException
     */
    private static WSDLReader getWsdlReader() throws WSDLException {
        if (wsdlReader == null) {
            wsdlReader = getWsdlFactory().newWSDLReader();
            wsdlReader.setFeature("javax.wsdl.verbose", true);
            wsdlReader.setFeature("javax.wsdl.importDocuments", true);
        }
        return wsdlReader;
    }
 
    /**
     * @return
     * @throws ParserConfigurationException
     */
    public static DocumentBuilder getDBBuilder() throws ParserConfigurationException {
        if (documentBuilder == null) {
            // 得到DOM解析器的工厂实例
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            // 从DOM工厂中获得DOM解析器
            documentBuilder = dbFactory.newDocumentBuilder();
        }
        return documentBuilder;
    }
 
    /**
     * @param wsdlUrl
     * @return
     * @throws WSDLException
     */
    public static Document getDefinitionDocument(String wsdlUrl) throws WSDLException {
        // 获取wsdl定义
        Definition def = getWsdlReader().readWSDL(wsdlUrl);
        // 转文档流
        WSDLWriter writer = getWsdlFactory().newWSDLWriter();
        Document document = writer.getDocument(def);
        return document;
    }
 
    /**
     * 得到document的查找工具xpath,不支持命名空间
     *
     * @return
     */
    public static XPath getXPath() {
        if (xPath == null) {
            xPath = XPathFactory.newInstance().newXPath();
        }
        return xPath;
    }
 
    /**
     * 得到wsdl文档中方法名
     *
     * @param wsdlUrl       wsdl地址
     * @param operationList 方法集合
     * @throws WSDLException
     */
    public static void getOperationList(String wsdlUrl, List<String> operationList) throws WSDLException {
        // 获取wsdl定义
        Definition def = getWsdlReader().readWSDL(wsdlUrl);
 
        // 遍历bindings
        Map bindings = def.getBindings();
        Iterator<Map.Entry> iterator = bindings.entrySet().iterator();
        while (iterator.hasNext()) {
            Binding binding = (Binding) iterator.next().getValue();
            if (binding != null) {
                List extEles = binding.getExtensibilityElements();
                if (extEles != null && extEles.size() > 0) {
                    ExtensibilityElement extensibilityElement = (ExtensibilityElement) extEles.get(0);
                    if (extensibilityElement != null) {
                        String namespaceUri = extensibilityElement.getElementType().getNamespaceURI();
                        // 默认使用soap1.1的binding,与soapui调用一致
                        if (WAIXPathConstant.SOAPBINDING11.equals(namespaceUri)
                                || WAIXPathConstant.SOAPBINDING12.equals(namespaceUri)) {
                            List<Operation> operations = binding.getPortType().getOperations();
                            for (Operation operation : operations) {
                                operationList.add(operation.getName());
                            }
                            break;
                        }
                    }
                }
            }
        }
    }
 
    /**
     * 根据操作名称查询具体对象 有点蛋疼,wsdl4j不存在直接寻找operation的api
     *
     * @param wsdlUrl
     * @param operationName
     * @return
     * @throws WSDLException
     */
    public static Operation getOperationByName(String wsdlUrl, String operationName) throws WSDLException {
        Operation targetOp = null;
        // 获取wsdl定义
        Definition def = getWsdlReader().readWSDL(wsdlUrl);
 
        // 遍历bindings
        Map bindings = def.getBindings();
        Iterator<Map.Entry> iterator = bindings.entrySet().iterator();
        while (iterator.hasNext()) {
            Binding binding = (Binding) iterator.next().getValue();
            if (binding != null) {
                List extEles = binding.getExtensibilityElements();
                if (extEles != null && extEles.size() > 0) {
                    ExtensibilityElement extensibilityElement = (ExtensibilityElement) extEles.get(0);
                    if (extensibilityElement != null) {
                        String namespaceUri = extensibilityElement.getElementType().getNamespaceURI();
                        // 默认使用soap1.1的binding,与soapui调用一致
                        if (WAIXPathConstant.SOAPBINDING11.equals(namespaceUri)) {
                            List<Operation> operations = binding.getPortType().getOperations();
                            for (Operation operation : operations) {
                                if (operation.getName().equals(operationName)) {
                                    targetOp = operation;
                                    break;
                                }
                            }
                            break;
                        }
                        if (WAIXPathConstant.SOAPBINDING12.equals(namespaceUri)) {
                            List<Operation> operations = binding.getPortType().getOperations();
                            for (Operation operation : operations) {
                                if (operation.getName().equals(operationName)) {
                                    targetOp = operation;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
        return targetOp;
    }
 
    /**
     * 获取对应方法的详细参数名及类型
     *
     * @param wsdlUrl
     * @param operationName 操作名
     * @return
     * @throws WSDLException
     */
    public static List<ParameterInfo> getMethodParams(String wsdlUrl, String operationName) throws WSDLException {
        Operation operation = getOperationByName(wsdlUrl, operationName);
        List<ParameterInfo> parameterInfoList = null;
        if (operation == null) {
            log.error("can not find operation " + operationName + " , please check again");
            throw new RuntimeException("can not find operation " + operationName + " , please check again");
        } else {
            // 输入
            Map inputParts = operation.getInput().getMessage().getParts();
            parameterInfoList = findParamsByOperation(wsdlUrl, inputParts);
        }
        return parameterInfoList;
    }
 
    /**
     * 获取对应方法的返回类型
     *
     * @param wsdlUrl
     * @param operationName 操作名
     * @return
     * @throws WSDLException
     */
    public static List<ParameterInfo> getMethodReturn(String wsdlUrl, String operationName) throws WSDLException {
        // 我更希望返回值限定为json串,会方便很多
        Operation operation = getOperationByName(wsdlUrl, operationName);
        List<ParameterInfo> parameterInfoList = null;
        if (operation == null) {
            log.error("can not find operation " + operationName + " , please check again");
            throw new RuntimeException("can not find operation " + operationName + " , please check again");
        } else {
            // 输出
            Map outputParts = operation.getOutput().getMessage().getParts();
            parameterInfoList = findParamsByOperation(wsdlUrl, outputParts);
        }
        return parameterInfoList;
    }
 
    /**
     * 获取wsdl数据类型定义容器Types中XML Schema前缀,用于拼接形成完整标签 目前不需要使用,因为对XML Schema均忽略命名空间
     *
     * @param wsdlUrl
     * @return
     * @throws WSDLException
     */
    @Deprecated
    public static String getSchemaPrefix(String wsdlUrl) throws WSDLException {
        String prefix = "";
        // 获取wsdl定义
        Definition def = getWsdlReader().readWSDL(wsdlUrl);
 
        // 获取Types XML Schema文档定义
        Types types = def.getTypes();
 
        // 获取标签前缀定义
        ExtensibilityElement extensibilityElement = (ExtensibilityElement) types.getExtensibilityElements().get(0);
        Schema schema = (Schema) extensibilityElement;
        prefix = schema.getElement().getPrefix();
        if (!StringUtils.isBlank(prefix)) {
            // 前缀非空时加入:
            prefix += ":";
        } else {
            prefix = "";
        }
        return prefix;
    }
 
    /**
     * 查找所有的import结点
     *
     * @param document
     * @param xpath
     * @return
     * @throws Exception
     */
    public static List<Document> getImportDocumentList(Document document, XPath xpath) throws Exception {
        List<Document> importDocumentList = new ArrayList<Document>();
        // 查找def中所有的import
        NodeList importNodeList = (NodeList) xpath.evaluate(WAIXPathConstant.IMPORTXPATH, document,
                XPathConstants.NODESET);
        if (importNodeList != null) {
            for (int i = 0; i < importNodeList.getLength(); i++) {
                Node importNode = importNodeList.item(i);
                String location = DOMUtil.getAttributeValue(importNode, "location");
                // 把要解析的xml文档读入DOM解析器
                if (!StringUtils.isBlank(location)) {
                    Document importDocument = getDefinitionDocument(location);
                    importDocumentList.add(importDocument);
                }
            }
        }
 
        // 查找schema中所有的import
        NodeList importSchemaNodeList = (NodeList) xpath.evaluate(WAIXPathConstant.SCHEMAIMPORTPATH, document,
                XPathConstants.NODESET);
        if (importSchemaNodeList != null) {
            for (int i = 0; i < importSchemaNodeList.getLength(); i++) {
                Node importNode = importSchemaNodeList.item(i);
                String location = DOMUtil.getAttributeValue(importNode, "schemaLocation");
                // 把要解析的xml文档读入DOM解析器
                if (!StringUtils.isBlank(location)) {
                    Document importDocument = getDBBuilder().parse(location);
                    importDocumentList.add(importDocument);
                }
            }
        }
        return importDocumentList;
    }
 
    /**
     * 遍历import 目前不使用
     *
     * @param definition
     * @param operationList
     * @throws WSDLException
     */
    private static void findImport(Definition definition, List<String> operationList) throws WSDLException {
        Map imports = definition.getImports();
        Iterator<Map.Entry> iterator = imports.entrySet().iterator();
        while (iterator.hasNext()) {
            Import anImport = (Import) iterator.next().getValue();
            log.info("import nameSpace:" + anImport.getNamespaceURI() + ",location:" + anImport.getLocationURI());
            // 递归
            getOperationList(anImport.getLocationURI(), operationList);
        }
    }
 
    /**
     * 遍历part寻找对应方法参数
     *
     * @param wsdlUrl
     * @param parts
     * @return
     * @throws WSDLException
     */
    private static List<ParameterInfo> findParamsByOperation(String wsdlUrl, Map parts) throws WSDLException {
        Document document = getDefinitionDocument(wsdlUrl);
        // 参数list
        List<ParameterInfo> paramsList = new ArrayList<ParameterInfo>();
        String schemaXPath = WAIXPathConstant.SCHEMAXPATH;
 
        Iterator<Map.Entry> entryIterator = parts.entrySet().iterator();
        while (entryIterator.hasNext()) {
            Part part = (Part) entryIterator.next().getValue();
            // RPC样式,取type,此时请求并非soap协议,不使用
            if (part.getTypeName() != null) {
                String typeName = part.getTypeName().getLocalPart();
            }
            // 文档样式,取element
            if (part.getElementName() != null) {
                String typeName = part.getElementName().getLocalPart();
                try {
                    WADOMUtil.getInputParams(paramsList, document, typeName, schemaXPath, null, true);
                } catch (Exception e) {
                    log.error(e.getMessage());
                    e.printStackTrace();
                }
            }
        }
        return paramsList;
    }
 
    /**
     * 测试XPath是否存在结点,一般自用
     *
     * @param wsdlUrl
     * @param path
     * @throws ParserConfigurationException
     * @throws XPathExpressionException
     * @throws IOException
     * @throws SAXException
     * @throws WSDLException
     */
    @Deprecated
    public static void testXpath(String wsdlUrl, String path)
            throws ParserConfigurationException, XPathExpressionException, IOException, SAXException, WSDLException {
        Document document = getDefinitionDocument(wsdlUrl);
        XPath xpath = XPathFactory.newInstance().newXPath();
        Node node = (Node) xpath.evaluate(path, document, XPathConstants.NODE);
        if (node != null) {
            System.out.println("ok");
        }
    }
}