Repository: mina Updated Branches: refs/heads/2.0 05a5c2f2e -> 850bc14dd
o Fixed some missing Javadoc o Fixed some sonarlint warnings Project: http://git-wip-us.apache.org/repos/asf/mina/repo Commit: http://git-wip-us.apache.org/repos/asf/mina/commit/850bc14d Tree: http://git-wip-us.apache.org/repos/asf/mina/tree/850bc14d Diff: http://git-wip-us.apache.org/repos/asf/mina/diff/850bc14d Branch: refs/heads/2.0 Commit: 850bc14ddd2efdf4ab0dedc5d6131b77ae878f7a Parents: 05a5c2f Author: Emmanuel Lécharny <elecha...@symas.com> Authored: Thu Dec 8 20:27:47 2016 +0100 Committer: Emmanuel Lécharny <elecha...@symas.com> Committed: Thu Dec 8 20:27:47 2016 +0100 ---------------------------------------------------------------------- .../filter/ssl/BogusTrustManagerFactory.java | 38 +++++++++++++++----- .../apache/mina/filter/ssl/KeyStoreFactory.java | 9 +++-- .../mina/filter/ssl/SslContextFactory.java | 22 ++++++++++-- .../org/apache/mina/filter/ssl/SslFilter.java | 3 +- .../org/apache/mina/filter/ssl/SslHandler.java | 34 +++++++++++------- 5 files changed, 78 insertions(+), 28 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mina/blob/850bc14d/mina-core/src/main/java/org/apache/mina/filter/ssl/BogusTrustManagerFactory.java ---------------------------------------------------------------------- diff --git a/mina-core/src/main/java/org/apache/mina/filter/ssl/BogusTrustManagerFactory.java b/mina-core/src/main/java/org/apache/mina/filter/ssl/BogusTrustManagerFactory.java index bfaa2fd..5984e69 100644 --- a/mina-core/src/main/java/org/apache/mina/filter/ssl/BogusTrustManagerFactory.java +++ b/mina-core/src/main/java/org/apache/mina/filter/ssl/BogusTrustManagerFactory.java @@ -39,22 +39,27 @@ import javax.net.ssl.X509TrustManager; * @author <a href="http://mina.apache.org">Apache MINA Project</a> */ public class BogusTrustManagerFactory extends TrustManagerFactory { - - public BogusTrustManagerFactory() { - super(new BogusTrustManagerFactorySpi(), new Provider("MinaBogus", 1.0, "") { - private static final long serialVersionUID = -4024169055312053827L; - }, "MinaBogus"); - } - private static final X509TrustManager X509 = new X509TrustManager() { + /** + * {@inheritDoc} + */ + @Override public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { // Do nothing } + /** + * {@inheritDoc} + */ + @Override public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { // Do nothing } + /** + * {@inheritDoc} + */ + @Override public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } @@ -62,18 +67,35 @@ public class BogusTrustManagerFactory extends TrustManagerFactory { private static final TrustManager[] X509_MANAGERS = new TrustManager[] { X509 }; - private static class BogusTrustManagerFactorySpi extends TrustManagerFactorySpi { + /** + * Creates a new BogusTrustManagerFactory instance + */ + public BogusTrustManagerFactory() { + super(new BogusTrustManagerFactorySpi(), new Provider("MinaBogus", 1.0, "") { + private static final long serialVersionUID = -4024169055312053827L; + }, "MinaBogus"); + } + private static class BogusTrustManagerFactorySpi extends TrustManagerFactorySpi { + /** + * {@inheritDoc} + */ @Override protected TrustManager[] engineGetTrustManagers() { return X509_MANAGERS; } + /** + * {@inheritDoc} + */ @Override protected void engineInit(KeyStore keystore) throws KeyStoreException { // noop } + /** + * {@inheritDoc} + */ @Override protected void engineInit(ManagerFactoryParameters managerFactoryParameters) throws InvalidAlgorithmParameterException { http://git-wip-us.apache.org/repos/asf/mina/blob/850bc14d/mina-core/src/main/java/org/apache/mina/filter/ssl/KeyStoreFactory.java ---------------------------------------------------------------------- diff --git a/mina-core/src/main/java/org/apache/mina/filter/ssl/KeyStoreFactory.java b/mina-core/src/main/java/org/apache/mina/filter/ssl/KeyStoreFactory.java index 1859ceb..b0903fa 100644 --- a/mina-core/src/main/java/org/apache/mina/filter/ssl/KeyStoreFactory.java +++ b/mina-core/src/main/java/org/apache/mina/filter/ssl/KeyStoreFactory.java @@ -148,12 +148,15 @@ public class KeyStoreFactory { ByteArrayOutputStream out = new ByteArrayOutputStream(); try { for (;;) { - int data = dataStream.read(); - if (data < 0) { + int readByte = dataStream.read(); + + if (readByte < 0) { break; } - out.write(data); + + out.write(readByte); } + setData(out.toByteArray()); } finally { try { http://git-wip-us.apache.org/repos/asf/mina/blob/850bc14d/mina-core/src/main/java/org/apache/mina/filter/ssl/SslContextFactory.java ---------------------------------------------------------------------- diff --git a/mina-core/src/main/java/org/apache/mina/filter/ssl/SslContextFactory.java b/mina-core/src/main/java/org/apache/mina/filter/ssl/SslContextFactory.java index b66610b..04e3d4c 100644 --- a/mina-core/src/main/java/org/apache/mina/filter/ssl/SslContextFactory.java +++ b/mina-core/src/main/java/org/apache/mina/filter/ssl/SslContextFactory.java @@ -88,15 +88,24 @@ public class SslContextFactory { private int serverSessionTimeout = -1; + /** + * Create a new SSLContext instance,using the {@link KeyManagerFactory} and the + * {@link TrustManagerFactory}. + * + * @return The created instance + * @throws Exception If we weren't able to create the SSLContext insyance + */ public SSLContext newInstance() throws Exception { KeyManagerFactory kmf = this.keyManagerFactory; TrustManagerFactory tmf = this.trustManagerFactory; if (kmf == null) { String algorithm = keyManagerFactoryAlgorithm; + if (algorithm == null && keyManagerFactoryAlgorithmUseDefault) { algorithm = KeyManagerFactory.getDefaultAlgorithm(); } + if (algorithm != null) { if (keyManagerFactoryProvider == null) { kmf = KeyManagerFactory.getInstance(algorithm); @@ -108,9 +117,11 @@ public class SslContextFactory { if (tmf == null) { String algorithm = trustManagerFactoryAlgorithm; + if (algorithm == null && trustManagerFactoryAlgorithmUseDefault) { algorithm = TrustManagerFactory.getDefaultAlgorithm(); } + if (algorithm != null) { if (trustManagerFactoryProvider == null) { tmf = TrustManagerFactory.getInstance(algorithm); @@ -121,21 +132,26 @@ public class SslContextFactory { } KeyManager[] keyManagers = null; + if (kmf != null) { kmf.init(keyManagerFactoryKeyStore, keyManagerFactoryKeyStorePassword); keyManagers = kmf.getKeyManagers(); } + TrustManager[] trustManagers = null; + if (tmf != null) { if (trustManagerFactoryParameters != null) { tmf.init(trustManagerFactoryParameters); } else { tmf.init(trustManagerFactoryKeyStore); } + trustManagers = tmf.getTrustManagers(); } - SSLContext context = null; + SSLContext context; + if (provider == null) { context = SSLContext.getInstance(protocol); } else { @@ -183,6 +199,7 @@ public class SslContextFactory { if (protocol == null) { throw new IllegalArgumentException("protocol"); } + this.protocol = protocol; } @@ -194,8 +211,7 @@ public class SslContextFactory { * return by {@link KeyManagerFactory#getDefaultAlgorithm()} will be used. * The default value of this property is <tt>true</tt>. * - * @param useDefault - * <tt>true</tt> or <tt>false</tt>. + * @param useDefault <tt>true</tt> or <tt>false</tt>. */ public void setKeyManagerFactoryAlgorithmUseDefault(boolean useDefault) { this.keyManagerFactoryAlgorithmUseDefault = useDefault; http://git-wip-us.apache.org/repos/asf/mina/blob/850bc14d/mina-core/src/main/java/org/apache/mina/filter/ssl/SslFilter.java ---------------------------------------------------------------------- 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 7acb123..c2b92c2 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 @@ -568,7 +568,7 @@ public class SslFilter extends IoFilterAdapter { return; } - List<WriteRequest> newFailedRequests = new ArrayList<WriteRequest>(failedRequests.size() - 1); + List<WriteRequest> newFailedRequests = new ArrayList<>(failedRequests.size() - 1); for (WriteRequest r : failedRequests) { if (!isCloseNotify(r.getMessage())) { @@ -676,6 +676,7 @@ public class SslFilter extends IoFilterAdapter { if (isSslStarted(session)) { future = initiateClosure(nextFilter, session); future.addListener(new IoFutureListener<IoFuture>() { + @Override public void operationComplete(IoFuture future) { nextFilter.filterClose(session); } http://git-wip-us.apache.org/repos/asf/mina/blob/850bc14d/mina-core/src/main/java/org/apache/mina/filter/ssl/SslHandler.java ---------------------------------------------------------------------- diff --git a/mina-core/src/main/java/org/apache/mina/filter/ssl/SslHandler.java b/mina-core/src/main/java/org/apache/mina/filter/ssl/SslHandler.java index 8cd1c80..509e678 100644 --- a/mina-core/src/main/java/org/apache/mina/filter/ssl/SslHandler.java +++ b/mina-core/src/main/java/org/apache/mina/filter/ssl/SslHandler.java @@ -62,7 +62,7 @@ import org.slf4j.LoggerFactory; /** No qualifier*/ class SslHandler { /** A logger for this class */ - private final static Logger LOGGER = LoggerFactory.getLogger(SslHandler.class); + private static final Logger LOGGER = LoggerFactory.getLogger(SslHandler.class); /** The SSL Filter which has created this handler */ private final SslFilter sslFilter; @@ -70,12 +70,12 @@ class SslHandler { /** The current session */ private final IoSession session; - private final Queue<IoFilterEvent> preHandshakeEventQueue = new ConcurrentLinkedQueue<IoFilterEvent>(); + private final Queue<IoFilterEvent> preHandshakeEventQueue = new ConcurrentLinkedQueue<>(); - private final Queue<IoFilterEvent> filterWriteEventQueue = new ConcurrentLinkedQueue<IoFilterEvent>(); + private final Queue<IoFilterEvent> filterWriteEventQueue = new ConcurrentLinkedQueue<>(); /** A queue used to stack all the incoming data until the SSL session is established */ - private final Queue<IoFilterEvent> messageReceivedEventQueue = new ConcurrentLinkedQueue<IoFilterEvent>(); + private final Queue<IoFilterEvent> messageReceivedEventQueue = new ConcurrentLinkedQueue<>(); private SSLEngine sslEngine; @@ -400,13 +400,13 @@ class SslHandler { * @return buffer with data */ /* no qualifier */IoBuffer fetchAppBuffer() { - if (this.appBuffer == null) { + if (appBuffer == null) { return IoBuffer.allocate(0); } else { - IoBuffer appBuffer = this.appBuffer.flip(); - this.appBuffer = null; + IoBuffer newAppBuffer = appBuffer.flip(); + appBuffer = null; - return appBuffer.shrink(); + return newAppBuffer.shrink(); } } @@ -417,11 +417,13 @@ class SslHandler { */ /* no qualifier */IoBuffer fetchOutNetBuffer() { IoBuffer answer = outNetBuffer; + if (answer == null) { return emptyBuffer; } outNetBuffer = null; + return answer.shrink(); } @@ -599,6 +601,7 @@ class SslHandler { for (;;) { result = sslEngine.wrap(emptyBuffer.buf(), outNetBuffer.buf()); + if (result.getStatus() == SSLEngineResult.Status.BUFFER_OVERFLOW) { outNetBuffer.capacity(outNetBuffer.capacity() << 1); outNetBuffer.limit(outNetBuffer.capacity()); @@ -662,10 +665,11 @@ class SslHandler { throw newSsle; } - IoBuffer outNetBuffer = fetchOutNetBuffer(); - if (outNetBuffer != null && outNetBuffer.hasRemaining()) { + IoBuffer currentOutNetBuffer = fetchOutNetBuffer(); + + if (currentOutNetBuffer != null && currentOutNetBuffer.hasRemaining()) { writeFuture = new DefaultWriteFuture(session); - sslFilter.filterWrite(nextFilter, session, new DefaultWriteRequest(outNetBuffer, writeFuture)); + sslFilter.filterWrite(nextFilter, session, new DefaultWriteRequest(currentOutNetBuffer, writeFuture)); } } } finally { @@ -746,8 +750,8 @@ class SslHandler { SSLEngineResult res; - Status status = null; - HandshakeStatus handshakeStatus = null; + Status status; + HandshakeStatus handshakeStatus; do { // Decode the incoming data @@ -810,6 +814,10 @@ class SslHandler { return copy; } + /** + * {@inheritDoc} + */ + @Override public String toString() { StringBuilder sb = new StringBuilder();