forked from plesk/api-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPleskApiClient.java
More file actions
91 lines (69 loc) · 2.83 KB
/
PleskApiClient.java
File metadata and controls
91 lines (69 loc) · 2.83 KB
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
// Copyright 1999-2016. Parallels IP Holdings GmbH. All Rights Reserved.
import java.net.*;
import java.io.*;
import javax.net.ssl.*;
import java.security.cert.X509Certificate;
class PleskApiClient {
private String host;
private String login;
private String password;
private String secretKey;
public PleskApiClient(String host) {
this.host = host;
}
public void setCredentials(String login, String password) {
this.login = login;
this.password = password;
}
public void setSecretKey(String secretKey) {
this.secretKey = secretKey;
}
public String request(String request) throws Exception {
this.setTrustAllSslCertificates();
URL url = new URL("https://" + this.host + ":8443/enterprise/control/agent.php");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "text/xml");
connection.setRequestProperty("HTTP_PRETTY_PRINT", "TRUE");
if (null != this.secretKey) {
connection.setRequestProperty("KEY", this.secretKey);
} else {
connection.setRequestProperty("HTTP_AUTH_LOGIN", this.login);
connection.setRequestProperty("HTTP_AUTH_PASSWD", this.password);
}
connection.setDoOutput(true);
DataOutputStream writer = new DataOutputStream(connection.getOutputStream());
writer.writeBytes(request);
writer.flush();
writer.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String response = "";
String inputLine;
while ((inputLine = reader.readLine()) != null) {
response += inputLine + "\n";
}
reader.close();
return response;
}
private void setTrustAllSslCertificates() throws Exception {
TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
}
};
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HostnameVerifier allHostsValid = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
}
}