vyommani commented on code in PR #872:
URL: https://github.com/apache/ranger/pull/872#discussion_r3123661829


##########
agents-common/src/main/java/org/apache/ranger/plugin/util/RangerRESTClient.java:
##########
@@ -340,259 +366,44 @@ public TrustManager[] getTrustManagers(String 
trustStoreFile, String trustStoreF
         return tmList;
     }
 
-    public ClientResponse get(String relativeUrl, Map<String, String> params) 
throws Exception {
-        ClientResponse finalResponse = null;
-        int            startIndex    = this.lastKnownActiveUrlIndex;
-        int            retryAttempt  = 0;
-
-        for (int index = 0; index < configuredURLs.size(); index++) {
-            int currentIndex = (startIndex + index) % configuredURLs.size();
-
-            try {
-                WebResource.Builder webResource = 
createWebResource(currentIndex, relativeUrl, params);
-
-                finalResponse = 
webResource.accept(RangerRESTUtils.REST_EXPECTED_MIME_TYPE).type(RangerRESTUtils.REST_MIME_TYPE_JSON).get(ClientResponse.class);
-
-                if (finalResponse != null) {
-                    if (finalResponse.getStatusInfo().getFamily() == 
Response.Status.Family.SERVER_ERROR) {
-                        throw new ClientHandlerException("Response status: " + 
finalResponse.getStatus());
-                    }
-
-                    setLastKnownActiveUrlIndex(currentIndex);
-
-                    break;
-                }
-            } catch (ClientHandlerException ex) {
-                if (shouldRetry(configuredURLs.get(currentIndex), index, 
retryAttempt, ex)) {
-                    retryAttempt++;
-
-                    index = -1; // start from first url
-                }
-            }
-        }
-
-        return finalResponse;
+    public Response get(String relativeUrl, Map<String, String> params) throws 
Exception {
+        return performRequest("GET", relativeUrl, params, null, null);
     }
 
-    public ClientResponse get(String relativeUrl, Map<String, String> params, 
Cookie sessionId) throws Exception {
-        ClientResponse finalResponse = null;
-        int            startIndex    = this.lastKnownActiveUrlIndex;
-        int            retryAttempt  = 0;
-
-        for (int index = 0; index < configuredURLs.size(); index++) {
-            int currentIndex = (startIndex + index) % configuredURLs.size();
-
-            try {
-                WebResource.Builder br = createWebResource(currentIndex, 
relativeUrl, params, sessionId);
-
-                finalResponse = 
br.accept(RangerRESTUtils.REST_EXPECTED_MIME_TYPE).type(RangerRESTUtils.REST_MIME_TYPE_JSON).get(ClientResponse.class);
-
-                if (finalResponse != null) {
-                    if (finalResponse.getStatusInfo().getFamily() == 
Response.Status.Family.SERVER_ERROR) {
-                        throw new ClientHandlerException("Response status: " + 
finalResponse.getStatus());
-                    }
-
-                    setLastKnownActiveUrlIndex(currentIndex);
-
-                    break;
-                }
-            } catch (ClientHandlerException ex) {
-                if (shouldRetry(configuredURLs.get(currentIndex), index, 
retryAttempt, ex)) {
-                    retryAttempt++;
-
-                    index = -1; // start from first url
-                }
-            }
-        }
-
-        return finalResponse;
+    public Response get(String relativeUrl, Map<String, String> params, Cookie 
sessionId) throws Exception {
+        return performRequest("GET", relativeUrl, params, null, sessionId);
     }
 
-    public ClientResponse post(String relativeUrl, Map<String, String> params, 
Object obj) throws Exception {
-        ClientResponse finalResponse = null;
-        int            startIndex    = this.lastKnownActiveUrlIndex;
-        int            retryAttempt  = 0;
-
-        for (int index = 0; index < configuredURLs.size(); index++) {
-            int currentIndex = (startIndex + index) % configuredURLs.size();
-
-            try {
-                WebResource.Builder webResource = 
createWebResource(currentIndex, relativeUrl, params);
-
-                finalResponse = 
webResource.accept(RangerRESTUtils.REST_EXPECTED_MIME_TYPE).type(RangerRESTUtils.REST_MIME_TYPE_JSON).post(ClientResponse.class,
 toJson(obj));
-
-                if (finalResponse != null) {
-                    setLastKnownActiveUrlIndex(currentIndex);
-
-                    break;
-                }
-            } catch (ClientHandlerException ex) {
-                if (shouldRetry(configuredURLs.get(currentIndex), index, 
retryAttempt, ex)) {
-                    retryAttempt++;
-
-                    index = -1; // start from first url
-                }
-            }
-        }
-
-        return finalResponse;
+    public Response post(String relativeUrl, Map<String, String> params, 
Object obj) throws Exception {
+        return performRequest("POST", relativeUrl, params, obj, null);
     }
 
-    public ClientResponse post(String relativeURL, Map<String, String> params, 
Object obj, Cookie sessionId) throws Exception {
-        ClientResponse response     = null;
-        int            startIndex   = this.lastKnownActiveUrlIndex;
-        int            retryAttempt = 0;
-
-        for (int index = 0; index < configuredURLs.size(); index++) {
-            int currentIndex = (startIndex + index) % configuredURLs.size();
-
-            try {
-                WebResource.Builder br = createWebResource(currentIndex, 
relativeURL, params, sessionId);
-
-                response = 
br.accept(RangerRESTUtils.REST_EXPECTED_MIME_TYPE).type(RangerRESTUtils.REST_MIME_TYPE_JSON).post(ClientResponse.class,
 toJson(obj));
-
-                if (response != null) {
-                    setLastKnownActiveUrlIndex(currentIndex);
-
-                    break;
-                }
-            } catch (ClientHandlerException ex) {
-                if (shouldRetry(configuredURLs.get(currentIndex), index, 
retryAttempt, ex)) {
-                    retryAttempt++;
-
-                    index = -1; // start from first url
-                }
-            }
-        }
-
-        return response;
+    public Response post(String relativeURL, Map<String, String> params, 
Object obj, Cookie sessionId) throws Exception {
+        return performRequest("POST", relativeURL, params, obj, sessionId);
     }
 
-    public ClientResponse delete(String relativeUrl, Map<String, String> 
params) throws Exception {
-        ClientResponse finalResponse = null;
-        int            startIndex    = this.lastKnownActiveUrlIndex;
-        int            retryAttempt  = 0;
-
-        for (int index = 0; index < configuredURLs.size(); index++) {
-            int currentIndex = (startIndex + index) % configuredURLs.size();
-
-            try {
-                WebResource.Builder webResource = 
createWebResource(currentIndex, relativeUrl, params);
-
-                finalResponse = 
webResource.accept(RangerRESTUtils.REST_EXPECTED_MIME_TYPE).type(RangerRESTUtils.REST_MIME_TYPE_JSON).delete(ClientResponse.class);
-
-                if (finalResponse != null) {
-                    setLastKnownActiveUrlIndex(currentIndex);
-
-                    break;
-                }
-            } catch (ClientHandlerException ex) {
-                if (shouldRetry(configuredURLs.get(currentIndex), index, 
retryAttempt, ex)) {
-                    retryAttempt++;
-
-                    index = -1; // start from first url
-                }
-            }
-        }
-
-        return finalResponse;
+    public Response delete(String relativeUrl, Map<String, String> params) 
throws Exception {
+        return performRequest("DELETE", relativeUrl, params, null, null);
     }
 
-    public ClientResponse delete(String relativeURL, Map<String, String> 
params, Cookie sessionId) throws Exception {
-        ClientResponse response     = null;
-        int            startIndex   = this.lastKnownActiveUrlIndex;
-        int            retryAttempt = 0;
-
-        for (int index = 0; index < configuredURLs.size(); index++) {
-            int currentIndex = (startIndex + index) % configuredURLs.size();
-
-            try {
-                WebResource.Builder br = createWebResource(currentIndex, 
relativeURL, params, sessionId);
-
-                response = br.delete(ClientResponse.class);
-
-                if (response != null) {
-                    setLastKnownActiveUrlIndex(currentIndex);
-
-                    break;
-                }
-            } catch (ClientHandlerException ex) {
-                if (shouldRetry(configuredURLs.get(currentIndex), index, 
retryAttempt, ex)) {
-                    retryAttempt++;
-
-                    index = -1; // start from first url
-                }
-            }
-        }
-
-        return response;
+    public Response delete(String relativeURL, Map<String, String> params, 
Cookie sessionId) throws Exception {
+        return performRequest("DELETE", relativeURL, params, null, sessionId);
     }
 
-    public ClientResponse put(String relativeUrl, Map<String, String> params, 
Object obj) throws Exception {
-        ClientResponse finalResponse = null;
-        int            startIndex    = this.lastKnownActiveUrlIndex;
-        int            retryAttempt  = 0;
-
-        for (int index = 0; index < configuredURLs.size(); index++) {
-            int currentIndex = (startIndex + index) % configuredURLs.size();
-
-            try {
-                WebResource.Builder webResource = 
createWebResource(currentIndex, relativeUrl, params);
-
-                finalResponse = 
webResource.accept(RangerRESTUtils.REST_EXPECTED_MIME_TYPE).type(RangerRESTUtils.REST_MIME_TYPE_JSON).put(ClientResponse.class,
 toJson(obj));
-
-                if (finalResponse != null) {
-                    setLastKnownActiveUrlIndex(currentIndex);
-
-                    break;
-                }
-            } catch (ClientHandlerException ex) {
-                if (shouldRetry(configuredURLs.get(currentIndex), index, 
retryAttempt, ex)) {
-                    retryAttempt++;
-
-                    index = -1; // start from first url
-                }
-            }
-        }
-
-        return finalResponse;
+    public Response put(String relativeUrl, Map<String, String> params, Object 
obj) throws Exception {
+        return performRequest("PUT", relativeUrl, params, obj, null);
     }
 
-    public ClientResponse put(String relativeURL, Object request, Cookie 
sessionId) throws Exception {
-        ClientResponse response     = null;
-        int            startIndex   = this.lastKnownActiveUrlIndex;
-        int            retryAttempt = 0;
-
-        for (int index = 0; index < configuredURLs.size(); index++) {
-            int currentIndex = (startIndex + index) % configuredURLs.size();
-
-            try {
-                WebResource.Builder br = createWebResource(currentIndex, 
relativeURL, null, sessionId);
-
-                response = 
br.accept(RangerRESTUtils.REST_EXPECTED_MIME_TYPE).type(RangerRESTUtils.REST_MIME_TYPE_JSON).put(ClientResponse.class,
 toJson(request));
-
-                if (response != null) {
-                    setLastKnownActiveUrlIndex(currentIndex);
-
-                    break;
-                }
-            } catch (ClientHandlerException ex) {
-                if (shouldRetry(configuredURLs.get(currentIndex), index, 
retryAttempt, ex)) {
-                    retryAttempt++;
-
-                    index = -1; // start from first url
-                }
-            }
-        }
-        return response;
+    public Response put(String relativeURL, Object request, Cookie sessionId) 
throws Exception {
+        return performRequest("PUT", relativeURL, null, request, sessionId);

Review Comment:
   I recommend introducing an enum for HTTP methods PUT, GET, POST, and DELETE 
within RangerRESTClient to enhance code readability and prevent string literals 
from being scattered throughout the codebase.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to