View Javadoc
1   /*
2    * Copyright 2001-2009 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    *      http://www.apache.org/licenses/LICENSE-2.0
8    * 
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.apache.juddi.v3.client.mapping.wsdl;
16  
17  import java.net.URI;
18  import java.net.URISyntaxException;
19  import java.net.URL;
20  
21  import javax.wsdl.Definition;
22  import javax.wsdl.WSDLException;
23  import javax.wsdl.factory.WSDLFactory;
24  import javax.wsdl.xml.WSDLLocator;
25  import javax.wsdl.xml.WSDLReader;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.apache.juddi.v3.client.ClassUtil;
30  
31  import com.ibm.wsdl.factory.WSDLFactoryImpl;
32  import java.io.File;
33  import java.net.MalformedURLException;
34  
35  /**
36   * A WSDL parser/reader
37   * @author <a href="mailto:kstam@apache.org">Kurt T Stam</a>
38   * @author <a href="mailto:alexoree@apache.org">Alex O'Ree</a> - Modified for supporting http based credentials
39   */
40  public class ReadWSDL {
41  	
42          private boolean IgnoreSSLErrors = false;
43  	private final Log log = LogFactory.getLog(this.getClass());
44  	
45  	public Definition readWSDL(String fileName) throws WSDLException  {
46                  Definition wsdlDefinition = null;
47                  WSDLFactory factory = WSDLFactoryImpl.newInstance();
48                  WSDLReader reader = factory.newWSDLReader();
49  
50                  try {
51                          File f = new File(fileName);
52                          URL url = null;
53                          if (f.exists()) {
54                                  log.info(fileName + " as a local file doesn't exist.");
55                                  url = f.toURI().toURL();
56                          } else {
57                                  url = ClassUtil.getResource(fileName, this.getClass());
58                          }
59                          if (url==null){
60                                  log.info(fileName + " as a class path resource doesn't exist.");
61                                  throw new WSDLException("null input", fileName);
62                          }
63                          URI uri = url.toURI();
64                          WSDLLocator locator = new WSDLLocatorImpl(uri);
65                          wsdlDefinition = reader.readWSDL(locator);
66                  } catch (URISyntaxException e) {
67                          log.error(e.getMessage(), e);
68                  } catch (MalformedURLException ex) {
69                          log.error(ex.getMessage(), ex);
70                  }
71                  return wsdlDefinition;
72  	}
73  	
74          /**
75           * Reads a WSDL file, assumes that credentials are required. This is useful for when the WSDL document itself
76           * is protected by HTTP based authentication mechanisms
77           * @param wsdlUrl
78           * @param username
79           * @param password
80           * @return a Definition object representing the WSDL
81           * @throws WSDLException 
82           */
83          public Definition readWSDL(URL wsdlUrl, String username, String password) throws WSDLException, Exception {
84  	
85  		Definition wsdlDefinition = null;
86  		WSDLFactory factory = WSDLFactoryImpl.newInstance();
87  		WSDLReader reader = factory.newWSDLReader();
88                  URI uri=null;
89  		try {
90                          uri = wsdlUrl.toURI();
91                  } catch (Exception e) {
92  			log.error(e.getMessage(),e);
93                          throw new WSDLException("Unable to parse the URL", null, e);
94  		}
95  		WSDLLocatorImpl locator = new WSDLLocatorImpl(uri, username, password, IgnoreSSLErrors );
96                  try{
97  			wsdlDefinition = reader.readWSDL(locator);
98  		} catch (Exception e) {
99                          log.error(e.getMessage(),e);
100                         if (locator.getLastException()!=null)
101                         {
102                             log.error(e.getMessage(),locator.getLastException());
103                             throw locator.getLastException();
104                         }
105                         throw e;
106                         //throw new WSDLException("Error loading from " + wsdlUrl.toString(), null, e);
107 		}
108 		return wsdlDefinition;
109 	}
110         
111         /**
112          * Reads a WSDL file, assumes that credentials are not required. This is a convenience wrapper for
113          * readWSDL(URL wsdlUrl, null, null, null)
114          * @param wsdlUrl
115          * @return a Definition object representing the WSDL
116          * @throws WSDLException 
117          */
118 	public Definition readWSDL(URL wsdlUrl) throws Exception {
119 		return readWSDL(wsdlUrl, null, null);
120 	}
121 
122         /**
123          * It is optional to ignore SSL errors when attempting to parse a remote WSDL via https
124          * @return true if we are ignoring SSL errors
125          */
126     public boolean isIgnoreSSLErrors() {
127         return IgnoreSSLErrors;
128     }
129 
130     /**
131      * It is optional to ignore SSL errors when attempting to parse a remote WSDL via https
132      * @param IgnoreSSLErrors 
133      */
134     public void setIgnoreSSLErrors(boolean IgnoreSSLErrors) {
135         this.IgnoreSSLErrors = IgnoreSSLErrors;
136     }
137 	
138 	
139 	
140 	
141 }