Skip to content

Commit

Permalink
Code improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
franklupo committed Aug 26, 2019
1 parent ef2aa92 commit 1effa7d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>it.corsinvest.proxmoxve.api</groupId>
<artifactId>cv4pve-api-java</artifactId>
<version>1.1.2</version>
<version>1.1.3</version>
<packaging>jar</packaging>
<name>cv4pve-api-java</name>
<description>Corsinvest for Proxmox VE Client API JAVA</description>
Expand Down
50 changes: 32 additions & 18 deletions src/main/java/it/corsinvest/proxmoxve/api/ClientBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ public boolean login(String userName, String password, String realm) throws JSON
}
}

/**
* Returns the base URL used to interact with the Proxmox VE API.
*
* @return The proxmox API URL.
*/
public String getApiUrl() {
return "https://" + getHostname() + ":" + getPort() + "/api2/json";
}

private enum HttpMethod {
GET, POST, PUT, DELETE
}
Expand Down Expand Up @@ -182,8 +191,15 @@ public int getDebugLevel() {
return _debugLevel;
}

private void setToken(HttpURLConnection httpCon) {
if (_ticketCSRFPreventionToken != null) {
httpCon.setRequestProperty("CSRFPreventionToken", _ticketCSRFPreventionToken);
httpCon.setRequestProperty("Cookie", "PVEAuthCookie=" + _ticketPVEAuthCookie);
}
}

private Result executeAction(String resource, HttpMethod method, Map<String, Object> parameters) throws JSONException {
String url = "https://" + getHostname() + ":" + getPort() + "/api2/json" + resource;
String url = getApiUrl() + resource;

Map params = new LinkedHashMap<>();
if (parameters != null) {
Expand All @@ -192,11 +208,7 @@ private Result executeAction(String resource, HttpMethod method, Map<String, Obj
if (entry.getValue() instanceof Boolean) {
value = ((Boolean) entry.getValue()) ? "1" : "0";
}
try {
params.put(entry.getKey(), URLEncoder.encode(value, "UTF-8"));
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
params.put(entry.getKey(), value);
});
}

Expand Down Expand Up @@ -226,7 +238,6 @@ public void checkServerTrusted(X509Certificate[] certs, String authType) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}

// Create all-trusting host name verifier
// Create all-trusting host name verifier
HostnameVerifier allHostsValid = (String hostname, SSLSession session) -> true;

Expand All @@ -241,20 +252,24 @@ public void checkServerTrusted(X509Certificate[] certs, String authType) {
try {
switch (method) {
case GET: {
if (params.isEmpty()) {
} else {
if (!params.isEmpty()) {
StringBuilder urlParams = new StringBuilder();
params.forEach((key, value) -> {
urlParams.append(urlParams.length() > 0 ? "&" : "")
.append(key)
.append("=")
.append(value);
try {
urlParams.append(urlParams.length() > 0 ? "&" : "")
.append(key)
.append("=")
.append(URLEncoder.encode((String) value, "UTF-8"));
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(ClientBase.class.getName()).log(Level.SEVERE, null, ex);
}
});
url += "?" + urlParams.toString();
}

httpCon = (HttpURLConnection) new URL(url).openConnection();
httpCon.setRequestMethod("GET");
setToken(httpCon);
break;
}

Expand All @@ -273,6 +288,8 @@ public void checkServerTrusted(X509Certificate[] certs, String authType) {
httpCon.setRequestMethod(method + "");
httpCon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpCon.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
setToken(httpCon);

httpCon.setDoOutput(true);
httpCon.getOutputStream().write(postDataBytes);

Expand All @@ -282,6 +299,7 @@ public void checkServerTrusted(X509Certificate[] certs, String authType) {
case DELETE: {
httpCon = (HttpURLConnection) new URL(url).openConnection();
httpCon.setRequestMethod("DELETE");
setToken(httpCon);
break;
}
}
Expand All @@ -297,11 +315,6 @@ public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
}

if (_ticketCSRFPreventionToken != null) {
httpCon.setRequestProperty("CSRFPreventionToken", _ticketCSRFPreventionToken);
httpCon.setRequestProperty("Cookie", "PVEAuthCookie=" + _ticketPVEAuthCookie);
}

statusCode = httpCon.getResponseCode();
reasonPhrase = httpCon.getResponseMessage();

Expand Down Expand Up @@ -341,6 +354,7 @@ public Result getLastResult() {

/**
* Add indexed parameter
*
* @param parameters Parameters
* @param name Name parameter
* @param value Calues
Expand Down

0 comments on commit 1effa7d

Please sign in to comment.