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.BindingOperation;
22  import javax.wsdl.Definition;
23  import javax.wsdl.Message;
24  import javax.wsdl.Part;
25  import javax.wsdl.Port;
26  import javax.wsdl.Service;
27  import javax.wsdl.extensions.ExtensibilityElement;
28  import javax.wsdl.extensions.soap.SOAPAddress;
29  import javax.wsdl.extensions.soap.SOAPBinding;
30  import javax.wsdl.extensions.soap.SOAPBody;
31  import javax.wsdl.extensions.soap.SOAPFault;
32  import javax.wsdl.extensions.soap.SOAPOperation;
33  import javax.xml.namespace.QName;
34  
35  import org.apache.log4j.Logger;
36  
37  /***
38   * Wsdl-related tools
39   * 
40   * @author Ole.Matzura
41   */
42  
43  public class WsdlUtils
44  {
45     private final static Logger log = Logger.getLogger( WsdlUtils.class );
46     
47     public static ExtensibilityElement getExtensiblityElement(List list, Class clazz )
48     {
49        ExtensibilityElement[] elements = getExtensiblityElements( list, clazz );
50        return elements.length > 0 ? elements[0] : null;
51     }
52     
53     public static ExtensibilityElement [] getExtensiblityElements(List list, Class clazz )
54     {
55        List<ExtensibilityElement> result = new ArrayList<ExtensibilityElement>();
56        
57        for( Iterator<ExtensibilityElement> i = list.iterator(); i.hasNext(); )
58        {
59           ExtensibilityElement elm = (ExtensibilityElement) i.next();
60           if( clazz.isAssignableFrom( elm.getClass() ) )
61           {
62              result.add( elm );
63           }
64        }
65  
66        return result.toArray( new ExtensibilityElement[result.size()] );
67     }
68     
69     public static String getSoapAction(BindingOperation operation)
70     {
71        List list = operation.getExtensibilityElements();
72        SOAPOperation soapOperation = (SOAPOperation) WsdlUtils.getExtensiblityElement( list, SOAPOperation.class );
73  
74        return soapOperation == null ? null : soapOperation.getSoapActionURI();
75     }
76     
77     public static String[] getEndpointsForBinding(Definition definition, Binding binding)
78     {
79        List<String> result = new ArrayList<String>(); 
80        Map map = definition.getServices();
81        for( Iterator i = map.values().iterator(); i.hasNext(); )
82        {
83           Service service = (Service) i.next();
84           Map portMap = service.getPorts();
85           for( Iterator i2 = portMap.values().iterator(); i2.hasNext(); )
86           {
87              Port port = (Port) i2.next();
88              if( port.getBinding() == binding )
89              {
90                 SOAPAddress soapAddress = (SOAPAddress) WsdlUtils.getExtensiblityElement( port.getExtensibilityElements(), SOAPAddress.class );
91                 result.add( soapAddress.getLocationURI() );
92              }
93           }
94        }
95        
96        return result.toArray( new String[result.size()]);
97     }
98     
99     public static Binding findBindingForOperation(Definition definition, BindingOperation bindingOperation)
100    {
101       Map services = definition.getServices();
102       Iterator<Service> s = services.values().iterator();
103       
104       while( s.hasNext())
105       {
106          Map ports = s.next().getPorts();
107          Iterator<Port> p = ports.values().iterator();
108          while( p.hasNext())
109          {
110             Binding binding = p.next().getBinding();
111             List bindingOperations = binding.getBindingOperations();
112             for (Iterator iter = bindingOperations.iterator(); iter.hasNext();)
113             {
114                BindingOperation op = (BindingOperation) iter.next();
115                if( op.getName().equals( bindingOperation.getName() ))
116                   return binding;
117             }
118          }
119       }
120       
121       Map bindings = definition.getBindings();
122       Iterator<QName> names = bindings.keySet().iterator();
123       while( names.hasNext() ) 
124       {
125       	Binding binding = definition.getBinding( names.next() );
126       	List bindingOperations = binding.getBindingOperations();
127          for (Iterator iter = bindingOperations.iterator(); iter.hasNext();)
128          {
129             BindingOperation op = (BindingOperation) iter.next();
130             if( op.getName().equals( bindingOperation.getName() ))
131                return binding;
132          }
133       }
134       
135       return null;
136    }
137    
138    public static boolean isInputSoapEncoded(BindingOperation bindingOperation)
139    {
140       SOAPBody body = (SOAPBody) WsdlUtils.getExtensiblityElement(bindingOperation.getBindingInput()
141             .getExtensibilityElements(), SOAPBody.class);
142       
143       return body != null && body.getUse() != null && body.getUse().equalsIgnoreCase( "encoded" ) &&
144            body.getEncodingStyles().contains( "http://schemas.xmlsoap.org/soap/encoding/" );
145    }
146    
147    public static boolean isOutputSoapEncoded(BindingOperation bindingOperation)
148    {
149       SOAPBody body = (SOAPBody) WsdlUtils.getExtensiblityElement(bindingOperation.getBindingOutput()
150             .getExtensibilityElements(), SOAPBody.class);
151       
152       return body != null && body.getUse() != null && body.getUse().equalsIgnoreCase( "encoded" ) &&
153            body.getEncodingStyles().contains( "http://schemas.xmlsoap.org/soap/encoding/" );
154    }
155    
156    public static boolean isRpc(Definition definition, BindingOperation bindingOperation)
157    {
158       SOAPOperation soapOperation = (SOAPOperation) WsdlUtils.getExtensiblityElement( 
159             bindingOperation.getExtensibilityElements(), SOAPOperation.class );
160       
161       if( soapOperation != null && soapOperation.getStyle() != null )
162          return soapOperation.getStyle().equalsIgnoreCase("rpc");
163       
164       Binding binding = findBindingForOperation( definition, bindingOperation );
165       if( binding == null ) 
166       {
167          log.error( "Failed to find binding for operation [" + bindingOperation.getName() + "] in definition [" + 
168                definition.getDocumentBaseURI() + "]" );
169          return false;
170       }
171       
172       SOAPBinding soapBinding = (SOAPBinding) WsdlUtils.getExtensiblityElement( 
173             binding.getExtensibilityElements(), SOAPBinding.class );
174       
175       return soapBinding != null && soapBinding.getStyle() != null && soapBinding.getStyle().equalsIgnoreCase( "rpc" );
176    }
177    
178    public static Part[] getInputParts(BindingOperation operation)
179    {
180       List<Part> result = new ArrayList<Part>();
181       Message msg = operation.getOperation().getInput().getMessage();
182       SOAPBody body = (SOAPBody) WsdlUtils.getExtensiblityElement(operation.getBindingInput()
183             .getExtensibilityElements(), SOAPBody.class);
184 
185       if (body == null || body.getParts() == null)
186       {
187          result.addAll( msg.getOrderedParts( null ));
188       }
189       else
190       {
191          Iterator i = body.getParts().iterator();
192          while (i.hasNext())
193          {
194             String partName = (String) i.next();
195             Part part = msg.getPart( partName );
196             
197             result.add(part);
198          }
199       }
200 
201       return result.toArray(new Part[result.size()]);
202    }
203    
204    public static Part[] getFaultParts(BindingOperation bindingOperation, String faultName)
205    {
206       List<Part> result = new ArrayList<Part>();
207       
208       SOAPFault fault = (SOAPFault) WsdlUtils.getExtensiblityElement(bindingOperation.getBindingFault( faultName )
209             .getExtensibilityElements(), SOAPFault.class);
210 
211       if (fault != null )
212       {
213          result.addAll( bindingOperation.getOperation().getFault( fault.getName() ).getMessage().getOrderedParts( null ));
214       }
215       
216       return result.toArray(new Part[result.size()]);
217    }
218    
219    public static Part[] getOutputParts(BindingOperation operation)
220    {
221       List<Part> result = new ArrayList<Part>();
222       Message msg = operation.getOperation().getOutput().getMessage();
223       SOAPBody body = (SOAPBody) WsdlUtils.getExtensiblityElement(operation.getBindingOutput()
224             .getExtensibilityElements(), SOAPBody.class);
225 
226       if (body == null || body.getParts() == null)
227       {
228          result.addAll( msg.getOrderedParts( null ));
229       }
230       else
231       {
232          Iterator i = body.getParts().iterator();
233          while (i.hasNext())
234          {
235             String partName = (String) i.next();
236             Part part = msg.getPart( partName );
237             
238             result.add(part);
239          }
240       }
241 
242       return result.toArray(new Part[result.size()]);
243    }
244 }