[java-shib-shared] branch main updated: JSSH-8 - Servlet filter that implements its own filter-mapping layer

Scott Cantor cantor.2 at osu.edu
Thu Sep 29 20:49:39 UTC 2022


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

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

View the commit online:
http://git.shibboleth.net/view/?p=java-shib-shared.git;a=commit;h=1cdee04c1252cb2d0f39405222f25814669e3286

The following commit(s) were added to refs/heads/main by this push:
     new 1cdee04c JSSH-8 - Servlet filter that implements its own filter-mapping layer
1cdee04c is described below

commit 1cdee04c1252cb2d0f39405222f25814669e3286
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Thu Sep 29 16:49:36 2022 -0400

    JSSH-8 - Servlet filter that implements its own filter-mapping layer
    
    https://shibboleth.atlassian.net/browse/JSSH-8
    
    Rework prefix mapping mechanism using auto-wiring.
---
 .../spring/servlet}/RequestURLPrefixPredicate.java | 34 +++++++--------
 .../shared/spring/servlet/URLPrefix.java           | 51 ++++++++++++++++++++++
 .../shared/servlet/impl/StubbedFilter.java         |  2 +-
 3 files changed, 67 insertions(+), 20 deletions(-)

diff --git a/shib-networking/src/main/java/net/shibboleth/shared/servlet/impl/RequestURLPrefixPredicate.java b/shib-networking-spring/src/main/java/net/shibboleth/shared/spring/servlet/RequestURLPrefixPredicate.java
similarity index 67%
rename from shib-networking/src/main/java/net/shibboleth/shared/servlet/impl/RequestURLPrefixPredicate.java
rename to shib-networking-spring/src/main/java/net/shibboleth/shared/spring/servlet/RequestURLPrefixPredicate.java
index 380b252a..81dc0752 100644
--- a/shib-networking/src/main/java/net/shibboleth/shared/servlet/impl/RequestURLPrefixPredicate.java
+++ b/shib-networking-spring/src/main/java/net/shibboleth/shared/spring/servlet/RequestURLPrefixPredicate.java
@@ -15,44 +15,41 @@
  * limitations under the License.
  */
 
-package net.shibboleth.shared.servlet.impl;
+package net.shibboleth.shared.spring.servlet;
 
 import java.util.Collection;
 import java.util.Collections;
+import java.util.List;
 import java.util.function.Predicate;
 
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 
+import org.springframework.beans.factory.annotation.Autowired;
+
 import jakarta.servlet.ServletRequest;
 import jakarta.servlet.http.HttpServletRequest;
+import net.shibboleth.shared.annotation.ParameterName;
 import net.shibboleth.shared.annotation.constraint.NonnullElements;
-import net.shibboleth.shared.component.AbstractInitializableComponent;
-import net.shibboleth.shared.primitive.StringSupport;
 
 /**
  * Predicate based on comparing a request URL to a set of matching prefixes. 
  */
-public class RequestURLPrefixPredicate extends AbstractInitializableComponent implements Predicate<ServletRequest> {
+public class RequestURLPrefixPredicate implements Predicate<ServletRequest> {
 
     /** Prefixes to check for. */
-    @Nonnull @NonnullElements public Collection<String> matchingPrefixes;
+    @Nonnull @NonnullElements public Collection<URLPrefix> matchingPrefixes;
 
-    /** Constructor. */
-    public RequestURLPrefixPredicate() {
-        matchingPrefixes = Collections.emptyList();
-    }
-    
     /**
-     * Set matching URL prefixes.
+     * Constructor.
      * 
-     * @param prefixes URL prefixes
+     * @param prefixes prefixes to match 
      */
-    public void setMatchingPrefixes(@Nullable @NonnullElements Collection<String> prefixes) {
-        checkSetterPreconditions();
-        
+    @Autowired
+    public RequestURLPrefixPredicate(
+            @Nullable @NonnullElements @ParameterName(name="prefixes") final Collection<URLPrefix> prefixes) {
         if (prefixes != null) {
-            matchingPrefixes = StringSupport.normalizeStringCollection(prefixes);
+            matchingPrefixes = List.copyOf(prefixes);
         } else {
             matchingPrefixes = Collections.emptyList();
         }
@@ -60,12 +57,11 @@ public class RequestURLPrefixPredicate extends AbstractInitializableComponent im
     
     /** {@inheritDoc} */
     public boolean test(@Nullable final ServletRequest input) {
-        checkComponentActive();
 
         if (input instanceof HttpServletRequest) {
             final String uri = ((HttpServletRequest) input).getRequestURI();
-            for (final String s : matchingPrefixes) {
-                if (uri.startsWith(input.getServletContext().getContextPath() +  s)) {
+            for (final URLPrefix p : matchingPrefixes) {
+                if (uri.startsWith(input.getServletContext().getContextPath() +  p.getValue())) {
                     return true;
                 }
             }
diff --git a/shib-networking-spring/src/main/java/net/shibboleth/shared/spring/servlet/URLPrefix.java b/shib-networking-spring/src/main/java/net/shibboleth/shared/spring/servlet/URLPrefix.java
new file mode 100644
index 00000000..d3b43e6c
--- /dev/null
+++ b/shib-networking-spring/src/main/java/net/shibboleth/shared/spring/servlet/URLPrefix.java
@@ -0,0 +1,51 @@
+/*
+ * 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.shared.spring.servlet;
+
+import javax.annotation.Nonnull;
+
+import net.shibboleth.shared.annotation.ParameterName;
+import net.shibboleth.shared.annotation.constraint.NotEmpty;
+import net.shibboleth.shared.logic.Constraint;
+import net.shibboleth.shared.primitive.StringSupport;
+
+/** Used to allow for auto-wiring of values into {@link RequestURLPrefixPredicate}. */
+public class URLPrefix {
+    
+    /** The wrapped string. */
+    @Nonnull @NotEmpty final String wrappedString;
+    
+    /**
+     * Constructor.
+     *
+     * @param s string to wrap
+     */
+    public URLPrefix(@Nonnull @NotEmpty @ParameterName(name="s") final String s) {
+        wrappedString = Constraint.isNotNull(StringSupport.trimOrNull(s), "Input string cannot be null or empty");
+    }
+    
+    /**
+     * Get the wrapped value.
+     * 
+     * @return wrapped value
+     */
+    @Nonnull @NotEmpty public String getValue() {
+        return wrappedString;
+    }
+
+}
\ No newline at end of file
diff --git a/shib-networking/src/main/java/net/shibboleth/shared/servlet/impl/StubbedFilter.java b/shib-networking/src/main/java/net/shibboleth/shared/servlet/impl/StubbedFilter.java
index 7c60e97e..b439d5dd 100644
--- a/shib-networking/src/main/java/net/shibboleth/shared/servlet/impl/StubbedFilter.java
+++ b/shib-networking/src/main/java/net/shibboleth/shared/servlet/impl/StubbedFilter.java
@@ -53,7 +53,7 @@ public class StubbedFilter implements Filter {
     public StubbedFilter(@Nullable @NotEmpty @ParameterName(name="name") final String name) {
         className = StringSupport.trimOrNull(name);
         if (className == null) {
-            className = "Servlet Filter '" + getClass().getName() + "'";
+            className = "Servlet Filter " + getClass().getName();
         }
     }
     

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


More information about the commits mailing list