1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.rest.actions.support;
14
15 import java.awt.event.ActionEvent;
16 import java.net.MalformedURLException;
17 import java.net.URL;
18 import java.util.HashMap;
19 import java.util.Map;
20
21 import javax.swing.AbstractAction;
22 import javax.swing.DefaultCellEditor;
23 import javax.swing.JComboBox;
24
25 import com.eviware.soapui.SoapUI;
26 import com.eviware.soapui.config.RestParametersConfig;
27 import com.eviware.soapui.impl.rest.RestMethod;
28 import com.eviware.soapui.impl.rest.RestRequest;
29 import com.eviware.soapui.impl.rest.RestRequestInterface;
30 import com.eviware.soapui.impl.rest.RestResource;
31 import com.eviware.soapui.impl.rest.RestService;
32 import com.eviware.soapui.impl.rest.actions.resource.NewRestMethodAction;
33 import com.eviware.soapui.impl.rest.panels.resource.RestParamsTable;
34 import com.eviware.soapui.impl.rest.panels.resource.RestParamsTableModel;
35 import com.eviware.soapui.impl.rest.support.RestParamProperty;
36 import com.eviware.soapui.impl.rest.support.RestParamsPropertyHolder;
37 import com.eviware.soapui.impl.rest.support.RestUtils;
38 import com.eviware.soapui.impl.rest.support.XmlBeansRestParamsTestPropertyHolder;
39 import com.eviware.soapui.impl.wsdl.support.HelpUrls;
40 import com.eviware.soapui.model.ModelItem;
41 import com.eviware.soapui.support.MessageSupport;
42 import com.eviware.soapui.support.StringUtils;
43 import com.eviware.soapui.support.UISupport;
44 import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
45 import com.eviware.x.form.XFormDialog;
46 import com.eviware.x.form.support.ADialogBuilder;
47 import com.eviware.x.form.support.AField;
48 import com.eviware.x.form.support.AForm;
49 import com.eviware.x.form.support.AField.AFieldType;
50 import com.eviware.x.form.validators.RequiredValidator;
51
52 /***
53 * Actions for importing an existing soapUI project file into the current
54 * workspace
55 *
56 * @author Ole.Matzura
57 */
58
59 public abstract class NewRestResourceActionBase<T extends ModelItem> extends AbstractSoapUIAction<T>
60 {
61 private XFormDialog dialog;
62 private XmlBeansRestParamsTestPropertyHolder params;
63 private InternalRestParamsTable paramsTable;
64 public static final MessageSupport messages = MessageSupport.getMessages( NewRestResourceActionBase.class );
65
66 public NewRestResourceActionBase( String title, String description )
67 {
68 super( title, description );
69 }
70
71 public void perform( T service, Object param )
72 {
73 if( dialog == null )
74 {
75 dialog = ADialogBuilder.buildDialog( Form.class );
76 dialog.getFormField( Form.RESOURCENAME ).addFormFieldValidator( new RequiredValidator() );
77 dialog.getFormField( Form.EXTRACTPARAMS ).setProperty( "action", new ExtractParamsAction() );
78
79 }
80 else
81 {
82 dialog.setValue( Form.RESOURCENAME, "" );
83 dialog.setValue( Form.RESOURCEPATH, "" );
84 }
85
86 params = new XmlBeansRestParamsTestPropertyHolder( null, RestParametersConfig.Factory.newInstance() );
87
88 if( param instanceof URL )
89 {
90 String path = RestUtils.extractParams( param.toString(), params, false );
91 dialog.setValue( Form.RESOURCEPATH, path );
92
93 setNameFromPath( path );
94
95 if( paramsTable != null )
96 paramsTable.refresh();
97 }
98
99 paramsTable = new InternalRestParamsTable( params, ParamLocation.RESOURCE );
100 dialog.getFormField( Form.PARAMSTABLE ).setProperty( "component", paramsTable );
101
102 if( dialog.show() )
103 {
104 String path = dialog.getValue( Form.RESOURCEPATH );
105
106 try
107 {
108 URL url = new URL( path );
109 path = url.getPath();
110 }
111 catch( MalformedURLException e )
112 {
113 }
114
115 RestResource resource = createRestResource( service, path, dialog );
116 paramsTable.extractParams( resource.getParams(), ParamLocation.RESOURCE );
117
118
119
120
121
122
123
124
125
126 XmlBeansRestParamsTestPropertyHolder methodParams = new XmlBeansRestParamsTestPropertyHolder( null,
127 RestParametersConfig.Factory.newInstance() );
128 paramsTable.extractParams( methodParams, ParamLocation.METHOD );
129 SoapUI.getActionRegistry().getAction( NewRestMethodAction.SOAPUI_ACTION_ID ).perform( resource, methodParams );
130 }
131
132 paramsTable.release();
133 paramsTable = null;
134 params = null;
135 dialog.getFormField( Form.PARAMSTABLE ).setProperty( "component", paramsTable );
136 }
137
138 protected abstract RestResource createRestResource( T service, String path, XFormDialog dialog );
139
140 protected abstract RestMethod createRestMethod( RestResource resource, XFormDialog dialog );
141
142 private void setNameFromPath( String path )
143 {
144 String[] items = path.split( "/" );
145
146 if( items.length > 0 )
147 {
148 dialog.setValue( Form.RESOURCENAME, items[items.length - 1] );
149 }
150 }
151
152 protected void createRequest( RestMethod method )
153 {
154
155
156 RestRequest request = method.addNewRequest( "Request " + ( method.getRequestCount() + 1 ) );
157 UISupport.showDesktopPanel( request );
158 }
159
160 public enum ParamLocation
161 {
162 RESOURCE, METHOD
163 }
164
165 public static class InternalRestParamsTable extends RestParamsTable
166 {
167 private ParamLocation defaultLocation;
168
169 public InternalRestParamsTable( RestParamsPropertyHolder params, ParamLocation defaultLocation )
170 {
171 super( params, false );
172 this.defaultLocation = defaultLocation;
173 }
174
175 public void extractParams( RestParamsPropertyHolder params, ParamLocation location )
176 {
177 for( int i = 0; i < paramsTable.getRowCount(); i++ )
178 {
179 RestParamProperty prop = paramsTableModel.getParameterAt( i );
180 if( ( ( InternalRestParamsTableModel )paramsTableModel ).getParamLocationAt( i ) == location )
181 {
182 params.addParameter( prop );
183 }
184 }
185 }
186
187 protected RestParamsTableModel createTableModel( RestParamsPropertyHolder params )
188 {
189 return new InternalRestParamsTableModel( params );
190 }
191
192 protected void init( RestParamsPropertyHolder params, boolean showInspector )
193 {
194 super.init( params, showInspector );
195 paramsTable.setDefaultEditor( ParamLocation.class, new DefaultCellEditor( new JComboBox( new Object[] {
196 ParamLocation.RESOURCE, ParamLocation.METHOD } ) ) );
197 }
198
199 public class InternalRestParamsTableModel extends RestParamsTableModel
200 {
201 private Map<RestParamProperty, ParamLocation> locations = new HashMap<RestParamProperty, ParamLocation>();
202 private int columnCount;
203
204 public InternalRestParamsTableModel( RestParamsPropertyHolder params )
205 {
206 super( params );
207 columnCount = super.getColumnCount();
208 }
209
210 public int getColumnCount()
211 {
212 return columnCount + 1;
213 }
214
215 public ParamLocation getParamLocationAt( int rowIndex )
216 {
217 return ( ParamLocation )getValueAt( rowIndex, columnCount );
218 }
219
220 public Object getValueAt( int rowIndex, int columnIndex )
221 {
222 if( columnIndex != columnCount )
223 return super.getValueAt( rowIndex, columnIndex );
224 RestParamProperty name = params.getPropertyAt( rowIndex );
225 if( !locations.containsKey( name ) )
226 locations.put( name, defaultLocation );
227 return locations.get( name );
228 }
229
230 @Override
231 public String getColumnName( int column )
232 {
233 return column != columnCount ? super.getColumnName( column ) : "Location";
234 }
235
236 @Override
237 public Class<?> getColumnClass( int columnIndex )
238 {
239 return columnIndex != columnCount ? super.getColumnClass( columnIndex ) : ParamLocation.class;
240 }
241
242 @Override
243 public void setValueAt( Object value, int rowIndex, int columnIndex )
244 {
245 if( columnIndex != columnCount )
246 super.setValueAt( value, rowIndex, columnIndex );
247 else
248 {
249 RestParamProperty name = params.getPropertyAt( rowIndex );
250 locations.put( name, ( ParamLocation )value );
251 }
252 }
253
254 }
255
256 }
257
258 private class ExtractParamsAction extends AbstractAction
259 {
260 public ExtractParamsAction()
261 {
262 super( "Extract Params" );
263 }
264
265 public void actionPerformed( ActionEvent e )
266 {
267 try
268 {
269 String path = RestUtils.extractParams( dialog.getValue( Form.RESOURCEPATH ), params, false );
270 dialog.setValue( Form.RESOURCEPATH, path );
271
272 if( StringUtils.isNullOrEmpty( dialog.getValue( Form.RESOURCENAME ) ) )
273 setNameFromPath( path );
274
275 paramsTable.refresh();
276 }
277 catch( Exception e1 )
278 {
279 UISupport.showInfoMessage( "No parameters to extract!" );
280 }
281 }
282 }
283
284 @AForm( name = "Form.Title", description = "Form.Description", helpUrl = HelpUrls.NEWRESTSERVICE_HELP_URL, icon = UISupport.TOOL_ICON_PATH )
285 public interface Form
286 {
287 @AField( description = "Form.ServiceName.Description", type = AFieldType.STRING )
288 public final static String RESOURCENAME = messages.get( "Form.ResourceName.Label" );
289
290 @AField( description = "Form.ServiceUrl.Description", type = AFieldType.STRING )
291 public final static String RESOURCEPATH = messages.get( "Form.ResourcePath.Label" );
292
293 @AField( description = "Form.ExtractParams.Description", type = AFieldType.ACTION )
294 public final static String EXTRACTPARAMS = messages.get( "Form.ExtractParams.Label" );
295
296 @AField( description = "Form.ParamsTable.Description", type = AFieldType.COMPONENT )
297 public final static String PARAMSTABLE = messages.get( "Form.ParamsTable.Label" );
298
299
300
301
302
303 }
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338 }