You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ranger.apache.org by sp...@apache.org on 2021/05/19 14:56:21 UTC
[ranger] branch ranger-2.2 updated:
RANGER-3227-Add-GenericType-for-response-handling
This is an automated email from the ASF dual-hosted git repository.
spolavarapu pushed a commit to branch ranger-2.2
in repository https://gitbox.apache.org/repos/asf/ranger.git
The following commit(s) were added to refs/heads/ranger-2.2 by this push:
new e01d39c RANGER-3227-Add-GenericType-for-response-handling
e01d39c is described below
commit e01d39c954fa196c321f7f91ef15a518b34bde49
Author: Abhishek Kumar <ab...@cloudera.com>
AuthorDate: Wed May 12 21:15:29 2021 -0400
RANGER-3227-Add-GenericType-for-response-handling
Signed-off-by: Sailaja Polavarapu <sp...@cloudera.com>
---
.../main/java/org/apache/ranger/RangerClient.java | 114 ++++++++++++++-------
.../ranger/examples/sampleclient/SampleClient.java | 15 +++
2 files changed, 91 insertions(+), 38 deletions(-)
diff --git a/intg/src/main/java/org/apache/ranger/RangerClient.java b/intg/src/main/java/org/apache/ranger/RangerClient.java
index c7e32dc..a1e1c7a 100644
--- a/intg/src/main/java/org/apache/ranger/RangerClient.java
+++ b/intg/src/main/java/org/apache/ranger/RangerClient.java
@@ -18,6 +18,7 @@
*/
package org.apache.ranger;
+import com.sun.jersey.api.client.GenericType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.sun.jersey.api.client.ClientResponse;
@@ -179,11 +180,11 @@ public class RangerClient {
}
public void deleteServiceDef(long serviceDefId) throws RangerServiceException {
- callAPI(DELETE_SERVICEDEF_BY_ID.applyUrlFormat(serviceDefId), null, null, null);
+ callAPI(DELETE_SERVICEDEF_BY_ID.applyUrlFormat(serviceDefId), null);
}
public void deleteServiceDef(String serviceDefName) throws RangerServiceException {
- callAPI(DELETE_SERVICEDEF_BY_NAME.applyUrlFormat(serviceDefName), null, null, null);
+ callAPI(DELETE_SERVICEDEF_BY_NAME.applyUrlFormat(serviceDefName), null);
}
public RangerServiceDef getServiceDef(long serviceDefId) throws RangerServiceException {
@@ -195,7 +196,7 @@ public class RangerClient {
}
public List<RangerServiceDef> findServiceDefs(Map<String, String> filter) throws RangerServiceException {
- return callAPI(FIND_SERVICEDEFS, filter, null, List.class);
+ return callAPI(FIND_SERVICEDEFS, filter, null, new GenericType<List<RangerServiceDef>>(){});
}
@@ -215,11 +216,11 @@ public class RangerClient {
}
public void deleteService(long serviceId) throws RangerServiceException {
- callAPI(DELETE_SERVICE_BY_ID.applyUrlFormat(serviceId), null, null, null);
+ callAPI(DELETE_SERVICE_BY_ID.applyUrlFormat(serviceId), null);
}
public void deleteService(String serviceName) throws RangerServiceException {
- callAPI(DELETE_SERVICE_BY_NAME.applyUrlFormat(serviceName), null, null, null);
+ callAPI(DELETE_SERVICE_BY_NAME.applyUrlFormat(serviceName), null);
}
public RangerService getService(long serviceId) throws RangerServiceException {
@@ -231,7 +232,7 @@ public class RangerClient {
}
public List<RangerService> findServices(Map<String, String> filter) throws RangerServiceException {
- return callAPI(FIND_SERVICES, filter, null, List.class);
+ return callAPI(FIND_SERVICES, filter, null, new GenericType<List<RangerService>>(){});
}
@@ -255,7 +256,7 @@ public class RangerClient {
}
public void deletePolicy(long policyId) throws RangerServiceException {
- callAPI(DELETE_POLICY_BY_ID.applyUrlFormat(policyId), null, null, null);
+ callAPI(DELETE_POLICY_BY_ID.applyUrlFormat(policyId), null);
}
public void deletePolicy(String serviceName, String policyName) throws RangerServiceException {
@@ -264,7 +265,7 @@ public class RangerClient {
queryParams.put(PARAM_POLICY_NAME, policyName);
queryParams.put("servicename", serviceName);
- callAPI(DELETE_POLICY_BY_NAME, queryParams, null, null);
+ callAPI(DELETE_POLICY_BY_NAME, queryParams);
}
public RangerPolicy getPolicy(long policyId) throws RangerServiceException {
@@ -276,11 +277,11 @@ public class RangerClient {
}
public List<RangerPolicy> getPoliciesInService(String serviceName) throws RangerServiceException {
- return callAPI(GET_POLICIES_IN_SERVICE.applyUrlFormat(serviceName), null, null, List.class);
+ return callAPI(GET_POLICIES_IN_SERVICE.applyUrlFormat(serviceName), null, null, new GenericType<List<RangerPolicy>>(){});
}
public List<RangerPolicy> findPolicies(Map<String, String> filter) throws RangerServiceException {
- return callAPI(FIND_POLICIES, filter, null, List.class);
+ return callAPI(FIND_POLICIES, filter, null, new GenericType<List<RangerPolicy>>(){});
}
@@ -300,11 +301,11 @@ public class RangerClient {
}
public void deleteSecurityZone(long zoneId) throws RangerServiceException {
- callAPI(DELETE_ZONE_BY_ID.applyUrlFormat(zoneId), null, null, null);
+ callAPI(DELETE_ZONE_BY_ID.applyUrlFormat(zoneId), null);
}
public void deleteSecurityZone(String zoneName) throws RangerServiceException {
- callAPI(DELETE_ZONE_BY_NAME.applyUrlFormat(zoneName), null, null, null);
+ callAPI(DELETE_ZONE_BY_NAME.applyUrlFormat(zoneName), null);
}
public RangerSecurityZone getSecurityZone(long zoneId) throws RangerServiceException {
@@ -316,7 +317,7 @@ public class RangerClient {
}
public List<RangerSecurityZone> findSecurityZones(Map<String, String> filter) throws RangerServiceException {
- return callAPI(FIND_ZONES, filter, null, List.class);
+ return callAPI(FIND_ZONES, filter, null, new GenericType<List<RangerSecurityZone>>(){});
}
/*
@@ -331,7 +332,7 @@ public class RangerClient {
}
public void deleteRole(long roleId) throws RangerServiceException {
- callAPI(DELETE_ROLE_BY_ID.applyUrlFormat(roleId), null, null, null);
+ callAPI(DELETE_ROLE_BY_ID.applyUrlFormat(roleId), null);
}
public void deleteRole(String roleName, String execUser, String serviceName) throws RangerServiceException {
@@ -340,7 +341,7 @@ public class RangerClient {
queryParams.put(PARAM_EXEC_USER, execUser);
queryParams.put(PARAM_SERVICE_NAME, serviceName);
- callAPI(DELETE_ROLE_BY_NAME.applyUrlFormat(roleName), queryParams, null, null);
+ callAPI(DELETE_ROLE_BY_NAME.applyUrlFormat(roleName), queryParams);
}
public RangerRole getRole(long roleId) throws RangerServiceException {
@@ -362,15 +363,15 @@ public class RangerClient {
queryParams.put(PARAM_EXEC_USER, execUser);
queryParams.put(PARAM_SERVICE_NAME, serviceName);
- return callAPI(GET_ALL_ROLE_NAMES.applyUrlFormat(serviceName), queryParams, null, List.class);
+ return callAPI(GET_ALL_ROLE_NAMES.applyUrlFormat(serviceName), queryParams, null, new GenericType<List<String>>(){});
}
public List<String> getUserRoles(String user) throws RangerServiceException {
- return callAPI(GET_USER_ROLES.applyUrlFormat(user), null, null, List.class);
+ return callAPI(GET_USER_ROLES.applyUrlFormat(user), null, null, new GenericType<List<String>>(){});
}
public List<RangerRole> findRoles(Map<String, String> filter) throws RangerServiceException {
- return callAPI(FIND_ROLES, filter, null, List.class);
+ return callAPI(FIND_ROLES, filter, null, new GenericType<List<RangerRole>>(){});
}
public RESTResponse grantRole(String serviceName, GrantRevokeRoleRequest request) throws RangerServiceException {
@@ -386,7 +387,7 @@ public class RangerClient {
* Admin APIs
*/
public List<RangerPluginInfo> getPluginsInfo() throws RangerServiceException {
- return callAPI(GET_PLUGIN_INFO, null, null, List.class);
+ return callAPI(GET_PLUGIN_INFO, null, null, new GenericType<List<RangerPluginInfo>>(){});
}
public void deletePolicyDeltas(int days, boolean reloadServicePoliciesCache) throws RangerServiceException {
@@ -395,7 +396,7 @@ public class RangerClient {
queryParams.put(PARAM_DAYS, String.valueOf(days));
queryParams.put(PARAM_RELOAD_SERVICE_POLICIES_CACHE, String.valueOf(reloadServicePoliciesCache));
- callAPI(DELETE_POLICY_DELTAS, queryParams, null, null);
+ callAPI(DELETE_POLICY_DELTAS, queryParams);
}
private ClientResponse invokeREST(API api, Map<String, String> params, Object request) throws RangerServiceException {
@@ -429,12 +430,10 @@ public class RangerClient {
return clientResponse;
}
- private <T> T callAPI(API api, Map<String, String> params, Object request, Class<T> responseType) throws RangerServiceException {
- T ret = null;
+ private ClientResponse responseHandler(API api, Map<String, String> params, Object request) throws RangerServiceException {
+ final ClientResponse clientResponse;
- if (LOG.isDebugEnabled()) {
- LOG.debug("==> callAPI({},{},{},{})",api, params, request, responseType);
- LOG.debug("------------------------------------------------------");
+ if (LOG.isDebugEnabled()){
LOG.debug("Call : {} {}", api.getMethod(), api.getNormalizedPath());
LOG.debug("Content-type : {} ", api.getConsumes());
LOG.debug("Accept : {} ", api.getProduces());
@@ -443,8 +442,6 @@ public class RangerClient {
}
}
- final ClientResponse clientResponse;
-
if (isSecureMode) {
clientResponse = Subject.doAs(sub, (PrivilegedAction<ClientResponse>) () -> {
try {
@@ -454,7 +451,9 @@ public class RangerClient {
}
return null;
});
- } else clientResponse = invokeREST(api,params,request);
+ } else {
+ clientResponse = invokeREST(api,params,request);
+ }
if (LOG.isDebugEnabled()) {
LOG.debug("method={}, path={}, contentType={}, accept={}, httpStatus={}", api.getMethod(), api.getNormalizedPath(), api.getConsumes(), api.getProduces(), (clientResponse != null ? clientResponse.getStatus() : "null"));
@@ -463,22 +462,61 @@ public class RangerClient {
if (clientResponse == null) {
throw new RangerServiceException(api, null);
} else if (clientResponse.getStatus() == api.getExpectedStatus().getStatusCode()) {
- if (responseType != null) {
- ret = clientResponse.getEntity(responseType);
-
- if (LOG.isDebugEnabled()) {
- LOG.debug("Response: {}", restClient.toJson(ret));
- LOG.debug("------------------------------------------------------");
- }
- }
+ return clientResponse;
} else if (clientResponse.getStatus() == ClientResponse.Status.SERVICE_UNAVAILABLE.getStatusCode()) {
LOG.error("Ranger Admin unavailable. HTTP Status: {}", clientResponse.getStatus());
} else {
throw new RangerServiceException(api, clientResponse);
}
+ return clientResponse;
+ }
- if(LOG.isDebugEnabled()){
- LOG.debug("<== callAPI({},{},{},{}), result = {}", api, params, request, responseType, ret);
+ private void callAPI(API api, Map<String, String> params) throws RangerServiceException {
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("==> callAPI({},{})",api, params);
+ }
+
+ responseHandler(api, params, null);
+
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("<== callAPI({},{})", api, params);
+ }
+ }
+
+ private <T> T callAPI(API api, Map<String, String> params, Object request, GenericType<T> responseType) throws RangerServiceException {
+ T ret = null;
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("==> callAPI({},{},{})",api, params, request);
+ LOG.debug("------------------------------------------------------");
+ }
+ final ClientResponse clientResponse = responseHandler(api, params, request);
+ if (responseType != null) {
+ ret = clientResponse.getEntity(responseType);
+
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Response: {}", restClient.toJson(ret));
+ LOG.debug("------------------------------------------------------");
+ LOG.debug("<== callAPI({},{},{},{}), result = {}", api, params, request, responseType, ret);
+ }
+ }
+ return ret;
+ }
+
+ private <T> T callAPI(API api, Map<String, String> params, Object request, Class<T> responseType) throws RangerServiceException {
+ T ret = null;
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("==> callAPI({},{},{})",api, params, request);
+ LOG.debug("------------------------------------------------------");
+ }
+ final ClientResponse clientResponse = responseHandler(api, params, request);
+ if (responseType != null) {
+ ret = clientResponse.getEntity(responseType);
+
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Response: {}", restClient.toJson(ret));
+ LOG.debug("------------------------------------------------------");
+ LOG.debug("<== callAPI({},{},{},{}), result = {}", api, params, request, responseType, ret);
+ }
}
return ret;
}
diff --git a/ranger-examples/sample-client/src/main/java/org/apache/ranger/examples/sampleclient/SampleClient.java b/ranger-examples/sample-client/src/main/java/org/apache/ranger/examples/sampleclient/SampleClient.java
index b380127..7d57950 100644
--- a/ranger-examples/sample-client/src/main/java/org/apache/ranger/examples/sampleclient/SampleClient.java
+++ b/ranger-examples/sample-client/src/main/java/org/apache/ranger/examples/sampleclient/SampleClient.java
@@ -118,6 +118,16 @@ public class SampleClient {
LOG.info("New Service created successfully {}", gsonBuilder.toJson(createdService));
/*
+ All Services
+ */
+ List<RangerService> services = rangerClient.findServices(filter);
+ String allServiceNames = "";
+ for (RangerService svc: services) {
+ allServiceNames = allServiceNames.concat(svc.getName() + " ");
+ }
+ LOG.info("List of Services : {}", allServiceNames);
+
+ /*
Policy Management
*/
@@ -189,6 +199,11 @@ public class SampleClient {
*/
List<RangerRole> allRoles = rangerClient.findRoles(filter);
LOG.info("List of Roles {}", gsonBuilder.toJson(allRoles));
+ String allRoleNames = "";
+ for (RangerRole role: allRoles) {
+ allRoleNames = allRoleNames.concat(role.getName() + " ");
+ }
+ LOG.info("List of Roles : {}", allRoleNames);
/*
Delete a role in Ranger