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.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 buildEditorComponent()
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 		if( xpathEditor != null )
103 			xpathEditor.release();
104 
105 		super.release();
106 	}
107 
108 	protected JXToolBar buildXPathEditorToolbar( DispatchXPathGroovyEditorModel editorModel )
109 	{
110 		JXToolBar toolbar = UISupport.createToolbar();
111 		toolbar.addSpace( 3 );
112 		addToolbarActions( editorModel, toolbar );
113 		toolbar.addGlue();
114 		toolbar.addFixed( ModelItemDesktopPanel.createActionButton( new ShowOnlineHelpAction(
115 				HelpUrls.MOCKOPERATION_XPATHDISPATCH_HELP_URL ), true ) );
116 		return toolbar;
117 	}
118 
119 	protected void addToolbarActions( DispatchXPathGroovyEditorModel editorModel, JXToolBar toolbar )
120 	{
121 		toolbar.addFixed( UISupport.createToolbarButton( editorModel.getRunAction() ) );
122 	}
123 
124 	public static class Factory implements MockOperationDispatchFactory
125 	{
126 		public MockOperationDispatcher build( WsdlMockOperation mockOperation )
127 		{
128 			return new XPathMockOperationDispatcher( mockOperation );
129 		}
130 	}
131 
132 	public class DispatchXPathGroovyEditorModel implements GroovyEditorModel
133 	{
134 		private RunXPathAction runXPathAction = new RunXPathAction();
135 
136 		public String[] getKeywords()
137 		{
138 			return new String[] { "define", "namespace" };
139 		}
140 
141 		public Action getRunAction()
142 		{
143 			return runXPathAction;
144 		}
145 
146 		public String getScript()
147 		{
148 			return getMockOperation().getDispatchPath();
149 		}
150 
151 		public Settings getSettings()
152 		{
153 			return getMockOperation().getSettings();
154 		}
155 
156 		public void setScript( String text )
157 		{
158 			getMockOperation().setDispatchPath( text );
159 		}
160 
161 		public String getScriptName()
162 		{
163 			return null;
164 		}
165 
166 		public void addPropertyChangeListener( PropertyChangeListener listener )
167 		{
168 		}
169 
170 		public void removePropertyChangeListener( PropertyChangeListener listener )
171 		{
172 		}
173 
174 		public ModelItem getModelItem()
175 		{
176 			return getMockOperation();
177 		}
178 	}
179 
180 	private class RunXPathAction extends AbstractAction
181 	{
182 		public RunXPathAction()
183 		{
184 			putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/run_groovy_script.gif" ) );
185 			putValue( Action.SHORT_DESCRIPTION, "Evaluates this xpath expression against the latest request" );
186 		}
187 
188 		public void actionPerformed( ActionEvent e )
189 		{
190 			WsdlMockResult lastMockResult = getMockOperation().getLastMockResult();
191 			if( lastMockResult == null )
192 			{
193 				UISupport.showErrorMessage( "Missing last request to select from" );
194 				return;
195 			}
196 
197 			try
198 			{
199 				WsdlMockResponse retVal = selectMockResponse( lastMockResult.getMockRequest(), null );
200 				UISupport.showInfoMessage( "XPath Selection returned [" + ( retVal == null ? "null" : retVal.getName() )
201 						+ "]" );
202 			}
203 			catch( Exception e1 )
204 			{
205 				SoapUI.logError( e1 );
206 			}
207 		}
208 	}
209 }