[java-identity-provider] 02/02: IDP-1148 add resourceRef to ResourceBackedMetadataProvider
Rod Widdowson
rdw at steadingsoftware.com
Tue Mar 21 06:30:44 EDT 2017
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=561dafa42d45971e8c9e439381cc97563f3e9bd0
commit 561dafa42d45971e8c9e439381cc97563f3e9bd0
Author: Rod Widdowson <rdw at steadingsoftware.com>
AuthorDate: Tue Mar 21 10:29:08 2017 +0000
IDP-1148 add resourceRef to ResourceBackedMetadataProvider
https://issues.shibboleth.net/jira/browse/IDP-1148
---
.../impl/ResourceBackedMetadataProviderParser.java | 41 ++++++++++++++++++++++
.../metadata/ResourceMetadataParserTest.java | 23 ++++++++----
.../profile/spring/relyingparty/metadata/beans.xml | 2 ++
.../spring/relyingparty/metadata/resourceRef.xml | 7 ++++
.../main/resources/schema/shibboleth-metadata.xsd | 7 +++-
5 files changed, 73 insertions(+), 7 deletions(-)
diff --git a/idp-profile-spring/src/main/java/net/shibboleth/idp/profile/spring/relyingparty/metadata/impl/ResourceBackedMetadataProviderParser.java b/idp-profile-spring/src/main/java/net/shibboleth/idp/profile/spring/relyingparty/metadata/impl/ResourceBackedMetadataProviderParser.java
index a25ff1d..924e7b1 100644
--- a/idp-profile-spring/src/main/java/net/shibboleth/idp/profile/spring/relyingparty/metadata/impl/ResourceBackedMetadataProviderParser.java
+++ b/idp-profile-spring/src/main/java/net/shibboleth/idp/profile/spring/relyingparty/metadata/impl/ResourceBackedMetadataProviderParser.java
@@ -19,6 +19,8 @@ package net.shibboleth.idp.profile.spring.relyingparty.metadata.impl;
import java.util.List;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
import javax.xml.namespace.QName;
import net.shibboleth.ext.spring.resource.ResourceHelper;
@@ -28,6 +30,7 @@ import net.shibboleth.idp.profile.spring.resource.impl.ClasspathResourceParser;
import net.shibboleth.idp.profile.spring.resource.impl.ResourceNamespaceHandler;
import net.shibboleth.idp.profile.spring.resource.impl.SVNResourceParser;
import net.shibboleth.utilities.java.support.primitive.StringSupport;
+import net.shibboleth.utilities.java.support.xml.AttributeSupport;
import net.shibboleth.utilities.java.support.xml.DOMTypeSupport;
import net.shibboleth.utilities.java.support.xml.ElementSupport;
@@ -60,6 +63,10 @@ public class ResourceBackedMetadataProviderParser extends AbstractReloadingMetad
/** Element name for the resource elements. */
public static final QName RESOURCES_NAME = new QName(AbstractMetadataProviderParser.METADATA_NAMESPACE,
"MetadataResource");
+
+ /** For direct injection of a Spring bean **/
+ public static final QName RESOURCE_REF = new QName("resourceRef");
+
/** Log. */
private final Logger log = LoggerFactory.getLogger(ResourceBackedMetadataProviderParser.class);
@@ -69,6 +76,10 @@ public class ResourceBackedMetadataProviderParser extends AbstractReloadingMetad
final List<Element> resources = ElementSupport.getChildElements(element, RESOURCES_NAME);
if (null == resources || resources.isEmpty()) {
+ if (AttributeSupport.hasAttribute(element, RESOURCE_REF)) {
+ return ResourceBackedMetadataResolver.class;
+ }
+
throw new BeanCreationException("No <Resource> specified for ResourceBackedMetadataProvider");
}
final QName qName = DOMTypeSupport.getXSIType(resources.get(0));
@@ -109,6 +120,11 @@ public class ResourceBackedMetadataProviderParser extends AbstractReloadingMetad
}
final List<Element> resources = ElementSupport.getChildElements(element, RESOURCES_NAME);
+ if (resources.isEmpty()) {
+ parseResource(StringSupport.trimOrNull(AttributeSupport.getAttributeValue(element, RESOURCE_REF)), parserContext, builder);
+ return;
+ }
+
if (resources.size() != 1) {
log.error("{}: Only one Resource may be supplied to a ResourceBackedMetadataProvider", parserContext
.getReaderContext().getResource().getDescription());
@@ -152,7 +168,32 @@ public class ResourceBackedMetadataProviderParser extends AbstractReloadingMetad
parseFilesystemResource(resources.get(0), parserContext, builder);
}
}
+
+ /**
+ * Parse the provided Attribute and populate an appropriate {@link ResourceBackedMetadataResolver}.
+ *
+ * @param beanReference the reference
+ * @param parserContext the parser context
+ * @param builder the builder for the {@link ResourceBackedMetadataResolver}.
+ */
+ private void parseResource(@Nullable final String beanReference, final ParserContext parserContext, @Nonnull final BeanDefinitionBuilder builder) {
+
+ if (null == beanReference) {
+ log.error("{} must not be empty", RESOURCE_REF.getLocalPart());
+ throw new BeanDefinitionParsingException(new Problem(
+ "Empty bean reference for a ResourceBackedMetadataProvider", new Location(parserContext
+ .getReaderContext().getResource())));
+ }
+
+ final BeanDefinitionBuilder resourceConverter =
+ BeanDefinitionBuilder.genericBeanDefinition(ResourceHelper.class);
+ resourceConverter.setLazyInit(true);
+ resourceConverter.setFactoryMethod("of");
+ resourceConverter.addConstructorArgReference(beanReference);
+ builder.addConstructorArgValue(resourceConverter.getBeanDefinition());
+ }
+
/**
* Parse the provided <Resource> and populate an appropriate {@link ResourceBackedMetadataResolver}.
*
diff --git a/idp-profile-spring/src/test/java/net/shibboleth/idp/profile/spring/relyingparty/metadata/ResourceMetadataParserTest.java b/idp-profile-spring/src/test/java/net/shibboleth/idp/profile/spring/relyingparty/metadata/ResourceMetadataParserTest.java
index 3341c12..f50692e 100644
--- a/idp-profile-spring/src/test/java/net/shibboleth/idp/profile/spring/relyingparty/metadata/ResourceMetadataParserTest.java
+++ b/idp-profile-spring/src/test/java/net/shibboleth/idp/profile/spring/relyingparty/metadata/ResourceMetadataParserTest.java
@@ -19,6 +19,8 @@ package net.shibboleth.idp.profile.spring.relyingparty.metadata;
import java.util.Iterator;
+import net.shibboleth.utilities.java.support.repository.RepositorySupport;
+
import org.opensaml.saml.metadata.resolver.MetadataResolver;
import org.opensaml.saml.metadata.resolver.impl.FileBackedHTTPMetadataResolver;
import org.opensaml.saml.metadata.resolver.impl.FilesystemMetadataResolver;
@@ -31,8 +33,6 @@ import org.springframework.mock.env.MockPropertySource;
import org.testng.Assert;
import org.testng.annotations.Test;
-import net.shibboleth.utilities.java.support.repository.RepositorySupport;
-
public class ResourceMetadataParserTest extends AbstractMetadataParserTest {
private static final String PROP_MDURL = "metadataURL";
@@ -103,7 +103,7 @@ public class ResourceMetadataParserTest extends AbstractMetadataParserTest {
}
@Test public void httpEntity() throws Exception {
- MockPropertySource propSource = singletonPropertySource(PROP_MDURL,
+ final MockPropertySource propSource = singletonPropertySource(PROP_MDURL,
RepositorySupport.buildHTTPResourceURL(REPO_IDP, ENTITY_XML, false));
final MetadataResolver resolver = getBean(HTTPMetadataResolver.class, propSource, "resourceHTTPEntity.xml", "beans.xml");
@@ -115,7 +115,7 @@ public class ResourceMetadataParserTest extends AbstractMetadataParserTest {
}
@Test public void httpEntities() throws Exception {
- MockPropertySource propSource = singletonPropertySource(PROP_MDURL,
+ final MockPropertySource propSource = singletonPropertySource(PROP_MDURL,
RepositorySupport.buildHTTPResourceURL(REPO_IDP, ENTITIES_XML, false));
final MetadataResolver resolver = getBean(HTTPMetadataResolver.class, propSource, "resourceHTTPEntities.xml", "beans.xml");
@@ -127,7 +127,7 @@ public class ResourceMetadataParserTest extends AbstractMetadataParserTest {
}
@Test public void fileHttpEntity() throws Exception {
- MockPropertySource propSource = singletonPropertySource(PROP_MDURL,
+ final MockPropertySource propSource = singletonPropertySource(PROP_MDURL,
RepositorySupport.buildHTTPResourceURL(REPO_IDP, ENTITY_XML, false));
final MetadataResolver resolver = getBean(FileBackedHTTPMetadataResolver.class, propSource, "resourceFileBackedHTTPEntity.xml", "beans.xml");
@@ -139,7 +139,7 @@ public class ResourceMetadataParserTest extends AbstractMetadataParserTest {
}
@Test public void fileHttpEntities() throws Exception {
- MockPropertySource propSource = singletonPropertySource(PROP_MDURL,
+ final MockPropertySource propSource = singletonPropertySource(PROP_MDURL,
RepositorySupport.buildHTTPResourceURL(REPO_IDP, ENTITIES_XML, false));
final MetadataResolver resolver = getBean(FileBackedHTTPMetadataResolver.class, propSource, "resourceFileBackedHTTPEntities.xml", "beans.xml");
@@ -161,6 +161,17 @@ public class ResourceMetadataParserTest extends AbstractMetadataParserTest {
Assert.assertNotNull(resolver.resolveSingle(criteriaFor(IDP_ID)));
Assert.assertNull(resolver.resolveSingle(criteriaFor(SP_ID)));
}
+
+ @Test public void ref() throws Exception {
+
+ final MetadataResolver resolver = getBean(ResourceBackedMetadataResolver.class, "resourceRef.xml", "beans.xml");
+
+ Assert.assertEquals(resolver.getId(), "resourceRefEntity");
+
+ Assert.assertNotNull(resolver.resolveSingle(criteriaFor(IDP_ID)));
+ Assert.assertNull(resolver.resolveSingle(criteriaFor(SP_ID)));
+ }
+
@Test(expectedExceptions={BeanCreationException.class,}, enabled=false) public void svnParams() throws Exception {
getBean(MetadataResolver.class, "svnParams.xml", "beans.xml");
diff --git a/idp-profile-spring/src/test/resources/net/shibboleth/idp/profile/spring/relyingparty/metadata/beans.xml b/idp-profile-spring/src/test/resources/net/shibboleth/idp/profile/spring/relyingparty/metadata/beans.xml
index ef0390c..a1d5ecf 100644
--- a/idp-profile-spring/src/test/resources/net/shibboleth/idp/profile/spring/relyingparty/metadata/beans.xml
+++ b/idp-profile-spring/src/test/resources/net/shibboleth/idp/profile/spring/relyingparty/metadata/beans.xml
@@ -86,5 +86,7 @@
<bean id="metadata.LocalDynamicSourceKeyGenerator" class="org.opensaml.saml.metadata.resolver.impl.IdentityEntityIDGenerator" />
<bean id="metadata.CriterionPredicateRegistry" class="org.opensaml.saml.metadata.criteria.entity.impl.EntityDescriptorCriterionPredicateRegistry" />
+
+ <bean id="metadata.classpathRef" class="org.springframework.core.io.ClassPathResource" c:_0="/net/shibboleth/idp/profile/spring/relyingparty/metadata/entity.xml"/>
</beans>
\ No newline at end of file
diff --git a/idp-profile-spring/src/test/resources/net/shibboleth/idp/profile/spring/relyingparty/metadata/resourceRef.xml b/idp-profile-spring/src/test/resources/net/shibboleth/idp/profile/spring/relyingparty/metadata/resourceRef.xml
new file mode 100644
index 0000000..e9bc88a
--- /dev/null
+++ b/idp-profile-spring/src/test/resources/net/shibboleth/idp/profile/spring/relyingparty/metadata/resourceRef.xml
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<MetadataProvider
+ xmlns="urn:mace:shibboleth:2.0:metadata" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="urn:mace:shibboleth:2.0:metadata http://shibboleth.net/schema/idp/shibboleth-metadata.xsd"
+
+ id="resourceRefEntity" xsi:type="ResourceBackedMetadataProvider" resourceRef="metadata.classpathRef"/>
+
\ No newline at end of file
diff --git a/idp-schema/src/main/resources/schema/shibboleth-metadata.xsd b/idp-schema/src/main/resources/schema/shibboleth-metadata.xsd
index 40e05e8..eba3a38 100644
--- a/idp-schema/src/main/resources/schema/shibboleth-metadata.xsd
+++ b/idp-schema/src/main/resources/schema/shibboleth-metadata.xsd
@@ -53,7 +53,7 @@
<complexContent>
<extension base="shibmd:ReloadingMetadataProviderType">
<sequence>
- <element name="MetadataResource" type="res:ResourceType">
+ <element name="MetadataResource" type="res:ResourceType" minOccurs="0">
<annotation>
<documentation>The resource from which metadata will be read.</documentation>
</annotation>
@@ -64,6 +64,11 @@
<documentation>This property is deprecated, use maxRefreshDelay instead.</documentation>
</annotation>
</attribute>
+ <attribute name="resourceRef" type="string">
+ <annotation>
+ <documentation>the name of a bean with the (Spring) resource to use</documentation>
+ </annotation>
+ </attribute>
</extension>
</complexContent>
</complexType>
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the commits
mailing list