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

rcordier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit 8039e544c95099ca70fa76bef6fb0c4dd9200b64
Author: Benoit Tellier <[email protected]>
AuthorDate: Mon Dec 6 15:52:12 2021 +0700

    JAMES-3680 SMTPServer: Use an enum for authRequired
    
    Instead of a collection of integers
---
 .../james/modules/protocols/SmtpGuiceProbe.java    |  4 +-
 .../apache/james/smtpserver/netty/SMTPServer.java  | 44 +++++++++++++---------
 2 files changed, 30 insertions(+), 18 deletions(-)

diff --git 
a/server/container/guice/protocols/smtp/src/main/java/org/apache/james/modules/protocols/SmtpGuiceProbe.java
 
b/server/container/guice/protocols/smtp/src/main/java/org/apache/james/modules/protocols/SmtpGuiceProbe.java
index cb2f44a..5c38a77 100644
--- 
a/server/container/guice/protocols/smtp/src/main/java/org/apache/james/modules/protocols/SmtpGuiceProbe.java
+++ 
b/server/container/guice/protocols/smtp/src/main/java/org/apache/james/modules/protocols/SmtpGuiceProbe.java
@@ -18,6 +18,8 @@
  ****************************************************************/
 package org.apache.james.modules.protocols;
 
+import static 
org.apache.james.smtpserver.netty.SMTPServer.AuthenticationRequired.REQUIRED;
+
 import java.net.InetSocketAddress;
 import java.util.function.Function;
 import java.util.function.Predicate;
@@ -63,7 +65,7 @@ public class SmtpGuiceProbe implements GuiceProbe {
     }
 
     public Port getSmtpAuthRequiredPort() {
-        return getPort(server -> ((SMTPServer) server).getAuthRequired() == 
SMTPServer.AUTH_REQUIRED);
+        return getPort(server -> ((SMTPServer) 
server).getAuthRequired().equals(REQUIRED));
     }
 
     private Port getPort(Predicate<? super AbstractConfigurableAsyncServer> 
filter) {
diff --git 
a/server/protocols/protocols-smtp/src/main/java/org/apache/james/smtpserver/netty/SMTPServer.java
 
b/server/protocols/protocols-smtp/src/main/java/org/apache/james/smtpserver/netty/SMTPServer.java
index b22381c..ff966f5 100644
--- 
a/server/protocols/protocols-smtp/src/main/java/org/apache/james/smtpserver/netty/SMTPServer.java
+++ 
b/server/protocols/protocols-smtp/src/main/java/org/apache/james/smtpserver/netty/SMTPServer.java
@@ -18,6 +18,9 @@
  ****************************************************************/
 package org.apache.james.smtpserver.netty;
 
+import static 
org.apache.james.smtpserver.netty.SMTPServer.AuthenticationRequired.ANNOUNCE;
+import static 
org.apache.james.smtpserver.netty.SMTPServer.AuthenticationRequired.DISABLED;
+
 import java.util.Locale;
 
 import javax.inject.Inject;
@@ -50,13 +53,26 @@ import org.slf4j.LoggerFactory;
 public class SMTPServer extends AbstractProtocolAsyncServer implements 
SMTPServerMBean {
     private static final Logger LOGGER = 
LoggerFactory.getLogger(AbstractProtocolAsyncServer.class);
 
+    public enum AuthenticationRequired {
+        DISABLED,
+        REQUIRED,
+        ANNOUNCE;
+
+        public static AuthenticationRequired parse(String authRequiredString) {
+            if (authRequiredString.equals("true")) {
+                return REQUIRED;
+            } else if (authRequiredString.equals("announce")) {
+                return ANNOUNCE;
+            } else {
+                return DISABLED;
+            }
+        }
+    }
+
     /**
      * Whether authentication is required to use this SMTP server.
      */
-    public static final int AUTH_DISABLED = 0;
-    public static final int AUTH_REQUIRED = 1;
-    public static final int AUTH_ANNOUNCE = 2;
-    private int authRequired = AUTH_DISABLED;
+    private AuthenticationRequired authRequired = DISABLED;
     
     /**
      * Whether the server needs helo to be send first
@@ -132,21 +148,15 @@ public class SMTPServer extends 
AbstractProtocolAsyncServer implements SMTPServe
         super.doConfigure(configuration);
         if (isEnabled()) {
             String authRequiredString = 
configuration.getString("authRequired", "false").trim().toLowerCase(Locale.US);
-            if (authRequiredString.equals("true")) {
-                authRequired = AUTH_REQUIRED;
-            } else if (authRequiredString.equals("announce")) {
-                authRequired = AUTH_ANNOUNCE;
-            } else {
-                authRequired = AUTH_DISABLED;
-            }
-            if (authRequired != AUTH_DISABLED) {
+            authRequired = AuthenticationRequired.parse(authRequiredString);
+            if (authRequired != DISABLED) {
                 LOGGER.info("This SMTP server requires authentication.");
             } else {
                 LOGGER.info("This SMTP server does not require 
authentication.");
             }
 
             authorizedAddresses = 
configuration.getString("authorizedAddresses", null);
-            if (authRequired == AUTH_DISABLED && authorizedAddresses == null) {
+            if (authRequired == DISABLED && authorizedAddresses == null) {
                 /*
                  * if SMTP AUTH is not required then we will use
                  * authorizedAddresses to determine whether or not to relay
@@ -187,7 +197,7 @@ public class SMTPServer extends AbstractProtocolAsyncServer 
implements SMTPServe
 
             verifyIdentity = configuration.getBoolean("verifyIdentity", false);
 
-            if (authRequired == AUTH_DISABLED && verifyIdentity) {
+            if (authRequired == DISABLED && verifyIdentity) {
                 throw new ConfigurationException(
                     "SMTP configuration: 'verifyIdentity' can't be set to true 
if 'authRequired' is set to false.");
             }
@@ -244,10 +254,10 @@ public class SMTPServer extends 
AbstractProtocolAsyncServer implements SMTPServe
 
         @Override
         public boolean isAuthRequired(String remoteIP) {
-            if (SMTPServer.this.authRequired == AUTH_ANNOUNCE) {
+            if (SMTPServer.this.authRequired == ANNOUNCE) {
                 return true;
             }
-            boolean authRequired = SMTPServer.this.authRequired != 
AUTH_DISABLED;
+            boolean authRequired = SMTPServer.this.authRequired != DISABLED;
             if (authorizedNetworks != null) {
                 authRequired = authRequired && 
!SMTPServer.this.authorizedNetworks.matchInetNetwork(remoteIP);
             }
@@ -334,7 +344,7 @@ public class SMTPServer extends AbstractProtocolAsyncServer 
implements SMTPServe
         return new AllButStartTlsLineChannelHandlerFactory("starttls", 
AbstractChannelPipelineFactory.MAX_LINE_LENGTH);
     }
 
-    public int getAuthRequired() {
+    public AuthenticationRequired getAuthRequired() {
         return authRequired;
     }
 }

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to