5
0
mirror of https://github.com/apache/sqoop.git synced 2025-05-19 02:10:54 +08:00

SQOOP-2774: Sqoop2: Remove the notion of RolesBean

(Jarek Jarcec Cecho via Colin Ma)
This commit is contained in:
Colin Ma 2016-01-15 11:25:41 +08:00
parent 67214a48c5
commit 63002273d0
5 changed files with 46 additions and 52 deletions

View File

@ -52,10 +52,10 @@ public AuthorizationResourceRequest(DelegationTokenAuthenticatedURL.Token token)
super(token); super(token);
} }
public RolesBean readRoles(String serverUrl) { public RoleBean readRoles(String serverUrl) {
String response = super.get(serverUrl + RESOURCE + ROLES); String response = super.get(serverUrl + RESOURCE + ROLES);
JSONObject jsonObject = JSONUtils.parse(response); JSONObject jsonObject = JSONUtils.parse(response);
RolesBean bean = new RolesBean(); RoleBean bean = new RoleBean();
bean.restore(jsonObject); bean.restore(jsonObject);
return bean; return bean;
} }
@ -72,7 +72,7 @@ public void dropRole(String serverUrl, MRole role) {
} }
public void grantRevokeRole(String serverUrl, List<MRole> roles, List<MPrincipal> principals, boolean isGrant) { public void grantRevokeRole(String serverUrl, List<MRole> roles, List<MPrincipal> principals, boolean isGrant) {
RolesBean rolesBean = new RolesBean(roles); RoleBean rolesBean = new RoleBean(roles);
PrincipalBean principalsBean = new PrincipalBean(principals); PrincipalBean principalsBean = new PrincipalBean(principals);
// Extract all config inputs including sensitive inputs // Extract all config inputs including sensitive inputs
JSONObject jsonObject = new JSONObject(); JSONObject jsonObject = new JSONObject();
@ -85,12 +85,12 @@ public void grantRevokeRole(String serverUrl, List<MRole> roles, List<MPrincipal
} }
} }
public RolesBean readRolesByPrincipal(String serverUrl, MPrincipal principal) { public RoleBean readRolesByPrincipal(String serverUrl, MPrincipal principal) {
String response = super.get(serverUrl + RESOURCE + ROLES String response = super.get(serverUrl + RESOURCE + ROLES
+ "?principal_name=" + UrlSafeUtils.urlEncode(principal.getName()) + "?principal_name=" + UrlSafeUtils.urlEncode(principal.getName())
+ "&principal_type=" + UrlSafeUtils.urlEncode(principal.getType())); + "&principal_type=" + UrlSafeUtils.urlEncode(principal.getType()));
JSONObject jsonObject = JSONUtils.parse(response); JSONObject jsonObject = JSONUtils.parse(response);
RolesBean bean = new RolesBean(); RoleBean bean = new RoleBean();
bean.restore(jsonObject); bean.restore(jsonObject);
return bean; return bean;
} }

View File

@ -178,7 +178,7 @@ public SubmissionsBean readSubmission(String jArg) {
return getSubmissionResourceRequest().read(serverUrl, jArg); return getSubmissionResourceRequest().read(serverUrl, jArg);
} }
public RolesBean readRoles() { public RoleBean readRoles() {
return getAuthorizationRequest().readRoles(serverUrl); return getAuthorizationRequest().readRoles(serverUrl);
} }
@ -198,7 +198,7 @@ public void revokeRole(List<MRole> roles, List<MPrincipal> principals) {
getAuthorizationRequest().grantRevokeRole(serverUrl, roles, principals, false); getAuthorizationRequest().grantRevokeRole(serverUrl, roles, principals, false);
} }
public RolesBean readRolesByPrincipal(MPrincipal principal) { public RoleBean readRolesByPrincipal(MPrincipal principal) {
return getAuthorizationRequest().readRolesByPrincipal(serverUrl, principal); return getAuthorizationRequest().readRolesByPrincipal(serverUrl, principal);
} }

View File

@ -33,7 +33,7 @@
@InterfaceStability.Unstable @InterfaceStability.Unstable
public class RoleBean implements JsonBean { public class RoleBean implements JsonBean {
public static final String ROLE = "role"; private static final String ROLES = "roles";
private static final String NAME = "name"; private static final String NAME = "name";
private List<MRole> roles; private List<MRole> roles;
@ -45,7 +45,7 @@ public List<MRole> getRoles() {
// For "extract" // For "extract"
public RoleBean(MRole role) { public RoleBean(MRole role) {
this(); this();
this.roles = new ArrayList<MRole>(); this.roles = new ArrayList<>();
this.roles.add(role); this.roles.add(role);
} }
@ -61,9 +61,16 @@ public RoleBean() {
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public JSONObject extract(boolean skipSensitive) { public JSONObject extract(boolean skipSensitive) {
JSONObject role = new JSONObject(); JSONArray rolesArray = extractRoles();
role.put(ROLE, extractRole(roles.get(0))); JSONObject roles = new JSONObject();
return role; roles.put(ROLES, rolesArray);
return roles;
}
@Override
public void restore(JSONObject json) {
JSONArray rolesArray = JSONUtils.getJSONArray(json, ROLES);
restoreRoles(rolesArray);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -84,15 +91,8 @@ private JSONObject extractRole(MRole role) {
return object; return object;
} }
@Override
public void restore(JSONObject json) {
roles = new ArrayList<MRole>();
JSONObject obj = JSONUtils.getJSONObject(json, ROLE);
roles.add(restoreRole(obj));
}
protected void restoreRoles(JSONArray array) { protected void restoreRoles(JSONArray array) {
roles = new ArrayList<MRole>(); roles = new ArrayList<>();
for (Object obj : array) { for (Object obj : array) {
roles.add(restoreRole(obj)); roles.add(restoreRole(obj));
} }

View File

@ -17,47 +17,41 @@
*/ */
package org.apache.sqoop.json; package org.apache.sqoop.json;
import org.apache.sqoop.classification.InterfaceAudience;
import org.apache.sqoop.classification.InterfaceStability;
import org.apache.sqoop.model.MRole; import org.apache.sqoop.model.MRole;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
import org.testng.annotations.Test;
import java.util.LinkedList;
import java.util.List; import java.util.List;
@InterfaceAudience.Private import static org.testng.AssertJUnit.assertEquals;
@InterfaceStability.Unstable
public class RolesBean extends RoleBean {
private static final String ROLES = "roles"; public class TestRoleBean {
// For "extract" @Test
public RolesBean(MRole role) { public void testLinkSerialization() {
super(role); MRole godRole = new MRole("god");
} MRole janitorRole = new MRole("janitor");
List<MRole> roles = new LinkedList<>();
roles.add(godRole);
roles.add(janitorRole);
public RolesBean(List<MRole> roles) { // Serialize it to JSON object
super(roles); RoleBean outputBean = new RoleBean(roles);
JSONObject json = outputBean.extract(false);
} // "Move" it across network in text form
String jsonString = json.toJSONString();
// For "restore" // Retrieved transferred object
public RolesBean() { JSONObject parsedJson = JSONUtils.parse(jsonString);
} RoleBean inputBean = new RoleBean(roles);
inputBean.restore(parsedJson);
@Override assertEquals(inputBean.getRoles().size(), 2);
@SuppressWarnings("unchecked") assertEquals(inputBean.getRoles().get(0).getName(), "god");
public JSONObject extract(boolean skipSensitive) { assertEquals(inputBean.getRoles().get(1).getName(), "janitor");
JSONArray rolesArray = super.extractRoles();
JSONObject roles = new JSONObject();
roles.put(ROLES, rolesArray);
return roles;
}
@Override
public void restore(JSONObject json) {
JSONArray rolesArray = JSONUtils.getJSONArray(json, ROLES);
restoreRoles(rolesArray);
} }
} }

View File

@ -145,12 +145,12 @@ private JsonBean getRoles(RequestContext ctx) {
MPrincipal principal = new MPrincipal(principal_name, principal_type); MPrincipal principal = new MPrincipal(principal_name, principal_type);
manager.logAuditEvent(ctx.getUserName(), manager.logAuditEvent(ctx.getUserName(),
ctx.getRequest().getRemoteAddr(), "get", "roles by principal", principal.toString()); ctx.getRequest().getRemoteAddr(), "get", "roles by principal", principal.toString());
return new RolesBean(handler.getRolesByPrincipal(principal)); return new RoleBean(handler.getRolesByPrincipal(principal));
} else { } else {
// get all roles in the system // get all roles in the system
manager.logAuditEvent(ctx.getUserName(), manager.logAuditEvent(ctx.getUserName(),
ctx.getRequest().getRemoteAddr(), "get", "roles", "all"); ctx.getRequest().getRemoteAddr(), "get", "roles", "all");
return new RolesBean(handler.getAllRoles()); return new RoleBean(handler.getAllRoles());
} }
} }
@ -258,7 +258,7 @@ private JsonBean grantRevokeRole(RequestContext ctx, boolean isGrant) {
AuthorizationHandler handler = AuthorizationManager.getInstance().getAuthorizationHandler(); AuthorizationHandler handler = AuthorizationManager.getInstance().getAuthorizationHandler();
AuditLoggerManager manager = AuditLoggerManager.getInstance(); AuditLoggerManager manager = AuditLoggerManager.getInstance();
RolesBean rolesBean = new RolesBean(); RoleBean rolesBean = new RoleBean();
PrincipalBean principalsBean = new PrincipalBean(); PrincipalBean principalsBean = new PrincipalBean();
try { try {