2 * Copyright 2016 ZTE Corporation.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.openo.carbon.bpel.util;
18 import java.io.IOException;
19 import java.util.ArrayList;
20 import java.util.List;
22 import java.util.Properties;
24 import javax.naming.ConfigurationException;
25 import javax.xml.parsers.DocumentBuilderFactory;
27 import org.apache.commons.httpclient.HttpClient;
28 import org.apache.log4j.Logger;
30 import com.eviware.soapui.impl.wsdl.WsdlInterface;
31 import com.eviware.soapui.impl.wsdl.WsdlProject;
32 import com.eviware.soapui.impl.wsdl.support.wsdl.WsdlLoader;
33 import com.eviware.soapui.model.iface.Operation;
34 import com.eviware.soapui.model.iface.Request;
36 public class SoapUtil {
37 private static final Logger log = Logger.getLogger(SoapUtil.class);
39 private DocumentBuilderFactory docBuilderFactory;
40 private Map<String, WsdlInterface[]> wsdls = new LRULinkedHashMap<String, WsdlInterface[]>(256);
43 docBuilderFactory = DocumentBuilderFactory.newInstance();
44 docBuilderFactory.setNamespaceAware(true);
47 public Request[] getRequestTemplate(String wsdlUrl)
48 throws UnsupportedOperationException, IOException {
49 Request[] requests = new Request[0];
50 List<Request> requestList = new ArrayList<Request>();
51 Operation operationInst = null;
52 WsdlInterface[] wsdlInterfaces = getWsdlInterfaces(wsdlUrl, null);
53 for (WsdlInterface wsdlInterface : wsdlInterfaces) {
54 Operation opr = wsdlInterface.getOperationAt(0);
57 String requestTemplate = operationInst.getRequestAt(0).getRequestContent();
58 requestList.add(operationInst.getRequestAt(0));
61 requests = requestList.toArray(new Request[0]);
69 * @param httpClientProps
72 * @throws UnsupportedOperationException
74 private Operation getOperation(String wsdl, String operation, Properties httpClientProps)
75 throws IOException, UnsupportedOperationException {
76 WsdlInterface[] wsdlInterfaces = getWsdlInterfaces(wsdl, httpClientProps);
77 for (WsdlInterface wsdlInterface : wsdlInterfaces) {
78 Operation operationInst = wsdlInterface.getOperationByName(operation);
80 if (operationInst != null) {
85 wsdlInterfaces = getWsdlInterfaces(wsdl, httpClientProps);
86 for (WsdlInterface wsdlInterface : wsdlInterfaces) {
87 Operation operationInst = wsdlInterface.getOperationByName(operation);
88 if (operationInst != null) {
93 throw new UnsupportedOperationException(
94 "Operation '" + operation + "' not supported by WSDL '" + wsdl + "'.");
100 * @param httpClientProps
102 * @throws IOException
104 private WsdlInterface[] getWsdlInterfaces(String wsdl, Properties httpClientProps)
107 WsdlInterface[] wsdlInterfaces = wsdls.get(wsdl);
108 if (wsdlInterfaces == null) {
109 WsdlProject wsdlProject = new WsdlProject();
110 WsdlLoader wsdlLoader = createWsdlLoader(wsdl, httpClientProps);
112 wsdlInterfaces = wsdlProject.importWsdl(wsdl, true, wsdlLoader);
114 wsdls.put(wsdl, wsdlInterfaces);
116 return wsdlInterfaces;
117 } catch (Exception e) {
119 log.error(e.getMessage());
120 throw new RuntimeException("Failed to import WSDL '" + wsdl + "'.");
127 * @param httpClientProps
129 * @throws ConfigurationException
131 private WsdlLoader createWsdlLoader(String wsdl, Properties httpClientProps)
132 throws ConfigurationException {
133 HttpClient httpClient = new HttpClient();
134 return new ClientWsdlLoader(wsdl, httpClient);
137 public static void main(String[] args) {
138 SoapUtil soapUtil = new SoapUtil();
141 String url = "http://10.74.151.36:9763/services/initService?wsdl";
142 Request[] requestXMLs = soapUtil.getRequestTemplate(url);
144 for (Request requestXML : requestXMLs) {
145 String requestJson = Xml2JsonUtil.xml2JSON(requestXML.getRequestContent());
146 System.out.println("====================================");
147 System.out.println(requestJson);
150 } catch (UnsupportedOperationException e1) {
151 e1.printStackTrace();
152 } catch (IOException e1) {
153 e1.printStackTrace();