View Javadoc
1   /*
2    * Copyright 2001-2011 The Apache Software Foundation.
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   *
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   * Implementation of the interface {@link WSDLLocatorImpl}.
46   *
47   * @author <a href="mailto:kstam@apache.org">Kurt T Stam</a> 
48   * @author <a href="mailto:alexoree@apache.org">Alex O'Ree</a> - Modified for supporting http based credentials 
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       * Constructor taking the URI to the WSDL. This class implements the
61       * {@link WSDLLocator} Interface.
62       *
63       * @param baseURI - URI of the WSDL
64       */
65      public WSDLLocatorImpl(URI baseURI) {
66          this.baseURI = baseURI;
67          this.ignoreSSLErrors = false;
68      }
69  
70      /**
71       * Constructor taking the URI to the WSDL. This class implements the
72       * {@link WSDLLocator} Interface and includes support for HTTP
73       * Authentication
74       *
75       * @param baseURI
76       * @param username
77       * @param password
78       * @param ignoreSSLErrors 
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       * see  {@link #getBaseInputSource getBaseInputSource}  WSDLLocator.getBaseInputSource
89       */
90      public InputSource getBaseInputSource() {
91          return getImportFromUrl(baseURI.toString());
92      }
93  
94      /**
95       * Internal method to normalize the importUrl. The importLocation can be
96       * relative to the parentLocation.
97       *
98       * @param parentLocation
99       * @param importLocation
100      * @return a url
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                 //System.out.println(response1.getStatusLine());
165                 // HttpEntity entity1 = response1.getEntity();
166                 // do something useful with the response body
167                 // and ensure it is fully consumed
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             //  InputStream inputStream = importUrl.openStream();
180             //inputSource = new InputSource(inputStream);
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     * see also {@link #getImportInputSource WSDLLocator.getImportInputSource} 
196     * @param parentLocation
197     * @param importLocation
198     * @return input source
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      * see also {@link WSDLLocator#getBaseURI WSDLLocatorImpl.getBaseURI} 
214      * @return string
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      * {@link WSDLLocator#getLatestImportURI getLatestImportURI}
230      * @return string
231      */
232     public String getLatestImportURI() {
233         return latestImportURI;
234     }
235 
236     /**
237      * {@link WSDLLocator#close close}
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      * Returns the last exception or null if there wasn't any. This was done because the authors of WSDLLocator apparently thought it would always work
267      * @return the last exception or null
268      */
269     public Exception getLastException()
270     {
271         return lastException;
272     }
273 }