This is an automated email from the ASF dual-hosted git repository.

twolf pushed a commit to branch dev_3.0
in repository https://gitbox.apache.org/repos/asf/mina-sshd.git

commit a376b67bce4baff2ad5bdbb831c02e28b016b9f6
Author: Thomas Wolf <tw...@apache.org>
AuthorDate: Sat Apr 5 09:21:15 2025 +0200

    Simplify FilterChain
    
    Move iterating to the next or previous FilterContext out of the
    FilterChain into the FilterContext itself. This simplifies the
    FilterChain interface.
---
 .../sshd/common/filter/DefaultFilterChain.java     | 40 +++-------------------
 .../org/apache/sshd/common/filter/FilterChain.java | 25 --------------
 .../apache/sshd/common/filter/FilterContext.java   | 39 ++++++++++++---------
 .../common/session/helpers/AbstractSession.java    | 11 +++---
 4 files changed, 30 insertions(+), 85 deletions(-)

diff --git 
a/sshd-core/src/main/java/org/apache/sshd/common/filter/DefaultFilterChain.java 
b/sshd-core/src/main/java/org/apache/sshd/common/filter/DefaultFilterChain.java
index 9075c13ac..294af54f9 100644
--- 
a/sshd-core/src/main/java/org/apache/sshd/common/filter/DefaultFilterChain.java
+++ 
b/sshd-core/src/main/java/org/apache/sshd/common/filter/DefaultFilterChain.java
@@ -18,13 +18,8 @@
  */
 package org.apache.sshd.common.filter;
 
-import java.io.IOException;
 import java.util.Objects;
 
-import org.apache.sshd.common.io.IoWriteFuture;
-import org.apache.sshd.common.util.Readable;
-import org.apache.sshd.common.util.buffer.Buffer;
-
 /**
  * A default implementation of a {@link FilterChain}.
  */
@@ -45,7 +40,7 @@ public class DefaultFilterChain implements FilterChain {
 
     @Override
     public synchronized FilterContext addFirst(Filter filter) {
-        FilterContext ctx = new FilterContext(this, filter);
+        FilterContext ctx = new FilterContext(filter);
         filter.adding(ctx);
         ctx.prev = null;
         ctx.next = head;
@@ -62,7 +57,7 @@ public class DefaultFilterChain implements FilterChain {
 
     @Override
     public synchronized FilterContext addLast(Filter filter) {
-        FilterContext ctx = new FilterContext(this, filter);
+        FilterContext ctx = new FilterContext(filter);
         filter.adding(ctx);
         ctx.next = null;
         ctx.prev = tail;
@@ -80,7 +75,7 @@ public class DefaultFilterChain implements FilterChain {
     @Override
     public synchronized FilterContext addBefore(Filter filter, FilterContext 
before) {
         Objects.requireNonNull(before);
-        FilterContext ctx = new FilterContext(this, filter);
+        FilterContext ctx = new FilterContext(filter);
         filter.adding(ctx);
         ctx.next = before;
         ctx.prev = before.prev;
@@ -97,7 +92,7 @@ public class DefaultFilterChain implements FilterChain {
     @Override
     public synchronized FilterContext addAfter(Filter filter, FilterContext 
after) {
         Objects.requireNonNull(after);
-        FilterContext ctx = new FilterContext(this, filter);
+        FilterContext ctx = new FilterContext(filter);
         filter.adding(ctx);
         ctx.prev = after;
         ctx.next = after.next;
@@ -121,31 +116,4 @@ public class DefaultFilterChain implements FilterChain {
         return tail == null ? null : tail.filter;
     }
 
-    @Override
-    public IoWriteFuture send(FilterContext current, int cmd, Buffer message) 
throws IOException {
-        FilterContext ctx = current.prev;
-        while (ctx != null) {
-            OutputHandler handler = ctx.filter.out();
-            if (handler != null) {
-                return handler.send(cmd, message);
-            }
-            ctx = ctx.prev;
-        }
-        throw new IllegalStateException("Fell off filter chain in send from " 
+ current.filter);
-    }
-
-    @Override
-    public void passOn(FilterContext current, Readable message) throws 
Exception {
-        FilterContext ctx = current.next;
-        while (ctx != null) {
-            InputHandler handler = ctx.filter.in();
-            if (handler != null) {
-                handler.received(message);
-                return;
-            }
-            ctx = ctx.next;
-        }
-        throw new IllegalStateException("Unhandled message: fell off filter 
chain in receive after " + current.filter);
-    }
-
 }
diff --git 
a/sshd-core/src/main/java/org/apache/sshd/common/filter/FilterChain.java 
b/sshd-core/src/main/java/org/apache/sshd/common/filter/FilterChain.java
index f32bd4e7e..204dd2634 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/filter/FilterChain.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/filter/FilterChain.java
@@ -18,12 +18,6 @@
  */
 package org.apache.sshd.common.filter;
 
-import java.io.IOException;
-
-import org.apache.sshd.common.io.IoWriteFuture;
-import org.apache.sshd.common.util.Readable;
-import org.apache.sshd.common.util.buffer.Buffer;
-
 /**
  * A general chain of {@link Filter}s.
  */
@@ -53,23 +47,4 @@ public interface FilterChain {
 
     Filter getLast();
 
-    /**
-     * Pass on an outgoing message to the next filter before {@code current} 
that has an {@link OutputHandler}.
-     *
-     * @param  current     {@link Filter} that is passing on the message
-     * @param  cmd         the SSH command code of the buffer being written; 
must also be included in the buffer
-     * @param  message     being passed on
-     * @return             an {@link IoWriteFuture} that is fulfilled when the 
message has been sent.
-     * @throws IOException if an error occurs
-     */
-    IoWriteFuture send(FilterContext current, int cmd, Buffer message) throws 
IOException;
-
-    /**
-     * Pass on an incoming message to the next filter after {@code current} 
that has an {@link InputHandler}.
-     *
-     * @param  current   {@link Filter} that is passing on the message
-     * @param  message   being passed on
-     * @throws Exception if an error occurs
-     */
-    void passOn(FilterContext current, Readable message) throws Exception;
 }
diff --git 
a/sshd-core/src/main/java/org/apache/sshd/common/filter/FilterContext.java 
b/sshd-core/src/main/java/org/apache/sshd/common/filter/FilterContext.java
index f99e88af7..a17529df5 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/filter/FilterContext.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/filter/FilterContext.java
@@ -27,28 +27,16 @@ import org.apache.sshd.common.util.buffer.Buffer;
 
 public final class FilterContext {
 
+    final Filter filter;
+
     volatile FilterContext prev;
 
     volatile FilterContext next;
 
-    final Filter filter;
-
-    private final FilterChain chain;
-
-    FilterContext(FilterChain chain, Filter filter) {
-        this.chain = Objects.requireNonNull(chain);
+    FilterContext(Filter filter) {
         this.filter = Objects.requireNonNull(filter);
     }
 
-    /**
-     * Retrieves the {@link FilterChain} containing this context.
-     *
-     * @return the {@link FilterChain}
-     */
-    public FilterChain chain() {
-        return chain;
-    }
-
     /**
      * Pass on an outgoing message to the next filter before this one that has 
an {@link OutputHandler}.
      *
@@ -58,7 +46,15 @@ public final class FilterContext {
      * @throws IOException if an error occurs
      */
     public IoWriteFuture send(int cmd, Buffer message) throws IOException {
-        return chain.send(this, cmd, message);
+        FilterContext ctx = prev;
+        while (ctx != null) {
+            OutputHandler handler = ctx.filter.out();
+            if (handler != null) {
+                return handler.send(cmd, message);
+            }
+            ctx = ctx.prev;
+        }
+        throw new IllegalStateException("Fell off filter chain in send from " 
+ filter);
     }
 
     /**
@@ -68,6 +64,15 @@ public final class FilterContext {
      * @throws Exception if an error occurs
      */
     public void passOn(Readable message) throws Exception {
-        chain.passOn(this, message);
+        FilterContext ctx = next;
+        while (ctx != null) {
+            InputHandler handler = ctx.filter.in();
+            if (handler != null) {
+                handler.received(message);
+                return;
+            }
+            ctx = ctx.next;
+        }
+        throw new IllegalStateException("Unhandled message: fell off filter 
chain in receive after " + filter);
     }
 }
diff --git 
a/sshd-core/src/main/java/org/apache/sshd/common/session/helpers/AbstractSession.java
 
b/sshd-core/src/main/java/org/apache/sshd/common/session/helpers/AbstractSession.java
index ec6c783ba..8f6bf4bf5 100644
--- 
a/sshd-core/src/main/java/org/apache/sshd/common/session/helpers/AbstractSession.java
+++ 
b/sshd-core/src/main/java/org/apache/sshd/common/session/helpers/AbstractSession.java
@@ -220,7 +220,10 @@ public abstract class AbstractSession extends 
SessionHelper {
      * @throws Exception on errors
      */
     protected void start() throws Exception {
-        boolean isConfigured = !filters.isEmpty();
+        if (filters.isEmpty()) {
+            setupFilterChain();
+        }
+
         IoFilter ioSessionConnector = new IoFilter() {
 
             @Override
@@ -235,12 +238,6 @@ public abstract class AbstractSession extends 
SessionHelper {
         };
         filters.addFirst(ioSessionConnector);
 
-        if (!isConfigured) {
-            setupFilterChain();
-        }
-
-        // Temporary. This is work in progress, and actually a lot is still 
handled by the SSH session.
-        // The idea is to migrate parts step by step into filters on this 
filter chain.
         IoFilter sessionConnector = new IoFilter() {
             @Override
             public InputHandler in() {

Reply via email to