[java-metadata-aggregator] 03/09: Add PathSegment and SHA1 string transformer utilities

Ian Young ian at iay.org.uk
Wed Oct 5 05:34:46 EDT 2016


This is an automated email from the git hooks/post-receive script.

iay pushed a commit to branch master
in repository java-metadata-aggregator.

View the commit online:
http://git.shibboleth.net/view/?p=java-metadata-aggregator.git;a=commit;h=5ea76ec09c638451b5978d99b69b55caf78e5503

commit 5ea76ec09c638451b5978d99b69b55caf78e5503
Author: Ian Young <ian at iay.org.uk>
AuthorDate: Mon Aug 29 15:36:12 2016 +0100

    Add PathSegment and SHA1 string transformer utilities
    
    These are primarily intended for use with
    FilesInDirectoryMultiOutputStrategy.
    
    (cherry picked from commit c7f78d436c22fd237c1b6138dc1e0905ad7536b9)
---
 .../util/PathSegmentStringTransformer.java         | 39 ++++++++++++++++++++++
 .../metadata/util/SHA1StringTransformer.java       | 38 +++++++++++++++++++++
 .../util/PathSegmentStringTransformerTest.java     | 36 ++++++++++++++++++++
 .../metadata/util/SHA1StringTransformerTest.java   | 19 +++++++++++
 4 files changed, 132 insertions(+)

diff --git a/aggregator-pipeline/src/main/java/net/shibboleth/metadata/util/PathSegmentStringTransformer.java b/aggregator-pipeline/src/main/java/net/shibboleth/metadata/util/PathSegmentStringTransformer.java
new file mode 100644
index 0000000..18de7e6
--- /dev/null
+++ b/aggregator-pipeline/src/main/java/net/shibboleth/metadata/util/PathSegmentStringTransformer.java
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development, 
+ * Inc. (UCAID) under one or more contributor license agreements.  See the 
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID 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 net.shibboleth.metadata.util;
+
+import javax.annotation.Nonnull;
+
+import com.google.common.base.Function;
+import com.google.common.net.UrlEscapers;
+
+/**
+ * A {@link Function} that transforms a {@link String} such that it can be used
+ * as a path segment in a URL.
+ * 
+ * Note that this is emphatically <i>not</u> the same as {@link java.net.URLEncoder},
+ * which is intended for the query parameter use case.
+ */
+public class PathSegmentStringTransformer implements Function<String, String> {
+
+    @Override
+    public String apply(@Nonnull final String input) {
+        return UrlEscapers.urlPathSegmentEscaper().escape(input);
+    }
+
+}
diff --git a/aggregator-pipeline/src/main/java/net/shibboleth/metadata/util/SHA1StringTransformer.java b/aggregator-pipeline/src/main/java/net/shibboleth/metadata/util/SHA1StringTransformer.java
new file mode 100644
index 0000000..a66d596
--- /dev/null
+++ b/aggregator-pipeline/src/main/java/net/shibboleth/metadata/util/SHA1StringTransformer.java
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development, 
+ * Inc. (UCAID) under one or more contributor license agreements.  See the 
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID 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 net.shibboleth.metadata.util;
+
+import javax.annotation.Nonnull;
+
+import org.cryptacular.util.CodecUtil;
+import org.cryptacular.util.HashUtil;
+
+import com.google.common.base.Function;
+
+/**
+ * A {@link Function} that transforms a {@link String} into a hex-encoded representation of
+ * the SHA-1 digest of the string.
+ */
+public class SHA1StringTransformer implements Function<String, String> {
+
+    @Override
+    public String apply(@Nonnull final String input) {
+        return CodecUtil.hex(HashUtil.sha1(input.getBytes()));
+    }
+
+}
diff --git a/aggregator-pipeline/src/test/java/net/shibboleth/metadata/util/PathSegmentStringTransformerTest.java b/aggregator-pipeline/src/test/java/net/shibboleth/metadata/util/PathSegmentStringTransformerTest.java
new file mode 100644
index 0000000..2b439d8
--- /dev/null
+++ b/aggregator-pipeline/src/test/java/net/shibboleth/metadata/util/PathSegmentStringTransformerTest.java
@@ -0,0 +1,36 @@
+
+package net.shibboleth.metadata.util;
+
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+import com.google.common.base.Function;
+
+public class PathSegmentStringTransformerTest {
+    
+    private static final Function<String, String> transform =
+            new PathSegmentStringTransformer();
+
+    @Test public void testMDQExample() {
+        Assert.assertEquals(transform.apply("http://example.org/idp"),
+                "http:%2F%2Fexample.org%2Fidp");
+    }
+
+    @Test public void testSpace() {
+        Assert.assertEquals(transform.apply("a b"), "a%20b");
+    }
+    
+    // https://www.talisman.org/~erlkonig/misc/lunatech%5Ewhat-every-webdev-must-know-about-url-encoding/#Thereservedcharactersaredifferentforeachpart
+    @Test public void testBlue() {
+        Assert.assertEquals(transform.apply("blue+light blue"),
+                "blue+light%20blue");
+    }
+
+    @Test public void testMDQExample2() {
+        Assert.assertEquals(transform.apply("blue/green+light blue"),
+                "blue%2Fgreen+light%20blue");
+    }
+    @Test public void testPercent() {
+        Assert.assertEquals(transform.apply("%"), "%25");
+    }
+}
diff --git a/aggregator-pipeline/src/test/java/net/shibboleth/metadata/util/SHA1StringTransformerTest.java b/aggregator-pipeline/src/test/java/net/shibboleth/metadata/util/SHA1StringTransformerTest.java
new file mode 100644
index 0000000..7c7636e
--- /dev/null
+++ b/aggregator-pipeline/src/test/java/net/shibboleth/metadata/util/SHA1StringTransformerTest.java
@@ -0,0 +1,19 @@
+
+package net.shibboleth.metadata.util;
+
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+import com.google.common.base.Function;
+
+public class SHA1StringTransformerTest {
+
+    private static final Function<String, String> transform =
+            new SHA1StringTransformer();
+
+    @Test public void testMDQExample() {
+        Assert.assertEquals(transform.apply("http://example.org/service"),
+                "11d72e8cf351eb6c75c721e838f469677ab41bdb");
+    }
+
+}

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list