View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2007 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.impl.wsdl.panels.request.components.editor.views.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.wsdl.WsdlInterface;
24  import com.eviware.soapui.impl.wsdl.WsdlOperation;
25  import com.eviware.soapui.impl.wsdl.WsdlRequest;
26  import com.eviware.soapui.impl.wsdl.actions.mockresponse.ApplyOutgoingWSSToMockResponseAction;
27  import com.eviware.soapui.impl.wsdl.actions.request.AddWSSUsernameTokenAction;
28  import com.eviware.soapui.impl.wsdl.actions.request.AddWSTimestampAction;
29  import com.eviware.soapui.impl.wsdl.actions.request.ApplyOutgoingWSSToRequestAction;
30  import com.eviware.soapui.impl.wsdl.mock.WsdlMockResponse;
31  import com.eviware.soapui.impl.wsdl.panels.mockoperation.WsdlMockResponseMessageExchange;
32  import com.eviware.soapui.impl.wsdl.panels.mockoperation.WsdlMockResultMessageExchange;
33  import com.eviware.soapui.impl.wsdl.panels.request.components.editor.XmlEditor;
34  import com.eviware.soapui.impl.wsdl.panels.request.components.editor.XmlEditorView;
35  import com.eviware.soapui.impl.wsdl.panels.request.components.editor.support.ValidationError;
36  import com.eviware.soapui.impl.wsdl.panels.request.components.editor.views.registry.RequestEditorViewFactory;
37  import com.eviware.soapui.impl.wsdl.panels.request.components.editor.views.registry.ResponseEditorViewFactory;
38  import com.eviware.soapui.impl.wsdl.support.MessageExchangeModelItem;
39  import com.eviware.soapui.impl.wsdl.support.wsdl.WsdlValidator;
40  import com.eviware.soapui.impl.wsdl.support.wss.DefaultWssContainer;
41  import com.eviware.soapui.impl.wsdl.support.wss.OutgoingWss;
42  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext;
43  import com.eviware.soapui.impl.wsdl.teststeps.WsdlResponseMessageExchange;
44  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequest;
45  import com.eviware.soapui.model.ModelItem;
46  import com.eviware.soapui.model.testsuite.AssertionError;
47  import com.eviware.soapui.support.propertyexpansion.PropertyExpansionPopupListener;
48  import com.eviware.soapui.support.xml.JXEditTextArea;
49  
50  /***
51   * Factory for default "XML" source editor view in soapUI
52   * 
53   * @author ole.matzura
54   */
55  
56  public class XmlSourceEditorFactory implements ResponseEditorViewFactory, RequestEditorViewFactory
57  {
58  	public static final String VIEW_ID = "Source";
59  
60  	public XmlEditorView createEditorView(XmlEditor editor)
61  	{
62  		return new XmlSourceEditorView( editor );
63  	}
64  
65  	public String getViewId()
66  	{
67  		return VIEW_ID;
68  	}
69  
70  	public XmlEditorView createRequestEditorView( XmlEditor editor, ModelItem modelItem )
71  	{
72  		if( modelItem instanceof WsdlRequest )
73  		{
74  			return new WsdlRequestXmlSourceEditor( editor, ( WsdlRequest ) modelItem );
75  		}
76  		else if( modelItem instanceof WsdlMockResponse )
77  		{
78  			return new WsdlMockRequestXmlSourceEditor( editor, ( WsdlMockResponse ) modelItem ); 
79  		}
80  		else if( modelItem instanceof MessageExchangeModelItem )
81  		{
82  			return new XmlSourceEditorView( editor );
83  		}
84  		
85  		return null;
86  	}
87  	
88  	public XmlEditorView createResponseEditorView( XmlEditor editor, ModelItem modelItem )
89  	{
90  		if( modelItem instanceof WsdlRequest )
91  		{
92  			return new WsdlResponseXmlSourceEditor( editor, ( WsdlRequest ) modelItem );
93  		}
94  		else if( modelItem instanceof WsdlMockResponse )
95  		{
96  			return new WsdlMockResponseXmlSourceEditor( editor, ( WsdlMockResponse ) modelItem ); 
97  		}
98  		else if( modelItem instanceof MessageExchangeModelItem )
99  		{
100 			return new XmlSourceEditorView( editor );
101 		}
102 		
103 		return null;
104 	}
105 	
106 	/***
107 	 * XmlSource editor for a WsdlRequest
108 	 * 
109 	 * @author ole.matzura
110 	 */
111 	
112 	public static class WsdlRequestXmlSourceEditor extends XmlSourceEditorView
113 	{
114 		private final WsdlRequest request;
115 		private JMenu applyMenu;
116 
117 		public WsdlRequestXmlSourceEditor(XmlEditor xmlEditor, WsdlRequest request )
118 		{
119 			super(xmlEditor);
120 			this.request = request;
121 		}
122 
123 		protected ValidationError[] validateXml( String xml )
124 		{
125 			WsdlOperation operation = (WsdlOperation) request.getOperation();
126 			WsdlValidator validator = new WsdlValidator(((WsdlInterface) operation.getInterface()).getWsdlContext());
127 
128 			WsdlResponseMessageExchange wsdlResponseMessageExchange = new WsdlResponseMessageExchange( request );
129 			wsdlResponseMessageExchange.setRequestContent( xml );
130 			return validator.assertRequest( wsdlResponseMessageExchange, false );
131 		}
132 		
133 		@Override
134 		protected void buildUI()
135 		{
136 			super.buildUI();
137 			PropertyExpansionPopupListener.enable( getInputArea(), request );
138 		}
139 
140 		protected void buildPopup(JPopupMenu inputPopup, JXEditTextArea editArea )
141 		{
142 			super.buildPopup(inputPopup, editArea );
143 
144 			inputPopup.insert( new JSeparator(), 2 );
145 			inputPopup.insert(new AddWSSUsernameTokenAction(request),3);
146 			inputPopup.insert(new AddWSTimestampAction(request),4);
147 			inputPopup.insert( applyMenu = new JMenu( "Apply Outgoing WSS"),5);
148 			
149 			inputPopup.addPopupMenuListener( new PopupMenuListener() {
150 
151 				public void popupMenuCanceled( PopupMenuEvent e )
152 				{
153 					
154 				}
155 
156 				public void popupMenuWillBecomeInvisible( PopupMenuEvent e )
157 				{
158 					
159 				}
160 
161 				public void popupMenuWillBecomeVisible( PopupMenuEvent e )
162 				{
163 					applyMenu.removeAll();
164 					DefaultWssContainer wss = request.getOperation().getInterface().getProject().getWssContainer();
165 					List<OutgoingWss> outgoingWssList = wss.getOutgoingWssList();
166 					applyMenu.setEnabled( !outgoingWssList.isEmpty() );
167 					
168 					for( OutgoingWss outgoing : outgoingWssList )
169 					{
170 						applyMenu.add( new ApplyOutgoingWSSToRequestAction( request, outgoing ) );
171 					}
172 				}} );
173 		}
174 
175 		public WsdlRequest getRequest()
176 		{
177 			return request;
178 		}
179 	}
180 	
181 	/***
182 	 * XmlSource editor for a WsdlMockRequest
183 	 * 
184 	 * @author ole.matzura
185 	 */
186 	
187 	public static class WsdlMockRequestXmlSourceEditor extends XmlSourceEditorView
188 	{
189 		private final WsdlMockResponse mockResponse;
190 
191 		public WsdlMockRequestXmlSourceEditor(XmlEditor xmlEditor, WsdlMockResponse mockResponse )
192 		{
193 			super(xmlEditor);
194 			this.mockResponse = mockResponse;
195 		}
196 
197 		protected ValidationError[] validateXml( String xml )
198 		{
199 			WsdlOperation operation = mockResponse.getMockOperation().getOperation();
200 			
201 			if( operation == null )
202 			{
203 				return new ValidationError[] { new AssertionError( "Missing operation for MockResponse")};
204 			}
205 			
206 			WsdlValidator validator = new WsdlValidator(((WsdlInterface) operation.getInterface()).getWsdlContext());
207 			return validator.assertRequest( new WsdlMockResultMessageExchange( mockResponse.getMockResult(), mockResponse ), false );
208 		}
209 
210 		protected void buildPopup(JPopupMenu inputPopup, JXEditTextArea editArea )
211 		{
212 			super.buildPopup(inputPopup, editArea );
213 			inputPopup.insert( new JSeparator(), 2 );
214 		}
215 	}
216 	
217 	/***
218 	 * XmlSource editor for a WsdlResponse
219 	 * 
220 	 * @author ole.matzura
221 	 */
222 	
223 	public static class WsdlResponseXmlSourceEditor extends XmlSourceEditorView
224 	{
225 		private final WsdlRequest request;
226 
227 		public WsdlResponseXmlSourceEditor(XmlEditor xmlEditor, WsdlRequest request )
228 		{
229 			super(xmlEditor);
230 			this.request = request;
231 		}
232 
233 		protected ValidationError[] validateXml( String xml )
234 		{
235 			if( request instanceof WsdlTestRequest )
236 			{
237 				WsdlTestRequest testRequest = (WsdlTestRequest)request;
238 				testRequest.assertResponse( new WsdlTestRunContext(  testRequest.getTestStep() ));
239 			}
240 			
241 			WsdlOperation operation = (WsdlOperation)request.getOperation();
242 			WsdlValidator validator = new WsdlValidator(((WsdlInterface) operation.getInterface()).getWsdlContext());
243 
244 			return validator.assertResponse( new WsdlResponseMessageExchange( request ), false );
245 		}
246 	}
247 	
248 	/***
249 	 * XmlSource editor for a WsdlMockResponse
250 	 * 
251 	 * @author ole.matzura
252 	 */
253 	
254 	public static class WsdlMockResponseXmlSourceEditor extends XmlSourceEditorView
255 	{
256 		private final WsdlMockResponse mockResponse;
257 		private JMenu applyMenu;
258 
259 		public WsdlMockResponseXmlSourceEditor(XmlEditor xmlEditor, WsdlMockResponse mockResponse)
260 		{
261 			super(xmlEditor);
262 			this.mockResponse = mockResponse;
263 		}
264 
265 		protected ValidationError[] validateXml( String xml )
266 		{
267 			WsdlOperation operation = mockResponse.getMockOperation().getOperation();
268 			if( operation == null )
269 			{
270 				return new ValidationError[] { new AssertionError( "Missing operation for MockResponse")};
271 			}
272 			
273 			WsdlValidator validator = new WsdlValidator(((WsdlInterface) operation.getInterface()).getWsdlContext());
274 			return validator.assertResponse( new WsdlMockResponseMessageExchange( mockResponse ), false );
275 		}
276 
277 		public WsdlMockResponse getMockResponse()
278 		{
279 			return mockResponse;
280 		}
281 		
282 		protected void buildPopup(JPopupMenu inputPopup, JXEditTextArea editArea )
283 		{
284 			super.buildPopup(inputPopup, editArea );
285 
286 			inputPopup.insert( applyMenu = new JMenu( "Apply Outgoing WSS"),2);
287 			
288 			inputPopup.addPopupMenuListener( new PopupMenuListener() {
289 
290 				public void popupMenuCanceled( PopupMenuEvent e )
291 				{
292 					
293 				}
294 
295 				public void popupMenuWillBecomeInvisible( PopupMenuEvent e )
296 				{
297 					
298 				}
299 
300 				public void popupMenuWillBecomeVisible( PopupMenuEvent e )
301 				{
302 					applyMenu.removeAll();
303 					DefaultWssContainer wss = mockResponse.getMockOperation().getMockService().getProject().getWssContainer();
304 					List<OutgoingWss> outgoingWssList = wss.getOutgoingWssList();
305 					applyMenu.setEnabled( !outgoingWssList.isEmpty() );
306 					
307 					for( OutgoingWss outgoing : outgoingWssList )
308 					{
309 						applyMenu.add( new ApplyOutgoingWSSToMockResponseAction( mockResponse, outgoing ) );
310 					}
311 				}} );
312 		}
313 	}
314 }