View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 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.model.propertyexpansion.DefaultPropertyExpansionContext;
29  import com.eviware.soapui.model.propertyexpansion.PropertyExpander;
30  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionContext;
31  import com.eviware.soapui.support.SoapUIException;
32  
33  public class WsdlInterfaceFactory implements InterfaceFactory<WsdlInterface>
34  {
35  	public final static String WSDL_TYPE = "wsdl";
36  	private final static Logger log = Logger.getLogger( WsdlInterfaceFactory.class );
37  
38  	public WsdlInterface build( WsdlProject project, InterfaceConfig config )
39  	{
40  		return new WsdlInterface( project, ( WsdlInterfaceConfig )config.changeType( WsdlInterfaceConfig.type ) );
41  	}
42  
43  	public WsdlInterface createNew( WsdlProject project, String name )
44  	{
45  		WsdlInterface iface = new WsdlInterface( project, ( WsdlInterfaceConfig )project.getConfig().addNewInterface()
46  				.changeType( WsdlInterfaceConfig.type ) );
47  		iface.setName( name );
48  
49  		return iface;
50  	}
51  
52  	public static WsdlInterface[] importWsdl( WsdlProject project, String url, boolean createRequests )
53  			throws SoapUIException
54  	{
55  		return importWsdl( project, url, createRequests, null, null );
56  	}
57  
58  	public static WsdlInterface[] importWsdl( WsdlProject project, String url, boolean createRequests,
59  			WsdlLoader wsdlLoader ) throws SoapUIException
60  	{
61  		return importWsdl( project, url, createRequests, null, wsdlLoader );
62  	}
63  
64  	public static WsdlInterface[] importWsdl( WsdlProject project, String url, boolean createRequests,
65  			QName bindingName, WsdlLoader wsdlLoader ) throws SoapUIException
66  	{
67  		WsdlInterface[] result;
68  
69  		PropertyExpansionContext context = new DefaultPropertyExpansionContext( project.getModelItem() );
70  		url = PropertyExpander.expandProperties( context, url );
71  		try
72  		{
73  			result = WsdlImporter.importWsdl( project, url, bindingName, wsdlLoader );
74  		}
75  		catch( Exception e )
76  		{
77  			log.error( "Error importing wsdl: " + e );
78  			SoapUI.logError( e );
79  			throw new SoapUIException( "Error importing wsdl", e );
80  		}
81  
82  		try
83  		{
84  			if( createRequests && result != null )
85  			{
86  				for( WsdlInterface iface : result )
87  				{
88  					for( int c = 0; c < iface.getOperationCount(); c++ )
89  					{
90  						WsdlOperation operation = iface.getOperationAt( c );
91  						WsdlRequest request = operation.addNewRequest( "Request 1" );
92  						try
93  						{
94  							String requestContent = operation.createRequest( true );
95  							request.setRequestContent( requestContent );
96  						}
97  						catch( Exception e )
98  						{
99  							SoapUI.logError( e );
100 						}
101 					}
102 				}
103 			}
104 		}
105 		catch( Exception e )
106 		{
107 			log.error( "Error creating requests: " + e.getMessage() );
108 			throw new SoapUIException( "Error creating requests", e );
109 		}
110 
111 		return result;
112 	}
113 }