[java-opensaml] branch main updated: Add a depth-first child context search function.

Scott Cantor cantor.2 at osu.edu
Wed Sep 4 20:02:26 UTC 2024


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

scantor pushed a commit to branch main
in repository java-opensaml.

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

The following commit(s) were added to refs/heads/main by this push:
     new da128a383 Add a depth-first child context search function.
da128a383 is described below

commit da128a383310be2dc1f1359ceabd1fbebb79642f
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Wed Sep 4 16:02:23 2024 -0400

    Add a depth-first child context search function.
---
 .../opensaml/messaging/context/BaseContext.java    | 10 ++--
 .../navigate/RecursiveTypedChildContextLookup.java | 69 ++++++++++++++++++++++
 2 files changed, 74 insertions(+), 5 deletions(-)

diff --git a/opensaml-messaging-api/src/main/java/org/opensaml/messaging/context/BaseContext.java b/opensaml-messaging-api/src/main/java/org/opensaml/messaging/context/BaseContext.java
index 36f76cc8a..feabe0952 100644
--- a/opensaml-messaging-api/src/main/java/org/opensaml/messaging/context/BaseContext.java
+++ b/opensaml-messaging-api/src/main/java/org/opensaml/messaging/context/BaseContext.java
@@ -303,11 +303,11 @@ public abstract class BaseContext implements Iterable<BaseContext> {
     /** Remove from our parent (if there is one).
      */
     public void removeFromParent() {
-        final BaseContext parent = getParent();
-        if (parent == null) {
+        final BaseContext localParent = getParent();
+        if (localParent == null) {
             return;
         }
-        parent.removeSubcontext(this);
+        localParent.removeSubcontext(this);
     }
 
     /**
@@ -316,7 +316,7 @@ public abstract class BaseContext implements Iterable<BaseContext> {
      * @param <T> the type of subcontext being operated on
      * @param clazz the subcontext class to remove
      */
-    public <T extends BaseContext>void removeSubcontext(@Nonnull final Class<T> clazz) {
+    public <T extends BaseContext> void removeSubcontext(@Nonnull final Class<T> clazz) {
         final BaseContext subcontext = getSubcontext(clazz);
         if (subcontext != null) {
             removeSubcontext(subcontext);
@@ -370,7 +370,7 @@ public abstract class BaseContext implements Iterable<BaseContext> {
             throw new MessageRuntimeException("Error creating subcontext", e);
         }
     }
-    
+        
     /**
      * Iterator decorator which disallows the remove() operation on the iterator.
      */
diff --git a/opensaml-messaging-api/src/main/java/org/opensaml/messaging/context/navigate/RecursiveTypedChildContextLookup.java b/opensaml-messaging-api/src/main/java/org/opensaml/messaging/context/navigate/RecursiveTypedChildContextLookup.java
new file mode 100644
index 000000000..893130bb9
--- /dev/null
+++ b/opensaml-messaging-api/src/main/java/org/opensaml/messaging/context/navigate/RecursiveTypedChildContextLookup.java
@@ -0,0 +1,69 @@
+/*
+ * Licensed 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.opensaml.messaging.context.navigate;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+import org.opensaml.messaging.context.BaseContext;
+
+import net.shibboleth.shared.annotation.ParameterName;
+import net.shibboleth.shared.logic.Constraint;
+
+/**
+ * A {@link ContextDataLookupFunction} that peforms a depth-first search of context's children 
+ * and returns the first nested child context that is an instance of the specified type.
+ * 
+ * @param <ChildContext> type of the child context
+ * 
+ * @since 5.2.0
+ */
+public class RecursiveTypedChildContextLookup<ChildContext extends BaseContext>
+        implements ContextDataLookupFunction<BaseContext, ChildContext> {
+    
+    /** The target parent class. */
+    @Nonnull private Class<ChildContext> childClass;
+    
+    /**
+     * Constructor.
+     *
+     * @param targetClass the target parent class 
+     */
+    public RecursiveTypedChildContextLookup(
+            @Nonnull @ParameterName(name="targetClass") final Class<ChildContext> targetClass) {
+        childClass = Constraint.isNotNull(targetClass, "Child Class cannot be null");
+    }
+
+    /** {@inheritDoc} */
+    @Nullable public ChildContext apply(@Nullable final BaseContext input) {
+        if (input == null) {
+            return null;
+        }
+
+        for (final BaseContext subcontext : input) {
+            if (childClass.isInstance(subcontext)) {
+                return childClass.cast(subcontext);
+            }
+            
+            final ChildContext found = apply(subcontext);
+            if (found != null) {
+                return found;
+            }
+        }
+
+        return null;
+    }
+    
+}
\ No newline at end of file

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


More information about the commits mailing list