View Javadoc

1   /*
2    *  soapui, copyright (C) 2005 Ole Matzura / eviware.com 
3    *
4    *  SoapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
7    *
8    *  SoapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.soapui.impl.wsdl.support;
14  
15  import java.util.ArrayList;
16  import java.util.Iterator;
17  import java.util.List;
18  import java.util.Map;
19  
20  import javax.wsdl.Binding;
21  import javax.wsdl.Port;
22  import javax.wsdl.Service;
23  import javax.wsdl.extensions.soap.SOAPAddress;
24  import javax.wsdl.factory.WSDLFactory;
25  import javax.wsdl.xml.WSDLReader;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  
30  import com.eviware.soapui.SoapUI;
31  import com.eviware.soapui.impl.wsdl.WsdlInterface;
32  import com.eviware.soapui.impl.wsdl.WsdlProject;
33  import com.eviware.soapui.model.iface.Interface;
34  
35  /***
36   * Importer for WsdlInterfaces from WSDL urls / files
37   * 
38   * @author Ole.Matzura
39   */
40  
41  public class WsdlImporter
42  {
43     private final SoapUI soapUI;
44     private WSDLFactory factory;
45     private WSDLReader wsdlReader;
46     private List<BindingImporter> bindingImporters = new ArrayList<BindingImporter>();
47     private static WsdlImporter instance;
48     
49     private final static Log log = LogFactory.getLog( WsdlImporter.class );
50     
51     private WsdlImporter(SoapUI soapUI)
52     {
53        this.soapUI = soapUI;
54  
55        try
56        {
57           factory = WSDLFactory.newInstance();
58           wsdlReader = factory.newWSDLReader();
59           wsdlReader.setFeature("javax.wsdl.verbose", true);
60           wsdlReader.setFeature("javax.wsdl.importDocuments", true);
61           
62           bindingImporters.add( new SoapHttpBindingImporter() );
63        }
64        catch (Exception e)
65        {
66           e.printStackTrace();
67        }
68     }
69  
70     public static WsdlImporter getInstance()
71     {
72        if( instance == null )
73           instance = new WsdlImporter( SoapUI.getInstance() );
74        
75        return instance;
76     }
77     
78     public WsdlInterface [] importWsdl( WsdlProject project, String wsdlUrl ) throws Exception
79     {
80        WsdlContext wsdlContext = new WsdlContext( wsdlUrl );
81        wsdlContext.load();
82        
83        List<Interface> result = new ArrayList<Interface>();
84        
85        Map bindingMap = wsdlContext.getDefinition().getBindings();
86        if( bindingMap.isEmpty() )
87        {
88           Map serviceMap = wsdlContext.getDefinition().getServices();
89           if( serviceMap.isEmpty() )
90              throw new RuntimeException( "Missing bindings and services in [" + wsdlUrl + "]" );
91           
92           Iterator i = serviceMap.values().iterator();
93           while( i.hasNext() )
94           {
95              Service service = (Service) i.next();
96              Map portMap = service.getPorts();
97              Iterator i2 = portMap.values().iterator();
98              while( i2.hasNext() )
99              {
100                Port port = (Port) i2.next();
101                
102                Binding binding = port.getBinding();
103                WsdlInterface iface = importBinding(project, wsdlContext, binding);
104                if( iface != null )
105                {
106 	               SOAPAddress address = (SOAPAddress) WsdlUtils.getExtensiblityElement( port.getExtensibilityElements(), SOAPAddress.class );
107 	               if( address != null )
108 	                  iface.addEndpoint( address.getLocationURI() );
109 	               
110 	               result.add( iface );
111                }
112             }
113          }
114       }
115       else
116       {
117          Iterator i = bindingMap.values().iterator();
118          while( i.hasNext() )
119          {
120             Binding binding = (Binding) i.next();
121             WsdlInterface iface = importBinding(project, wsdlContext, binding);
122             if( iface != null )
123             	result.add( iface );
124          }
125       }
126       
127       return result.toArray( new WsdlInterface[result.size()]);
128    }
129 
130    private WsdlInterface importBinding(WsdlProject project, WsdlContext wsdlContext, Binding binding ) throws Exception
131    {
132       log.info( "Finding importer for " + binding.getQName() );
133       for( int c = 0; c < bindingImporters.size(); c++ )
134       {
135          BindingImporter importer = bindingImporters.get( c );
136          if( importer.canImport( binding ))
137          {
138             log.info( "Importing binding " + binding.getQName() );
139             WsdlInterface iface = importer.importBinding( project, wsdlContext, binding );
140             iface.setDefinition( wsdlContext.getUrl() );
141             return iface;
142          }
143       }
144       log.info( "Missing importer for " + binding.getQName() );
145       
146       return null;
147    }
148    
149   
150   
151 }