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