fateh288 commented on code in PR #659:
URL: https://github.com/apache/ranger/pull/659#discussion_r2380430832


##########
authz-api/src/main/java/org/apache/ranger/authz/util/RangerResourceTemplate.java:
##########
@@ -0,0 +1,184 @@
+/*
+ * 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.ranger.authz.util;
+
+import org.apache.ranger.authz.api.RangerAuthzException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static 
org.apache.ranger.authz.api.RangerAuthzApiErrorCode.INVALID_RESOURCE_TEMPLATE_UNEXPECTED_MARKER_AT;
+
+public class RangerResourceTemplate {
+    private static final Logger LOG = 
LoggerFactory.getLogger(RangerResourceTemplate.class);
+
+    private static final char RESOURCE_START_CHAR = '{';
+    private static final char RESOURCE_END_CHAR   = '}';
+    private static final char ESCAPE_CHAR         = '\\';
+
+    private final String   template;   // examples: 
{database}.{table}.{column}, ofs://{bucket}/{volume}/{path}
+    private final String[] resources;  // examples: [database, table, column], 
  [bucket, volume, path]
+    private final String   prefix;     // examples: "",                        
  "ofs://"
+    private final String[] separators; // examples: [".", "."],                
  ["/", "/"]
+    private final String   suffix;
+
+    public RangerResourceTemplate(String template) throws RangerAuthzException 
{
+        List<String>  resources    = new ArrayList<>();
+        List<String>  separators   = new ArrayList<>();
+        String        prefix       = null;
+        boolean       isInEscape   = false;
+        boolean       isInResource = false;
+        StringBuilder currentToken = new StringBuilder();
+
+        for (int i = 0; i < template.length(); i++) {
+            char c = template.charAt(i);
+
+            if (isInEscape) {
+                currentToken.append(c);
+
+                isInEscape = false;
+            } else if (c == ESCAPE_CHAR) {
+                isInEscape = true;
+            } else if (c == RESOURCE_START_CHAR) {
+                if (isInResource) {
+                    throw new 
RangerAuthzException(INVALID_RESOURCE_TEMPLATE_UNEXPECTED_MARKER_AT, template, 
RESOURCE_START_CHAR, i);
+                }
+
+                if (prefix == null) {
+                    prefix = currentToken.toString();
+                } else {
+                    separators.add(currentToken.toString());
+                }
+
+                isInResource = true;
+
+                currentToken.setLength(0);
+            } else if (c == RESOURCE_END_CHAR) {
+                if (!isInResource) {
+                    throw new 
RangerAuthzException(INVALID_RESOURCE_TEMPLATE_UNEXPECTED_MARKER_AT, template, 
RESOURCE_END_CHAR, i);
+                }
+
+                resources.add(currentToken.toString());
+
+                isInResource = false;
+
+                currentToken.setLength(0);
+            } else {
+                currentToken.append(c);
+            }
+        }
+
+        this.template   = template;
+        this.resources  = resources.toArray(new String[0]);
+        this.separators = separators.toArray(new String[0]);

Review Comment:
   Why can't we directly use ArrayList as the class variable (for resources and 
separators here) instead of trying to convert List<String> into String[] 



-- 
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