View Javadoc

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