Add wso2bpel-ext code
[vfc/nfvo/wfengine.git] / wso2bpel-ext / wso2bpel-core / wso2bpel-mgr / src / main / java / org / openo / carbon / bpel / util / ClientWsdlLoader.java
1 /**
2  * Copyright 2016 [ZTE] and others.
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.ByteArrayInputStream;
19 import java.io.InputStream;
20 import java.net.URL;
21 import org.apache.log4j.Logger;
22
23 import org.apache.commons.httpclient.HttpClient;
24 import org.apache.commons.httpclient.HttpException;
25 import org.apache.commons.httpclient.HttpStatus;
26 import org.apache.commons.httpclient.methods.GetMethod;
27
28 import com.eviware.soapui.impl.wsdl.support.wsdl.WsdlLoader;
29
30 public class ClientWsdlLoader extends WsdlLoader {
31   private static Logger logger = Logger.getLogger(ClientWsdlLoader.class);
32
33   private boolean isAborted = false;
34   private HttpClient httpClient;
35
36   public ClientWsdlLoader(String url, HttpClient httpClient) {
37     super(url);
38     this.httpClient = httpClient;
39   }
40
41   public InputStream load(String url) throws Exception {
42     GetMethod httpGetMethod;
43
44     if (url.startsWith("file")) {
45       return new URL(url).openStream();
46     }
47
48     // Authentication is not being overridden on the method. It needs
49     // to be present on the supplied HttpClient instance!
50     httpGetMethod = new GetMethod(url);
51     httpGetMethod.setDoAuthentication(true);
52
53     try {
54       int result = httpClient.executeMethod(httpGetMethod);
55
56       if (result != HttpStatus.SC_OK) {
57         if (result < 200 || result > 299) {
58           throw new HttpException(
59               "Received status code '" + result + "' on WSDL HTTP (GET) request: '" + url + "'.");
60         } else {
61           logger.warn(
62               "Received status code '" + result + "' on WSDL HTTP (GET) request: '" + url + "'.");
63         }
64       }
65
66       return new ByteArrayInputStream(httpGetMethod.getResponseBody());
67     } finally {
68       httpGetMethod.releaseConnection();
69     }
70   }
71
72   public boolean abort() {
73     isAborted = true;
74     return true;
75   }
76
77   public boolean isAborted() {
78     return isAborted;
79   }
80
81   public void close() {}
82 }