Clean up CustomFilterAdapter 71/101671/2
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 1 Jul 2022 09:31:56 +0000 (11:31 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 1 Jul 2022 09:36:57 +0000 (11:36 +0200)
Make sure internal variable is an ImmutableList and compact the code a
bit.

Change-Id: I13d65b2ce8d79724533b77a64aee8d2e28b1807b
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
aaa-filterchain/src/main/java/org/opendaylight/aaa/filterchain/filters/CustomFilterAdapter.java

index 193ae4b98c08ae83210d08a8438e019f5ac6608d..d028e360feee95fdc54b5d9f6ff070c13a271fac 100644 (file)
@@ -5,12 +5,10 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.aaa.filterchain.filters;
 
 import com.google.common.collect.ImmutableList;
 import java.io.IOException;
-import java.util.Collections;
 import java.util.List;
 import java.util.stream.Collectors;
 import javax.servlet.Filter;
@@ -43,7 +41,6 @@ import org.slf4j.LoggerFactory;
  * middle of requests, causing inconsistent behavior.
  */
 public class CustomFilterAdapter implements Filter, CustomFilterAdapterListener {
-
     private static final Logger LOG = LoggerFactory.getLogger(CustomFilterAdapter.class);
 
     private final CustomFilterAdapterConfiguration customFilterAdapterConfig;
@@ -51,11 +48,11 @@ public class CustomFilterAdapter implements Filter, CustomFilterAdapterListener
     private FilterConfig filterConfig;
 
     /**
-     * Stores the injected filter chain. TODO can this be an ArrayList?
+     * Stores the injected filter chain.
      */
-    private volatile List<Filter> injectedFilterChain = Collections.emptyList();
+    private volatile ImmutableList<Filter> injectedFilterChain = ImmutableList.of();
 
-    public CustomFilterAdapter(CustomFilterAdapterConfiguration customFilterAdapterConfig) {
+    public CustomFilterAdapter(final CustomFilterAdapterConfiguration customFilterAdapterConfig) {
         this.customFilterAdapterConfig = customFilterAdapterConfig;
     }
 
@@ -71,11 +68,11 @@ public class CustomFilterAdapter implements Filter, CustomFilterAdapterListener
         // chain is the existing chain of responsibility, and filterChain
         // contains the new links to inject into the existing chain. Since
         // Jersey spawns <code>chain</code> for each request, a new chain
-        List<Filter> localFilterChain = injectedFilterChain;
-        if (!localFilterChain.isEmpty()) {
-            AAAFilterChain.createAAAFilterChain().doFilter(request, response, chain, localFilterChain);
-        } else {
+        final var localFilterChain = injectedFilterChain;
+        if (localFilterChain.isEmpty()) {
             chain.doFilter(request, response);
+        } else {
+            AAAFilterChain.createAAAFilterChain().doFilter(request, response, chain, localFilterChain);
         }
     }
 
@@ -83,32 +80,23 @@ public class CustomFilterAdapter implements Filter, CustomFilterAdapterListener
     public void init(final FilterConfig newFilterConfig) throws ServletException {
         LOG.info("Initializing CustomFilterAdapter");
 
-        this.filterConfig = newFilterConfig;
+        filterConfig = newFilterConfig;
 
         // register as a listener for config admin changes
-        customFilterAdapterConfig.registerCustomFilterAdapterConfigurationListener(CustomFilterAdapter.this);
-    }
-
-    /**
-     * Updates the injected filter chain.
-     *
-     * @param filterChain
-     *            The injected chain
-     */
-    private void setInjectedFilterChain(final List<Filter> filterChain) {
-        this.injectedFilterChain = ImmutableList.copyOf(filterChain);
-        final String commaSeperatedFilterChain = this.injectedFilterChain.stream()
-                .map(i -> i.getClass().getSimpleName()).collect(Collectors.joining(","));
-        LOG.info("Injecting a new filter chain with {} Filters: {}", filterChain.size(), commaSeperatedFilterChain);
+        customFilterAdapterConfig.registerCustomFilterAdapterConfigurationListener(this);
     }
 
     @Override
     public void updateInjectedFilters(final List<Filter> injectedFilters) {
-        this.setInjectedFilterChain(injectedFilters);
+        injectedFilterChain = ImmutableList.copyOf(injectedFilters);
+        if (LOG.isInfoEnabled()) {
+            LOG.info("Injecting a new filter chain with {} Filters: {}", injectedFilters.size(),
+                injectedFilterChain.stream().map(i -> i.getClass().getSimpleName()).collect(Collectors.joining(",")));
+        }
     }
 
     @Override
     public FilterConfig getFilterConfig() {
-        return this.filterConfig;
+        return filterConfig;
     }
 }