1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.rest.actions.support;
14
15 import com.eviware.soapui.config.RestParametersConfig;
16 import com.eviware.soapui.impl.rest.RestRequest;
17 import com.eviware.soapui.impl.rest.RestResource;
18 import com.eviware.soapui.impl.rest.panels.resource.RestParamsTable;
19 import com.eviware.soapui.impl.rest.support.RestUtils;
20 import com.eviware.soapui.impl.rest.support.XmlBeansRestParamsTestPropertyHolder;
21 import com.eviware.soapui.impl.support.AbstractHttpRequest.RequestMethod;
22 import com.eviware.soapui.impl.wsdl.support.HelpUrls;
23 import com.eviware.soapui.model.ModelItem;
24 import com.eviware.soapui.support.MessageSupport;
25 import com.eviware.soapui.support.StringUtils;
26 import com.eviware.soapui.support.UISupport;
27 import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
28 import com.eviware.x.form.XFormDialog;
29 import com.eviware.x.form.support.ADialogBuilder;
30 import com.eviware.x.form.support.AField;
31 import com.eviware.x.form.support.AField.AFieldType;
32 import com.eviware.x.form.support.AForm;
33 import com.eviware.x.form.validators.RequiredValidator;
34
35 import javax.swing.*;
36 import java.awt.event.ActionEvent;
37 import java.net.MalformedURLException;
38 import java.net.URL;
39
40 /***
41 * Actions for importing an existing soapUI project file into the current workspace
42 *
43 * @author Ole.Matzura
44 */
45
46 public abstract class NewRestResourceActionBase<T extends ModelItem> extends AbstractSoapUIAction<T>
47 {
48 private XFormDialog dialog;
49 private XmlBeansRestParamsTestPropertyHolder params;
50 private RestParamsTable paramsTable;
51 public static final MessageSupport messages = MessageSupport.getMessages( NewRestResourceActionBase.class );
52
53 public NewRestResourceActionBase( String title, String description )
54 {
55 super( title, description );
56 }
57
58 public void perform( T service, Object param )
59 {
60 if( dialog == null )
61 {
62 dialog = ADialogBuilder.buildDialog( Form.class );
63 dialog.getFormField(Form.RESOURCENAME ).addFormFieldValidator(new RequiredValidator());
64 dialog.getFormField(Form.EXTRACTPARAMS).setProperty("action", new ExtractParamsAction() );
65 dialog.setBooleanValue( Form.CREATEREQUEST, true );
66 }
67 else
68 {
69 dialog.setValue( Form.RESOURCENAME, "" );
70 dialog.setValue( Form.RESOURCEPATH, "" );
71 }
72
73 params = new XmlBeansRestParamsTestPropertyHolder( service,
74 RestParametersConfig.Factory.newInstance() );
75
76 if( param instanceof URL )
77 {
78 URL url = (URL) param;
79
80 String path = RestUtils.extractParams(url, params, false);
81 dialog.setValue(Form.RESOURCEPATH, path );
82
83 setNameFromPath( path );
84
85 if( paramsTable != null )
86 paramsTable.refresh();
87 }
88
89 paramsTable = new RestParamsTable( params, false );
90 dialog.getFormField(Form.PARAMSTABLE).setProperty("component", paramsTable);
91
92 if( dialog.show() )
93 {
94 String path = dialog.getValue(Form.RESOURCEPATH);
95
96 try
97 {
98 URL url = new URL( path );
99 path = url.getPath();
100 }
101 catch (MalformedURLException e)
102 {
103 }
104
105 RestResource resource = createRestResource( service, path, dialog );
106
107 resource.getParams().addParameters( params );
108
109 UISupport.select(resource);
110
111 if( dialog.getBooleanValue(Form.CREATEREQUEST))
112 {
113 createRequest( resource );
114 }
115 }
116 }
117
118 protected abstract RestResource createRestResource( T service, String path, XFormDialog dialog );
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158 private void setNameFromPath( String path )
159 {
160 String[] items = path.split("/");
161
162 if( items.length > 0 )
163 {
164 dialog.setValue( Form.RESOURCENAME, items[items.length-1]);
165 }
166 }
167
168 protected void createRequest(RestResource resource)
169 {
170 RestRequest request = resource.addNewRequest( dialog.getValue(Form.RESOURCENAME));
171 request.setMethod( RequestMethod.GET );
172 UISupport.showDesktopPanel( request );
173 }
174
175 private class ExtractParamsAction extends AbstractAction
176 {
177 public ExtractParamsAction()
178 {
179 super( "Extract Params" );
180 }
181
182 public void actionPerformed(ActionEvent e)
183 {
184 try
185 {
186 String path = RestUtils.extractParams( new URL( dialog.getValue(Form.RESOURCEPATH)), params, false);
187 dialog.setValue( Form.RESOURCEPATH, path );
188
189 if( StringUtils.isNullOrEmpty( dialog.getValue( Form.RESOURCENAME )))
190 setNameFromPath( path );
191
192 paramsTable.refresh();
193 }
194 catch (MalformedURLException e1)
195 {
196 UISupport.showInfoMessage("No parameters to extract!");
197 }
198 }
199 }
200
201 @AForm( name="Form.Title", description = "Form.Description", helpUrl=HelpUrls.NEWRESTSERVICE_HELP_URL, icon=UISupport.TOOL_ICON_PATH)
202 public interface Form
203 {
204 @AField( description = "Form.ServiceName.Description", type = AFieldType.STRING )
205 public final static String RESOURCENAME = messages.get("Form.ResourceName.Label");
206
207 @AField(description = "Form.ServiceUrl.Description", type = AFieldType.STRING )
208 public final static String RESOURCEPATH = messages.get("Form.ResourcePath.Label");
209
210 @AField(description = "Form.ExtractParams.Description", type = AFieldType.ACTION )
211 public final static String EXTRACTPARAMS = messages.get("Form.ExtractParams.Label");
212
213 @AField(description = "Form.ParamsTable.Description", type = AFieldType.COMPONENT )
214 public final static String PARAMSTABLE = messages.get("Form.ParamsTable.Label");
215
216 @AField(description = "Form.CreateRequest.Description", type = AFieldType.BOOLEAN )
217 public final static String CREATEREQUEST = messages.get("Form.CreateRequest.Label");
218 }
219 }