View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 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  
13  package com.eviware.soapui.support.editor.views.xml.source;
14  
15  import java.util.List;
16  
17  import javax.swing.JMenu;
18  import javax.swing.JPopupMenu;
19  import javax.swing.JSeparator;
20  import javax.swing.event.PopupMenuEvent;
21  import javax.swing.event.PopupMenuListener;
22  
23  import com.eviware.soapui.impl.rest.RestRequest;
24  import com.eviware.soapui.impl.rest.RestRequestInterface;
25  import com.eviware.soapui.impl.support.http.HttpRequestInterface;
26  import com.eviware.soapui.impl.wadl.support.WadlValidator;
27  import com.eviware.soapui.impl.wsdl.WsdlOperation;
28  import com.eviware.soapui.impl.wsdl.WsdlRequest;
29  import com.eviware.soapui.impl.wsdl.actions.mockresponse.AddWsaHeadersToMockResponseAction;
30  import com.eviware.soapui.impl.wsdl.actions.mockresponse.ApplyOutgoingWSSToMockResponseAction;
31  import com.eviware.soapui.impl.wsdl.actions.mockresponse.RemoveAllOutgoingWSSFromMockResponseAction;
32  import com.eviware.soapui.impl.wsdl.actions.mockresponse.RemoveWsaHeadersFromMockResponseAction;
33  import com.eviware.soapui.impl.wsdl.actions.request.AddWSSUsernameTokenAction;
34  import com.eviware.soapui.impl.wsdl.actions.request.AddWSTimestampAction;
35  import com.eviware.soapui.impl.wsdl.actions.request.AddWsaHeadersToRequestAction;
36  import com.eviware.soapui.impl.wsdl.actions.request.ApplyOutgoingWSSToRequestAction;
37  import com.eviware.soapui.impl.wsdl.actions.request.RemoveAllOutgoingWSSFromRequestAction;
38  import com.eviware.soapui.impl.wsdl.actions.request.RemoveWsaHeadersFromRequestAction;
39  import com.eviware.soapui.impl.wsdl.mock.WsdlMockResponse;
40  import com.eviware.soapui.impl.wsdl.panels.mockoperation.WsdlMockResponseMessageExchange;
41  import com.eviware.soapui.impl.wsdl.panels.mockoperation.WsdlMockResultMessageExchange;
42  import com.eviware.soapui.impl.wsdl.support.MessageExchangeModelItem;
43  import com.eviware.soapui.impl.wsdl.support.wsdl.WsdlValidator;
44  import com.eviware.soapui.impl.wsdl.support.wss.DefaultWssContainer;
45  import com.eviware.soapui.impl.wsdl.support.wss.OutgoingWss;
46  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext;
47  import com.eviware.soapui.impl.wsdl.teststeps.RestResponseMessageExchange;
48  import com.eviware.soapui.impl.wsdl.teststeps.WsdlResponseMessageExchange;
49  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequest;
50  import com.eviware.soapui.model.ModelItem;
51  import com.eviware.soapui.model.testsuite.AssertionError;
52  import com.eviware.soapui.support.editor.Editor;
53  import com.eviware.soapui.support.editor.EditorView;
54  import com.eviware.soapui.support.editor.registry.RequestEditorViewFactory;
55  import com.eviware.soapui.support.editor.registry.ResponseEditorViewFactory;
56  import com.eviware.soapui.support.editor.xml.XmlDocument;
57  import com.eviware.soapui.support.editor.xml.XmlEditor;
58  import com.eviware.soapui.support.editor.xml.XmlEditorView;
59  import com.eviware.soapui.support.editor.xml.support.ValidationError;
60  import com.eviware.soapui.support.propertyexpansion.PropertyExpansionPopupListener;
61  import com.eviware.soapui.support.xml.JXEditTextArea;
62  
63  /***
64   * Factory for default "XML" source editor view in soapUI
65   * 
66   * @author ole.matzura
67   */
68  
69  public class XmlSourceEditorViewFactory implements ResponseEditorViewFactory, RequestEditorViewFactory
70  {
71  	public static final String VIEW_ID = "Source";
72  
73  	public XmlEditorView createEditorView( XmlEditor editor )
74  	{
75  		return new XmlSourceEditorView<ModelItem>( editor, null );
76  	}
77  
78  	public String getViewId()
79  	{
80  		return VIEW_ID;
81  	}
82  
83  	public EditorView<?> createRequestEditorView( Editor<?> editor, ModelItem modelItem )
84  	{
85  		if( modelItem instanceof WsdlRequest )
86  		{
87  			return new WsdlRequestXmlSourceEditor( ( XmlEditor )editor, ( WsdlRequest )modelItem );
88  		}
89  		else if( modelItem instanceof WsdlMockResponse )
90  		{
91  			return new WsdlMockRequestXmlSourceEditor( ( XmlEditor )editor, ( WsdlMockResponse )modelItem );
92  		}
93  		else if( modelItem instanceof MessageExchangeModelItem )
94  		{
95  			return new XmlSourceEditorView<MessageExchangeModelItem>( ( XmlEditor )editor,
96  					( MessageExchangeModelItem )modelItem );
97  		}
98  
99  		return null;
100 	}
101 
102 	public EditorView<?> createResponseEditorView( Editor<?> editor, ModelItem modelItem )
103 	{
104 		if( modelItem instanceof WsdlRequest )
105 		{
106 			return new WsdlResponseXmlSourceEditor( ( XmlEditor )editor, ( WsdlRequest )modelItem );
107 		}
108 		else if( modelItem instanceof WsdlMockResponse )
109 		{
110 			return new WsdlMockResponseXmlSourceEditor( ( XmlEditor )editor, ( WsdlMockResponse )modelItem );
111 		}
112 		else if( modelItem instanceof HttpRequestInterface<?> )
113 		{
114 			return new RestResponseXmlSourceEditor( ( XmlEditor )editor, ( HttpRequestInterface<?> )modelItem );
115 		}
116 		else if( modelItem instanceof MessageExchangeModelItem )
117 		{
118 			return new XmlSourceEditorView<MessageExchangeModelItem>( ( XmlEditor )editor,
119 					( MessageExchangeModelItem )modelItem );
120 		}
121 
122 		return null;
123 	}
124 
125 	/***
126 	 * XmlSource editor for a WsdlRequest
127 	 * 
128 	 * @author ole.matzura
129 	 */
130 
131 	public static class WsdlRequestXmlSourceEditor extends XmlSourceEditorView<WsdlRequest>
132 	{
133 		private JMenu applyMenu;
134 		private JMenu wsaApplyMenu;
135 
136 		public WsdlRequestXmlSourceEditor( XmlEditor xmlEditor, WsdlRequest request )
137 		{
138 			super( xmlEditor, request );
139 		}
140 
141 		protected ValidationError[] validateXml( String xml )
142 		{
143 			WsdlOperation operation = getModelItem().getOperation();
144 			WsdlValidator validator = new WsdlValidator( ( operation.getInterface() ).getWsdlContext() );
145 
146 			WsdlResponseMessageExchange wsdlResponseMessageExchange = new WsdlResponseMessageExchange( getModelItem() );
147 			wsdlResponseMessageExchange.setRequestContent( xml );
148 			return validator.assertRequest( wsdlResponseMessageExchange, false );
149 		}
150 
151 		@Override
152 		protected void buildUI()
153 		{
154 			super.buildUI();
155 			PropertyExpansionPopupListener.enable( getInputArea(), getModelItem() );
156 		}
157 
158 		protected void buildPopup( JPopupMenu inputPopup, JXEditTextArea editArea )
159 		{
160 			super.buildPopup( inputPopup, editArea );
161 
162 			inputPopup.insert( new JSeparator(), 2 );
163 			inputPopup.insert( new AddWSSUsernameTokenAction( getModelItem() ), 3 );
164 			inputPopup.insert( new AddWSTimestampAction( getModelItem() ), 4 );
165 			inputPopup.insert( applyMenu = new JMenu( "Outgoing WSS" ), 5 );
166 			inputPopup.insert( wsaApplyMenu = new JMenu( "WS-A headers" ), 6 );
167 
168 			inputPopup.addPopupMenuListener( new PopupMenuListener()
169 			{
170 
171 				public void popupMenuCanceled( PopupMenuEvent e )
172 				{
173 
174 				}
175 
176 				public void popupMenuWillBecomeInvisible( PopupMenuEvent e )
177 				{
178 
179 				}
180 
181 				public void popupMenuWillBecomeVisible( PopupMenuEvent e )
182 				{
183 					applyMenu.removeAll();
184 					DefaultWssContainer wss = getModelItem().getOperation().getInterface().getProject().getWssContainer();
185 					List<OutgoingWss> outgoingWssList = wss.getOutgoingWssList();
186 					applyMenu.setEnabled( !outgoingWssList.isEmpty() );
187 
188 					for( OutgoingWss outgoing : outgoingWssList )
189 					{
190 						applyMenu.add( new ApplyOutgoingWSSToRequestAction( getModelItem(), outgoing ) );
191 					}
192 					applyMenu.add( new RemoveAllOutgoingWSSFromRequestAction( getModelItem() ) );
193 
194 					wsaApplyMenu.removeAll();
195 					wsaApplyMenu.add( new AddWsaHeadersToRequestAction( getModelItem() ) );
196 					wsaApplyMenu.add( new RemoveWsaHeadersFromRequestAction( getModelItem() ) );
197 					wsaApplyMenu.setEnabled( getModelItem().getWsaConfig().isWsaEnabled() );
198 				}
199 			} );
200 		}
201 
202 		public WsdlRequest getRequest()
203 		{
204 			return getModelItem();
205 		}
206 	}
207 
208 	/***
209 	 * XmlSource editor for a WsdlMockRequest
210 	 * 
211 	 * @author ole.matzura
212 	 */
213 
214 	public static class WsdlMockRequestXmlSourceEditor extends XmlSourceEditorView<WsdlMockResponse>
215 	{
216 		public WsdlMockRequestXmlSourceEditor( XmlEditor xmlEditor, WsdlMockResponse mockResponse )
217 		{
218 			super( xmlEditor, mockResponse );
219 		}
220 
221 		protected ValidationError[] validateXml( String xml )
222 		{
223 			WsdlOperation operation = getModelItem().getMockOperation().getOperation();
224 
225 			if( operation == null )
226 			{
227 				return new ValidationError[] { new AssertionError( "Missing operation for MockResponse" ) };
228 			}
229 
230 			WsdlValidator validator = new WsdlValidator( ( operation.getInterface() ).getWsdlContext() );
231 			return validator.assertRequest( new WsdlMockResultMessageExchange( getModelItem().getMockResult(),
232 					getModelItem() ), false );
233 		}
234 
235 		protected void buildPopup( JPopupMenu inputPopup, JXEditTextArea editArea )
236 		{
237 			super.buildPopup( inputPopup, editArea );
238 			// inputPopup.insert( new JSeparator(), 2 );
239 		}
240 	}
241 
242 	/***
243 	 * XmlSource editor for a WsdlResponse
244 	 * 
245 	 * @author ole.matzura
246 	 */
247 
248 	public static class WsdlResponseXmlSourceEditor extends XmlSourceEditorView<WsdlRequest>
249 	{
250 		public WsdlResponseXmlSourceEditor( XmlEditor xmlEditor, WsdlRequest request )
251 		{
252 			super( xmlEditor, request );
253 		}
254 
255 		protected ValidationError[] validateXml( String xml )
256 		{
257 			if( getModelItem() instanceof WsdlTestRequest )
258 			{
259 				WsdlTestRequest testRequest = ( WsdlTestRequest )getModelItem();
260 				testRequest.assertResponse( new WsdlTestRunContext( testRequest.getTestStep() ) );
261 			}
262 
263 			WsdlOperation operation = getModelItem().getOperation();
264 			WsdlValidator validator = new WsdlValidator( ( operation.getInterface() ).getWsdlContext() );
265 
266 			return validator.assertResponse( new WsdlResponseMessageExchange( getModelItem() ), false );
267 		}
268 	}
269 
270 	/***
271 	 * XmlSource editor for a WsdlMockResponse
272 	 * 
273 	 * @author ole.matzura
274 	 */
275 
276 	public static class WsdlMockResponseXmlSourceEditor extends XmlSourceEditorView<WsdlMockResponse>
277 	{
278 		private JMenu applyMenu;
279 		private JMenu wsaApplyMenu;
280 
281 		public WsdlMockResponseXmlSourceEditor( XmlEditor xmlEditor, WsdlMockResponse mockResponse )
282 		{
283 			super( xmlEditor, mockResponse );
284 		}
285 
286 		@Override
287 		protected void buildUI()
288 		{
289 			super.buildUI();
290 
291 			getValidateXmlAction().setEnabled( getModelItem().getMockOperation().getOperation().isBidirectional() );
292 		}
293 
294 		protected ValidationError[] validateXml( String xml )
295 		{
296 			WsdlOperation operation = getModelItem().getMockOperation().getOperation();
297 			if( operation == null )
298 			{
299 				return new ValidationError[] { new AssertionError( "Missing operation for MockResponse" ) };
300 			}
301 
302 			WsdlValidator validator = new WsdlValidator( ( operation.getInterface() ).getWsdlContext() );
303 			return validator.assertResponse( new WsdlMockResponseMessageExchange( getModelItem() ), false );
304 		}
305 
306 		public WsdlMockResponse getMockResponse()
307 		{
308 			return getModelItem();
309 		}
310 
311 		protected void buildPopup( JPopupMenu inputPopup, JXEditTextArea editArea )
312 		{
313 			super.buildPopup( inputPopup, editArea );
314 
315 			inputPopup.insert( applyMenu = new JMenu( "Outgoing WSS" ), 2 );
316 			inputPopup.insert( wsaApplyMenu = new JMenu( "WS-A headers" ), 3 );
317 
318 			inputPopup.addPopupMenuListener( new PopupMenuListener()
319 			{
320 
321 				public void popupMenuCanceled( PopupMenuEvent e )
322 				{
323 
324 				}
325 
326 				public void popupMenuWillBecomeInvisible( PopupMenuEvent e )
327 				{
328 
329 				}
330 
331 				public void popupMenuWillBecomeVisible( PopupMenuEvent e )
332 				{
333 					applyMenu.removeAll();
334 					DefaultWssContainer wss = getModelItem().getMockOperation().getMockService().getProject()
335 							.getWssContainer();
336 					List<OutgoingWss> outgoingWssList = wss.getOutgoingWssList();
337 					applyMenu.setEnabled( !outgoingWssList.isEmpty() );
338 
339 					for( OutgoingWss outgoing : outgoingWssList )
340 					{
341 						applyMenu.add( new ApplyOutgoingWSSToMockResponseAction( getModelItem(), outgoing ) );
342 					}
343 					applyMenu.add( new RemoveAllOutgoingWSSFromMockResponseAction( getModelItem() ) );
344 
345 					wsaApplyMenu.removeAll();
346 					wsaApplyMenu.add( new AddWsaHeadersToMockResponseAction( getModelItem() ) );
347 					wsaApplyMenu.add( new RemoveWsaHeadersFromMockResponseAction( getModelItem() ) );
348 					wsaApplyMenu.setEnabled( getModelItem().getWsaConfig().isWsaEnabled() );
349 
350 				}
351 			} );
352 		}
353 	}
354 
355 	private class RestResponseXmlSourceEditor extends XmlSourceEditorView<HttpRequestInterface<?>>
356 	{
357 		public RestResponseXmlSourceEditor( XmlEditor<XmlDocument> xmlEditor, HttpRequestInterface<?> restRequest )
358 		{
359 			super( xmlEditor, restRequest );
360 		}
361 
362 		protected ValidationError[] validateXml( String xml )
363 		{
364 			if( getModelItem() instanceof HttpRequestInterface
365 					|| ( ( RestRequestInterface )getModelItem() ).getResource() == null )
366 				return new ValidationError[0];
367 
368 			WadlValidator validator = new WadlValidator( ( ( RestRequestInterface )getModelItem() ).getResource()
369 					.getService().getWadlContext() );
370 			return validator.assertResponse( new RestResponseMessageExchange( ( RestRequest )getModelItem() ) );
371 		}
372 	}
373 }