1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.rest.panels.request;
14
15 import com.eviware.soapui.impl.rest.RestRequest;
16 import com.eviware.soapui.impl.support.AbstractHttpRequest.RequestMethod;
17 import com.eviware.soapui.impl.support.components.ModelItemXmlEditor;
18 import com.eviware.soapui.impl.support.panels.AbstractHttpRequestDesktopPanel;
19 import com.eviware.soapui.impl.wsdl.WsdlSubmitContext;
20 import com.eviware.soapui.impl.wsdl.submit.transports.http.HttpResponse;
21 import com.eviware.soapui.model.ModelItem;
22 import com.eviware.soapui.model.iface.Request.SubmitException;
23 import com.eviware.soapui.model.iface.Submit;
24 import com.eviware.soapui.support.DocumentListenerAdapter;
25 import com.eviware.soapui.support.UISupport;
26 import com.eviware.soapui.support.components.JUndoableTextField;
27 import com.eviware.soapui.support.components.JXToolBar;
28 import com.eviware.soapui.support.editor.xml.support.AbstractXmlDocument;
29
30 import javax.swing.*;
31 import javax.swing.text.Document;
32 import java.awt.*;
33 import java.awt.event.ItemEvent;
34 import java.awt.event.ItemListener;
35 import java.beans.PropertyChangeEvent;
36 import java.beans.PropertyChangeListener;
37
38 public abstract class AbstractRestRequestDesktopPanel<T extends ModelItem, T2 extends RestRequest>
39 extends AbstractHttpRequestDesktopPanel<T, T2>
40 {
41 private boolean updatingRequest;
42 private JComboBox methodCombo;
43 private JUndoableTextField pathTextField;
44 private JComboBox acceptCombo;
45 private JLabel pathLabel;
46 private boolean updating;
47
48
49 public AbstractRestRequestDesktopPanel( T modelItem, T2 requestItem )
50 {
51 super( modelItem, requestItem );
52
53 if( requestItem.getResource() != null )
54 requestItem.getResource().addPropertyChangeListener( this );
55 }
56
57 public void propertyChange( PropertyChangeEvent evt )
58 {
59 if( evt.getPropertyName().equals( "method" ) && !updatingRequest )
60 {
61 methodCombo.setSelectedItem( evt.getNewValue() );
62 }
63 else if( evt.getPropertyName().equals( "accept" ) && !updatingRequest )
64 {
65 acceptCombo.setSelectedItem( evt.getNewValue() );
66 }
67 else if( evt.getPropertyName().equals( "responseMediaTypes" ) && !updatingRequest )
68 {
69 Object item = acceptCombo.getSelectedItem();
70 acceptCombo.setModel( new DefaultComboBoxModel( ( Object[] ) evt.getNewValue() ) );
71 acceptCombo.setSelectedItem( item );
72 }
73 else if( evt.getPropertyName().equals( "path" ) &&
74 ( getRequest().getResource() == null || getRequest().getResource() == evt.getSource() ) )
75 {
76 if( pathLabel != null )
77 {
78 pathLabel.setText( getRequest().getResource().getFullPath() );
79 }
80
81 if( !updating )
82 {
83 updating = true;
84 pathTextField.setText( ( String ) evt.getNewValue() );
85 updating = false;
86 }
87 }
88
89 super.propertyChange( evt );
90 }
91
92 @Override
93 protected ModelItemXmlEditor<?, ?> buildRequestEditor()
94 {
95 return new RestRequestMessageEditor( getRequest() );
96 }
97
98 @Override
99 protected ModelItemXmlEditor<?, ?> buildResponseEditor()
100 {
101 return new RestResponseMessageEditor( getRequest() );
102 }
103
104 @Override
105 protected Submit doSubmit() throws SubmitException
106 {
107 return getRequest().submit( new WsdlSubmitContext( getModelItem() ), true );
108 }
109
110
111
112 @Override
113 protected String getHelpUrl()
114 {
115 return null;
116 }
117
118 @Override
119 protected void insertButtons( JXToolBar toolbar )
120 {
121 if( getRequest().getResource() == null )
122 {
123 addToolbarComponents( toolbar );
124 }
125 }
126
127 protected JComponent buildEndpointComponent()
128 {
129 return getRequest().getResource() == null ? null : super.buildEndpointComponent();
130 }
131
132 @Override
133 protected JComponent buildToolbar()
134 {
135 if( getRequest().getResource() != null )
136 {
137 JPanel panel = new JPanel( new BorderLayout() );
138 panel.add( super.buildToolbar(), BorderLayout.NORTH );
139
140 JXToolBar toolbar = UISupport.createToolbar();
141 addToolbarComponents( toolbar );
142
143 panel.add( toolbar, BorderLayout.SOUTH );
144 return panel;
145 }
146 else
147 {
148 return super.buildToolbar();
149 }
150 }
151
152 protected void addToolbarComponents( JXToolBar toolbar )
153 {
154 toolbar.addSeparator();
155 methodCombo = new JComboBox( new Object[]{RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT,
156 RequestMethod.DELETE, RequestMethod.HEAD} );
157
158 methodCombo.setSelectedItem( getRequest().getMethod() );
159 methodCombo.setToolTipText( "Set desired HTTP method" );
160 methodCombo.addItemListener( new ItemListener()
161 {
162
163 public void itemStateChanged( ItemEvent e )
164 {
165 updatingRequest = true;
166 getRequest().setMethod( ( RequestMethod ) methodCombo.getSelectedItem() );
167 updatingRequest = false;
168 }
169 } );
170
171 toolbar.addLabeledFixed( "Method", methodCombo );
172 toolbar.addSeparator();
173
174 if( getRequest().getResource() != null )
175 {
176 acceptCombo = new JComboBox( getRequest().getResponseMediaTypes() );
177 acceptCombo.setEditable( true );
178 acceptCombo.setToolTipText( "Sets accepted encoding(s) for response" );
179 acceptCombo.setSelectedItem( getRequest().getAccept() );
180 acceptCombo.addItemListener( new ItemListener()
181 {
182 public void itemStateChanged( ItemEvent e )
183 {
184 updatingRequest = true;
185 getRequest().setAccept( String.valueOf( acceptCombo.getSelectedItem() ) );
186 updatingRequest = false;
187 }
188 } );
189
190 toolbar.addLabeledFixed( "Accept", acceptCombo );
191 toolbar.addSeparator();
192
193 pathTextField = new JUndoableTextField();
194 pathTextField.setPreferredSize( new Dimension( 200, 20 ) );
195 pathTextField.setText( getRequest().getResource().getPath() );
196 pathTextField.getDocument().addDocumentListener( new DocumentListenerAdapter()
197 {
198 @Override
199 public void update( Document document )
200 {
201 if( updating )
202 return;
203
204 updating = true;
205 getRequest().getResource().setPath( pathTextField.getText() );
206 updating = false;
207 }
208 } );
209
210 toolbar.addLabeledFixed( "Resource Path:", pathTextField );
211
212 pathLabel = new JLabel( getRequest().getResource().getFullPath() );
213 pathLabel.setPreferredSize( new Dimension( 200, 20 ) );
214
215 toolbar.addSeparator();
216 toolbar.addLabeledFixed( "Full Path:", pathLabel );
217 }
218 else
219 {
220 pathTextField = new JUndoableTextField();
221 pathTextField.setPreferredSize( new Dimension( 300, 20 ) );
222 pathTextField.setText( getRequest().getPath() );
223 pathTextField.getDocument().addDocumentListener( new DocumentListenerAdapter()
224 {
225 @Override
226 public void update( Document document )
227 {
228 getRequest().setPath( pathTextField.getText() );
229 }
230 } );
231
232 toolbar.addLabeledFixed( "Request URL:", pathTextField );
233 }
234
235 toolbar.addSeparator();
236 }
237
238 protected boolean release()
239 {
240 if( getRequest().getResource() != null )
241 {
242 getRequest().getResource().removePropertyChangeListener( this );
243 }
244
245 return super.release();
246 }
247
248 public class RestRequestMessageEditor extends
249 AbstractHttpRequestDesktopPanel<?, ?>.AbstractHttpRequestMessageEditor<RestRequestDocument>
250 {
251 public RestRequestMessageEditor( RestRequest modelItem )
252 {
253 super( new RestRequestDocument( modelItem ) );
254 }
255 }
256
257 public class RestResponseMessageEditor extends
258 AbstractHttpRequestDesktopPanel<?, ?>.AbstractHttpResponseMessageEditor<RestResponseDocument>
259 {
260 public RestResponseMessageEditor( RestRequest modelItem )
261 {
262 super( new RestResponseDocument( modelItem ) );
263 }
264 }
265
266 public class RestRequestDocument extends AbstractXmlDocument implements PropertyChangeListener
267 {
268 private final RestRequest modelItem;
269 private boolean updating;
270
271 public RestRequestDocument( RestRequest modelItem )
272 {
273 this.modelItem = modelItem;
274
275 modelItem.addPropertyChangeListener( this );
276 }
277
278 public RestRequest getRequest()
279 {
280 return modelItem;
281 }
282
283 public String getXml()
284 {
285 return getRequest().getRequestContent();
286 }
287
288 public void setXml( String xml )
289 {
290 if( !updating )
291 {
292 updating = true;
293 getRequest().setRequestContent( xml );
294 updating = false;
295 }
296 }
297
298 public void propertyChange( PropertyChangeEvent evt )
299 {
300 if( evt.getPropertyName().equals( RestRequest.REQUEST_PROPERTY ) && !updating )
301 {
302 updating = true;
303 fireXmlChanged( ( String ) evt.getOldValue(), ( String ) evt.getNewValue() );
304 updating = false;
305 }
306 }
307 }
308
309 public class RestResponseDocument extends AbstractXmlDocument implements PropertyChangeListener
310 {
311 private final RestRequest modelItem;
312
313 public RestResponseDocument( RestRequest modelItem )
314 {
315 this.modelItem = modelItem;
316
317 modelItem.addPropertyChangeListener( RestRequest.RESPONSE_PROPERTY, this );
318 }
319
320 public RestRequest getRequest()
321 {
322 return modelItem;
323 }
324
325 public String getXml()
326 {
327 return modelItem.getResponseContentAsXml();
328 }
329
330 public void setXml( String xml )
331 {
332 HttpResponse response = getRequest().getResponse();
333 if( response != null )
334 response.setResponseContent( xml );
335 }
336
337 public void propertyChange( PropertyChangeEvent evt )
338 {
339 fireXmlChanged( evt.getOldValue() == null ? null : ( ( HttpResponse ) evt.getOldValue() ).getContentAsString(),
340 getXml() );
341 }
342 }
343 }