KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > protocol > http > control > gui > WebServiceSamplerGui


1 // $Header: /home/cvs/jakarta-jmeter/src/protocol/http/org/apache/jmeter/protocol/http/control/gui/WebServiceSamplerGui.java,v 1.11 2004/03/05 01:38:02 sebb Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter.protocol.http.control.gui;
20
21 import java.awt.Dimension JavaDoc;
22 import java.awt.Font JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.net.MalformedURLException JavaDoc;
25 import java.net.URL JavaDoc;
26
27 import javax.swing.JButton JavaDoc;
28 import javax.swing.JCheckBox JavaDoc;
29 import javax.swing.JOptionPane JavaDoc;
30 import javax.swing.JLabel JavaDoc;
31 import javax.swing.JPanel JavaDoc;
32 import javax.swing.border.Border JavaDoc;
33 import javax.swing.border.EmptyBorder JavaDoc;
34
35 import org.apache.jmeter.protocol.http.sampler.WebServiceSampler;
36 import org.apache.jmeter.samplers.gui.AbstractSamplerGui;
37 import org.apache.jmeter.testelement.TestElement;
38 import org.apache.jmeter.util.JMeterUtils;
39 import org.apache.jmeter.gui.util.FilePanel;
40 import org.apache.jorphan.gui.JLabeledChoice;
41 import org.apache.jorphan.gui.JLabeledTextArea;
42 import org.apache.jorphan.gui.JLabeledTextField;
43 import org.apache.jorphan.gui.layout.VerticalLayout;
44 import org.apache.jmeter.protocol.http.util.WSDLHelper;
45
46 /**
47  * This is the GUI for the webservice samplers. It extends
48  * AbstractSamplerGui and is modeled after the SOAP sampler
49  * GUI. I've added instructional notes to the GUI for
50  * instructional purposes. XML parsing is pretty heavy
51  * weight, therefore the notes address those situations.
52  * <br>
53  * Created on: Jun 26, 2003
54  *
55  * @author Peter Lin
56  * @version $Id: WebServiceSamplerGui.java,v 1.11 2004/03/05 01:38:02 sebb Exp $
57  */

58 public class WebServiceSamplerGui
59     extends AbstractSamplerGui
60     implements java.awt.event.ActionListener JavaDoc
61 {
62
63     JLabeledTextField urlField =
64         new JLabeledTextField(JMeterUtils.getResString("url"));
65     JLabeledTextField soapAction =
66         new JLabeledTextField(
67             JMeterUtils.getResString("webservice_soap_action"));
68     JLabeledTextArea soapXml =
69         new JLabeledTextArea(JMeterUtils.getResString("soap_data_title"), null);
70     JLabeledTextField wsdlField =
71         new JLabeledTextField(JMeterUtils.getResString("wsdl_url"));
72     JButton JavaDoc wsdlButton = new JButton JavaDoc(JMeterUtils.getResString("load_wsdl"));
73     JButton JavaDoc selectButton =
74         new JButton JavaDoc(JMeterUtils.getResString("configure_wsdl"));
75     JLabeledChoice wsdlMethods = null;
76     WSDLHelper HELPER = null;
77     FilePanel soapXmlFile =
78         new FilePanel(JMeterUtils.getResString("get_xml_from_file"), ".xml");
79     JLabeledTextField randomXmlFile =
80         new JLabeledTextField(JMeterUtils.getResString("get_xml_from_random"));
81
82     /**
83      * We create several JLabel objects to display
84      * usage instructions in the GUI. The reason
85      * there are multiple labels is to make sure
86      * it displays correctly.
87      */

88     JLabel JavaDoc wsdlMessage =
89         new JLabel JavaDoc(JMeterUtils.getResString("get_xml_message"));
90     JLabel JavaDoc wsdlMessage2 =
91         new JLabel JavaDoc(JMeterUtils.getResString("get_xml_message2"));
92     JLabel JavaDoc wsdlMessage3 =
93         new JLabel JavaDoc(JMeterUtils.getResString("get_xml_message3"));
94     JLabel JavaDoc wsdlMessage4 =
95         new JLabel JavaDoc(JMeterUtils.getResString("get_xml_message4"));
96     JLabel JavaDoc wsdlMessage5 =
97         new JLabel JavaDoc(JMeterUtils.getResString("get_xml_message5"));
98
99     /**
100      * This is the font for the note.
101      */

102     Font JavaDoc plainText = new Font JavaDoc("plain", Font.PLAIN, 10);
103     
104     /**
105      * checkbox for memory cache.
106      */

107     JCheckBox JavaDoc memCache =
108         new JCheckBox JavaDoc(JMeterUtils.getResString("memory_cache"), true);
109         
110     /**
111      * checkbox for reading the response
112      */

113     JCheckBox JavaDoc readResponse =
114         new JCheckBox JavaDoc(JMeterUtils.getResString("read_soap_response"));
115         
116     /**
117      * checkbox for use proxy
118      */

119     JCheckBox JavaDoc useProxy =
120         new JCheckBox JavaDoc(JMeterUtils.getResString("webservice_use_proxy"));
121
122     /**
123      * text field for the proxy host
124      */

125     JLabeledTextField proxyHost =
126         new JLabeledTextField(
127             JMeterUtils.getResString("webservice_proxy_host"));
128     /**
129      * text field for the proxy port
130      */

131     JLabeledTextField proxyPort =
132         new JLabeledTextField(
133             JMeterUtils.getResString("webservice_proxy_port"));
134     /**
135      * Text note about read response and it's usage.
136      */

137     JLabel JavaDoc readMessage =
138         new JLabel JavaDoc(JMeterUtils.getResString("read_response_note"));
139     JLabel JavaDoc readMessage2 =
140         new JLabel JavaDoc(JMeterUtils.getResString("read_response_note2"));
141     JLabel JavaDoc readMessage3 =
142         new JLabel JavaDoc(JMeterUtils.getResString("read_response_note3"));
143
144     /**
145      * Text note for proxy
146      */

147     JLabel JavaDoc proxyMessage =
148         new JLabel JavaDoc(JMeterUtils.getResString("webservice_proxy_note"));
149     JLabel JavaDoc proxyMessage2 =
150         new JLabel JavaDoc(JMeterUtils.getResString("webservice_proxy_note2"));
151     JLabel JavaDoc proxyMessage3 =
152         new JLabel JavaDoc(JMeterUtils.getResString("webservice_proxy_note3"));
153
154     public WebServiceSamplerGui()
155     {
156         init();
157     }
158
159     public String JavaDoc getLabelResource()
160     {
161         return "webservice_sampler_title";
162     }
163
164     /**
165      * @see org.apache.jmeter.gui.JMeterGUIComponent#createTestElement()
166      */

167     public TestElement createTestElement()
168     {
169         WebServiceSampler sampler = new WebServiceSampler();
170         this.configureTestElement(sampler);
171         try
172         {
173             URL JavaDoc url = new URL JavaDoc(urlField.getText());
174             sampler.setDomain(url.getHost());
175             if (url.getPort() != -1)
176             {
177                 sampler.setPort(url.getPort());
178             }
179             else
180             {
181                 sampler.setPort(80);
182             }
183             sampler.setProtocol(url.getProtocol());
184             sampler.setMethod(WebServiceSampler.POST);
185             sampler.setPath(url.getPath());
186             sampler.setSoapAction(soapAction.getText());
187             sampler.setXmlData(soapXml.getText());
188             sampler.setXmlFile(soapXmlFile.getFilename());
189             sampler.setXmlPathLoc(randomXmlFile.getText());
190             sampler.setMemoryCache(memCache.isSelected());
191             sampler.setReadResponse(readResponse.isSelected());
192             sampler.setUseProxy(useProxy.isSelected());
193             sampler.setProxyHost(proxyHost.getText());
194             sampler.setProxyPort(proxyPort.getText());
195         }
196         catch (MalformedURLException JavaDoc e)
197         {
198         }
199         return sampler;
200     }
201
202     /**
203      * Modifies a given TestElement to mirror the data in the gui components.
204      * @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement)
205      */

206     public void modifyTestElement(TestElement s)
207     {
208         WebServiceSampler sampler = (WebServiceSampler) s;
209         this.configureTestElement(sampler);
210         try
211         {
212             URL JavaDoc url = new URL JavaDoc(urlField.getText());
213             sampler.setDomain(url.getHost());
214             if (url.getPort() != -1)
215             {
216                 sampler.setPort(url.getPort());
217             }
218             else
219             {
220                 sampler.setPort(80);
221             }
222             sampler.setProtocol(url.getProtocol());
223             sampler.setMethod(WebServiceSampler.POST);
224             sampler.setPath(url.getPath());
225             sampler.setSoapAction(soapAction.getText());
226             sampler.setXmlData(soapXml.getText());
227             sampler.setXmlFile(soapXmlFile.getFilename());
228             sampler.setXmlPathLoc(randomXmlFile.getText());
229             sampler.setMemoryCache(memCache.isSelected());
230             sampler.setReadResponse(readResponse.isSelected());
231             sampler.setUseProxy(useProxy.isSelected());
232             sampler.setProxyHost(proxyHost.getText());
233             sampler.setProxyPort(proxyPort.getText());
234         }
235         catch (MalformedURLException JavaDoc e)
236         {
237         }
238     }
239
240     /**
241      * init() adds soapAction to the mainPanel. The class
242      * reuses logic from SOAPSampler, since it is common.
243      */

244     private void init()
245     {
246         this.setLayout(
247             new VerticalLayout(5, VerticalLayout.LEFT, VerticalLayout.TOP));
248         wsdlMessage.setFont(plainText);
249         wsdlMessage2.setFont(plainText);
250         wsdlMessage3.setFont(plainText);
251         wsdlMessage4.setFont(plainText);
252         wsdlMessage5.setFont(plainText);
253         readMessage.setFont(plainText);
254         readMessage2.setFont(plainText);
255         readMessage3.setFont(plainText);
256
257         // MAIN PANEL
258
JPanel JavaDoc mainPanel = new JPanel JavaDoc();
259         Border JavaDoc margin = new EmptyBorder JavaDoc(10, 10, 5, 10);
260         mainPanel.setBorder(margin);
261         mainPanel.setLayout(new VerticalLayout(5, VerticalLayout.LEFT));
262
263         // TITLE
264
JLabel JavaDoc panelTitleLabel = new JLabel JavaDoc(getStaticLabel());
265         Font JavaDoc curFont = panelTitleLabel.getFont();
266         int curFontSize = curFont.getSize();
267         curFontSize += 4;
268         panelTitleLabel.setFont(
269             new Font JavaDoc(curFont.getFontName(), curFont.getStyle(), curFontSize));
270         mainPanel.add(panelTitleLabel);
271         // NAME
272
mainPanel.add(getNamePanel());
273
274         // Button for browsing webservice wsdl
275
JPanel JavaDoc wsdlEntry = new JPanel JavaDoc();
276         mainPanel.add(wsdlEntry);
277         wsdlEntry.add(wsdlField);
278         wsdlEntry.add(wsdlButton);
279         wsdlButton.addActionListener(this);
280
281         // Web Methods
282
JPanel JavaDoc listPanel = new JPanel JavaDoc();
283         JLabel JavaDoc selectLabel = new JLabel JavaDoc("Web Methods");
284         wsdlMethods = new JLabeledChoice();
285         mainPanel.add(listPanel);
286         listPanel.add(selectLabel);
287         listPanel.add(wsdlMethods);
288         listPanel.add(selectButton);
289         selectButton.addActionListener(this);
290
291         mainPanel.add(urlField);
292         mainPanel.add(soapAction);
293         // OPTIONAL TASKS
294
// we create a preferred size for the soap text area
295
// the width is the same as the soap file browser
296
Dimension JavaDoc pref = new Dimension JavaDoc(400,200);
297         soapXml.setPreferredSize(pref);
298         mainPanel.add(soapXml);
299         mainPanel.add(soapXmlFile);
300         mainPanel.add(wsdlMessage);
301         mainPanel.add(wsdlMessage2);
302         mainPanel.add(wsdlMessage3);
303         mainPanel.add(wsdlMessage4);
304         mainPanel.add(wsdlMessage5);
305         mainPanel.add(randomXmlFile);
306         mainPanel.add(memCache);
307         mainPanel.add(readResponse);
308         mainPanel.add(readMessage);
309         mainPanel.add(readMessage2);
310         mainPanel.add(readMessage3);
311
312         // add the proxy elements
313
mainPanel.add(useProxy);
314         useProxy.addActionListener(this);
315         mainPanel.add(proxyHost);
316         mainPanel.add(proxyPort);
317         // add the proxy notes
318
proxyMessage.setFont(plainText);
319         proxyMessage2.setFont(plainText);
320         proxyMessage3.setFont(plainText);
321         mainPanel.add(proxyMessage);
322         mainPanel.add(proxyMessage2);
323         mainPanel.add(proxyMessage3);
324         
325         this.add(mainPanel);
326     }
327
328     /**
329      * the implementation loads the URL and the soap
330      * action for the request.
331      */

332     public void configure(TestElement el)
333     {
334         super.configure(el);
335         WebServiceSampler sampler = (WebServiceSampler) el;
336         try
337         {
338             // only set the URL if the host is not null
339
if (sampler.getUrl() != null && sampler.getUrl().getHost() != null)
340             {
341                 urlField.setText(sampler.getUrl().toString());
342             }
343             soapAction.setText(sampler.getSoapAction());
344         }
345         catch (MalformedURLException JavaDoc e)
346         {
347         }
348         // we build the string URL
349
int port = sampler.getPort();
350         String JavaDoc strUrl = sampler.getProtocol() + "://" + sampler.getDomain();
351         if (port != -1 && port != 80)
352         {
353             strUrl += ":" + sampler.getPort();
354         }
355         strUrl += sampler.getPath() + "?" + sampler.getQueryString();
356         urlField.setText(strUrl);
357         soapXml.setText(sampler.getXmlData());
358         soapXmlFile.setFilename(sampler.getXmlFile());
359         randomXmlFile.setText(sampler.getXmlPathLoc());
360         memCache.setSelected(sampler.getMemoryCache());
361         readResponse.setSelected(sampler.getReadResponse());
362         useProxy.setSelected(sampler.getUseProxy());
363         if (sampler.getProxyHost().length() == 0){
364             proxyHost.setEnabled(false);
365         } else {
366             proxyHost.setText(sampler.getProxyHost());
367         }
368         if (sampler.getProxyPort() == 0){
369             proxyPort.setEnabled(false);
370         } else {
371             proxyPort.setText(String.valueOf(sampler.getProxyPort()));
372         }
373     }
374
375     /**
376      * configure the sampler from the WSDL. If the
377      * WSDL did not include service node, it will
378      * use the original URL minus the querystring.
379      * That may not be correct, so we should
380      * probably add a note. For Microsoft webservices
381      * it will work, since that's how IIS works.
382      */

383     public void configureFromWSDL()
384     {
385         if (HELPER.getBinding() != null)
386         {
387             this.urlField.setText(HELPER.getBinding());
388         }
389         else
390         {
391             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
392             buf.append("http://" + HELPER.getURL().getHost());
393             if (HELPER.getURL().getPort() != -1)
394             {
395                 buf.append(":" + HELPER.getURL().getPort());
396             }
397             else
398             {
399                 buf.append(":" + 80);
400             }
401             buf.append(HELPER.getURL().getPath());
402             this.urlField.setText(buf.toString());
403         }
404         this.soapAction.setText(
405             HELPER.getSoapAction((String JavaDoc) this.wsdlMethods.getText()));
406     }
407
408     /**
409      * The method uses WSDLHelper to get the information
410      * from the WSDL. Since the logic for getting the
411      * description is isolated to this method, we can
412      * easily replace it with a different WSDL driver
413      * later on.
414      * @param url
415      * @return array of web methods
416      */

417     public String JavaDoc[] browseWSDL(String JavaDoc url)
418     {
419         try
420         {
421             HELPER = new WSDLHelper(url);
422             HELPER.parse();
423             return HELPER.getWebMethods();
424         }
425         catch (Exception JavaDoc exception)
426         {
427             JOptionPane.showConfirmDialog(
428                 this,
429                 JMeterUtils.getResString("wsdl_helper_error"),
430                 "Warning",
431                 JOptionPane.OK_CANCEL_OPTION,
432                 JOptionPane.ERROR_MESSAGE);
433             return null;
434         }
435     }
436
437     /**
438      * method from ActionListener
439      * @param event that occurred
440      */

441     public void actionPerformed(ActionEvent JavaDoc event)
442     {
443         if (event.getSource() == selectButton)
444         {
445             this.configureFromWSDL();
446         }
447         else if (event.getSource() == useProxy)
448         {
449             // if use proxy is checked, we enable
450
// the text fields for the host and port
451
boolean use = useProxy.isSelected();
452             if (use){
453                 proxyHost.setEnabled(true);
454                 proxyPort.setEnabled(true);
455             } else {
456                 proxyHost.setEnabled(false);
457                 proxyPort.setEnabled(false);
458             }
459         }
460         else
461         {
462             if (this.urlField.getText() != null)
463             {
464                 String JavaDoc[] wsdlData = browseWSDL(wsdlField.getText());
465                 if (wsdlData != null)
466                 {
467                     wsdlMethods.setValues(wsdlData);
468                     wsdlMethods.repaint();
469                 }
470             }
471             else
472             {
473                 JOptionPane.showConfirmDialog(
474                     this,
475                     JMeterUtils.getResString("wsdl_url_error"),
476                     "Warning",
477                     JOptionPane.OK_CANCEL_OPTION,
478                     JOptionPane.ERROR_MESSAGE);
479             }
480         }
481     }
482
483 }
484
Popular Tags