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  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.RestTestRequestStep;
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( ((RestTestRequestStep) item).getRequestStepConfig().getService(), RestServiceFactory.REST_TYPE );
112       try
113       {
114          new WadlImporter( restService ).initFromWadl( path );
115       }
116       catch( Exception e )
117       {
118          UISupport.showErrorMessage( e );
119       }
120 	}
121 
122 	protected abstract boolean update();
123 
124 	private void loadWsdlFromUrl()
125 	{
126 		WsdlProject project = item.getTestCase().getTestSuite().getProject();
127 		String url = UISupport.prompt("Enter WSDL URL", "Add WSDL from URL", "");
128 		if (url == null)
129 			return;
130 
131 		importWsdl(project, url);
132 	}
133 
134 	private void loadWsdlFromFile()
135 	{
136 
137 		WsdlProject project = item.getTestCase().getTestSuite().getProject();
138 		File file = UISupport.getFileDialogs().open(this, "Select WSDL file", ".wsdl", "WSDL Files (*.wsdl)",
139 				ProjectDirProvider.getProjectFolder(project));
140 		if (file == null)
141 			return;
142 
143 		String path = file.getAbsolutePath();
144 		if (path == null)
145 			return;
146 
147 		importWsdl(project, file.getAbsolutePath());
148 	}
149 
150 	private void importWsdl(WsdlProject project, String file)
151 	{
152 		try
153 		{
154 			Boolean createRequests = UISupport
155 					.confirmOrCancel("Create default requests for all operations", "Import WSDL");
156 			if (createRequests == null)
157 				return;
158 
159 			Interface[] ifaces = WsdlInterfaceFactory.importWsdl(project, file, createRequests);
160 			if (ifaces.length > 0)
161 				UISupport.select(ifaces[0]);
162 		}
163 		catch (Exception ex)
164 		{
165 			UISupport.showErrorMessage(ex.getMessage() + ":" + ex.getCause());
166 		}
167 	}
168 
169 	public String getDescription()
170 	{
171 		return "Resolve: Import inteface";
172 	}
173 
174 	@Override
175 	public String toString()
176 	{
177 		return getDescription();
178 	}
179 }