[java-identity-provider] 03/11: IDP-1464 Introduced a processed UIInfo class

Rod Widdowson rdw at steadingsoftware.com
Tue Jun 11 09:38:21 EDT 2019


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

rdw pushed a commit to branch master
in repository java-identity-provider.

View the commit online:
http://git.shibboleth.net/view/?p=java-identity-provider.git;a=commit;h=c41b441a34a23606759fbeadb767fa2c0483e95a

commit c41b441a34a23606759fbeadb767fa2c0483e95a
Author: Rod Widdowson <rdw at steadingsoftware.com>
AuthorDate: Sat Jun 8 15:37:48 2019 +0100

    IDP-1464 Introduced a processed UIInfo class
    
    https://issues.shibboleth.net/jira/browse/IDP-1464
    
    This converts lists of language bearing OpenSAML objects
    into Maps<Locale, String>. We filter (and warn) against
    elements with no language except for the logo where we provide the
    Map for ones with languages and an array for the ones without.
---
 .../shibboleth/idp/saml/metadata/IdPUIInfo.java    | 226 +++++++++++++++++++++
 .../idp/saml/metadata/IdPUIInfoTest.java           |  50 +++++
 idp-saml-api/src/test/resources/UIInfo.xml         |  17 ++
 3 files changed, 293 insertions(+)

diff --git a/idp-saml-api/src/main/java/net/shibboleth/idp/saml/metadata/IdPUIInfo.java b/idp-saml-api/src/main/java/net/shibboleth/idp/saml/metadata/IdPUIInfo.java
new file mode 100644
index 0000000..6ad321c
--- /dev/null
+++ b/idp-saml-api/src/main/java/net/shibboleth/idp/saml/metadata/IdPUIInfo.java
@@ -0,0 +1,226 @@
+/*
+ * 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.idp.saml.metadata;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.function.Predicate;
+import java.util.stream.Collectors;
+
+import javax.annotation.Nonnull;
+
+import org.opensaml.saml.ext.saml2mdui.Keywords;
+import org.opensaml.saml.ext.saml2mdui.Logo;
+import org.opensaml.saml.ext.saml2mdui.UIInfo;
+import org.opensaml.saml.saml2.metadata.LocalizedName;
+import org.opensaml.saml.saml2.metadata.LocalizedURI;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import net.shibboleth.utilities.java.support.annotation.constraint.Unmodifiable;
+
+/**
+ * Class to contain a processed form of the {@link UIInfo} suitable for display purposes. */
+public class IdPUIInfo {
+    
+    /** logger. */
+    private static final Logger LOG = LoggerFactory.getLogger(IdPUIInfo.class);
+    
+    /** The Display Names as a map from locale to actual value.*/ 
+    @Nonnull @Unmodifiable private final Map<Locale, String> displayNames;
+    
+    /** The Keywords as a map from locale to lists of actual values.*/ 
+    @Nonnull @Unmodifiable private final Map<Locale, List<String>> keywordList;
+    
+    /** The Descriptions as a map from locale to actual value.*/ 
+    @Nonnull @Unmodifiable private final Map<Locale, String> descriptions;
+    
+    /** The Logos as a map from locale to actual value.*/ 
+    @Nonnull @Unmodifiable private final Map<Locale, List<Logo>> localeLogos;
+
+    /** The non Locale bearing Logos .*/ 
+    @Nonnull @Unmodifiable private final List<Logo> nonLocaleLogos;
+
+    /** The Information URLs as a map from locale to actual value.*/ 
+    @Nonnull @Unmodifiable private final Map<Locale, String> informationURLs;
+    
+    /** The Privacy Statement URLs as a map from locale to actual value.*/ 
+    @Nonnull @Unmodifiable private final Map<Locale, String> privacyStatementURLs; 
+    
+    /** Warning check against a non localized URL. */
+    private final Predicate<LocalizedURI> nullLanguageURL = new Predicate<>() {
+        public boolean test(final LocalizedURI u) {
+            if (u.getXMLLang() == null) {
+                LOG.warn("URI with value {} has no language associated, ignoring", u.getValue());
+                return false;
+            }
+            return true;
+        }
+    };
+    
+    /** Warning check against a non localized String. */
+    private final Predicate<LocalizedName> nullLanguageString = new Predicate<>() {
+        public boolean test(final LocalizedName u) {
+            if (u.getXMLLang() == null) {
+                LOG.warn("String with value {} has no language associated, ignoring", u.getValue());
+                return false;
+            }
+            return true;
+        }
+    };
+
+    /** Warning check against a non localized keyword. */
+    private final Predicate<Keywords> nullLanguageKeyword = new Predicate<>() {
+        public boolean test(final Keywords u) {
+            if (u.getXMLLang() == null) {
+                LOG.warn("String with value {} has no language associated, ignoring", u.getKeywords().toString());
+                return false;
+            }
+            return true;
+        }
+    };
+
+    /**
+     * Constructor.
+     *
+     * @param uiInfo The OpenSaml UIInfo to convert.
+     */
+    public IdPUIInfo(@Nonnull final UIInfo uiInfo) {
+        
+        displayNames = uiInfo.
+                getDisplayNames().
+                stream().
+                filter(nullLanguageString).
+                collect(Collectors.toUnmodifiableMap(
+                        displayName -> Locale.forLanguageTag(displayName.getXMLLang()), 
+                        displayName -> displayName.getValue()));
+        keywordList = uiInfo.
+                getKeywords().
+                stream().
+                filter(nullLanguageKeyword).
+                collect(Collectors.toUnmodifiableMap(
+                        keywords -> Locale.forLanguageTag(keywords.getXMLLang()), 
+                        keywords -> keywords.getKeywords()));
+        descriptions = uiInfo.
+                getDescriptions().
+                stream().
+                filter(nullLanguageString).
+                collect(Collectors.toUnmodifiableMap(
+                        description -> Locale.forLanguageTag(description.getXMLLang()), 
+                        description -> description.getValue()));
+        informationURLs = uiInfo.
+                getInformationURLs().
+                stream().
+                filter(nullLanguageURL).
+                collect(Collectors.toUnmodifiableMap(
+                        url -> Locale.forLanguageTag(url.getXMLLang()), 
+                        dn -> dn.getValue()));
+        privacyStatementURLs = uiInfo.
+                getPrivacyStatementURLs().
+                stream().
+                filter(nullLanguageURL).
+                collect(Collectors.toUnmodifiableMap(
+                        url -> Locale.forLanguageTag(url.getXMLLang()), 
+                        url -> url.getValue()));
+        
+        final List<Logo> noLocaleLogo = new ArrayList<>();
+        final Map<Locale, List<Logo>> withLocaleLogo = new HashMap<>();
+        for (final Logo logo : uiInfo.getLogos()) {
+            if (logo.getXMLLang() != null) {
+                final Locale l = Locale.forLanguageTag(logo.getXMLLang());
+                if (withLocaleLogo.get(l) == null) {
+                    withLocaleLogo.put(l, new ArrayList<>());
+                }
+                withLocaleLogo.get(l).add(logo);
+            } else { 
+                noLocaleLogo.add(logo);
+            }
+        }
+        localeLogos = Collections.unmodifiableMap(withLocaleLogo);
+        nonLocaleLogos = Collections.unmodifiableList(noLocaleLogo);
+    }
+
+    /** 
+     * Get the Display Names as a map from locale to actual value.
+     * 
+     * @return the display names
+     */
+    @Nonnull @Unmodifiable public Map<Locale, String> getDisplayNames() {
+        return displayNames; 
+    }
+    
+    /** 
+     * Get the keywords as a map from locale to actual value.
+     *
+     * @return the display names
+     */
+    @Nonnull @Unmodifiable public Map<Locale, List<String>> getKeywords() {
+        return keywordList;
+    }
+    
+    /**
+     * Return the descriptions as a map from locale to actual value.
+     *
+     * @return the descriptions names (if any)
+     */
+    @Nonnull @Unmodifiable public Map<Locale, String> getDescriptions() {
+        return descriptions;
+    }
+    
+    /**
+     * Get the logos as a map from locale to the OpenSAML {@link Logo}.
+     *
+     * @return the logos (if any)
+     */
+    @Nonnull @Unmodifiable public Map<Locale, List<Logo>> getLocaleLogos() {
+        return localeLogos;
+    }
+    
+    /**
+     * Get the logos with no associated locale to the OpenSAML {@link Logo}.
+     *
+     * @return the logos (if any)
+     */
+    @Nonnull @Unmodifiable public List<Logo> getNonLocaleLogos() {
+        return nonLocaleLogos;
+    }
+
+    
+    /** 
+     * Get the URLs as a map from locale to actual value.
+     * 
+     * @return the URLs (if any)
+     */
+    @Nonnull @Unmodifiable public Map<Locale, String> getInformationURLs() {
+        return informationURLs;
+    }
+    
+    /**
+     * Get the Privacy Statement URLsas a map from locale to actual value.
+     * 
+     * @return the URLs (if any)
+     */
+    @Nonnull @Unmodifiable public Map<Locale, String> getPrivacyStatementURLs() {
+        return privacyStatementURLs;
+    }
+    
+}
diff --git a/idp-saml-api/src/test/java/net/shibboleth/idp/saml/metadata/IdPUIInfoTest.java b/idp-saml-api/src/test/java/net/shibboleth/idp/saml/metadata/IdPUIInfoTest.java
new file mode 100644
index 0000000..760fa5d
--- /dev/null
+++ b/idp-saml-api/src/test/java/net/shibboleth/idp/saml/metadata/IdPUIInfoTest.java
@@ -0,0 +1,50 @@
+/*
+ * 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.idp.saml.metadata;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+
+import java.util.Locale;
+
+import org.opensaml.core.xml.XMLObjectBaseTestCase;
+import org.opensaml.saml.ext.saml2mdui.UIInfo;
+import org.testng.annotations.Test;
+
+/**
+ * test for the {@link IdPUIInfo}.
+ */
+public class IdPUIInfoTest extends XMLObjectBaseTestCase {
+    
+    @Test public void test() {
+    
+        final UIInfo samluiinfo = unmarshallElement("/UIInfo.xml");
+        final IdPUIInfo uiInfo = new IdPUIInfo(samluiinfo);
+        
+        assertEquals(uiInfo.getDisplayNames().size(), 2);
+        assertNotNull(uiInfo.getDisplayNames().get(Locale.forLanguageTag("en-us")));
+        assertEquals(uiInfo.getDescriptions().size(), 1);
+        assertEquals(uiInfo.getKeywords().size(), 1);
+        assertEquals(uiInfo.getLocaleLogos().size(), 1);
+        assertEquals(uiInfo.getLocaleLogos().get(Locale.forLanguageTag("en")).size(), 2);
+        assertEquals(uiInfo.getNonLocaleLogos().size(), 1);
+        assertEquals(uiInfo.getInformationURLs().size(), 1);
+        assertEquals(uiInfo.getPrivacyStatementURLs().size(), 1);
+        
+    }
+}
diff --git a/idp-saml-api/src/test/resources/UIInfo.xml b/idp-saml-api/src/test/resources/UIInfo.xml
new file mode 100644
index 0000000..31cc2d5
--- /dev/null
+++ b/idp-saml-api/src/test/resources/UIInfo.xml
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<UIInfo xmlns="urn:oasis:names:tc:SAML:metadata:ui" 
+	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xsi:schemaLocation="urn:oasis:names:tc:SAML:metadata:ui http://docs.oasis-open.org/security/saml/Post2.0/sstc-saml-metadata-ui/v1.0/cs01/xsd/sstc-saml-metadata-ui-v1.0.xsd">
+  <DisplayName xml:lang="en-us">Dn</DisplayName>
+  <DisplayName xml:lang="en-gb">Dn</DisplayName>
+  <Description xml:lang="en">Desc</Description>
+  <Description >Desc</Description>
+  <Keywords xml:lang="en">a s r</Keywords>
+  <Keywords >a s r</Keywords>
+  <InformationURL xml:lang="fr">infoURL</InformationURL>
+  <InformationURL >infoURL</InformationURL>
+  <Logo height="100" width="110">Logo2</Logo>
+  <Logo height="10" width="11" xml:lang="en">Logo1</Logo>
+  <Logo height="10" width="12" xml:lang="en">Logo3</Logo>
+  <PrivacyStatementURL xml:lang="fr">privateURL</PrivacyStatementURL>
+</UIInfo>

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


More information about the commits mailing list