1 18 19 package org.apache.jmeter.protocol.http.util; 20 21 import java.io.IOException ; 22 import java.net.HttpURLConnection ; 23 import java.net.MalformedURLException ; 24 import java.net.URL ; 25 import java.util.ArrayList ; 26 import java.util.HashMap ; 27 import java.util.Set ; 28 29 import javax.xml.parsers.DocumentBuilder ; 30 import javax.xml.parsers.DocumentBuilderFactory ; 31 import javax.xml.parsers.ParserConfigurationException ; 32 33 import org.w3c.dom.Document ; 34 import org.w3c.dom.Element ; 35 import org.w3c.dom.Node ; 36 import org.w3c.dom.NodeList ; 37 import org.xml.sax.SAXException ; 38 39 50 public class WSDLHelper 51 { 52 57 58 protected URL WSDLURL = null; 59 protected HttpURLConnection CONN = null; 60 protected Document WSDLDOC = null; 61 protected String SOAPBINDING = null; 62 public String BINDNAME = null; 63 protected Object [] SOAPOPS = null; 64 protected HashMap ACTIONS = new HashMap (); 65 66 69 public WSDLHelper(String url) throws MalformedURLException 70 { 71 try 72 { 73 WSDLURL = new URL (url); 74 } 75 catch (MalformedURLException exception) 76 { 77 throw exception; 78 } 79 } 80 81 85 public URL getURL() 86 { 87 return this.WSDLURL; 88 } 89 90 95 public String getBinding() 96 { 97 try 98 { 99 NodeList services = this.WSDLDOC.getElementsByTagName("service"); 100 Element node = (Element ) services.item(0); 103 NodeList ports = node.getElementsByTagName("port"); 104 for (int idx = 0; idx < ports.getLength(); idx++) 105 { 106 Element pnode = (Element ) ports.item(idx); 107 String portname = pnode.getAttribute("name"); 108 if (portname.equals(this.BINDNAME)) 111 { 112 NodeList servlist = 113 pnode.getElementsByTagName("soap:address"); 114 Element addr = (Element ) servlist.item(0); 115 this.SOAPBINDING = addr.getAttribute("location"); 116 return this.SOAPBINDING; 117 } 118 } 119 return null; 120 } 121 catch (Exception exception) 122 { 123 return null; 124 } 125 } 126 127 133 protected void connect() throws IOException 134 { 135 try 136 { 137 CONN = (HttpURLConnection ) WSDLURL.openConnection(); 138 } 139 catch (IOException exception) 140 { 141 throw exception; 142 } 143 } 144 145 148 protected void close() 149 { 150 try 151 { 152 CONN.getInputStream().close(); 153 } 154 catch (Exception exception) 155 { 156 } 158 } 159 160 164 protected void buildDocument() 165 throws ParserConfigurationException , IOException , SAXException 166 { 167 try 168 { 169 DocumentBuilderFactory dbfactory = 170 DocumentBuilderFactory.newInstance(); 171 DocumentBuilder docbuild = dbfactory.newDocumentBuilder(); 172 WSDLDOC = docbuild.parse(CONN.getInputStream()); 173 } 174 catch (ParserConfigurationException exception) 175 { 176 throw exception; 177 } 178 catch (IOException exception) 179 { 180 throw exception; 181 } 182 catch (SAXException exception) 183 { 184 throw exception; 185 } 186 } 187 188 193 public void parse() throws WSDLException 194 { 195 try 196 { 197 this.connect(); 198 this.buildDocument(); 199 SOAPOPS = this.getOperations(); 200 this.close(); 201 } 202 catch (IOException exception) 203 { 204 throw (new WSDLException(exception)); 205 } 206 catch (Exception exception) 207 { 208 throw (new WSDLException(exception)); 209 } 210 } 211 212 215 public String [] getWebMethods() 216 { 217 for (int idx = 0; idx < SOAPOPS.length; idx++) 218 { 219 Node act = (Node ) SOAPOPS[idx]; 221 NodeList opers = 223 ((Element ) act).getElementsByTagName("soap:operation"); 224 Element op = (Element ) opers.item(0); 226 String value = op.getAttribute("soapAction"); 227 String key = ((Element ) act).getAttribute("name"); 228 this.ACTIONS.put(key, value); 229 } 230 Set keys = this.ACTIONS.keySet(); 231 String [] stringmeth = new String [keys.size()]; 232 Object [] stringKeys = keys.toArray(); 233 System.arraycopy(stringKeys, 0, stringmeth, 0, keys.size()); 234 return stringmeth; 235 } 236 237 240 public String getSoapAction(String key) 241 { 242 return (String ) this.ACTIONS.get(key); 243 } 244 245 248 public Document getWSDLDocument() 249 { 250 return WSDLDOC; 251 } 252 253 258 public Object [] getSOAPBindings() 259 { 260 ArrayList list = new ArrayList (); 261 NodeList bindings = WSDLDOC.getElementsByTagName("binding"); 262 for (int idx = 0; idx < bindings.getLength(); idx++) 263 { 264 Element nd = (Element ) bindings.item(idx); 265 NodeList slist = nd.getElementsByTagName("soap:binding"); 266 if (slist.getLength() > 0) 267 { 268 this.BINDNAME = nd.getAttribute("name"); 270 list.add(nd); 271 } 272 } 273 if (list.size() > 0) 274 { 275 return list.toArray(); 276 } 277 else 278 { 279 return new Object [0]; 280 } 281 } 282 283 299 public Object [] getOperations() 300 { 301 Object [] res = this.getSOAPBindings(); 302 ArrayList ops = new ArrayList (); 303 for (int idx = 0; idx < res.length; idx++) 305 { 306 Element one = (Element ) res[idx]; 307 NodeList opnodes = one.getElementsByTagName("operation"); 308 for (int idz = 0; idz < opnodes.getLength(); idz++) 310 { 311 Element child = (Element ) opnodes.item(idz); 314 NodeList soapnode = 315 child.getElementsByTagName("soap:operation"); 316 if (soapnode.getLength() > 0) 317 { 318 ops.add(child); 319 } 320 } 321 } 322 return ops.toArray(); 323 } 324 325 330 public static void main(String [] args) 331 { 332 try 333 { 334 WSDLHelper help = 335 new WSDLHelper("http://localhost/testWS/Service1.asmx?WSDL"); 336 long start = System.currentTimeMillis(); 337 help.parse(); 338 String [] methods = help.getWebMethods(); 339 System.out.println("el: " + (System.currentTimeMillis() - start)); 340 for (int idx = 0; idx < methods.length; idx++) 341 { 342 System.out.println("method name: " + methods[idx]); 343 } 344 System.out.println("service url: " + help.getBinding()); 345 } 346 catch (Exception exception) 347 { 348 System.out.println("main method catch:"); 349 exception.printStackTrace(); 350 } 351 } 352 } 353 | Popular Tags |