pradeepagrawal8184 commented on code in PR #872:
URL: https://github.com/apache/ranger/pull/872#discussion_r3129633913
##########
agents-common/src/test/java/org/apache/ranger/admin/client/datatype/TestRESTResponse.java:
##########
@@ -77,16 +78,18 @@ public void test03_getMessage_usesMsgDescElseHttpStatus() {
}
@Test
- public void test04_fromClientResponse_parsesBodyAndSetsStatus() {
- ClientResponse clientResponse = mock(ClientResponse.class);
- when(clientResponse.getStatus()).thenReturn(201);
-
when(clientResponse.getEntity(String.class)).thenReturn("{\"statusCode\":0,\"msgDesc\":\"done\"}");
-
- RESTResponse resp = RESTResponse.fromClientResponse(clientResponse);
- assertNotNull(resp);
- assertEquals(201, resp.getHttpStatusCode());
- assertEquals(0, resp.getStatusCode());
- assertEquals("done", resp.getMsgDesc());
+ public void test04_fromClientResponse_parsesBodyAndSetsStatus() throws
Exception {
+ // Test with a null response to ensure default behavior works
+ RESTResponse nullResp = RESTResponse.fromClientResponse(null);
+ assertNotNull(nullResp);
+ assertEquals(0, nullResp.getHttpStatusCode());
+
+ // Test with a valid JSON string directly
+ String jsonResponse = "{\"statusCode\":0,\"msgDesc\":\"done\"}";
+ RESTResponse fromJson = RESTResponse.fromJson(jsonResponse);
+ assertNotNull(fromJson);
+ assertEquals(0, fromJson.getStatusCode());
+ assertEquals("done", fromJson.getMsgDesc());
Review Comment:
Done, please review
##########
agents-common/src/main/java/org/apache/ranger/plugin/util/RangerRESTClient.java:
##########
@@ -848,9 +722,12 @@ private boolean isSslEnabled(String url) {
}
private KeyManager[] getKeyManagers() {
+ KeyManager[] kmList = null;
Review Comment:
Done, please review
##########
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:
Done, please review
--
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]