License header is not correct
[vfc/nfvo/wfengine.git] / wso2bpel-ext / wso2bpel-core / wso2bpel-mgr / src / main / java / org / openo / carbon / bpel / util / SoapUtil.java
1 /**
2  * Copyright 2016 ZTE Corporation.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.openo.carbon.bpel.util;
17
18 import java.io.IOException;
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Properties;
23
24 import javax.naming.ConfigurationException;
25 import javax.xml.parsers.DocumentBuilderFactory;
26
27 import org.apache.commons.httpclient.HttpClient;
28 import org.apache.log4j.Logger;
29
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;
35
36 public class SoapUtil {
37   private static final Logger log = Logger.getLogger(SoapUtil.class);
38
39   private DocumentBuilderFactory docBuilderFactory;
40   private Map<String, WsdlInterface[]> wsdls = new LRULinkedHashMap<String, WsdlInterface[]>(256);
41
42   public SoapUtil() {
43     docBuilderFactory = DocumentBuilderFactory.newInstance();
44     docBuilderFactory.setNamespaceAware(true);
45   }
46
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);
55       if (opr != null) {
56         operationInst = opr;
57         String requestTemplate = operationInst.getRequestAt(0).getRequestContent();
58         requestList.add(operationInst.getRequestAt(0));
59       }
60     }
61     requests = requestList.toArray(new Request[0]);
62     return requests;
63   }
64
65   /**
66    * 
67    * @param wsdl
68    * @param operation
69    * @param httpClientProps
70    * @return
71    * @throws IOException
72    * @throws UnsupportedOperationException
73    */
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);
79
80       if (operationInst != null) {
81         return operationInst;
82       }
83     }
84     wsdls.remove(wsdl);
85     wsdlInterfaces = getWsdlInterfaces(wsdl, httpClientProps);
86     for (WsdlInterface wsdlInterface : wsdlInterfaces) {
87       Operation operationInst = wsdlInterface.getOperationByName(operation);
88       if (operationInst != null) {
89         return operationInst;
90       }
91     }
92
93     throw new UnsupportedOperationException(
94         "Operation '" + operation + "' not supported by WSDL '" + wsdl + "'.");
95   }
96
97   /**
98    * 
99    * @param wsdl
100    * @param httpClientProps
101    * @return
102    * @throws IOException
103    */
104   private WsdlInterface[] getWsdlInterfaces(String wsdl, Properties httpClientProps)
105       throws IOException {
106     try {
107       WsdlInterface[] wsdlInterfaces = wsdls.get(wsdl);
108       if (wsdlInterfaces == null) {
109         WsdlProject wsdlProject = new WsdlProject();
110         WsdlLoader wsdlLoader = createWsdlLoader(wsdl, httpClientProps);
111
112         wsdlInterfaces = wsdlProject.importWsdl(wsdl, true, wsdlLoader);
113
114         wsdls.put(wsdl, wsdlInterfaces);
115       }
116       return wsdlInterfaces;
117     } catch (Exception e) {
118       e.printStackTrace();
119       log.error(e.getMessage());
120       throw new RuntimeException("Failed to import WSDL '" + wsdl + "'.");
121     }
122   }
123
124   /**
125    * 
126    * @param wsdl
127    * @param httpClientProps
128    * @return
129    * @throws ConfigurationException
130    */
131   private WsdlLoader createWsdlLoader(String wsdl, Properties httpClientProps)
132       throws ConfigurationException {
133     HttpClient httpClient = new HttpClient();
134     return new ClientWsdlLoader(wsdl, httpClient);
135   }
136
137   public static void main(String[] args) {
138     SoapUtil soapUtil = new SoapUtil();
139
140     try {
141       String url = "http://10.74.151.36:9763/services/initService?wsdl";
142       Request[] requestXMLs = soapUtil.getRequestTemplate(url);
143
144       for (Request requestXML : requestXMLs) {
145         String requestJson = Xml2JsonUtil.xml2JSON(requestXML.getRequestContent());
146         System.out.println("====================================");
147         System.out.println(requestJson);
148       }
149
150     } catch (UnsupportedOperationException e1) {
151       e1.printStackTrace();
152     } catch (IOException e1) {
153       e1.printStackTrace();
154     }
155   }
156 }