1 18 19 package org.apache.jmeter.protocol.http.sampler; 20 21 import java.io.BufferedReader ; 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.IOException ; 25 import java.io.StringReader ; 26 import java.net.HttpURLConnection ; 27 import java.net.URL ; 28 import java.util.Random ; 29 30 import javax.xml.parsers.DocumentBuilder ; 31 32 import org.xml.sax.InputSource ; 33 34 import org.apache.jorphan.io.TextFile; 35 36 import org.apache.jmeter.gui.JMeterFileFilter; 37 import org.apache.jmeter.protocol.http.util.DOMPool; 38 import org.apache.jmeter.samplers.Entry; 39 import org.apache.jmeter.samplers.SampleResult; 40 import org.apache.jmeter.util.JMeterUtils; 41 import org.apache.soap.Envelope; 42 import org.apache.soap.messaging.Message; 43 import org.apache.soap.transport.SOAPTransport; 44 import org.apache.soap.transport.http.SOAPHTTPConnection; 45 import org.apache.soap.util.xml.XMLParserUtils; 46 import org.w3c.dom.Document ; 47 48 57 public class WebServiceSampler extends HTTPSampler 58 { 59 public static final String XML_DATA = "HTTPSamper.xml_data"; 60 public static final String SOAP_ACTION = "Soap.Action"; 61 public static final String XML_DATA_FILE = 62 "WebServiceSampler.xml_data_file"; 63 public static final String XML_PATH_LOC = "WebServiceSampler.xml_path_loc"; 64 public static final String MEMORY_CACHE = "WebServiceSampler.memory_cache"; 65 public static final String READ_RESPONSE = 66 "WebServiceSampler.read_response"; 67 public static final String USE_PROXY = "WebServiceSampler.use_proxy"; 68 public static final String PROXY_HOST = "WebServiceSampler.proxy_host"; 69 public static final String PROXY_PORT = "WebServiceSampler.proxy_port"; 70 71 76 protected String SOAPACTION = null; 77 78 81 transient SampleResult RESULT = null; 82 83 86 protected Document XMLMSG = null; 87 88 91 private int FILE_COUNT = -1; 92 93 96 private File [] FILE_LIST = null; 97 98 102 private Random RANDOM = new Random (); 103 104 112 protected static DocumentBuilder XDB = null; 113 114 protected String FILE_CONTENTS = null; 115 116 119 public void setXmlPathLoc(String path) 120 { 121 setProperty(XML_PATH_LOC, path); 122 } 123 124 128 public String getXmlPathLoc() 129 { 130 return getPropertyAsString(XML_PATH_LOC); 131 } 132 133 138 public void setXmlFile(String filename) 139 { 140 setProperty(XML_DATA_FILE, filename); 141 } 142 143 147 public String getXmlFile() 148 { 149 return getPropertyAsString(XML_DATA_FILE); 150 } 151 152 163 private File retrieveRuntimeXmlData() 164 { 165 String file = getRandomFileName(); 166 if (file.length() > 0) 167 { 168 if (this.getReadResponse()){ 169 TextFile tfile = new TextFile(file); 170 FILE_CONTENTS = tfile.getText(); 171 } 172 return new File (file); 173 } else { 174 return null; 175 } 176 } 177 178 185 protected String getRandomFileName() 186 { 187 if (this.getXmlPathLoc() != null) 188 { 189 File src = new File (this.getXmlPathLoc()); 190 if (src.isDirectory() && src.list() != null) 191 { 192 FILE_LIST = 193 src.listFiles( 194 new JMeterFileFilter(new String [] { ".xml" })); 195 this.FILE_COUNT = FILE_LIST.length; 196 File one = FILE_LIST[RANDOM.nextInt(FILE_COUNT)]; 197 return one.getAbsolutePath(); 199 } 200 else 201 { 202 return getXmlFile(); 203 } 204 } 205 else 206 { 207 return getXmlFile(); 208 } 209 } 210 211 215 public void setXmlData(String data) 216 { 217 setProperty(XML_DATA, data); 218 } 219 220 224 public String getXmlData() 225 { 226 return getPropertyAsString(XML_DATA); 227 } 228 229 233 public void setSoapAction(String data) 234 { 235 setProperty(SOAP_ACTION, data); 236 } 237 238 242 public String getSoapAction() 243 { 244 return getPropertyAsString(SOAP_ACTION); 245 } 246 247 251 public void setMemoryCache(boolean cache) 252 { 253 setProperty(MEMORY_CACHE, String.valueOf(cache)); 254 } 255 256 260 public boolean getMemoryCache() 261 { 262 return getPropertyAsBoolean(MEMORY_CACHE); 263 } 264 265 269 public void setReadResponse(boolean read) 270 { 271 setProperty(READ_RESPONSE, String.valueOf(read)); 272 } 273 274 278 public boolean getReadResponse() 279 { 280 return this.getPropertyAsBoolean(READ_RESPONSE); 281 } 282 283 287 public void setUseProxy(boolean proxy){ 288 setProperty(USE_PROXY, String.valueOf(proxy)); 289 } 290 291 295 public boolean getUseProxy(){ 296 return this.getPropertyAsBoolean(USE_PROXY); 297 } 298 299 303 public void setProxyHost(String host){ 304 setProperty(PROXY_HOST, host); 305 } 306 307 311 public String getProxyHost(){ 312 this.checkProxy(); 313 return this.getPropertyAsString(PROXY_HOST); 314 } 315 316 320 public void setProxyPort(String port){ 321 setProperty(PROXY_PORT, port); 322 } 323 324 328 public int getProxyPort(){ 329 this.checkProxy(); 330 return this.getPropertyAsInt(PROXY_PORT); 331 } 332 333 339 public void checkProxy(){ 340 if (System.getProperty("JMeter.NonGui") != null && 341 System.getProperty("JMeter.NonGui").equals("true")){ 342 this.setUseProxy(true); 343 String port = this.getPropertyAsString(PROXY_PORT); 345 String host = this.getPropertyAsString(PROXY_HOST); 346 if (host == null || host.length() == 0){ 347 if (System.getProperty("http.proxyHost") != null){ 350 host = System.getProperty("http.proxyHost"); 351 this.setProxyHost(host); 352 } 353 } 354 if (port == null || port.length() == 0){ 355 if (System.getProperty("http.proxyPort") != null){ 358 port = System.getProperty("http.proxyPort"); 359 this.setProxyPort(port); 360 } 361 } 362 } 363 } 364 365 369 public org.w3c.dom.Element createDocument() 370 { 371 if (getPropertyAsBoolean(MEMORY_CACHE)) 372 { 373 String next = this.getRandomFileName(); 374 if (DOMPool.getDocument(next) != null) 375 { 376 return ((Document ) DOMPool.getDocument(next)) 377 .getDocumentElement(); 378 } 379 else 380 { 381 return openDocument(next).getDocumentElement(); 382 } 383 } 384 else 385 { 386 return openDocument(null).getDocumentElement(); 387 } 388 } 389 390 395 protected Document openDocument(String key) 396 { 397 if (XDB == null) 398 { 399 XDB = XMLParserUtils.getXMLDocBuilder(); 400 } 401 Document doc = null; 402 if (getXmlFile().length() > 0 || getXmlPathLoc().length() > 0){ 405 try { 406 doc = XDB.parse(new FileInputStream (retrieveRuntimeXmlData())); 407 } catch (Exception e){ 408 } 410 } else { 411 FILE_CONTENTS = getXmlData(); 412 if (FILE_CONTENTS != null && FILE_CONTENTS.length() > 0) 413 { 414 try 415 { 416 doc = XDB.parse( 417 new InputSource (new StringReader (FILE_CONTENTS))); 418 } 419 catch (Exception ex) 420 { 421 } 423 } 424 } 425 if (this.getPropertyAsBoolean(MEMORY_CACHE)) 426 { 427 DOMPool.putDocument(key, doc); 428 } 429 return doc; 430 } 431 432 437 public SampleResult sample(Entry e) 438 { 439 return sample(); 440 } 441 442 447 public SampleResult sample() 448 { 449 RESULT = new SampleResult(); 450 sampleWithApache(); 451 return RESULT; 452 } 453 454 461 public void sampleWithApache() 462 { 463 try 464 { 465 org.w3c.dom.Element rdoc = createDocument(); 466 Envelope msgEnv = Envelope.unmarshall(rdoc); 467 Message msg = new Message(); 469 RESULT.sampleStart(); 470 SOAPHTTPConnection spconn = null; 471 if (this.getHeaderManager() != null && 476 this.getHeaderManager().getSOAPHeader() != null) { 477 spconn = (SOAPHTTPConnection)this.getHeaderManager(). 478 getSOAPHeader(); 479 } else { 480 spconn = new SOAPHTTPConnection(); 481 } 482 String phost = ""; 484 int pport = 0; 485 if (this.getUseProxy()){ 490 if (this.getProxyHost().length() > 0 && this.getProxyPort() > 0){ 491 phost = this.getProxyHost(); 492 pport = this.getProxyPort(); 493 } else { 494 if (System.getProperty("http.proxyHost") != null || 495 System.getProperty("http.proxyPort") != null){ 496 phost = System.getProperty("http.proxyHost"); 497 pport = Integer.parseInt( 498 System.getProperty("http.proxyPort")); 499 } 500 } 501 if (phost.length() > 0 && pport > 0){ 504 spconn.setProxyHost(phost); 505 spconn.setProxyPort(pport); 506 } 507 } 508 spconn.setMaintainSession(true); 510 msg.setSOAPTransport(spconn); 511 msg.send(this.getUrl(), this.getSoapAction(), msgEnv); 512 513 if (this.getHeaderManager() != null){ 514 this.getHeaderManager().setSOAPHeader(spconn); 515 } 516 517 SOAPTransport st = msg.getSOAPTransport(); 518 BufferedReader br = st.receive(); 519 RESULT.setDataType(SampleResult.TEXT); 520 if (this.getPropertyAsBoolean(READ_RESPONSE)) 521 { 522 StringBuffer buf = new StringBuffer (); 523 String line; 524 while ((line = br.readLine()) != null) 525 { 526 buf.append(line); 527 } 528 RESULT.sampleEnd(); 529 RESULT.setResponseData(buf.toString().getBytes()); 531 } 532 else 533 { 534 br.read(); 538 RESULT.sampleEnd(); 539 RESULT.setResponseData( 540 JMeterUtils 541 .getResString("read_response_message") 542 .getBytes()); 543 } 544 RESULT.setSuccessful(true); 545 RESULT.setSamplerData( 552 getUrl().getProtocol() 553 + "://" 554 + getUrl().getHost() 555 + "/" 556 + getUrl().getFile() 557 + "\n" 558 + FILE_CONTENTS); 559 RESULT.setDataEncoding( 560 st.getResponseSOAPContext().getContentType()); 561 RESULT.setResponseCode("200"); 566 br.close(); 567 msg = null; 568 st = null; 569 } 573 catch (Exception exception) 574 { 575 RESULT.setSuccessful(false); 577 } 578 } 579 580 585 public void addEncodedArgument(String name, String value, String metaData) 586 { 587 } 588 589 594 protected HttpURLConnection setupConnection(URL u, String method) 595 throws IOException 596 { 597 return null; 598 } 599 600 605 protected long connect() throws IOException 606 { 607 return -1; 608 } 609 } 610 | Popular Tags |