Skip to content

Commit

Permalink
Applied patch proposed by kllbzz (#41): reintroduced the autoStart fl…
Browse files Browse the repository at this point in the history
…ag for SSL (allowing the user to differ the HandShake after the addition in the chain if false)
  • Loading branch information
elecharny committed Nov 1, 2023
1 parent 337246a commit e9d4fae
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions mina-core/src/main/java/org/apache/mina/filter/ssl/SslFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ public class SslFilter extends IoFilterAdapter {

protected final SSLContext sslContext;

/** A flag used to tell the filter to start the handshake immediately (in onPostAdd method)
* alternatively handshake will be started after session is connected (in sessionOpened method)
* default value is true
**/
private final boolean autoStart;

/** A flag set if client authentication is required */
protected boolean needClientAuth = false;

Expand Down Expand Up @@ -110,9 +116,23 @@ public class SslFilter extends IoFilterAdapter {
* @param sslContext The SSLContext to use
*/
public SslFilter(SSLContext sslContext) {
this(sslContext, true);
}

/**
* Creates a new SSL filter using the specified {@link SSLContext}.
* If the <code>autostart</code> flag is set to <code>true</code>, the
* handshake will start immediately after the filter has been added
* to the chain.
*
* @param sslContext The SSLContext to use
* @param autoStart The flag used to tell the filter to start the handshake immediately
*/
public SslFilter(SSLContext sslContext, boolean autoStart) {
Objects.requireNonNull(sslContext, "ssl must not be null");

this.sslContext = sslContext;
this.autoStart = autoStart;
}

/**
Expand Down Expand Up @@ -245,8 +265,11 @@ public void onPreAdd(IoFilterChain parent, String name, NextFilter next) throws
@Override
public void onPostAdd(IoFilterChain parent, String name, NextFilter next) throws Exception {
IoSession session = parent.getSession();

if (session.isConnected()) {

// The SslFilter has been added *after* the session has been created and opened.
// We need to initiate the HandShake, this is done here, unless the user wants
// to differ the HandShake to later (and in this case autoStart is set to false)
if (session.isConnected() && autoStart) {
onConnected(next, session);
}

Expand Down Expand Up @@ -359,6 +382,7 @@ public void sessionOpened(NextFilter next, IoSession session) throws Exception {
}
}

// Used to initiate the HandShake if differed
onConnected(next, session);
super.sessionOpened(next, session);
}
Expand Down

0 comments on commit e9d4fae

Please sign in to comment.