diff --git a/mina-core/src/main/java/org/apache/mina/filter/ssl/SslFilter.java b/mina-core/src/main/java/org/apache/mina/filter/ssl/SslFilter.java
index c3f167e84..418be5ab2 100644
--- a/mina-core/src/main/java/org/apache/mina/filter/ssl/SslFilter.java
+++ b/mina-core/src/main/java/org/apache/mina/filter/ssl/SslFilter.java
@@ -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;
@@ -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 autostart
flag is set to true
, 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;
}
/**
@@ -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);
}
@@ -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);
}