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  package com.eviware.soapui.support.resolver;
13  
14  import java.io.File;
15  
16  import javax.swing.JOptionPane;
17  
18  import com.eviware.soapui.impl.WsdlInterfaceFactory;
19  import com.eviware.soapui.impl.rest.RestService;
20  import com.eviware.soapui.impl.rest.RestServiceFactory;
21  import com.eviware.soapui.impl.rest.support.WadlImporter;
22  import com.eviware.soapui.impl.wsdl.WsdlProject;
23  import com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStepInterface;
24  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep;
25  import com.eviware.soapui.model.iface.Interface;
26  import com.eviware.soapui.model.propertyexpansion.resolvers.providers.ProjectDirProvider;
27  import com.eviware.soapui.support.UISupport;
28  import com.eviware.soapui.support.resolver.ResolveContext.Resolver;
29  
30  public abstract class ImportInterfaceResolver implements Resolver
31  {
32  	private boolean resolved = false;
33  	private WsdlTestStep item;
34  
35  	public ImportInterfaceResolver( WsdlTestStep item )
36  	{
37  		this.item = item;
38  	}
39  
40  	public String getResolvedPath()
41  	{
42  		return "";
43  	}
44  
45  	public boolean isResolved()
46  	{
47  		return resolved;
48  	}
49  
50  	public boolean resolve()
51  	{
52  		String[] options = { "File(Wsdl)", "Url(Wsdl)", "File(Wadl)", "Url(Wadl)", "Cancel" };
53  		int choosed = JOptionPane
54  				.showOptionDialog( UISupport.getMainFrame(), "Choose source for new interface from ...",
55  						"New interface source", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null,
56  						options, null );
57  		switch( choosed )
58  		{
59  		case 0 :
60  			loadWsdlFromFile();
61  			resolved = update();
62  			break;
63  		case 1 :
64  			loadWsdlFromUrl();
65  			resolved = update();
66  			break;
67  		case 2 :
68  			loadWadlFromFile();
69  			resolved = update();
70  			break;
71  		case 3 :
72  			loadWadlFromUrl();
73  			resolved = update();
74  			break;
75  		default :
76  			resolved = false;
77  			break;
78  		}
79  
80  		return resolved;
81  	}
82  
83  	private void loadWadlFromUrl()
84  	{
85  		WsdlProject project = item.getTestCase().getTestSuite().getProject();
86  		String url = UISupport.prompt( "Enter WADL URL", "Add WADL from URL", "" );
87  		if( url == null )
88  			return;
89  
90  		importWadl( project, url );
91  
92  	}
93  
94  	private void loadWadlFromFile()
95  	{
96  		WsdlProject project = item.getTestCase().getTestSuite().getProject();
97  		File file = UISupport.getFileDialogs().open( this, "Select WADL file", ".wadl", "WADL Files (*.wadl)",
98  				ProjectDirProvider.getProjectFolder( project ) );
99  		if( file == null )
100 			return;
101 
102 		String path = file.getAbsolutePath();
103 		if( path == null )
104 			return;
105 
106 		importWadl( project, "file:/" + path );
107 	}
108 
109 	private void importWadl( WsdlProject project, String path )
110 	{
111 		RestService restService = ( RestService )project.addNewInterface( ( ( RestTestRequestStepInterface )item )
112 				.getRequestStepConfig().getService(), RestServiceFactory.REST_TYPE );
113 		try
114 		{
115 			new WadlImporter( restService ).initFromWadl( path );
116 		}
117 		catch( Exception e )
118 		{
119 			UISupport.showErrorMessage( e );
120 		}
121 	}
122 
123 	protected abstract boolean update();
124 
125 	private void loadWsdlFromUrl()
126 	{
127 		WsdlProject project = item.getTestCase().getTestSuite().getProject();
128 		String url = UISupport.prompt( "Enter WSDL URL", "Add WSDL from URL", "" );
129 		if( url == null )
130 			return;
131 
132 		importWsdl( project, url );
133 	}
134 
135 	private void loadWsdlFromFile()
136 	{
137 
138 		WsdlProject project = item.getTestCase().getTestSuite().getProject();
139 		File file = UISupport.getFileDialogs().open( this, "Select WSDL file", ".wsdl", "WSDL Files (*.wsdl)",
140 				ProjectDirProvider.getProjectFolder( project ) );
141 		if( file == null )
142 			return;
143 
144 		String path = file.getAbsolutePath();
145 		if( path == null )
146 			return;
147 
148 		importWsdl( project, file.getAbsolutePath() );
149 	}
150 
151 	private void importWsdl( WsdlProject project, String file )
152 	{
153 		try
154 		{
155 			Boolean createRequests = UISupport.confirmOrCancel( "Create default requests for all operations",
156 					"Import WSDL" );
157 			if( createRequests == null )
158 				return;
159 
160 			Interface[] ifaces = WsdlInterfaceFactory.importWsdl( project, file, createRequests );
161 			if( ifaces.length > 0 )
162 				UISupport.select( ifaces[0] );
163 		}
164 		catch( Exception ex )
165 		{
166 			UISupport.showErrorMessage( ex.getMessage() + ":" + ex.getCause() );
167 		}
168 	}
169 
170 	public String getDescription()
171 	{
172 		return "Resolve: Import inteface";
173 	}
174 
175 	@Override
176 	public String toString()
177 	{
178 		return getDescription();
179 	}
180 }