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