Skip to content

Commit

Permalink
Added a timeout feature for the connection
Browse files Browse the repository at this point in the history
  • Loading branch information
franklupo committed Mar 2, 2024
1 parent f5633fe commit f8ee968
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 25 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ The result is class **Result** and contain methods:
* Form Proxmox VE 6.2 support Api Token for user
* Login with One-time password for Two-factor authentication
* Support for Proxy
* Set Timeout for the Connection.

## Api token

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>it.corsinvest.proxmoxve</groupId>
<artifactId>cv4pve-api-java</artifactId>
<version>8.1.1</version>
<version>8.1.2</version>
<packaging>jar</packaging>
<name>cv4pve-api-java</name>
<description>Corsinvest for Proxmox VE Client API Java</description>
Expand Down
70 changes: 46 additions & 24 deletions src/main/java/it/corsinvest/proxmoxve/api/PveClientBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class PveClientBase {
private ResponseType _responseType = ResponseType.JSON;
private String _apiToken;
private Proxy _proxy = Proxy.NO_PROXY;
private int _timeout = 0;

public PveClientBase(String hostname, int port) {
_hostname = hostname;
Expand Down Expand Up @@ -107,6 +108,27 @@ public void setResponseType(ResponseType responseType) {
_responseType = responseType;
}

/**
* Set timeout connection
*
* @param timeout
*/
public void setTimeout(int timeout) {
if (timeout < 0) {
throw new IllegalArgumentException("timeout can not be negative");
}
_timeout = timeout;
}

/**
* Return timeout connection
*
* @return
*/
public int getTimeout() {
return _timeout;
}

/**
* Creation ticket from login.
*
Expand All @@ -132,7 +154,7 @@ public boolean login(String username, String password) throws JSONException, Pve
*
* @param username user name
* @param password password connection
* @param realm pam/pve or custom
* @param realm pam/pve or custom
*
* @return boolean
* @throws JSONException
Expand All @@ -148,8 +170,8 @@ public boolean login(String username, String password, String realm)
*
* @param username user name
* @param password password connection
* @param realm pam/pve or custom
* @param otp One-time password for Two-factor authentication.
* @param realm pam/pve or custom
* @param otp One-time password for Two-factor authentication.
*
* @return boolean
* @throws JSONException
Expand Down Expand Up @@ -190,7 +212,7 @@ public String getApiUrl() {
/**
* Execute method GET
*
* @param resource Url request
* @param resource Url request
* @param parameters Additional parameters
* @return Result
* @throws JSONException
Expand All @@ -202,7 +224,7 @@ public Result get(String resource, Map<String, Object> parameters) throws JSONEx
/**
* Execute method PUT
*
* @param resource Url request
* @param resource Url request
* @param parameters Additional parameters
* @return Result
* @throws JSONException
Expand All @@ -214,7 +236,7 @@ public Result set(String resource, Map<String, Object> parameters) throws JSONEx
/**
* Execute method POST
*
* @param resource Url request
* @param resource Url request
* @param parameters Additional parameters
* @return Result
* @throws JSONException
Expand All @@ -226,7 +248,7 @@ public Result create(String resource, Map<String, Object> parameters) throws JSO
/**
* Execute method DELETE
*
* @param resource Url request
* @param resource Url request
* @param parameters Additional parameters
* @return Result
* @throws JSONException
Expand Down Expand Up @@ -282,6 +304,12 @@ private void setToken(HttpURLConnection httpCon) {
}
}

private void setConnectionTimeout(HttpURLConnection httpCon) {
if (_timeout > 0) {
httpCon.setConnectTimeout(_timeout);
}
}

private Result executeAction(String resource, MethodType methodType, Map<String, Object> parameters)
throws JSONException {
String url = getApiUrl() + resource;
Expand Down Expand Up @@ -310,15 +338,15 @@ private Result executeAction(String resource, MethodType methodType, Map<String,
parameters.entrySet().stream().filter((entry) -> (entry.getValue() != null)).forEachOrdered((entry) -> {
Object value = entry.getValue();
if (value instanceof Boolean) {
params.put(entry.getKey(), ((Boolean) value) ? 1 : 0);
params.put(entry.getKey(), Boolean.TRUE.equals(value) ? 1 : 0);
} else {
params.put(entry.getKey(), value);
}
});
}

// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
Expand All @@ -331,7 +359,7 @@ public void checkClientTrusted(X509Certificate[] certs, String authType) {
@Override
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
}};
} };

// Install the all-trusting trust manager
try {
Expand Down Expand Up @@ -369,26 +397,19 @@ public void checkServerTrusted(X509Certificate[] certs, String authType) {

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

case SET:
case CREATE: {
/*
var postData = new StringBuilder();
params.forEach((key, value) -> {
postData.append(postData.length() > 0 ? "&" : "").append(key).append("=").append(value);
});
var postDataBytes = postData.toString().getBytes("UTF-8");
*/

String data = new JSONObject(params).toString();
httpCon = (HttpURLConnection) new URL(url).openConnection(_proxy);
httpCon.setRequestMethod(httpMethod);
httpCon.setRequestProperty("Content-Type", "application/json");
httpCon.setRequestProperty("Content-Length", String.valueOf(data.length()));
setConnectionTimeout(httpCon);
setToken(httpCon);

httpCon.setDoOutput(true);
Expand All @@ -399,6 +420,7 @@ public void checkServerTrusted(X509Certificate[] certs, String authType) {
case DELETE: {
httpCon = (HttpURLConnection) new URL(url).openConnection(_proxy);
httpCon.setRequestMethod("DELETE");
setConnectionTimeout(httpCon);
setToken(httpCon);
break;
}
Expand Down Expand Up @@ -476,8 +498,8 @@ public Result getLastResult() {
* Add indexed parameter
*
* @param parameters Parameters
* @param name Name parameter
* @param value Values
* @param name Name parameter
* @param value Values
*/
public static void addIndexedParameter(Map<String, Object> parameters, String name, Map<Integer, String> value) {
if (value != null) {
Expand All @@ -490,8 +512,8 @@ public static void addIndexedParameter(Map<String, Object> parameters, String na
/**
* Wait for task to finish
*
* @param task Task identifier
* @param wait Millisecond wait next check
* @param task Task identifier
* @param wait Millisecond wait next check
* @param timeOut Millisecond timeout
* @return 0 Success
* @throws JSONException
Expand Down Expand Up @@ -542,7 +564,7 @@ public String getExitStatusTask(String task) throws JSONException {
/**
* Convert JSONArray To List
*
* @param <T> Type of data
* @param <T> Type of data
* @param array Array JSON
* @return T List of Type of data
* @throws JSONException
Expand Down

0 comments on commit f8ee968

Please sign in to comment.