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.text.Collator;
16  import java.util.Collections;
17  import java.util.Comparator;
18  import java.util.Iterator;
19  import java.util.List;
20  
21  import javax.wsdl.Binding;
22  import javax.wsdl.BindingOperation;
23  import javax.wsdl.PortType;
24  import javax.wsdl.extensions.soap.SOAPBinding;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  import com.eviware.soapui.impl.wsdl.WsdlInterface;
30  import com.eviware.soapui.impl.wsdl.WsdlOperation;
31  import com.eviware.soapui.impl.wsdl.WsdlProject;
32  
33  /***
34   * BindingImporter that can import a WsdlInterface from an SOAP/HTTP binding
35   * 
36   * @author Ole.Matzura
37   */
38  
39  public class SoapHttpBindingImporter implements BindingImporter
40  {
41     private final static Log log = LogFactory.getLog( SoapHttpBindingImporter.class );
42     
43     public boolean canImport(Binding binding)
44     {
45        List list = binding.getExtensibilityElements();
46        SOAPBinding soapBinding = (SOAPBinding) WsdlUtils.getExtensiblityElement( list, SOAPBinding.class );
47        return soapBinding == null ? false : 
48           soapBinding.getTransportURI().startsWith( "http://schemas.xmlsoap.org/soap/http" );
49     }
50  
51     public WsdlInterface importBinding( WsdlProject project, WsdlContext wsdlContext, Binding binding ) throws Exception
52     {
53        PortType portType = binding.getPortType();
54        
55        WsdlInterface iface = project.addNewInterface(portType.getQName().getLocalPart());
56        iface.setBindingName( binding.getQName() );
57        
58        String [] endpoints = WsdlUtils.getEndpointsForBinding( wsdlContext.getDefinition(), binding );
59        for (int i = 0; i < endpoints.length; i++)
60        {
61           log.info("importing endpoint " + endpoints[i]);
62           iface.addEndpoint( endpoints[i] );
63        }
64        
65        SoapRequestBuilder builder = new SoapRequestBuilder( wsdlContext );
66        iface.setSoapRequestBuilder( builder );
67        
68        List<BindingOperation> list = binding.getBindingOperations();
69        Collections.sort( list, new Comparator<BindingOperation>()
70        {
71           public int compare(BindingOperation o1, BindingOperation o2)
72           {
73              return Collator.getInstance().compare( o1.getOperation().getName(), o2.getOperation().getName() );
74           }
75        });
76        
77        for (Iterator iter = list.iterator(); iter.hasNext();)
78        {
79           BindingOperation operation = (BindingOperation) iter.next();
80           WsdlOperation op = iface.addNewOperation();
81           
82           log.info("importing operation " + operation.getName() );
83           
84           op.setAction( WsdlUtils.getSoapAction( operation ));
85           op.setName( operation.getOperation().getName() );
86           op.setBindingOperationName( operation.getName() );
87        }
88        
89        return iface;
90     }
91  }