-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_connect.java
128 lines (115 loc) · 4.88 KB
/
test_connect.java
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPFactory;
import javax.xml.soap.SOAPFault;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamResult;
import java.security.SecureRandom;
import java.security.Security;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Iterator;
import java.io.ByteArrayOutputStream;
try {
//Access to Zimbra with SSL protocol
System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
System.setProperty("javax.net.ssl.trustStore", "C:/keystore.jks");
System.setProperty("javax.net.ssl.trustStorePassword", "password");
//First create the connection
SOAPConnectionFactory soapConnFactory = SOAPConnectionFactory.newInstance();
SOAPConnection connection = soapConnFactory.createConnection();
//Next, create the actual message
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage message = messageFactory.createMessage();
//Create objects for the message parts
SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
SOAPHeader header = envelope.getHeader();
SOAPBody body = envelope.getBody();
SOAPFactory soapFactory = SOAPFactory.newInstance();
//<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\">
// <soap:Header>
// <context xmlns=\"urn:zimbra\"/>
// </soap:Header>
// <soap:Body>
// <AuthRequest xmlns=\"urn:zimbraAccount\">
// <account by=\"name\">admin</account>
// <password>*netixia*</password>
// </AuthRequest>
// </soap:Body>
//</soap:Envelope>
//Populate envelope
envelope.setPrefix("soap");
envelope.addNamespaceDeclaration("soap", "http://www.w3.org/2003/05/soap-envelope");
//Populate header
header.setPrefix("soap");
SOAPHeaderElement context = header.addHeaderElement(soapFactory.createName("context", "", "urn:zimbra"));
context.setPrefix("");
//Populate body
body.setPrefix("soap");
SOAPBodyElement authRequest = body.addBodyElement(soapFactory.createName("AuthRequest","","urn:zimbraAccount"));
SOAPElement account = authRequest.addChildElement("account");
account.addAttribute(soapFactory.createName("by"), "name");
account.addTextNode("admin");
authRequest.addChildElement("password").addTextNode("******");
//Save the message
message.saveChanges();
//Check the input
System.out.println("\nREQUEST:\n");
message.writeTo(System.out);
System.out.println();
//Send the message and get a reply
//Set the destination
String destination = "https://zimbrax.ville-chateauroux.fr:7071/service/admin/soap";
//Send the message
SOAPMessage reply = connection.call(message, destination);
//Check the output
System.out.println("\nRESPONSE:\n");
//Create the transformer
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
//Extract the content of the reply
Source sourceContent = reply.getSOAPPart().getContent();
//Set the output for the transformation
StreamResult result = new StreamResult(System.out);
transformer.transform(sourceContent, result);
System.out.println();
//Handles the received SOAP message
System.out.println("\nELEMENTS:\n");
SOAPBody replyBody = reply.getSOAPPart().getEnvelope().getBody();
if (replyBody.hasFault()) {
SOAPFault fault = replyBody.getFault();
throw new SOAPException("Erreur en récupérant la réponse:" + fault.getFaultString());
}
Iterator iter = replyBody.getChildElements();
if (iter.hasNext()) {
SOAPElement element1 = (SOAPElement)iter.next();
System.out.println(element1.getElementName().getLocalName());
System.out.println(element1.getValue());
iter = element1.getChildElements();
while (iter.hasNext()) {
SOAPElement element2 = (SOAPElement)iter.next();
System.out.print(element2.getElementName().getLocalName() + ": ");
System.out.println(element2.getValue());
}
}
else System.out.println("pas de correspondance");
//Close the connection
connection.close();
} catch(Exception e) {
System.out.println(e.getMessage());
}