1 18 19 package org.apache.jmeter.protocol.http.sampler; 20 21 import java.io.IOException ; 22 import java.io.PrintWriter ; 23 import java.net.HttpURLConnection ; 24 import java.net.MalformedURLException ; 25 import java.net.URL ; 26 import java.net.URLConnection ; 27 28 import org.apache.jmeter.samplers.Entry; 29 import org.apache.jmeter.samplers.SampleResult; 30 import org.apache.jorphan.logging.LoggingManager; 31 import org.apache.log.Logger; 32 import org.apache.jmeter.protocol.http.control.HeaderManager; 33 import org.apache.jmeter.protocol.http.control.Header; 34 35 41 public class SoapSampler extends HTTPSampler 42 { 43 private static Logger log = LoggingManager.getLoggerForClass(); 44 public static final String XML_DATA = "HTTPSamper.xml_data"; 45 public static final String URL_DATA = "SoapSampler.URL_DATA"; 46 47 public void setXmlData(String data) 48 { 49 setProperty(XML_DATA, data); 50 } 51 52 public String getXmlData() 53 { 54 return getPropertyAsString(XML_DATA); 55 } 56 57 public String getURLData() 58 { 59 return getPropertyAsString(URL_DATA); 60 } 61 62 public void setURLData(String url) 63 { 64 setProperty(URL_DATA, url); 65 } 66 67 74 public void setPostHeaders(URLConnection connection) throws IOException 75 { 76 ((HttpURLConnection ) connection).setRequestMethod("POST"); 77 connection.setRequestProperty( 78 "Content-Length", 79 "" + getXmlData().length()); 80 if (getHeaderManager() != null){ 85 HeaderManager mngr = getHeaderManager(); 88 int headerSize = mngr.size(); 89 for (int idx=0; idx < headerSize; idx++){ 91 Header hd = mngr.getHeader(idx); 92 connection.setRequestProperty(hd.getName(),hd.getValue()); 93 } 94 } else { 95 connection.setRequestProperty("Content-Type", "text/xml"); 97 } 98 connection.setDoOutput(true); 99 } 100 101 108 public void sendPostData(URLConnection connection) throws IOException 109 { 110 PrintWriter out = new PrintWriter (connection.getOutputStream()); 111 out.print(getXmlData()); 112 out.close(); 113 } 114 115 118 public SampleResult sample(Entry e) 119 { 120 try 121 { 122 URL url = new URL (getURLData()); 123 setDomain(url.getHost()); 124 setPort(url.getPort()); 125 setProtocol(url.getProtocol()); 126 setMethod(POST); 127 if (url.getQuery() != null && url.getQuery().compareTo("") != 0) 128 { 129 setPath(url.getPath() + "?" + url.getQuery()); 130 } 131 else 132 { 133 setPath(url.getPath()); 134 } 135 URLConnection conn = url.openConnection(); 137 setPostHeaders(conn); 138 } 139 catch (MalformedURLException e1) 140 { 141 log.error("Bad url: " + getURLData(), e1); 142 } 143 catch (IOException e1){ 144 log.error("Bad url: " + getURLData(), e1); 145 } 146 return super.sample(e); 147 } 148 149 public String toString() 150 { 151 try 152 { 153 String xml = getXmlData(); 154 if (xml.length() > 100) 155 { 156 xml = xml.substring(0, 100); 157 } 158 return this.getUrl().toString() + "\nXML Data: " + xml; 159 } 160 catch (MalformedURLException e) 161 { 162 return ""; 163 } 164 } 165 } 166 | Popular Tags |