diff --git a/README.md b/README.md index 31fec97..189db4c 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/pom.xml b/pom.xml index 7b9933d..4f927af 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 it.corsinvest.proxmoxve cv4pve-api-java - 8.1.1 + 8.1.2 jar cv4pve-api-java Corsinvest for Proxmox VE Client API Java diff --git a/src/main/java/it/corsinvest/proxmoxve/api/PveClientBase.java b/src/main/java/it/corsinvest/proxmoxve/api/PveClientBase.java index 38ab20c..b2cbb95 100644 --- a/src/main/java/it/corsinvest/proxmoxve/api/PveClientBase.java +++ b/src/main/java/it/corsinvest/proxmoxve/api/PveClientBase.java @@ -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; @@ -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. * @@ -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 @@ -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 @@ -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 @@ -202,7 +224,7 @@ public Result get(String resource, Map parameters) throws JSONEx /** * Execute method PUT * - * @param resource Url request + * @param resource Url request * @param parameters Additional parameters * @return Result * @throws JSONException @@ -214,7 +236,7 @@ public Result set(String resource, Map parameters) throws JSONEx /** * Execute method POST * - * @param resource Url request + * @param resource Url request * @param parameters Additional parameters * @return Result * @throws JSONException @@ -226,7 +248,7 @@ public Result create(String resource, Map parameters) throws JSO /** * Execute method DELETE * - * @param resource Url request + * @param resource Url request * @param parameters Additional parameters * @return Result * @throws JSONException @@ -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 parameters) throws JSONException { String url = getApiUrl() + resource; @@ -310,7 +338,7 @@ private Result executeAction(String resource, MethodType methodType, Map (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); } @@ -318,7 +346,7 @@ private Result executeAction(String resource, MethodType methodType, Map { - 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); @@ -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; } @@ -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 parameters, String name, Map value) { if (value != null) { @@ -490,8 +512,8 @@ public static void addIndexedParameter(Map 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 @@ -542,7 +564,7 @@ public String getExitStatusTask(String task) throws JSONException { /** * Convert JSONArray To List * - * @param Type of data + * @param Type of data * @param array Array JSON * @return T List of Type of data * @throws JSONException