alopresto commented on a change in pull request #302:
URL: https://github.com/apache/nifi-registry/pull/302#discussion_r490514473



##########
File path: 
nifi-registry-core/nifi-registry-security-utils/src/main/java/org/apache/nifi/registry/security/util/ProxiedEntitiesUtils.java
##########
@@ -39,6 +41,55 @@
 
     private static final String ANONYMOUS_CHAIN = "<>";
 
+    /**
+     * Formats a list of DN/usernames to be set as a HTTP header using well 
known conventions.
+     *
+     * @param proxiedEntities the raw identities (usernames and DNs) to be 
formatted as a chain
+     * @return the value to use in the X-ProxiedEntitiesChain header
+     */
+    public static String getProxiedEntitiesChain(final String[] 
proxiedEntities) {
+        if (proxiedEntities == null) {
+            return null;
+        }
+
+        final List<String> proxiedEntityChain = Arrays.stream(proxiedEntities)
+                .map(ProxiedEntitiesUtils::formatProxyDn)
+                .collect(Collectors.toList());
+        return StringUtils.join(proxiedEntityChain, "");
+    }
+
+    /**
+     * Tokenizes the specified proxy chain.
+     *
+     * @param rawProxyChain raw chain
+     * @return tokenized proxy chain
+     */
+    public static List<String> tokenizeProxiedEntitiesChain(final String 
rawProxyChain) {
+        final List<String> proxyChain = new ArrayList<>();
+        if (!StringUtils.isEmpty(rawProxyChain)) {
+            // Split the String on the >< token
+            rawProxyChain.split("><");
+            List<String> elements = 
Arrays.asList(StringUtils.splitByWholeSeparatorPreserveAllTokens(rawProxyChain, 
"><"));
+
+            // Remove the leading < from the first element
+            elements.set(0, elements.get(0).replaceFirst(LT, ""));

Review comment:
       I'm a little confused by this process; if the first `<` is removed, 
shouldn't the last element always have a second trailing `>` (that must be 
removed to maintain pairing)? 
   
   Also, could this be simplified to 
`Arrays.asList(StringUtils.splitBy...(rawProxyChain.substring(1, 
rawProxyChain.length() - 1))` and avoid the first/last element modification?




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

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to