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  			break;
62  		case 1:
63  			loadWsdlFromUrl();
64  			break;
65  		case 2:
66  			loadWadlFromFile();
67  			break;
68  		case 3:
69  			loadWadlFromUrl();
70  			break;
71  		default:
72  			break;
73  		}
74  		resolved = update();
75  		return resolved;
76  	}
77  
78  	private void loadWadlFromUrl()
79  	{
80  		WsdlProject project = item.getTestCase().getTestSuite().getProject();
81  		String url = UISupport.prompt("Enter WADL URL", "Add WADL from URL", "");
82  		if (url == null)
83  			return;
84  		
85  		importWadl(project, url);
86        
87  	}
88  
89  	private void loadWadlFromFile()
90  	{
91  		WsdlProject project = item.getTestCase().getTestSuite().getProject();
92  		File file = UISupport.getFileDialogs().open(this, "Select WADL file", ".wadl", "WADL Files (*.wadl)",
93  				ProjectDirProvider.getProjectFolder(project));
94  		if (file == null)
95  			return;
96  
97  		String path = file.getAbsolutePath();
98  		if (path == null)
99  			return;
100 		
101 		importWadl(project, "file:/" + path);
102 	}
103 
104 	private void importWadl(WsdlProject project, String path)
105 	{
106 		RestService restService = (RestService) project.addNewInterface( ((RestTestRequestStep) item).getRequestStepConfig().getService(), RestServiceFactory.REST_TYPE );
107       try
108       {
109          new WadlImporter( restService ).initFromWadl( path );
110       }
111       catch( Exception e )
112       {
113          UISupport.showErrorMessage( e );
114       }
115 	}
116 
117 	protected abstract boolean update();
118 
119 	private void loadWsdlFromUrl()
120 	{
121 		WsdlProject project = item.getTestCase().getTestSuite().getProject();
122 		String url = UISupport.prompt("Enter WSDL URL", "Add WSDL from URL", "");
123 		if (url == null)
124 			return;
125 
126 		importWsdl(project, url);
127 	}
128 
129 	private void loadWsdlFromFile()
130 	{
131 
132 		WsdlProject project = item.getTestCase().getTestSuite().getProject();
133 		File file = UISupport.getFileDialogs().open(this, "Select WSDL file", ".wsdl", "WSDL Files (*.wsdl)",
134 				ProjectDirProvider.getProjectFolder(project));
135 		if (file == null)
136 			return;
137 
138 		String path = file.getAbsolutePath();
139 		if (path == null)
140 			return;
141 
142 		importWsdl(project, file.getAbsolutePath());
143 	}
144 
145 	private void importWsdl(WsdlProject project, String file)
146 	{
147 		try
148 		{
149 			Boolean createRequests = UISupport
150 					.confirmOrCancel("Create default requests for all operations", "Import WSDL");
151 			if (createRequests == null)
152 				return;
153 
154 			Interface[] ifaces = WsdlInterfaceFactory.importWsdl(project, file, createRequests);
155 			if (ifaces.length > 0)
156 				UISupport.select(ifaces[0]);
157 		}
158 		catch (Exception ex)
159 		{
160 			UISupport.showErrorMessage(ex.getMessage() + ":" + ex.getCause());
161 		}
162 	}
163 
164 	public String getDescription()
165 	{
166 		return "Resolve: Import inteface";
167 	}
168 
169 	@Override
170 	public String toString()
171 	{
172 		return getDescription();
173 	}
174 }