1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
| public class SoapHelper {
// connection properties
public String server = "";
public String username = "";
public String password = "";
public String session = ""; // this is the session id returned by the server upon successful login
// API
public String service = "";
public String method = "";
public StringBuffer request = new StringBuffer(); // this is what we send to the server
public String response = ""; // this is what the server return to us
public SoapHelper(String server) {
this.server = server;
}
private String getURI() {
return "https://" + this.server + this.session;
}
private SOAPMessage makeMessage(String nodeName, String xmlStr, boolean asResponse) throws Exception {
MessageFactory msgFactory = MessageFactory.newInstance();
SOAPMessage message = msgFactory.createMessage();
SOAPPart part = message.getSOAPPart();
SOAPEnvelope envelope = part.getEnvelope();
envelope.addNamespaceDeclaration("xsi", "http://www.w3.org/1999/XMLSchema-instance");
envelope.addNamespaceDeclaration("xsd", "http://www.w3.org/1999/XMLSchema");
SOAPBody body = envelope.getBody();
SOAPElement element = body.addChildElement(envelope.createName("ns1:" + this.method + (asResponse ? "Response" : "")));
element.addAttribute(envelope.createName("xmlns:ns1"), "urn:" + this.service);
element.addAttribute(envelope.createName("ns1"), "http://schemas.xmlsoap.org/soap/encoding");
SOAPElement ele2 = element.addChildElement(envelope.createName(nodeName));
ele2.addAttribute(envelope.createName("xsi:type"), "xsd:string");
ele2.addTextNode(xmlStr);
if (!asResponse) message.saveChanges();
return message;
}
public void doRequest(String service, String method, String xml)
{
this.service = service;
this.method = method;
request.setLength(0); // clear the buffer
request.append(xml);
try
{
sendRequest();
}
catch (Exception e)
{
e.printStackTrace();
}
}
private boolean sendRequest() throws Exception {
try {
SOAPConnectionFactory conFactory = SOAPConnectionFactory.newInstance();
SOAPConnection con = conFactory.createConnection();
URL endpoint = new URL(this.getURI());
SOAPMessage message = this.makeMessage("msgstr", this.request.toString(), false);
SOAPMessage retval = con.call(message, endpoint);
//extraction du XML en String lisible du message SOAP
this.response = extractXML(retval);
} catch (Exception e) {
this.response = e.getMessage();
}
return true;
}
private String extractXML(SOAPMessage message) throws Exception {
SOAPPart part = message.getSOAPPart();
SOAPEnvelope env = part.getEnvelope();
SOAPElement element = null;
SOAPBody body = env.getBody();
String returnxml = null;
if (body.hasFault()) {
throw new Exception(body.getFault().getFaultString());
} else {
Iterator iter = body.getChildElements();
while (iter.hasNext()) {
element = (SOAPElement) iter.next();
Name name = element.getElementName();
if (name.getQualifiedName().startsWith("ns1")) { // e.g ns1:Authenticate
break;
}
}
iter = element.getChildElements(env.createName("return"));
while (iter.hasNext()) {
element = (SOAPElement) iter.next();
returnxml = element.getValue();
break;
}
}
return returnxml;
} |
Partager