View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
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;
14  
15  import javax.xml.namespace.QName;
16  
17  import org.apache.log4j.Logger;
18  
19  import com.eviware.soapui.SoapUI;
20  import com.eviware.soapui.config.InterfaceConfig;
21  import com.eviware.soapui.config.WsdlInterfaceConfig;
22  import com.eviware.soapui.impl.wsdl.WsdlInterface;
23  import com.eviware.soapui.impl.wsdl.WsdlOperation;
24  import com.eviware.soapui.impl.wsdl.WsdlProject;
25  import com.eviware.soapui.impl.wsdl.WsdlRequest;
26  import com.eviware.soapui.impl.wsdl.support.wsdl.WsdlImporter;
27  import com.eviware.soapui.impl.wsdl.support.wsdl.WsdlLoader;
28  import com.eviware.soapui.support.SoapUIException;
29  
30  public class WsdlInterfaceFactory implements InterfaceFactory<WsdlInterface>
31  {
32  	public final static String WSDL_TYPE = "wsdl";
33  	private final static Logger log = Logger.getLogger( WsdlInterfaceFactory.class );
34  	
35  	public WsdlInterface build(WsdlProject project, InterfaceConfig config)
36  	{
37  		return new WsdlInterface( project, (WsdlInterfaceConfig) config.changeType(WsdlInterfaceConfig.type));
38  	}
39  
40  	public WsdlInterface createNew(WsdlProject project, String name)
41  	{
42  		WsdlInterface iface = new WsdlInterface( project, (WsdlInterfaceConfig) project.getConfig().addNewInterface().changeType(WsdlInterfaceConfig.type));
43        iface.setName( name );
44  		
45  		return iface;
46  	}
47  	
48  	public static WsdlInterface [] importWsdl( WsdlProject project, String url, boolean createRequests ) throws SoapUIException
49  	{
50  		return importWsdl( project, url, createRequests, null, null );
51  	}
52     
53  	public static WsdlInterface [] importWsdl( WsdlProject project, String url, boolean createRequests, WsdlLoader wsdlLoader ) throws SoapUIException
54  	{
55  		return importWsdl( project, url, createRequests, null, wsdlLoader );
56  	}
57  	
58     public static WsdlInterface [] importWsdl( WsdlProject project, String url, boolean createRequests, QName bindingName, WsdlLoader wsdlLoader ) throws SoapUIException
59     {
60     	WsdlInterface[] result;
61     	
62        try
63        {
64           result = WsdlImporter.importWsdl( project, url, bindingName, wsdlLoader );
65        }
66        catch (Exception e)
67        {
68           log.error( "Error importing wsdl: " + e );
69           SoapUI.logError( e );
70           throw new SoapUIException( "Error importing wsdl", e );
71        }
72        
73        try
74        {      
75  			if( createRequests && result != null )
76           {
77           	for (WsdlInterface iface : result)
78  				{
79           		for( int c = 0; c < iface.getOperationCount(); c++ )
80           		{
81           			WsdlOperation operation = iface.getOperationAt( c );
82           			WsdlRequest request = operation.addNewRequest( "Request 1");
83                    try
84                    {
85                       String requestContent = operation.createRequest( true );
86  							request.setRequestContent( requestContent);
87                    }
88                    catch (Exception e)
89                    {
90                       SoapUI.logError( e );
91                    }
92           		}
93  				}
94           }
95        }
96        catch (Exception e)
97        {
98        	log.error( "Error creating requests: " + e.getMessage() );
99           throw new SoapUIException( "Error creating requests", e );
100       }
101       
102       return result;
103    }
104 }