From a6601a2bfde529fa58575721da0b2ad491c1e8a1 Mon Sep 17 00:00:00 2001 From: Virtually Nick Date: Fri, 21 Jun 2019 12:54:42 -0400 Subject: [PATCH] GUACAMOLE-579: Change fromAttribute to canonicalize --- .../guacamole/auth/cas/CASTokenName.java | 33 --------------- .../cas/ticket/TicketValidationService.java | 2 +- .../ldap/AuthenticationProviderService.java | 2 +- .../org/apache/guacamole/token/TokenName.java | 42 +++++++++---------- .../apache/guacamole/token/TokenNameTest.java | 36 ++++++++-------- 5 files changed, 41 insertions(+), 74 deletions(-) delete mode 100644 extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/CASTokenName.java diff --git a/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/CASTokenName.java b/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/CASTokenName.java deleted file mode 100644 index c5193e92b..000000000 --- a/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/CASTokenName.java +++ /dev/null @@ -1,33 +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.guacamole.auth.cas; - -import org.apache.guacamole.token.TokenName; - -/** - * Utility class for generating parameter token names. - */ -public class CASTokenName extends TokenName { - - public static String fromAttribute(String name) { - return fromAttribute(name, "CAS_"); - } - -} diff --git a/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/ticket/TicketValidationService.java b/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/ticket/TicketValidationService.java index f3198de93..628b28dcf 100644 --- a/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/ticket/TicketValidationService.java +++ b/extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/ticket/TicketValidationService.java @@ -119,7 +119,7 @@ public class TicketValidationService { // Convert remaining attributes that have values to Strings for (Entry attr : ticketAttrs.entrySet()) { - String tokenName = TokenName.fromAttribute(attr.getKey(), + String tokenName = TokenName.canonicalize(attr.getKey(), CAS_ATTRIBUTE_TOKEN_PREFIX); Object value = attr.getValue(); if (value != null) diff --git a/extensions/guacamole-auth-ldap/src/main/java/org/apache/guacamole/auth/ldap/AuthenticationProviderService.java b/extensions/guacamole-auth-ldap/src/main/java/org/apache/guacamole/auth/ldap/AuthenticationProviderService.java index ac1fee4ce..949d1c87d 100644 --- a/extensions/guacamole-auth-ldap/src/main/java/org/apache/guacamole/auth/ldap/AuthenticationProviderService.java +++ b/extensions/guacamole-auth-ldap/src/main/java/org/apache/guacamole/auth/ldap/AuthenticationProviderService.java @@ -315,7 +315,7 @@ public class AuthenticationProviderService { // Convert each retrieved attribute into a corresponding token for (Object attrObj : attrSet) { LDAPAttribute attr = (LDAPAttribute)attrObj; - tokens.put(TokenName.fromAttribute(attr.getName(), + tokens.put(TokenName.canonicalize(attr.getName(), LDAP_ATTRIBUTE_TOKEN_PREFIX), attr.getStringValue()); } diff --git a/guacamole-ext/src/main/java/org/apache/guacamole/token/TokenName.java b/guacamole-ext/src/main/java/org/apache/guacamole/token/TokenName.java index 1e90114e5..4b36377e9 100644 --- a/guacamole-ext/src/main/java/org/apache/guacamole/token/TokenName.java +++ b/guacamole-ext/src/main/java/org/apache/guacamole/token/TokenName.java @@ -28,13 +28,13 @@ import java.util.regex.Pattern; public abstract class TokenName { /** - * Pattern which matches logical groupings of words within an - * attribute name. This pattern is intended to match logical groupings + * Pattern which matches logical groupings of words within a + * string. This pattern is intended to match logical groupings * regardless of the naming convention used: "CamelCase", * "headlessCamelCase", "lowercase_with_underscores", * "lowercase-with-dashes" or even "aVery-INCONSISTENTMix_ofAllStyles". */ - private static final Pattern ATTRIBUTE_NAME_GROUPING = Pattern.compile( + private static final Pattern STRING_NAME_GROUPING = Pattern.compile( // "Camel" word groups "\\p{javaUpperCase}\\p{javaLowerCase}+" @@ -61,29 +61,29 @@ public abstract class TokenName { /** * Generates the name of the parameter token that should be populated with - * the value of the given attribute. The name of the attribute will - * automatically be transformed from "CamelCase", "headlessCamelCase", - * "lowercase_with_underscores", and "mixes_ofBoth_Styles" to consistent - * "UPPERCASE_WITH_UNDERSCORES". Each returned attribute will be prefixed - * with value provided in the prefix. The value provided in prefix will - * be prepended to the attribute name, but will itself not be transformed. + * the given string. The provided string will be automatically transformed + * from "CamelCase", "headlessCamelCase", "lowercase_with_underscores", + * and "mixes_ofBoth_Styles" to consistent "UPPERCASE_WITH_UNDERSCORES". + * Each returned attribute will be prefixed with the string value provided + * in the prefix. The value provided in prefix will be prepended to the + * attribute name, but will itself not be transformed. * * @param name - * The name of the attribute to use to generate the token name. + * The string to be used to generate the token name. * * @param prefix * The prefix to prepend to the generated token name. * * @return * The name of the parameter token that should be populated with the - * value of the attribute having the given name. + * given string. */ - public static String fromAttribute(final String name, final String prefix) { + public static String canonicalize(final String name, final String prefix) { // If even one logical word grouping cannot be found, default to - // simply converting the attribute to uppercase and adding the + // simply converting the string to uppercase and adding the // prefix - Matcher groupMatcher = ATTRIBUTE_NAME_GROUPING.matcher(name); + Matcher groupMatcher = STRING_NAME_GROUPING.matcher(name); if (!groupMatcher.find()) return prefix + name.toUpperCase(); @@ -102,19 +102,19 @@ public abstract class TokenName { } /** - * Generate the name of a parameter from the value of the given attribute, - * and with a blank prefix such that the token name will simply be the - * transformed version of the attribute name. + * Generate the name of a parameter from the given string, and with a blank + * prefix such that the token name will simply be the transformed version + * of the string. * * @param name - * The name of the attribute to use to generate the token name. + * The string to use to generate the token name. * * @return * The name of the parameter token that should be populated with the - * value of the attribute having the given name. + * given string. */ - public static String fromAttribute(final String name) { - return fromAttribute(name, ""); + public static String canonicalize(final String name) { + return canonicalize(name, ""); } } diff --git a/guacamole-ext/src/test/java/org/apache/guacamole/token/TokenNameTest.java b/guacamole-ext/src/test/java/org/apache/guacamole/token/TokenNameTest.java index 08c819158..7b9d3c2b1 100644 --- a/guacamole-ext/src/test/java/org/apache/guacamole/token/TokenNameTest.java +++ b/guacamole-ext/src/test/java/org/apache/guacamole/token/TokenNameTest.java @@ -29,28 +29,28 @@ import org.junit.Test; public class TokenNameTest { /** - * Verifies that TokenName.fromAttribute() generates token names as + * Verifies that TokenName.canonicalize() generates token names as * specified, regardless of the naming convention of the attribute. */ @Test - public void testFromAttribute() { - assertEquals("A", TokenName.fromAttribute("a")); - assertEquals("B", TokenName.fromAttribute("b")); - assertEquals("1", TokenName.fromAttribute("1")); - assertEquals("SOME_URL", TokenName.fromAttribute("someURL")); - assertEquals("LOWERCASE_WITH_DASHES", TokenName.fromAttribute("lowercase-with-dashes")); - assertEquals("HEADLESS_CAMEL_CASE", TokenName.fromAttribute("headlessCamelCase")); - assertEquals("CAMEL_CASE", TokenName.fromAttribute("CamelCase")); - assertEquals("CAMEL_CASE", TokenName.fromAttribute("CamelCase")); - assertEquals("LOWERCASE_WITH_UNDERSCORES", TokenName.fromAttribute("lowercase_with_underscores")); - assertEquals("UPPERCASE_WITH_UNDERSCORES", TokenName.fromAttribute("UPPERCASE_WITH_UNDERSCORES")); - assertEquals("A_VERY_INCONSISTENT_MIX_OF_ALL_STYLES", TokenName.fromAttribute("aVery-INCONSISTENTMix_ofAllStyles")); - assertEquals("ABC_123_DEF_456", TokenName.fromAttribute("abc123def456")); - assertEquals("ABC_123_DEF_456", TokenName.fromAttribute("ABC123DEF456")); - assertEquals("WORD_A_WORD_AB_WORD_ABC_WORD", TokenName.fromAttribute("WordAWordABWordABCWord")); + public void testCanonicalize() { + assertEquals("A", TokenName.canonicalize("a")); + assertEquals("B", TokenName.canonicalize("b")); + assertEquals("1", TokenName.canonicalize("1")); + assertEquals("SOME_URL", TokenName.canonicalize("someURL")); + assertEquals("LOWERCASE_WITH_DASHES", TokenName.canonicalize("lowercase-with-dashes")); + assertEquals("HEADLESS_CAMEL_CASE", TokenName.canonicalize("headlessCamelCase")); + assertEquals("CAMEL_CASE", TokenName.canonicalize("CamelCase")); + assertEquals("CAMEL_CASE", TokenName.canonicalize("CamelCase")); + assertEquals("LOWERCASE_WITH_UNDERSCORES", TokenName.canonicalize("lowercase_with_underscores")); + assertEquals("UPPERCASE_WITH_UNDERSCORES", TokenName.canonicalize("UPPERCASE_WITH_UNDERSCORES")); + assertEquals("A_VERY_INCONSISTENT_MIX_OF_ALL_STYLES", TokenName.canonicalize("aVery-INCONSISTENTMix_ofAllStyles")); + assertEquals("ABC_123_DEF_456", TokenName.canonicalize("abc123def456")); + assertEquals("ABC_123_DEF_456", TokenName.canonicalize("ABC123DEF456")); + assertEquals("WORD_A_WORD_AB_WORD_ABC_WORD", TokenName.canonicalize("WordAWordABWordABCWord")); - assertEquals("AUTH_ATTRIBUTE", TokenName.fromAttribute("Attribute", "AUTH_")); - assertEquals("auth_SOMETHING", TokenName.fromAttribute("Something", "auth_")); + assertEquals("AUTH_ATTRIBUTE", TokenName.canonicalize("Attribute", "AUTH_")); + assertEquals("auth_SOMETHING", TokenName.canonicalize("Something", "auth_")); } }