📄 webservicesampler.java
字号:
* The method will check to see if JMeter was started in NonGui mode. If it
* was, it will try to pick up the proxy host and port values if they were
* passed to JMeter.java.
*/
private void checkProxy() {
if (System.getProperty("JMeter.NonGui") != null && System.getProperty("JMeter.NonGui").equals("true")) {
this.setUseProxy(true);
// we check to see if the proxy host and port are set
String port = this.getPropertyAsString(PROXY_PORT);
String host = this.getPropertyAsString(PROXY_HOST);
if (host == null || host.length() == 0) {
// it's not set, lets check if the user passed
// proxy host and port from command line
host = System.getProperty("http.proxyHost");
if (host != null) {
this.setProxyHost(host);
}
}
if (port == null || port.length() == 0) {
// it's not set, lets check if the user passed
// proxy host and port from command line
port = System.getProperty("http.proxyPort");
if (port != null) {
this.setProxyPort(port);
}
}
}
}
/*
* This method uses Apache soap util to create the proper DOM elements.
*
* @return Element
*/
private org.w3c.dom.Element createDocument() throws SAXException, IOException {
Document doc = null;
String next = this.getRandomFileName();//get filename or ""
/* Note that the filename is also used as a key to the pool (if used)
** Documents provided in the testplan are not currently pooled, as they may change
* between samples.
*/
if (next.length() > 0 && getMemoryCache()) {
doc = DOMPool.getDocument(next);
if (doc == null){
doc = openDocument(next);
if (doc != null) {// we created the document
DOMPool.putDocument(next, doc);
}
}
} else { // Must be local content - or not using pool
doc = openDocument(next);
}
if (doc == null) {
return null;
}
return doc.getDocumentElement();
}
/**
* Open the file and create a Document.
*
* @param file - input filename or empty if using data from tesplan
* @return Document
* @throws IOException
* @throws SAXException
*/
private Document openDocument(String file) throws SAXException, IOException {
/*
* Consider using Apache commons pool to create a pool of document
* builders or make sure XMLParserUtils creates builders efficiently.
*/
DocumentBuilder XDB = XMLParserUtils.getXMLDocBuilder();
XDB.setErrorHandler(null);//Suppress messages to stdout
Document doc = null;
// if either a file or path location is given,
// get the file object.
if (file.length() > 0) {// we have a file
if (this.getReadResponse()) {
TextFile tfile = new TextFile(file);
fileContents = tfile.getText();
}
doc = XDB.parse(new FileInputStream(file));
} else {// must be a "here" document
fileContents = getXmlData();
if (fileContents != null && fileContents.length() > 0) {
doc = XDB.parse(new InputSource(new StringReader(fileContents)));
} else {
log.warn("No post data provided!");
}
}
return doc;
}
/*
* Required to satisfy HTTPSamplerBase Should not be called, as we override
* sample()
*/
protected HTTPSampleResult sample(URL u, String s, boolean b, int i) {
throw new RuntimeException("Not implemented - should not be called");
}
/**
* Sample the URL using Apache SOAP driver. Implementation note for myself
* and those that are curious. Current logic marks the end after the
* response has been read. If read response is set to false, the buffered
* reader will read, but do nothing with it. Essentially, the stream from
* the server goes into the ether.
*/
public SampleResult sample() {
SampleResult result = new SampleResult();
result.setSuccessful(false); // Assume it will fail
result.setResponseCode("000"); // ditto $NON-NLS-1$
result.setSampleLabel(getName());
try {
result.setURL(this.getUrl());
org.w3c.dom.Element rdoc = createDocument();
if (rdoc == null)
throw new SOAPException("Could not create document", null);
Envelope msgEnv = Envelope.unmarshall(rdoc);
// create a new message
Message msg = new Message();
result.sampleStart();
SOAPHTTPConnection spconn = null;
// if a blank HeaderManager exists, try to
// get the SOAPHTTPConnection. After the first
// request, there should be a connection object
// stored with the cookie header info.
if (this.getHeaderManager() != null && this.getHeaderManager().getSOAPHeader() != null) {
spconn = (SOAPHTTPConnection) this.getHeaderManager().getSOAPHeader();
} else {
spconn = new SOAPHTTPConnection();
}
spconn.setTimeout(getTimeoutAsInt());
// set the auth. thanks to KiYun Roe for contributing the patch
// I cleaned up the patch slightly. 5-26-05
if (getAuthManager() != null) {
if (getAuthManager().getAuthForURL(getUrl()) != null) {
AuthManager authmanager = getAuthManager();
Authorization auth = authmanager.getAuthForURL(getUrl());
spconn.setUserName(auth.getUser());
spconn.setPassword(auth.getPass());
} else {
log.warn("the URL for the auth was null." + " Username and password not set");
}
}
// check the proxy
String phost = "";
int pport = 0;
// if use proxy is set, we try to pick up the
// proxy host and port from either the text
// fields or from JMeterUtil if they were passed
// from command line
if (this.getUseProxy()) {
if (this.getProxyHost().length() > 0 && this.getProxyPort() > 0) {
phost = this.getProxyHost();
pport = this.getProxyPort();
} else {
if (System.getProperty("http.proxyHost") != null || System.getProperty("http.proxyPort") != null) {
phost = System.getProperty("http.proxyHost");
pport = Integer.parseInt(System.getProperty("http.proxyPort"));
}
}
// if for some reason the host is blank and the port is
// zero, the sampler will fail silently
if (phost.length() > 0 && pport > 0) {
spconn.setProxyHost(phost);
spconn.setProxyPort(pport);
if (PROXY_USER.length()>0 && PROXY_PASS.length()>0){
spconn.setProxyUserName(PROXY_USER);
spconn.setProxyPassword(PROXY_PASS);
}
}
}
// by default we maintain the session.
spconn.setMaintainSession(true);
msg.setSOAPTransport(spconn);
msg.send(this.getUrl(), this.getSoapAction(), msgEnv);
if (this.getHeaderManager() != null) {
this.getHeaderManager().setSOAPHeader(spconn);
}
SOAPTransport st = msg.getSOAPTransport();
result.setDataType(SampleResult.TEXT);
BufferedReader br = null;
// check to see if SOAPTransport is not nul and receive is
// also not null. hopefully this will improve the error
// reporting. 5/13/05 peter lin
if (st != null && st.receive() != null) {
br = st.receive();
if (getReadResponse()) {
StringBuffer buf = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
buf.append(line);
}
result.sampleEnd();
// set the response
result.setResponseData(buf.toString().getBytes());
} else {
// by not reading the response
// for real, it improves the
// performance on slow clients
br.read();
result.sampleEnd();
result.setResponseData(JMeterUtils.getResString("read_response_message").getBytes()); //$NON-NLS-1$
}
result.setSuccessful(true);
result.setResponseCodeOK();
result.setResponseHeaders(this.convertSoapHeaders(st.getHeaders()));
} else {
result.sampleEnd();
result.setSuccessful(false);
if (st != null){
result.setResponseData(st.getResponseSOAPContext().getContentType().getBytes());
}
result.setResponseHeaders("error");
}
// 1-22-04 updated the sampler so that when read
// response is set, it also sets SamplerData with
// the XML message, so users can see what was
// sent. if read response is not checked, it will
// not set sampler data with the request message.
// peter lin.
// Removed URL, as that is already stored elsewere
result.setSamplerData(fileContents);// WARNING - could be large
result.setEncodingAndType(st.getResponseSOAPContext().getContentType());
// setting this is just a formality, since
// soap will return a descriptive error
// message, soap errors within the response
// are preferred.
if (br != null) {
br.close();
}
// reponse code doesn't really apply, since
// the soap driver doesn't provide a
// response code
} catch (IllegalArgumentException exception){
String message = exception.getMessage();
log.warn(message);
result.setResponseMessage(message);
} catch (SAXException exception) {
log.warn(exception.toString());
result.setResponseMessage(exception.getMessage());
} catch (SOAPException exception) {
log.warn(exception.toString());
result.setResponseMessage(exception.getMessage());
} catch (MalformedURLException exception) {
String message = exception.getMessage();
log.warn(message);
result.setResponseMessage(message);
} catch (IOException exception) {
String message = exception.getMessage();
log.warn(message);
result.setResponseMessage(message);
} catch (NoClassDefFoundError error){
log.error("Missing class: ",error);
result.setResponseMessage(error.toString());
} catch (Exception exception) {
if ("javax.mail.MessagingException".equals(exception.getClass().getName())){
log.warn(exception.toString());
result.setResponseMessage(exception.getMessage());
} else {
throw new RuntimeException(exception);
}
}
return result;
}
/**
* We override this to prevent the wrong encoding and provide no
* implementation. We want to reuse the other parts of HTTPSampler, but not
* the connection. The connection is handled by the Apache SOAP driver.
*/
public void addEncodedArgument(String name, String value, String metaData) {
}
public String convertSoapHeaders(Hashtable ht) {
Enumeration en = ht.keys();
StringBuffer buf = new StringBuffer();
while (en.hasMoreElements()) {
Object key = en.nextElement();
buf.append((String) key).append("=").append((String) ht.get(key)).append("\n"); //$NON-NLS-1$ //$NON-NLS-2$
}
return buf.toString();
}
public String getTimeout() {
return getPropertyAsString(TIMEOUT);
}
public int getTimeoutAsInt() {
return getPropertyAsInt(TIMEOUT);
}
public void setTimeout(String text) {
setProperty(TIMEOUT, text);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -