1 18 19 package org.apache.jmeter.protocol.http.control.gui; 20 21 import java.awt.Dimension ; 22 import java.awt.Font ; 23 import java.awt.event.ActionEvent ; 24 import java.net.MalformedURLException ; 25 import java.net.URL ; 26 27 import javax.swing.JButton ; 28 import javax.swing.JCheckBox ; 29 import javax.swing.JOptionPane ; 30 import javax.swing.JLabel ; 31 import javax.swing.JPanel ; 32 import javax.swing.border.Border ; 33 import javax.swing.border.EmptyBorder ; 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 58 public class WebServiceSamplerGui 59 extends AbstractSamplerGui 60 implements java.awt.event.ActionListener 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 wsdlButton = new JButton (JMeterUtils.getResString("load_wsdl")); 73 JButton selectButton = 74 new JButton (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 88 JLabel wsdlMessage = 89 new JLabel (JMeterUtils.getResString("get_xml_message")); 90 JLabel wsdlMessage2 = 91 new JLabel (JMeterUtils.getResString("get_xml_message2")); 92 JLabel wsdlMessage3 = 93 new JLabel (JMeterUtils.getResString("get_xml_message3")); 94 JLabel wsdlMessage4 = 95 new JLabel (JMeterUtils.getResString("get_xml_message4")); 96 JLabel wsdlMessage5 = 97 new JLabel (JMeterUtils.getResString("get_xml_message5")); 98 99 102 Font plainText = new Font ("plain", Font.PLAIN, 10); 103 104 107 JCheckBox memCache = 108 new JCheckBox (JMeterUtils.getResString("memory_cache"), true); 109 110 113 JCheckBox readResponse = 114 new JCheckBox (JMeterUtils.getResString("read_soap_response")); 115 116 119 JCheckBox useProxy = 120 new JCheckBox (JMeterUtils.getResString("webservice_use_proxy")); 121 122 125 JLabeledTextField proxyHost = 126 new JLabeledTextField( 127 JMeterUtils.getResString("webservice_proxy_host")); 128 131 JLabeledTextField proxyPort = 132 new JLabeledTextField( 133 JMeterUtils.getResString("webservice_proxy_port")); 134 137 JLabel readMessage = 138 new JLabel (JMeterUtils.getResString("read_response_note")); 139 JLabel readMessage2 = 140 new JLabel (JMeterUtils.getResString("read_response_note2")); 141 JLabel readMessage3 = 142 new JLabel (JMeterUtils.getResString("read_response_note3")); 143 144 147 JLabel proxyMessage = 148 new JLabel (JMeterUtils.getResString("webservice_proxy_note")); 149 JLabel proxyMessage2 = 150 new JLabel (JMeterUtils.getResString("webservice_proxy_note2")); 151 JLabel proxyMessage3 = 152 new JLabel (JMeterUtils.getResString("webservice_proxy_note3")); 153 154 public WebServiceSamplerGui() 155 { 156 init(); 157 } 158 159 public String getLabelResource() 160 { 161 return "webservice_sampler_title"; 162 } 163 164 167 public TestElement createTestElement() 168 { 169 WebServiceSampler sampler = new WebServiceSampler(); 170 this.configureTestElement(sampler); 171 try 172 { 173 URL url = new URL (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 e) 197 { 198 } 199 return sampler; 200 } 201 202 206 public void modifyTestElement(TestElement s) 207 { 208 WebServiceSampler sampler = (WebServiceSampler) s; 209 this.configureTestElement(sampler); 210 try 211 { 212 URL url = new URL (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 e) 236 { 237 } 238 } 239 240 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 JPanel mainPanel = new JPanel (); 259 Border margin = new EmptyBorder (10, 10, 5, 10); 260 mainPanel.setBorder(margin); 261 mainPanel.setLayout(new VerticalLayout(5, VerticalLayout.LEFT)); 262 263 JLabel panelTitleLabel = new JLabel (getStaticLabel()); 265 Font curFont = panelTitleLabel.getFont(); 266 int curFontSize = curFont.getSize(); 267 curFontSize += 4; 268 panelTitleLabel.setFont( 269 new Font (curFont.getFontName(), curFont.getStyle(), curFontSize)); 270 mainPanel.add(panelTitleLabel); 271 mainPanel.add(getNamePanel()); 273 274 JPanel wsdlEntry = new JPanel (); 276 mainPanel.add(wsdlEntry); 277 wsdlEntry.add(wsdlField); 278 wsdlEntry.add(wsdlButton); 279 wsdlButton.addActionListener(this); 280 281 JPanel listPanel = new JPanel (); 283 JLabel selectLabel = new JLabel ("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 Dimension pref = new Dimension (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 mainPanel.add(useProxy); 314 useProxy.addActionListener(this); 315 mainPanel.add(proxyHost); 316 mainPanel.add(proxyPort); 317 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 332 public void configure(TestElement el) 333 { 334 super.configure(el); 335 WebServiceSampler sampler = (WebServiceSampler) el; 336 try 337 { 338 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 e) 346 { 347 } 348 int port = sampler.getPort(); 350 String 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 383 public void configureFromWSDL() 384 { 385 if (HELPER.getBinding() != null) 386 { 387 this.urlField.setText(HELPER.getBinding()); 388 } 389 else 390 { 391 StringBuffer buf = new StringBuffer (); 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 ) this.wsdlMethods.getText())); 406 } 407 408 417 public String [] browseWSDL(String url) 418 { 419 try 420 { 421 HELPER = new WSDLHelper(url); 422 HELPER.parse(); 423 return HELPER.getWebMethods(); 424 } 425 catch (Exception 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 441 public void actionPerformed(ActionEvent event) 442 { 443 if (event.getSource() == selectButton) 444 { 445 this.configureFromWSDL(); 446 } 447 else if (event.getSource() == useProxy) 448 { 449 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 [] 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 |