[java-shib-shared] branch main updated: Null checking

Rod Widdowson rdw at steadingsoftware.com
Mon Jan 30 09:57:39 UTC 2023


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

rdw 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=e2018f4480b3be010305d30f8297fb16572bcce8

The following commit(s) were added to refs/heads/main by this push:
     new e2018f44 Null checking
e2018f44 is described below

commit e2018f4480b3be010305d30f8297fb16572bcce8
Author: Rod Widdowson <rdw at steadingsoftware.com>
AuthorDate: Sun Jan 29 15:23:08 2023 +0000

    Null checking
    
    Use non-null methods where they are available.
    Pick off some more low hanging fruit.
---
 .../net/shibboleth/shared/cli/AbstractCommandLine.java    |  5 +++--
 .../spring/servlet/impl/DynamicResponseHeaderFilter.java  | 15 +++++++--------
 .../spring/servlet/impl/SameSiteCookieHeaderFilter.java   |  8 ++++----
 .../shared/security/impl/BasicAccessControlService.java   |  6 +++---
 .../shared/security/impl/IPRangeAccessControl.java        |  9 ++++-----
 .../shared/spring/util/ApplicationContextBuilder.java     |  4 ++--
 .../net/shibboleth/shared/primitive/StringSupport.java    |  6 ++++--
 7 files changed, 27 insertions(+), 26 deletions(-)

diff --git a/shib-cli/src/main/java/net/shibboleth/shared/cli/AbstractCommandLine.java b/shib-cli/src/main/java/net/shibboleth/shared/cli/AbstractCommandLine.java
index 0103904b..b84e9664 100644
--- a/shib-cli/src/main/java/net/shibboleth/shared/cli/AbstractCommandLine.java
+++ b/shib-cli/src/main/java/net/shibboleth/shared/cli/AbstractCommandLine.java
@@ -266,8 +266,9 @@ public abstract class AbstractCommandLine<T extends CommandLineArguments> {
 
             getLogger().debug("Initializing Spring context with {}", configs);
 
-            final List<Resource> resources =
-                    args.getPropertyFiles().stream().map(loader::getResource).collect(Collectors.toUnmodifiableList());
+            @Nonnull final List<Resource> resources =
+                    args.getPropertyFiles().stream().map(loader::getResource).collect(
+                            CollectionSupport.nonnullCollector(Collectors.toUnmodifiableList())).get();
             final List<PropertySource<?>> propertySources = new ArrayList<>(resources.size());
             resources.forEach(r -> {
                 try {
diff --git a/shib-networking-spring/src/main/java/net/shibboleth/shared/spring/servlet/impl/DynamicResponseHeaderFilter.java b/shib-networking-spring/src/main/java/net/shibboleth/shared/spring/servlet/impl/DynamicResponseHeaderFilter.java
index 9c15e0da..f233d9e9 100644
--- a/shib-networking-spring/src/main/java/net/shibboleth/shared/spring/servlet/impl/DynamicResponseHeaderFilter.java
+++ b/shib-networking-spring/src/main/java/net/shibboleth/shared/spring/servlet/impl/DynamicResponseHeaderFilter.java
@@ -20,9 +20,7 @@ package net.shibboleth.shared.spring.servlet.impl;
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.HashMap;
-import java.util.List;
 import java.util.Map;
 import java.util.function.Function;
 
@@ -40,6 +38,7 @@ import jakarta.servlet.http.HttpServletRequest;
 import jakarta.servlet.http.HttpServletResponse;
 import jakarta.servlet.http.HttpServletResponseWrapper;
 import net.shibboleth.shared.annotation.constraint.NonnullElements;
+import net.shibboleth.shared.collection.CollectionSupport;
 import net.shibboleth.shared.collection.Pair;
 import net.shibboleth.shared.primitive.StringSupport;
 import net.shibboleth.shared.servlet.AbstractConditionalFilter;
@@ -61,8 +60,8 @@ public class DynamicResponseHeaderFilter extends AbstractConditionalFilter imple
     
     /** Constructor. */
     public DynamicResponseHeaderFilter() {
-        headers = Collections.emptyMap();
-        callbacks = Collections.emptyList();
+        headers = CollectionSupport.emptyMap();
+        callbacks = CollectionSupport.emptyList();
     }
     
     /**
@@ -81,7 +80,7 @@ public class DynamicResponseHeaderFilter extends AbstractConditionalFilter imple
                 }
             }
         } else {
-            headers = Collections.emptyMap();
+            headers = CollectionSupport.emptyMap();
         }
     }
     
@@ -93,9 +92,9 @@ public class DynamicResponseHeaderFilter extends AbstractConditionalFilter imple
     public void setCallbacks(@Nullable @NonnullElements
             final Collection<Function<Pair<HttpServletRequest,HttpServletResponse>,Boolean>> theCallbacks) {
         if (theCallbacks != null) {
-            callbacks = List.copyOf(theCallbacks);
+            callbacks = CollectionSupport.copyToList(theCallbacks);
         } else {
-            callbacks = Collections.emptyList();
+            callbacks = CollectionSupport.emptyList();
         }
     }
     
@@ -114,7 +113,7 @@ public class DynamicResponseHeaderFilter extends AbstractConditionalFilter imple
 
     /** {@inheritDoc} */
     @Override
-    protected void runFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain)
+    protected void runFilter(final @Nonnull ServletRequest request, final @Nonnull ServletResponse response, final @Nonnull FilterChain chain)
             throws IOException,
             ServletException {
         
diff --git a/shib-networking-spring/src/main/java/net/shibboleth/shared/spring/servlet/impl/SameSiteCookieHeaderFilter.java b/shib-networking-spring/src/main/java/net/shibboleth/shared/spring/servlet/impl/SameSiteCookieHeaderFilter.java
index 129ea9c6..06dae16f 100644
--- a/shib-networking-spring/src/main/java/net/shibboleth/shared/spring/servlet/impl/SameSiteCookieHeaderFilter.java
+++ b/shib-networking-spring/src/main/java/net/shibboleth/shared/spring/servlet/impl/SameSiteCookieHeaderFilter.java
@@ -21,7 +21,6 @@ import java.io.IOException;
 import java.io.PrintWriter;
 import java.net.HttpCookie;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -44,6 +43,7 @@ import jakarta.servlet.http.HttpServletResponse;
 import jakarta.servlet.http.HttpServletResponseWrapper;
 import net.shibboleth.shared.annotation.constraint.NonnullElements;
 import net.shibboleth.shared.annotation.constraint.NotEmpty;
+import net.shibboleth.shared.collection.CollectionSupport;
 import net.shibboleth.shared.logic.Constraint;
 import net.shibboleth.shared.primitive.LoggerFactory;
 import net.shibboleth.shared.primitive.StringSupport;
@@ -127,7 +127,7 @@ public class SameSiteCookieHeaderFilter extends AbstractConditionalFilter implem
     
     /** Constructor. */
     public SameSiteCookieHeaderFilter() {
-        sameSiteCookies = Collections.emptyMap();
+        sameSiteCookies = CollectionSupport.emptyMap();
     }
     
     /**
@@ -167,7 +167,7 @@ public class SameSiteCookieHeaderFilter extends AbstractConditionalFilter implem
                 }                
             }
         } else {
-            sameSiteCookies = Collections.emptyMap();
+            sameSiteCookies = CollectionSupport.emptyMap();
         }
         
     }
@@ -187,7 +187,7 @@ public class SameSiteCookieHeaderFilter extends AbstractConditionalFilter implem
 
     /** {@inheritDoc} */
     @Override
-    protected void runFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain)
+    protected void runFilter(final @Nonnull ServletRequest request, final @Nonnull ServletResponse response, final @Nonnull FilterChain chain)
             throws IOException, ServletException {
 
         if (!(response instanceof HttpServletResponse)) {
diff --git a/shib-security/src/main/java/net/shibboleth/shared/security/impl/BasicAccessControlService.java b/shib-security/src/main/java/net/shibboleth/shared/security/impl/BasicAccessControlService.java
index 031a2256..4560e065 100644
--- a/shib-security/src/main/java/net/shibboleth/shared/security/impl/BasicAccessControlService.java
+++ b/shib-security/src/main/java/net/shibboleth/shared/security/impl/BasicAccessControlService.java
@@ -17,7 +17,6 @@
 
 package net.shibboleth.shared.security.impl;
 
-import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -28,6 +27,7 @@ import org.slf4j.Logger;
 
 import jakarta.servlet.ServletRequest;
 import net.shibboleth.shared.annotation.constraint.NonnullElements;
+import net.shibboleth.shared.collection.CollectionSupport;
 import net.shibboleth.shared.component.AbstractIdentifiableInitializableComponent;
 import net.shibboleth.shared.primitive.LoggerFactory;
 import net.shibboleth.shared.primitive.StringSupport;
@@ -46,7 +46,7 @@ public class BasicAccessControlService extends AbstractIdentifiableInitializable
     
     /** Constructor. */
     public BasicAccessControlService() {
-        policyMap = Collections.emptyMap();
+        policyMap = CollectionSupport.emptyMap();
     }
     
     /**
@@ -67,7 +67,7 @@ public class BasicAccessControlService extends AbstractIdentifiableInitializable
                 }
             }
         } else {
-            policyMap = Collections.emptyMap();
+            policyMap = CollectionSupport.emptyMap();
         }
     }
 
diff --git a/shib-security/src/main/java/net/shibboleth/shared/security/impl/IPRangeAccessControl.java b/shib-security/src/main/java/net/shibboleth/shared/security/impl/IPRangeAccessControl.java
index fc5ed8a9..0c657107 100644
--- a/shib-security/src/main/java/net/shibboleth/shared/security/impl/IPRangeAccessControl.java
+++ b/shib-security/src/main/java/net/shibboleth/shared/security/impl/IPRangeAccessControl.java
@@ -18,8 +18,6 @@
 package net.shibboleth.shared.security.impl;
 
 import java.util.Collection;
-import java.util.Collections;
-import java.util.List;
 
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
@@ -30,6 +28,7 @@ import com.google.common.net.InetAddresses;
 
 import jakarta.servlet.ServletRequest;
 import net.shibboleth.shared.annotation.constraint.NonnullElements;
+import net.shibboleth.shared.collection.CollectionSupport;
 import net.shibboleth.shared.component.AbstractIdentifiableInitializableComponent;
 import net.shibboleth.shared.logic.Constraint;
 import net.shibboleth.shared.net.IPRange;
@@ -53,7 +52,7 @@ public class IPRangeAccessControl extends AbstractIdentifiableInitializableCompo
      *
      */
     public IPRangeAccessControl() {
-        allowedRanges = Collections.emptyList();
+        allowedRanges = CollectionSupport.emptyList();
     }
     
     /**
@@ -65,7 +64,7 @@ public class IPRangeAccessControl extends AbstractIdentifiableInitializableCompo
         checkSetterPreconditions();
         Constraint.isNotNull(ranges, "IPRange collection cannot be null");
         
-        allowedRanges = List.copyOf(ranges);
+        allowedRanges = CollectionSupport.copyToList(ranges);
     }
 
     /** {@inheritDoc} */
@@ -80,7 +79,7 @@ public class IPRangeAccessControl extends AbstractIdentifiableInitializableCompo
             log.debug("{} Evaluating client address '{}'", getLogPrefix(), addr);
             
             try {
-                final byte[] resolvedAddress = InetAddresses.forString(addr).getAddress();
+                @Nonnull final byte[] resolvedAddress = InetAddresses.forString(addr).getAddress();
                 for (final IPRange range : allowedRanges) {
                     if (range.contains(resolvedAddress)) {
                         log.debug("{} Granted access to client address '{}' (Operation: {}, Resource: {})",
diff --git a/shib-spring/src/main/java/net/shibboleth/shared/spring/util/ApplicationContextBuilder.java b/shib-spring/src/main/java/net/shibboleth/shared/spring/util/ApplicationContextBuilder.java
index 1a9a9a61..223ab9dd 100644
--- a/shib-spring/src/main/java/net/shibboleth/shared/spring/util/ApplicationContextBuilder.java
+++ b/shib-spring/src/main/java/net/shibboleth/shared/spring/util/ApplicationContextBuilder.java
@@ -153,7 +153,7 @@ public class ApplicationContextBuilder {
      */
     @Nonnull public ApplicationContextBuilder setUnresolvedServiceConfigurations(
             @Nonnull @NonnullElements final Collection<String> configs) {
-        configurationSources = List.copyOf(Constraint.isNotNull(configs, "Service configurations cannot be null"));
+        configurationSources = CollectionSupport.copyToList(Constraint.isNotNull(configs, "Service configurations cannot be null"));
         
         return this;
     }
@@ -167,7 +167,7 @@ public class ApplicationContextBuilder {
      */
     @Nonnull public ApplicationContextBuilder setServiceConfigurations(
             @Nonnull @NonnullElements final Collection<Resource> configs) {
-        configurationResources = List.copyOf(Constraint.isNotNull(configs, "Service configurations cannot be null"));
+        configurationResources = CollectionSupport.copyToList(Constraint.isNotNull(configs, "Service configurations cannot be null"));
         
         return this;
     }
diff --git a/shib-support/src/main/java/net/shibboleth/shared/primitive/StringSupport.java b/shib-support/src/main/java/net/shibboleth/shared/primitive/StringSupport.java
index b3c5fb0f..1b4bc96b 100644
--- a/shib-support/src/main/java/net/shibboleth/shared/primitive/StringSupport.java
+++ b/shib-support/src/main/java/net/shibboleth/shared/primitive/StringSupport.java
@@ -25,7 +25,6 @@ import java.nio.charset.Charset;
 import java.nio.charset.CharsetDecoder;
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
 import java.util.StringTokenizer;
@@ -180,7 +179,10 @@ public final class StringSupport {
             return CollectionSupport.emptyList();
         }
         
-        return values.stream().map(StringSupport::trimOrNull).filter(e->e != null).collect(Collectors.toList());
+        return values.stream().
+                map(StringSupport::trimOrNull).
+                filter(e->e != null).
+                collect(CollectionSupport.nonnullCollector(Collectors.toList())).get();
     }
 
     /** Null/empty preserving conversion from xs:boolean to {@link Boolean}.

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


More information about the commits mailing list