diff --git a/guacamole-ext/src/main/java/org/glyptodon/guacamole/token/TokenFilter.java b/guacamole-ext/src/main/java/org/glyptodon/guacamole/token/TokenFilter.java index 527942bfc..56a5dbabc 100644 --- a/guacamole-ext/src/main/java/org/glyptodon/guacamole/token/TokenFilter.java +++ b/guacamole-ext/src/main/java/org/glyptodon/guacamole/token/TokenFilter.java @@ -209,5 +209,26 @@ public class TokenFilter { return output.toString(); } - + + /** + * Given an arbitrary map containing String values, replace each non-null + * value with the corresponding filtered value. + * + * @param map + * The map whose values should be filtered. + */ + public void filterValues(Map map) { + + // For each map entry + for (Map.Entry entry : map.entrySet()) { + + // If value is non-null, filter value through this TokenFilter + String value = entry.getValue(); + if (value != null) + entry.setValue(filter(value)); + + } + + } + } diff --git a/guacamole-ext/src/test/java/org/glyptodon/guacamole/token/TokenFilterTest.java b/guacamole-ext/src/test/java/org/glyptodon/guacamole/token/TokenFilterTest.java index d1c433b00..9d0777c31 100644 --- a/guacamole-ext/src/test/java/org/glyptodon/guacamole/token/TokenFilterTest.java +++ b/guacamole-ext/src/test/java/org/glyptodon/guacamole/token/TokenFilterTest.java @@ -22,6 +22,8 @@ package org.glyptodon.guacamole.token; +import java.util.HashMap; +import java.util.Map; import org.junit.Test; import static org.junit.Assert.*; @@ -57,4 +59,45 @@ public class TokenFilterTest { } + /** + * Verifies that token replacement via filterValues() functions as + * specified. + */ + @Test + public void testFilterValues() { + + // Create token filter + TokenFilter tokenFilter = new TokenFilter(); + tokenFilter.setToken("TOKEN_A", "value-of-a"); + tokenFilter.setToken("TOKEN_B", "value-of-b"); + + // Create test map + Map map = new HashMap(); + map.put(1, "$$${NOPE}hello${TOKEN_A}world${TOKEN_B}$${NOT_A_TOKEN}"); + map.put(2, "${NOPE}hello${TOKEN_A}world${TOKEN_C}"); + map.put(3, null); + + // Filter map values + tokenFilter.filterValues(map); + + // Filter should not affect size of map + assertEquals(3, map.size()); + + // Filtered value 1 + assertEquals( + "$${NOPE}hellovalue-of-aworldvalue-of-b${NOT_A_TOKEN}", + map.get(1) + ); + + // Filtered value 2 + assertEquals( + "${NOPE}hellovalue-of-aworld${TOKEN_C}", + map.get(2) + ); + + // Null values are not filtered + assertNull(map.get(3)); + + } + }