This project has retired. For details please refer to its
        
        Attic page.
      
 
WSDLLocatorImpl xref
1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  package org.apache.juddi.v3.client.mapping.wsdl;
19  
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.io.StringReader;
23  import java.net.URI;
24  import java.net.URL;
25  
26  import javax.wsdl.xml.WSDLLocator;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.http.HttpResponse;
31  import org.apache.http.auth.AuthScope;
32  import org.apache.http.auth.UsernamePasswordCredentials;
33  import org.apache.http.client.ResponseHandler;
34  import org.apache.http.client.methods.HttpGet;
35  import org.apache.http.conn.ClientConnectionManager;
36  import org.apache.http.conn.scheme.Scheme;
37  import org.apache.http.conn.scheme.SchemeRegistry;
38  import org.apache.http.impl.client.BasicResponseHandler;
39  import org.apache.http.impl.client.DefaultHttpClient;
40  import org.apache.http.impl.conn.BasicClientConnectionManager;
41  import org.apache.juddi.v3.client.mapping.MockSSLSocketFactory;
42  import org.xml.sax.InputSource;
43  
44  
45  
46  
47  
48  
49  
50  public class WSDLLocatorImpl implements WSDLLocator {
51      private Exception lastException=null;
52      private final Log log = LogFactory.getLog(this.getClass());
53      private InputStream inputStream = null;
54      private URI baseURI;
55      private boolean ignoreSSLErrors = false;
56      private String latestImportURI;
57      private String username = null, password = null;
58  
59      
60  
61  
62  
63  
64  
65      public WSDLLocatorImpl(URI baseURI) {
66          this.baseURI = baseURI;
67          this.ignoreSSLErrors = false;
68      }
69  
70      
71  
72  
73  
74  
75  
76  
77  
78  
79  
80      public WSDLLocatorImpl(URI baseURI, String username, String password, boolean ignoreSSLErrors) {
81          this.baseURI = baseURI;
82          this.username = username;
83          this.password = password;
84          this.ignoreSSLErrors = ignoreSSLErrors;
85      }
86  
87      
88  
89  
90      public InputSource getBaseInputSource() {
91          return getImportFromUrl(baseURI.toString());
92      }
93  
94      
95  
96  
97  
98  
99  
100 
101 
102     protected URL constructImportUrl(String parentLocation, String importLocation) {
103         URL importUrl = null;
104         try {
105             URI importLocationURI = new URI(importLocation);
106             if (importLocationURI.getScheme() != null || parentLocation == null) {
107                 importUrl = importLocationURI.toURL();
108             } else {
109                 String parentDir = parentLocation.substring(0, parentLocation.lastIndexOf("/"));
110                 URI uri = new URI(parentDir + "/" + importLocation);
111                 importUrl = uri.normalize().toURL();
112             }
113         } catch (Exception e) {
114             log.error(e.getMessage(), e);
115         }
116         if (importUrl != null) {
117             log.debug("importUrl: " + importUrl.toExternalForm());
118         } else {
119             log.error("importUrl is null!");
120         }
121         return importUrl;
122     }
123 
124     private InputSource getImportFromUrl(String url) {
125         InputSource inputSource = null;
126         DefaultHttpClient httpclient = null;
127         try {
128             URL url2 = new URL(url);
129             if (!url.toLowerCase().startsWith("http")) {
130                 return getImportFromFile(url);
131             }
132             boolean usessl = false;
133             int port = 80;
134             if (url.toLowerCase().startsWith("https://")) {
135                 port = 443;
136                 usessl = true;
137             }
138 
139             if (url2.getPort() > 0) {
140                 port = url2.getPort();
141             }
142 
143             if (ignoreSSLErrors && usessl) {
144                 SchemeRegistry schemeRegistry = new SchemeRegistry();
145                 schemeRegistry.register(new Scheme("https", port, new MockSSLSocketFactory()));
146                 ClientConnectionManager cm = new BasicClientConnectionManager(schemeRegistry);
147                 httpclient = new DefaultHttpClient(cm);
148             } else {
149                 httpclient = new DefaultHttpClient();
150             }
151 
152             if (username != null && username.length() > 0
153                     && password != null && password.length() > 0) {
154 
155 
156                 httpclient.getCredentialsProvider().setCredentials(
157                         new AuthScope(url2.getHost(), port),
158                         new UsernamePasswordCredentials(username, password));
159             }
160             HttpGet httpGet = new HttpGet(url);
161             try {
162 
163                 HttpResponse response1 = httpclient.execute(httpGet);
164                 
165                 
166                 
167                 
168                 ResponseHandler<String> responseHandler = new BasicResponseHandler();
169                 String handleResponse = responseHandler.handleResponse(response1);
170                 StringReader sr = new StringReader(handleResponse);
171                 inputSource = new InputSource(sr);
172 
173 
174             } finally {
175                 httpGet.releaseConnection();
176 
177             }
178 
179             
180             
181             latestImportURI = url;
182         } catch (Exception e) {
183             log.error(e.getMessage());
184             log.debug(e.getMessage(), e);
185             lastException = e;
186         } finally {
187             if (httpclient != null) {
188                 httpclient.getConnectionManager().shutdown();
189             }
190         }
191         return inputSource;
192     }
193 
194    
195 
196 
197 
198 
199 
200     public InputSource getImportInputSource(String parentLocation, String importLocation) {
201         InputSource inputSource = null;
202         try {
203             URL importUrl = constructImportUrl(parentLocation, importLocation);
204             return getImportFromUrl(importUrl.toExternalForm());
205         } catch (Exception e) {
206             log.error(e.getMessage(), e);
207             lastException=e;
208         }
209         return inputSource;
210     }
211 
212     
213 
214 
215 
216     public String getBaseURI() {
217         String baseURIStr = null;
218         try {
219             baseURIStr = baseURI.toURL().toExternalForm();
220         } catch (IOException e) {
221             log.error(e.getMessage());
222             log.debug(e.getMessage(), e);
223             lastException=e;
224         }
225         return baseURIStr;
226     }
227 
228     
229 
230 
231 
232     public String getLatestImportURI() {
233         return latestImportURI;
234     }
235 
236     
237 
238 
239     public void close() {
240         if (inputStream != null) {
241             try {
242                 inputStream.close();
243             } catch (IOException e) {
244                 log.error(e.getMessage(), e);
245                 lastException=e;
246             }
247         }
248     }
249 
250     private InputSource getImportFromFile(String url) {
251         InputSource inputSource = null;
252         try {
253             URL importUrl = new URL(url);
254             inputStream = importUrl.openStream();
255             inputSource = new InputSource(inputStream);
256             latestImportURI = importUrl.toExternalForm();
257         } catch (Exception e) {
258             log.error(e.getMessage());
259             log.debug(e.getMessage(), e);
260             lastException=e;
261         }
262         return inputSource;
263     }
264     
265     
266 
267 
268 
269     public Exception getLastException()
270     {
271         return lastException;
272     }
273 }