* reader will read, but do nothing with it. Essentially, the stream from
* the server goes into the ether.
*/
@Override
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);
}
// set the response defaults
result.setDataEncoding(ENCODING);
result.setContentType("text/xml"); // $NON-NLS-1$
result.setDataType(SampleResult.TEXT);
result.setSamplerData(fileContents);// WARNING - could be large
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);
@SuppressWarnings("unchecked") // API uses raw types
final Map<String, String> headers = spconn.getHeaders();
result.setResponseHeaders(convertSoapHeaders(headers));
if (this.getHeaderManager() != null) {
this.getHeaderManager().setSOAPHeader(spconn);
}
BufferedReader br = null;
if (spconn.receive() != null) {
br = spconn.receive();
SOAPContext sc = spconn.getResponseSOAPContext();
// Set details from the actual response
// Needs to be done before response can be stored
final String contentType = sc.getContentType();
result.setContentType(contentType);
result.setEncodingAndType(contentType);
int length=0;
if (getReadResponse()) {
StringWriter sw = new StringWriter();
length=IOUtils.copy(br, sw);
result.sampleEnd();
result.setResponseData(sw.toString().getBytes(result.getDataEncodingWithDefault()));
} else {
// by not reading the response
// for real, it improves the
// performance on slow clients
length=br.read();
result.sampleEnd();
result.setResponseData(JMeterUtils.getResString("read_response_message"), null); //$NON-NLS-1$
}
// It is not possible to access the actual HTTP response code, so we assume no data means failure
if (length > 0){
result.setSuccessful(true);
result.setResponseCodeOK();
result.setResponseMessageOK();
} else {
result.setSuccessful(false);
result.setResponseCode("999");
result.setResponseMessage("Empty response");
}
} else {
result.sampleEnd();
result.setSuccessful(false);
final String contentType = spconn.getResponseSOAPContext().getContentType();
result.setContentType(contentType);
result.setEncodingAndType(contentType);
result.setResponseData(spconn.getResponseSOAPContext().toString().getBytes(result.getDataEncodingWithDefault()));
}
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 {
log.error("Problem processing the SOAP request", exception);
result.setResponseMessage(exception.toString());
}
} finally {
// Make sure the sample start time and sample end time are recorded
// in order not to confuse the statistics calculation methods: if
// an error occurs and an exception is thrown it is possible that
// the result.sampleStart() or result.sampleEnd() won't be called
if (result.getStartTime() == 0)
{
result.sampleStart();
}
if (result.getEndTime() == 0)
{
result.sampleEnd();
}
}
return result;
}