org.apache.maven.plugins
maven-shade-plugin
diff --git a/src/main/java/org/fuin/sjsm/Config.java b/src/main/java/org/fuin/sjsm/Config.java
index 424993f..20b03b7 100644
--- a/src/main/java/org/fuin/sjsm/Config.java
+++ b/src/main/java/org/fuin/sjsm/Config.java
@@ -40,9 +40,12 @@ public final class Config {
@Option(name = "-user", usage = "User", metaVar = "USER", required = true)
private String user;
- @Option(name = "-pw", usage = "Password", metaVar = "PW", required = true)
+ @Option(name = "-pw", usage = "Password", metaVar = "PW")
private String pw;
+ @Option(name = "-envPw", usage = "Name of an environment variable that contains the password", metaVar = "ENV_PW")
+ private String envPw;
+
@Option(name = "-from", usage = "Sender", metaVar = "SEND", required = true)
private String from;
@@ -149,6 +152,25 @@ public void setPw(final String pw) {
this.pw = pw;
}
+ /**
+ * Returns the name of an environment variable that contains the password.
+ *
+ * @return Environment variable with password.
+ */
+ public String getEnvPw() {
+ return envPw;
+ }
+
+ /**
+ * Sets the name of an environment variable that contains the password.
+ *
+ * @param envPw
+ * Environment variable to use.
+ */
+ public void setEnvPw(final String envPw) {
+ this.envPw = envPw;
+ }
+
/**
* Returns the important (X-Priority) flag.
*
@@ -440,7 +462,13 @@ public Properties createSessionProperties() {
public Authenticator createAuthenticator() {
return new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(user, pw);
+ final String p;
+ if (pw == null) {
+ p = System.getenv(envPw);
+ } else {
+ p = pw;
+ }
+ return new PasswordAuthentication(user, p);
}
};
}
diff --git a/src/main/java/org/fuin/sjsm/Messages.java b/src/main/java/org/fuin/sjsm/Messages.java
new file mode 100644
index 0000000..6584dc5
--- /dev/null
+++ b/src/main/java/org/fuin/sjsm/Messages.java
@@ -0,0 +1,31 @@
+package org.fuin.sjsm;
+
+import org.kohsuke.args4j.Localizable;
+
+import java.text.MessageFormat;
+import java.util.Locale;
+import java.util.ResourceBundle;
+
+/**
+ * Message constants.
+ */
+public enum Messages implements Localizable {
+
+ /** Neither 'pw' nor 'envPw' argument is set. */
+ MISSING_PASSWORD_OPTION,
+
+ /** The environment variable from 'envPw' argument is not set. */
+ PASSWORD_ENV_VAR_NOT_SET;
+
+ @Override
+ public String formatWithLocale(final Locale locale, final Object... args) {
+ final ResourceBundle localized = ResourceBundle.getBundle("sjsm-messages", locale);
+ return MessageFormat.format(localized.getString(name()), args);
+ }
+
+ @Override
+ public String format(final Object... args) {
+ return formatWithLocale(Locale.getDefault(), args);
+ }
+
+}
diff --git a/src/main/java/org/fuin/sjsm/SendMailApp.java b/src/main/java/org/fuin/sjsm/SendMailApp.java
index 4dc5cd4..feb2a6c 100644
--- a/src/main/java/org/fuin/sjsm/SendMailApp.java
+++ b/src/main/java/org/fuin/sjsm/SendMailApp.java
@@ -66,6 +66,16 @@ public void send(final Config config) {
}
+ private static void ensurePasswordIsSet(final CmdLineParser parser , final Config config) throws CmdLineException {
+ if (config.getPw() == null && config.getEnvPw() == null) {
+ throw new CmdLineException(parser, Messages.MISSING_PASSWORD_OPTION);
+ }
+ if (config.getEnvPw() != null && System.getenv(config.getEnvPw()) == null) {
+ throw new CmdLineException(parser, Messages.PASSWORD_ENV_VAR_NOT_SET, config.getEnvPw());
+ }
+ }
+
+
/**
* Main application method.
*
@@ -78,6 +88,7 @@ public static void main(final String[] args) {
final CmdLineParser parser = new CmdLineParser(config);
try {
parser.parseArgument(args);
+ ensurePasswordIsSet(parser, config);
new SendMailApp().send(config);
System.exit(0);
} catch (final CmdLineException ex) {
diff --git a/src/main/resources/sjsm-messages.properties b/src/main/resources/sjsm-messages.properties
new file mode 100644
index 0000000..eb2146a
--- /dev/null
+++ b/src/main/resources/sjsm-messages.properties
@@ -0,0 +1,2 @@
+MISSING_PASSWORD_OPTION=A password or an environment variable with the password is mandatory
+PASSWORD_ENV_VAR_NOT_SET=The environment variable {0} is not set (has no value)
\ No newline at end of file
diff --git a/src/test/java/org/fuin/sjsm/SendMailAppTest.java b/src/test/java/org/fuin/sjsm/SendMailAppTest.java
index a15b633..96871b1 100644
--- a/src/test/java/org/fuin/sjsm/SendMailAppTest.java
+++ b/src/test/java/org/fuin/sjsm/SendMailAppTest.java
@@ -1,26 +1,29 @@
/**
- * Copyright (C) 2015 Michael Schnell. All rights reserved.
+ * Copyright (C) 2015 Michael Schnell. All rights reserved.
* http://www.fuin.org/
- *
+ *
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at your option) any
* later version.
- *
+ *
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
- *
+ *
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see http://www.gnu.org/licenses/.
*/
package org.fuin.sjsm;
+import static com.github.stefanbirkner.systemlambda.SystemLambda.*;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.IOException;
import java.util.List;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.atomic.AtomicReference;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
@@ -122,4 +125,108 @@ void testSendMultipleReceiver() {
}
+ @Test
+ void testMissingPasswordOption() throws Exception {
+
+ final String[] args = new String[]{
+ "-host", "localhost",
+ "-port", "" + dumbster.getPort(),
+ "-user", "myaccount",
+ "-from", "does-not@matter.com",
+ "-to", "not@used.com",
+ "-subject", "Whatever",
+ "-message", "None"
+ };
+
+ final AtomicLong exitCode = new AtomicLong();
+ final String systemErr = tapSystemErr(() -> {
+ exitCode.set(catchSystemExit(() -> {
+ SendMailApp.main(args);
+ }));
+ });
+ assertThat(systemErr).contains("A password or an environment variable with the password is mandatory");
+ assertThat(exitCode.get()).isEqualTo(1);
+
+ }
+
+ @Test
+ void testPwOption() throws Exception {
+
+ final String[] args = new String[]{
+ "-host", "localhost",
+ "-port", "" + dumbster.getPort(),
+ "-user", "myaccount",
+ "-pw", "abc",
+ "-from", "does-not@matter.com",
+ "-to", "not@used.com",
+ "-subject", "Whatever",
+ "-message", "None",
+ "-smtp"
+ };
+
+ final AtomicLong exitCode = new AtomicLong();
+ final String systemOut = tapSystemOut(() -> {
+ exitCode.set(catchSystemExit(() -> {
+ SendMailApp.main(args);
+ }));
+ });
+ assertThat(systemOut).contains("Successfully sent message 'Whatever' to 'not@used.com");
+ assertThat(exitCode.get()).isEqualTo(0);
+
+ }
+
+ @Test
+ void testEnvPwOption() throws Exception {
+
+ final String[] args = new String[]{
+ "-host", "localhost",
+ "-port", "" + dumbster.getPort(),
+ "-user", "myaccount",
+ "-envPw", "MAIL_PW_TEST",
+ "-from", "does-not@matter.com",
+ "-to", "not@used.com",
+ "-subject", "Whatever",
+ "-message", "None",
+ "-smtp"
+ };
+
+ final AtomicLong exitCode = new AtomicLong();
+ final AtomicReference systemOut = new AtomicReference<>();
+ withEnvironmentVariable("MAIL_PW_TEST", "abc").execute(() -> {
+ systemOut.set(tapSystemOut(() -> {
+ exitCode.set(catchSystemExit(() -> {
+ SendMailApp.main(args);
+ }));
+ }));
+ });
+ assertThat(systemOut.get()).contains("Successfully sent message 'Whatever' to 'not@used.com");
+ assertThat(exitCode.get()).isEqualTo(0);
+
+ }
+
+ @Test
+ void testMissingEnvPassword() throws Exception {
+
+ final String[] args = new String[]{
+ "-host", "localhost",
+ "-port", "" + dumbster.getPort(),
+ "-user", "myaccount",
+ "-envPw", "NOT_EXISTING_PW_ENV_VAR",
+ "-from", "does-not@matter.com",
+ "-to", "not@used.com",
+ "-subject", "Whatever",
+ "-message", "None"
+ };
+
+ final AtomicLong exitCode = new AtomicLong();
+ final String systemErr = tapSystemErr(() -> {
+ exitCode.set(catchSystemExit(() -> {
+ SendMailApp.main(args);
+ }));
+ });
+ assertThat(systemErr).contains("The environment variable NOT_EXISTING_PW_ENV_VAR is not set");
+ assertThat(exitCode.get()).isEqualTo(1);
+
+ }
+
}