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