View Javadoc

1   /*
2    * soapUI, copyright (C) 2004-2010 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.mock.dispatch;
14  
15  import java.awt.BorderLayout;
16  import java.awt.event.ActionEvent;
17  import java.beans.PropertyChangeListener;
18  
19  import javax.swing.AbstractAction;
20  import javax.swing.Action;
21  import javax.swing.JComponent;
22  import javax.swing.JPanel;
23  
24  import org.apache.xmlbeans.XmlException;
25  import org.apache.xmlbeans.XmlObject;
26  
27  import com.eviware.soapui.SoapUI;
28  import com.eviware.soapui.impl.support.actions.ShowOnlineHelpAction;
29  import com.eviware.soapui.impl.wsdl.mock.DispatchException;
30  import com.eviware.soapui.impl.wsdl.mock.WsdlMockOperation;
31  import com.eviware.soapui.impl.wsdl.mock.WsdlMockRequest;
32  import com.eviware.soapui.impl.wsdl.mock.WsdlMockResponse;
33  import com.eviware.soapui.impl.wsdl.mock.WsdlMockResult;
34  import com.eviware.soapui.impl.wsdl.panels.teststeps.support.GroovyEditor;
35  import com.eviware.soapui.impl.wsdl.panels.teststeps.support.GroovyEditorModel;
36  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
37  import com.eviware.soapui.model.ModelItem;
38  import com.eviware.soapui.model.settings.Settings;
39  import com.eviware.soapui.support.StringUtils;
40  import com.eviware.soapui.support.UISupport;
41  import com.eviware.soapui.support.components.JXToolBar;
42  import com.eviware.soapui.support.xml.XmlUtils;
43  import com.eviware.soapui.ui.support.ModelItemDesktopPanel;
44  
45  public class XPathMockOperationDispatcher extends AbstractMockOperationDispatcher
46  {
47  	private GroovyEditor xpathEditor;
48  
49  	public XPathMockOperationDispatcher( WsdlMockOperation mockOperation )
50  	{
51  		super( mockOperation );
52  	}
53  
54  	public WsdlMockResponse selectMockResponse( WsdlMockRequest request, WsdlMockResult result )
55  			throws DispatchException
56  	{
57  		XmlObject xmlObject;
58  		try
59  		{
60  			xmlObject = request.getRequestXmlObject();
61  		}
62  		catch( XmlException e )
63  		{
64  			throw new DispatchException( "Error getting XmlObject for request: " + e );
65  		}
66  
67  		String path = getMockOperation().getDispatchPath();
68  		if( StringUtils.isNullOrEmpty( path ) )
69  			throw new DispatchException( "Missing dispatch XPath expression" );
70  
71  		String[] values = XmlUtils.selectNodeValues( xmlObject, path );
72  		for( String value : values )
73  		{
74  			WsdlMockResponse mockResponse = getMockOperation().getMockResponseByName( value );
75  			if( mockResponse != null )
76  				return mockResponse;
77  		}
78  
79  		return null;
80  	}
81  
82  	@Override
83  	public JComponent getEditorComponent()
84  	{
85  		JPanel xpathEditorPanel = new JPanel( new BorderLayout() );
86  		DispatchXPathGroovyEditorModel editorModel = new DispatchXPathGroovyEditorModel();
87  		xpathEditor = new GroovyEditor( editorModel );
88  		xpathEditorPanel.add( xpathEditor, BorderLayout.CENTER );
89  		xpathEditorPanel.add( buildXPathEditorToolbar( editorModel ), BorderLayout.PAGE_START );
90  
91  		return xpathEditorPanel;
92  	}
93  
94  	public GroovyEditor getXPathEditor()
95  	{
96  		return xpathEditor;
97  	}
98  
99  	@Override
100 	public void release()
101 	{
102 		releaseEditorComponent();
103 		super.release();
104 	}
105 
106 	@Override
107 	public void releaseEditorComponent()
108 	{
109 		if( xpathEditor != null )
110 			xpathEditor.release();
111 
112 		super.releaseEditorComponent();
113 	}
114 
115 	protected JXToolBar buildXPathEditorToolbar( DispatchXPathGroovyEditorModel editorModel )
116 	{
117 		JXToolBar toolbar = UISupport.createToolbar();
118 		toolbar.addSpace( 3 );
119 		addToolbarActions( editorModel, toolbar );
120 		toolbar.addGlue();
121 		toolbar.addFixed( ModelItemDesktopPanel.createActionButton( new ShowOnlineHelpAction(
122 				HelpUrls.MOCKOPERATION_XPATHDISPATCH_HELP_URL ), true ) );
123 		return toolbar;
124 	}
125 
126 	protected void addToolbarActions( DispatchXPathGroovyEditorModel editorModel, JXToolBar toolbar )
127 	{
128 		toolbar.addFixed( UISupport.createToolbarButton( editorModel.getRunAction() ) );
129 	}
130 
131 	public static class Factory implements MockOperationDispatchFactory
132 	{
133 		public MockOperationDispatcher build( WsdlMockOperation mockOperation )
134 		{
135 			return new XPathMockOperationDispatcher( mockOperation );
136 		}
137 	}
138 
139 	public class DispatchXPathGroovyEditorModel implements GroovyEditorModel
140 	{
141 		private RunXPathAction runXPathAction = new RunXPathAction();
142 
143 		public String[] getKeywords()
144 		{
145 			return new String[] { "define", "namespace" };
146 		}
147 
148 		public Action getRunAction()
149 		{
150 			return runXPathAction;
151 		}
152 
153 		public String getScript()
154 		{
155 			return getMockOperation().getDispatchPath();
156 		}
157 
158 		public Settings getSettings()
159 		{
160 			return getMockOperation().getSettings();
161 		}
162 
163 		public void setScript( String text )
164 		{
165 			getMockOperation().setDispatchPath( text );
166 		}
167 
168 		public String getScriptName()
169 		{
170 			return null;
171 		}
172 
173 		public void addPropertyChangeListener( PropertyChangeListener listener )
174 		{
175 		}
176 
177 		public void removePropertyChangeListener( PropertyChangeListener listener )
178 		{
179 		}
180 
181 		public ModelItem getModelItem()
182 		{
183 			return getMockOperation();
184 		}
185 	}
186 
187 	private class RunXPathAction extends AbstractAction
188 	{
189 		public RunXPathAction()
190 		{
191 			putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/run_groovy_script.gif" ) );
192 			putValue( Action.SHORT_DESCRIPTION, "Evaluates this xpath expression against the latest request" );
193 		}
194 
195 		public void actionPerformed( ActionEvent e )
196 		{
197 			WsdlMockResult lastMockResult = getMockOperation().getLastMockResult();
198 			if( lastMockResult == null )
199 			{
200 				UISupport.showErrorMessage( "Missing last request to select from" );
201 				return;
202 			}
203 
204 			try
205 			{
206 				WsdlMockResponse retVal = selectMockResponse( lastMockResult.getMockRequest(), null );
207 				UISupport.showInfoMessage( "XPath Selection returned [" + ( retVal == null ? "null" : retVal.getName() )
208 						+ "]" );
209 			}
210 			catch( Exception e1 )
211 			{
212 				SoapUI.logError( e1 );
213 			}
214 		}
215 	}
216 }