📄 webservicesampler.java
字号:
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.jmeter.protocol.http.sampler;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.StringReader;
import java.net.URL;
import java.net.MalformedURLException;
import java.util.Enumeration;
import java.util.Random;
import java.util.Hashtable;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.apache.jorphan.io.TextFile;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.jmeter.JMeter;
import org.apache.jmeter.gui.JMeterFileFilter;
import org.apache.jmeter.protocol.http.control.AuthManager;
import org.apache.jmeter.protocol.http.control.Authorization;
import org.apache.jmeter.protocol.http.util.DOMPool;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.log.Logger;
import org.apache.soap.Envelope;
import org.apache.soap.messaging.Message;
import org.apache.soap.transport.SOAPTransport;
import org.apache.soap.transport.http.SOAPHTTPConnection;
import org.apache.soap.util.xml.XMLParserUtils;
import org.apache.soap.SOAPException;
import org.w3c.dom.Document;
/**
* Sampler to handle Web Service requests. It uses Apache SOAP drivers to
* perform the XML generation, connection, SOAP encoding and other SOAP
* functions.
* <p>
* Created on: Jun 26, 2003
*
*/
public class WebServiceSampler extends HTTPSamplerBase {
private static Logger log = LoggingManager.getLoggerForClass();
//+ JMX file attribut names - do not change!
private static final String XML_DATA = "HTTPSamper.xml_data"; //$NON-NLS-1$
private static final String SOAP_ACTION = "Soap.Action"; //$NON-NLS-1$
private static final String XML_DATA_FILE = "WebServiceSampler.xml_data_file"; //$NON-NLS-1$
private static final String XML_PATH_LOC = "WebServiceSampler.xml_path_loc"; //$NON-NLS-1$
private static final String MEMORY_CACHE = "WebServiceSampler.memory_cache"; //$NON-NLS-1$
private static final String READ_RESPONSE = "WebServiceSampler.read_response"; //$NON-NLS-1$
private static final String USE_PROXY = "WebServiceSampler.use_proxy"; //$NON-NLS-1$
private static final String PROXY_HOST = "WebServiceSampler.proxy_host"; //$NON-NLS-1$
private static final String PROXY_PORT = "WebServiceSampler.proxy_port"; //$NON-NLS-1$
private static final String WSDL_URL = "WebserviceSampler.wsdl_url"; //$NON-NLS-1$
private static final String TIMEOUT = "WebserviceSampler.timeout"; //$NON-NLS-1$
//- JMX file attribut names - do not change!
private static final String PROXY_USER =
JMeterUtils.getPropDefault(JMeter.HTTP_PROXY_USER,""); // $NON-NLS-1$
private static final String PROXY_PASS =
JMeterUtils.getPropDefault(JMeter.HTTP_PROXY_PASS,""); // $NON-NLS-1$
/*
* Random class for generating random numbers.
*/
private final Random RANDOM = new Random();
private String fileContents = null;
/**
* Set the path where XML messages are stored for random selection.
*/
public void setXmlPathLoc(String path) {
setProperty(XML_PATH_LOC, path);
}
/**
* Get the path where XML messages are stored. this is the directory where
* JMeter will randomly select a file.
*/
public String getXmlPathLoc() {
return getPropertyAsString(XML_PATH_LOC);
}
/**
* it's kinda obvious, but we state it anyways. Set the xml file with a
* string path.
*
* @param filename
*/
public void setXmlFile(String filename) {
setProperty(XML_DATA_FILE, filename);
}
/**
* Get the file location of the xml file.
*
* @return String file path.
*/
public String getXmlFile() {
return getPropertyAsString(XML_DATA_FILE);
}
/**
* Method is used internally to check if a random file should be used for
* the message. Messages must be valid. This is one way to load test with
* different messages. The limitation of this approach is parsing XML takes
* CPU resources, so it could affect JMeter GUI responsiveness.
*
* @return String filename
*/
protected String getRandomFileName() {
if (this.getXmlPathLoc() != null) {
File src = new File(this.getXmlPathLoc());
if (src.isDirectory() && src.list() != null) {
File [] fileList = src.listFiles(new JMeterFileFilter(new String[] { ".xml" }, false));
File one = fileList[RANDOM.nextInt(fileList.length)];
// return the absolutePath of the file
return one.getAbsolutePath();
} else {
return getXmlFile();
}
} else {
return getXmlFile();
}
}
/**
* Set the XML data.
*
* @param data
*/
public void setXmlData(String data) {
setProperty(XML_DATA, data);
}
/**
* Get the XML data as a string.
*
* @return String data
*/
public String getXmlData() {
return getPropertyAsString(XML_DATA);
}
/**
* Set the soap action which should be in the form of an URN.
*
* @param data
*/
public void setSoapAction(String data) {
setProperty(SOAP_ACTION, data);
}
/**
* Return the soap action string.
*
* @return String soap action
*/
public String getSoapAction() {
return getPropertyAsString(SOAP_ACTION);
}
/**
* Set the memory cache.
*
* @param cache
*/
public void setMemoryCache(boolean cache) {
setProperty(MEMORY_CACHE, String.valueOf(cache));
}
/**
* Get the memory cache.
*
* @return boolean cache
*/
public boolean getMemoryCache() {
return getPropertyAsBoolean(MEMORY_CACHE);
}
/**
* Set whether the sampler should read the response or not.
*
* @param read
*/
public void setReadResponse(boolean read) {
setProperty(READ_RESPONSE, String.valueOf(read));
}
/**
* Return whether or not to read the response.
*
* @return boolean
*/
public boolean getReadResponse() {
return this.getPropertyAsBoolean(READ_RESPONSE);
}
/**
* Set whether or not to use a proxy
*
* @param proxy
*/
public void setUseProxy(boolean proxy) {
setProperty(USE_PROXY, String.valueOf(proxy));
}
/**
* Return whether or not to use proxy
*
* @return true if should use proxy
*/
public boolean getUseProxy() {
return this.getPropertyAsBoolean(USE_PROXY);
}
/**
* Set the proxy hostname
*
* @param host
*/
public void setProxyHost(String host) {
setProperty(PROXY_HOST, host);
}
/**
* Return the proxy hostname
*
* @return the proxy hostname
*/
public String getProxyHost() {
this.checkProxy();
return this.getPropertyAsString(PROXY_HOST);
}
/**
* Set the proxy port
*
* @param port
*/
public void setProxyPort(String port) {
setProperty(PROXY_PORT, port);
}
/**
* Return the proxy port
*
* @return the proxy port
*/
public int getProxyPort() {
this.checkProxy();
return this.getPropertyAsInt(PROXY_PORT);
}
/**
*
* @param url
*/
public void setWsdlURL(String url) {
this.setProperty(WSDL_URL, url);
}
/**
* method returns the WSDL URL
*
* @return the WSDL URL
*/
public String getWsdlURL() {
return getPropertyAsString(WSDL_URL);
}
/*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -