sqoop-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From co...@apache.org
Subject sqoop git commit: SQOOP-2772: Sqoop2: Remove the notion of PrincipalsBean (Jarek Jarcec Cecho via Colin Ma)
Date Thu, 14 Jan 2016 05:45:23 GMT
Repository: sqoop
Updated Branches:
  refs/heads/sqoop2 2df3db3e7 -> 88654b95f


SQOOP-2772: Sqoop2: Remove the notion of PrincipalsBean
 (Jarek Jarcec Cecho via Colin Ma)


Project: http://git-wip-us.apache.org/repos/asf/sqoop/repo
Commit: http://git-wip-us.apache.org/repos/asf/sqoop/commit/88654b95
Tree: http://git-wip-us.apache.org/repos/asf/sqoop/tree/88654b95
Diff: http://git-wip-us.apache.org/repos/asf/sqoop/diff/88654b95

Branch: refs/heads/sqoop2
Commit: 88654b95fac2d52c9acbf8b7af6d65bd5cff5529
Parents: 2df3db3
Author: Colin Ma <colin@apache.org>
Authored: Thu Jan 14 14:35:35 2016 +0800
Committer: Colin Ma <colin@apache.org>
Committed: Thu Jan 14 14:35:35 2016 +0800

----------------------------------------------------------------------
 .../request/AuthorizationResourceRequest.java   |  8 +--
 .../client/request/SqoopResourceRequests.java   |  2 +-
 .../org/apache/sqoop/json/PrincipalBean.java    | 26 ++++----
 .../org/apache/sqoop/json/PrincipalsBean.java   | 63 --------------------
 .../apache/sqoop/json/TestPrincipalBean.java    | 60 +++++++++++++++++++
 .../handler/AuthorizationRequestHandler.java    |  6 +-
 6 files changed, 81 insertions(+), 84 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/sqoop/blob/88654b95/client/src/main/java/org/apache/sqoop/client/request/AuthorizationResourceRequest.java
----------------------------------------------------------------------
diff --git a/client/src/main/java/org/apache/sqoop/client/request/AuthorizationResourceRequest.java
b/client/src/main/java/org/apache/sqoop/client/request/AuthorizationResourceRequest.java
index ca05d1b..e99e913 100644
--- a/client/src/main/java/org/apache/sqoop/client/request/AuthorizationResourceRequest.java
+++ b/client/src/main/java/org/apache/sqoop/client/request/AuthorizationResourceRequest.java
@@ -73,7 +73,7 @@ public class AuthorizationResourceRequest extends ResourceRequest {
 
   public void grantRevokeRole(String serverUrl, List<MRole> roles, List<MPrincipal>
principals, boolean isGrant) {
     RolesBean rolesBean = new RolesBean(roles);
-    PrincipalsBean principalsBean = new PrincipalsBean(principals);
+    PrincipalBean principalsBean = new PrincipalBean(principals);
     // Extract all config inputs including sensitive inputs
     JSONObject jsonObject = new JSONObject();
     jsonObject.putAll(rolesBean.extract(false));
@@ -95,17 +95,17 @@ public class AuthorizationResourceRequest extends ResourceRequest {
     return bean;
   }
 
-  public PrincipalsBean readPrincipalsByRole(String serverUrl, MRole role) {
+  public PrincipalBean readPrincipalsByRole(String serverUrl, MRole role) {
     String response = super.get(serverUrl + RESOURCE + PRINCIPALS
             + "?role_name=" + UrlSafeUtils.urlEncode(role.getName()));
     JSONObject jsonObject = JSONUtils.parse(response);
-    PrincipalsBean bean = new PrincipalsBean();
+    PrincipalBean bean = new PrincipalBean();
     bean.restore(jsonObject);
     return bean;
   }
 
   public void grantRevokePrivilege(String serverUrl, List<MPrincipal> principals, List<MPrivilege>
privileges, boolean isGrant) {
-    PrincipalsBean principalsBean = new PrincipalsBean(principals);
+    PrincipalBean principalsBean = new PrincipalBean(principals);
     // Extract all config inputs including sensitive inputs
     JSONObject jsonObject = new JSONObject();
     jsonObject.putAll(principalsBean.extract(false));

http://git-wip-us.apache.org/repos/asf/sqoop/blob/88654b95/client/src/main/java/org/apache/sqoop/client/request/SqoopResourceRequests.java
----------------------------------------------------------------------
diff --git a/client/src/main/java/org/apache/sqoop/client/request/SqoopResourceRequests.java
b/client/src/main/java/org/apache/sqoop/client/request/SqoopResourceRequests.java
index 3109718..c962842 100644
--- a/client/src/main/java/org/apache/sqoop/client/request/SqoopResourceRequests.java
+++ b/client/src/main/java/org/apache/sqoop/client/request/SqoopResourceRequests.java
@@ -202,7 +202,7 @@ public class SqoopResourceRequests {
     return getAuthorizationRequest().readRolesByPrincipal(serverUrl, principal);
   }
 
-  public PrincipalsBean readPrincipalsByRole(MRole role) {
+  public PrincipalBean readPrincipalsByRole(MRole role) {
     return getAuthorizationRequest().readPrincipalsByRole(serverUrl, role);
   }
 

http://git-wip-us.apache.org/repos/asf/sqoop/blob/88654b95/common/src/main/java/org/apache/sqoop/json/PrincipalBean.java
----------------------------------------------------------------------
diff --git a/common/src/main/java/org/apache/sqoop/json/PrincipalBean.java b/common/src/main/java/org/apache/sqoop/json/PrincipalBean.java
index f89d9ee..c664027 100644
--- a/common/src/main/java/org/apache/sqoop/json/PrincipalBean.java
+++ b/common/src/main/java/org/apache/sqoop/json/PrincipalBean.java
@@ -33,7 +33,7 @@ import java.util.List;
 @InterfaceStability.Unstable
 public class PrincipalBean implements JsonBean {
 
-  public static final String PRINCIPAL = "principal";
+  private static final String PRINCIPALS = "principals";
   private static final String NAME = "name";
   private static final String TYPE = "type";
 
@@ -46,7 +46,7 @@ public class PrincipalBean implements JsonBean {
   // For "extract"
   public PrincipalBean(MPrincipal principal) {
     this();
-    this.principals = new ArrayList<MPrincipal>();
+    this.principals = new ArrayList<>();
     this.principals.add(principal);
   }
 
@@ -62,9 +62,16 @@ public class PrincipalBean implements JsonBean {
   @Override
   @SuppressWarnings("unchecked")
   public JSONObject extract(boolean skipSensitive) {
-    JSONObject principal = new JSONObject();
-    principal.put(PRINCIPAL, extractPrincipal(principals.get(0)));
-    return principal;
+    JSONArray principalsArray = extractPrincipals();
+    JSONObject principals = new JSONObject();
+    principals.put(PRINCIPALS, principalsArray);
+    return principals;
+  }
+
+  @Override
+  public void restore(JSONObject json) {
+    JSONArray principalsArray = JSONUtils.getJSONArray(json, PRINCIPALS);
+    restorePrincipals(principalsArray);
   }
 
   @SuppressWarnings("unchecked")
@@ -86,15 +93,8 @@ public class PrincipalBean implements JsonBean {
     return object;
   }
 
-  @Override
-  public void restore(JSONObject json) {
-    principals = new ArrayList<MPrincipal>();
-    JSONObject obj = JSONUtils.getJSONObject(json, PRINCIPAL);
-    principals.add(restorePrincipal(obj));
-  }
-
   protected void restorePrincipals(JSONArray array) {
-    principals = new ArrayList<MPrincipal>();
+    principals = new ArrayList<>();
     for (Object obj : array) {
       principals.add(restorePrincipal(obj));
     }

http://git-wip-us.apache.org/repos/asf/sqoop/blob/88654b95/common/src/main/java/org/apache/sqoop/json/PrincipalsBean.java
----------------------------------------------------------------------
diff --git a/common/src/main/java/org/apache/sqoop/json/PrincipalsBean.java b/common/src/main/java/org/apache/sqoop/json/PrincipalsBean.java
deleted file mode 100644
index d52a462..0000000
--- a/common/src/main/java/org/apache/sqoop/json/PrincipalsBean.java
+++ /dev/null
@@ -1,63 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.sqoop.json;
-
-import org.apache.sqoop.classification.InterfaceAudience;
-import org.apache.sqoop.classification.InterfaceStability;
-import org.apache.sqoop.model.MPrincipal;
-import org.json.simple.JSONArray;
-import org.json.simple.JSONObject;
-
-import java.util.List;
-
-@InterfaceAudience.Private
-@InterfaceStability.Unstable
-public class PrincipalsBean extends PrincipalBean {
-
-  private static final String PRINCIPALS = "principals";
-
-  // For "extract"
-  public PrincipalsBean(MPrincipal principal) {
-    super(principal);
-  }
-
-  public PrincipalsBean(List<MPrincipal> principals) {
-    super(principals);
-
-  }
-
-  // For "restore"
-  public PrincipalsBean() {
-  }
-
-  @Override
-  @SuppressWarnings("unchecked")
-  public JSONObject extract(boolean skipSensitive) {
-    JSONArray principalsArray = super.extractPrincipals();
-    JSONObject principals = new JSONObject();
-    principals.put(PRINCIPALS, principalsArray);
-    return principals;
-  }
-
-  @Override
-  public void restore(JSONObject json) {
-    JSONArray principalsArray = JSONUtils.getJSONArray(json, PRINCIPALS);
-    restorePrincipals(principalsArray);
-  }
-
-}

http://git-wip-us.apache.org/repos/asf/sqoop/blob/88654b95/common/src/test/java/org/apache/sqoop/json/TestPrincipalBean.java
----------------------------------------------------------------------
diff --git a/common/src/test/java/org/apache/sqoop/json/TestPrincipalBean.java b/common/src/test/java/org/apache/sqoop/json/TestPrincipalBean.java
new file mode 100644
index 0000000..cecb450
--- /dev/null
+++ b/common/src/test/java/org/apache/sqoop/json/TestPrincipalBean.java
@@ -0,0 +1,60 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sqoop.json;
+
+import org.apache.sqoop.model.MPrincipal;
+import org.json.simple.JSONObject;
+import org.testng.annotations.Test;
+
+import java.util.LinkedList;
+import java.util.List;
+
+import static org.testng.AssertJUnit.assertEquals;
+
+public class TestPrincipalBean {
+
+  @Test
+  public void testSerialization() {
+    MPrincipal principalUser = new MPrincipal("jarcec", MPrincipal.TYPE.USER);
+    MPrincipal principalGroup = new MPrincipal("sqoop", MPrincipal.TYPE.GROUP);
+    List<MPrincipal> principals = new LinkedList<>();
+    principals.add(principalUser);
+    principals.add(principalGroup);
+
+    // Serialize it to JSON object
+    PrincipalBean outputBean = new PrincipalBean(principals);
+    JSONObject json = outputBean.extract(false);
+
+    // "Move" it across network in text form
+    String jsonString = json.toJSONString();
+
+    // Retrieved transferred object
+    JSONObject parsedJson = JSONUtils.parse(jsonString);
+    PrincipalBean inputBean = new PrincipalBean();
+    inputBean.restore(parsedJson);
+
+    assertEquals(inputBean.getPrincipals().size(), 2);
+
+    assertEquals(inputBean.getPrincipals().get(0).getName(), "jarcec");
+    assertEquals(inputBean.getPrincipals().get(0).getType(), "USER");
+
+    assertEquals(inputBean.getPrincipals().get(1).getName(), "sqoop");
+    assertEquals(inputBean.getPrincipals().get(1).getType(), "GROUP");
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/sqoop/blob/88654b95/server/src/main/java/org/apache/sqoop/handler/AuthorizationRequestHandler.java
----------------------------------------------------------------------
diff --git a/server/src/main/java/org/apache/sqoop/handler/AuthorizationRequestHandler.java
b/server/src/main/java/org/apache/sqoop/handler/AuthorizationRequestHandler.java
index 1ed63e4..45bf4d0 100644
--- a/server/src/main/java/org/apache/sqoop/handler/AuthorizationRequestHandler.java
+++ b/server/src/main/java/org/apache/sqoop/handler/AuthorizationRequestHandler.java
@@ -164,7 +164,7 @@ public class AuthorizationRequestHandler implements RequestHandler {
       MRole role = new MRole(role_name);
       manager.logAuditEvent(ctx.getUserName(),
               ctx.getRequest().getRemoteAddr(), "get", "principals by role", role.toString());
-      return new PrincipalsBean(handler.getPrincipalsByRole(role));
+      return new PrincipalBean(handler.getPrincipalsByRole(role));
     } else {
       throw new SqoopException(SecurityError.AUTH_0012, "Can't get role name");
     }
@@ -259,7 +259,7 @@ public class AuthorizationRequestHandler implements RequestHandler {
     AuditLoggerManager manager = AuditLoggerManager.getInstance();
 
     RolesBean rolesBean = new RolesBean();
-    PrincipalsBean principalsBean = new PrincipalsBean();
+    PrincipalBean principalsBean = new PrincipalBean();
 
     try {
       JSONObject json = JSONUtils.parse(ctx.getRequest().getReader());
@@ -290,7 +290,7 @@ public class AuthorizationRequestHandler implements RequestHandler {
     AuthorizationHandler handler = AuthorizationManager.getInstance().getAuthorizationHandler();
     AuditLoggerManager manager = AuditLoggerManager.getInstance();
 
-    PrincipalsBean principalsBean = new PrincipalsBean();
+    PrincipalBean principalsBean = new PrincipalBean();
     PrivilegesBean privilegesBean = new PrivilegesBean();
 
     try {


Mime
View raw message