Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rely on data-binding to (re)configure descriptor #39

Merged
merged 8 commits into from
Aug 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 91 additions & 37 deletions src/main/java/hudson/tasks/Mailer.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import static hudson.Util.fixEmptyAndTrim;

import hudson.BulkChange;
import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
Expand Down Expand Up @@ -78,6 +79,7 @@
import org.apache.tools.ant.types.selectors.SelectorUtils;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.export.Exported;
Expand Down Expand Up @@ -249,12 +251,15 @@ public static final class DescriptorImpl extends BuildStepDescriptor<Publisher>
*/
private String hudsonUrl;

/**
* If non-null, use SMTP-AUTH with these information.
*/
private String smtpAuthUsername;
/** @deprecated as of 1.22, use {@link #authentication} */
@Deprecated
private transient String smtpAuthUsername;

@Deprecated
/** @deprecated as of 1.22, use {@link #authentication} */
private transient Secret smtpAuthPassword;

private Secret smtpAuthPassword;
private SMTPAuthentication authentication;

/**
* The e-mail address that Hudson puts to "From:" field in outgoing e-mails.
Expand Down Expand Up @@ -318,8 +323,10 @@ public String getReplyToAddress() {
return replyToAddress;
}

@DataBoundSetter
public void setReplyToAddress(String address) {
this.replyToAddress = Util.fixEmpty(address);
save();
}

/** JavaMail session. */
Expand Down Expand Up @@ -375,27 +382,18 @@ protected PasswordAuthentication getPasswordAuthentication() {

@Override
public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
// this code is brain dead
smtpHost = nullify(json.getString("smtpServer"));
setReplyToAddress(json.getString("replyToAddress"));

defaultSuffix = nullify(json.getString("defaultSuffix"));

if(json.has("useSMTPAuth")) {
JSONObject auth = json.getJSONObject("useSMTPAuth");
smtpAuthUsername = nullify(auth.getString("smtpAuthUserName"));
smtpAuthPassword = Secret.fromString(nullify(auth.getString("smtpAuthPasswordSecret")));
} else {
smtpAuthUsername = null;
smtpAuthPassword = null;
BulkChange b = new BulkChange(this);
try {
// reset optional authentication to default before data-binding
// Would not be necessary by https://github.com/jenkinsci/jenkins/pull/3669
setAuthentication(null);
req.bindJSON(this, json);
b.commit();
} catch (IOException e) {
b.abort();
throw new FormException("Failed to apply configuration", e, null);
}
smtpPort = nullify(json.getString("smtpPort"));
useSsl = json.getBoolean("useSsl");
charset = json.getString("charset");
if (charset == null || charset.length() == 0)
charset = "UTF-8";

save();
return true;
}

Expand All @@ -404,6 +402,12 @@ private String nullify(String v) {
return v;
}

public String getSmtpHost() {
return smtpHost;
}

@Deprecated
/** @deprecated as of 1.23, use {@link #getSmtpHost()} */
public String getSmtpServer() {
return smtpHost;
}
Expand All @@ -427,6 +431,7 @@ private JenkinsLocationConfiguration getJenkinsLocationConfiguration() {
* @deprecated as of 1.4
* Use {@link JenkinsLocationConfiguration}
*/
@Deprecated
public String getAdminAddress() {
return getJenkinsLocationConfiguration().getAdminAddress();
}
Expand All @@ -435,17 +440,29 @@ public String getAdminAddress() {
* @deprecated as of 1.4
* Use {@link JenkinsLocationConfiguration}
*/
@Deprecated
public String getUrl() {
return getJenkinsLocationConfiguration().getUrl();
}

/**
* @deprecated as of 1.21
* Use {@link #authentication}
*/
@Deprecated
public String getSmtpAuthUserName() {
return smtpAuthUsername;
if (authentication == null) return null;
return authentication.getUsername();
}

/**
* @deprecated as of 1.21
* Use {@link #authentication}
*/
@Deprecated
public String getSmtpAuthPassword() {
if (smtpAuthPassword==null) return null;
return Secret.toString(smtpAuthPassword);
if (authentication == null) return null;
return Secret.toString(authentication.getPassword());
}

public Secret getSmtpAuthPasswordSecret() {
Expand All @@ -460,24 +477,23 @@ public String getSmtpPort() {
return smtpPort;
}

public String getSmtpHost() {
return smtpHost;
}

public String getCharset() {
String c = charset;
if (c == null || c.length() == 0) c = "UTF-8";
return c;
}

@DataBoundSetter
public void setDefaultSuffix(String defaultSuffix) {
this.defaultSuffix = defaultSuffix;
save();
}

/**
* @deprecated as of 1.4
* Use {@link JenkinsLocationConfiguration}
*/
@Deprecated
public void setHudsonUrl(String hudsonUrl) {
getJenkinsLocationConfiguration().setUrl(hudsonUrl);
}
Expand All @@ -486,29 +502,60 @@ public void setHudsonUrl(String hudsonUrl) {
* @deprecated as of 1.4
* Use {@link JenkinsLocationConfiguration}
*/
@Deprecated
public void setAdminAddress(String adminAddress) {
getJenkinsLocationConfiguration().setAdminAddress(adminAddress);
}

@DataBoundSetter
public void setSmtpHost(String smtpHost) {
this.smtpHost = smtpHost;
this.smtpHost = nullify(smtpHost);
save();
}

@DataBoundSetter
public void setUseSsl(boolean useSsl) {
this.useSsl = useSsl;
save();
}

@DataBoundSetter
public void setSmtpPort(String smtpPort) {
this.smtpPort = smtpPort;
save();
}

public void setCharset(String chaset) {
this.charset = chaset;

@DataBoundSetter
public void setCharset(String charset) {
if (charset == null || charset.length() == 0) {
charset = "UTF-8";
}
this.charset = charset;
save();
}

@DataBoundSetter
public void setAuthentication(@CheckForNull SMTPAuthentication authentication) {
this.authentication = authentication;
save();
}

@CheckForNull
public SMTPAuthentication getAuthentication() {
return authentication;
}

/**
* @deprecated as of 1.21
* Use {@link #authentication}
*/
@Deprecated
public void setSmtpAuth(String userName, String password) {
this.smtpAuthUsername = userName;
this.smtpAuthPassword = Secret.fromString(password);
if (userName == null && password == null) {
this.authentication = null;
} else {
this.authentication = new SMTPAuthentication(userName, Secret.fromString(password));
}
}

@Override
Expand All @@ -524,6 +571,13 @@ public Publisher newInstance(StaplerRequest req, JSONObject formData) throws For
return m;
}

private Object readResolve() {
if (smtpAuthPassword != null) {
authentication = new SMTPAuthentication(smtpAuthUsername, smtpAuthPassword);
}
return this;
}

public FormValidation doAddressCheck(@QueryParameter String value) {
try {
new InternetAddress(value);
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/hudson/tasks/SMTPAuthentication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package hudson.tasks;

import hudson.Extension;
import hudson.model.AbstractDescribableImpl;
import hudson.model.Descriptor;
import hudson.util.Secret;
import org.kohsuke.stapler.DataBoundConstructor;

/**
* @author <a href="mailto:[email protected]">Nicolas De Loof</a>
*/
public class SMTPAuthentication extends AbstractDescribableImpl<SMTPAuthentication> {

private String username;

private Secret password;

@DataBoundConstructor
public SMTPAuthentication(String username, Secret password) {
this.username = username;
this.password = password;
}

public String getUsername() {
return username;
}

public Secret getPassword() {
return password;
}

@Extension
public static class DescriptorImpl extends Descriptor<SMTPAuthentication> {

@Override
public String getDisplayName() {
return "Use SMTP Authentication";
}
}
}
14 changes: 4 additions & 10 deletions src/main/resources/hudson/tasks/Mailer/global.jelly
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,16 @@ THE SOFTWARE.
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<f:section title="${%E-mail Notification}">
<f:entry title="${%SMTP server}" field="smtpServer">
<f:entry title="${%SMTP server}" field="smtpHost">
<f:textbox />
</f:entry>
<f:entry title="${%Default user e-mail suffix}" field="defaultSuffix">
<f:textbox />
</f:entry>
<f:advanced>
<f:optionalBlock name="useSMTPAuth" title="${%Use SMTP Authentication}" checked="${descriptor.smtpAuthUserName!=null}"
help="${descriptor.getHelpFile('smtpAuth')}">
<f:entry title="${%User Name}" field="smtpAuthUserName">
<f:textbox />
</f:entry>
<f:entry title="${%Password}" field="smtpAuthPasswordSecret">
<f:password />
</f:entry>
</f:optionalBlock>

<f:optionalProperty title="${%Use SMTP Authentication}" field="authentication"/>

<f:entry title="${%Use SSL}" field="useSsl">
<f:checkbox />
</f:entry>
Expand Down
22 changes: 22 additions & 0 deletions src/main/resources/hudson/tasks/Mailer/global.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# The MIT License

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ndeloof Why is this empty "global.properties" file added for Mailer but no "config.properties" for SMTPAuthentication?

#
# Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Eric Lefevre-Ardant
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

2 changes: 0 additions & 2 deletions src/main/resources/hudson/tasks/Mailer/global_da.properties
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ E-mail\ Notification=E-mail p\u00e5mindelser
Default\ user\ e-mail\ suffix=Standard e-mail endelse
Use\ SSL=Brug SSL
SMTP\ Port=SMTP Port
Use\ SMTP\ Authentication=Benyt SMTP Authentificering

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ndeloof Did you miss to remove Password in line 23?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ndeloof "${%Use SMTP Authentication}" is still used in "Mailer/global.jelly".
Is it correct to move it from "Mailer/global*.properties" to "SMTPAuthentication/config*.properties"?

SMTP\ server=SMTP Server
User\ Name=Brugernavn
System\ Admin\ E-mail\ Address=Systemadministratorens E-mail adresse
Charset=Karakters\u00e6t
Test\ configuration\ by\ sending\ test\ e-mail=Test konfigurationen ved at sende en e-mail
Expand Down
3 changes: 0 additions & 3 deletions src/main/resources/hudson/tasks/Mailer/global_de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ SMTP\ server=SMTP-Server
Default\ user\ e-mail\ suffix=Standardendung f\u00FCr E-Mail-Adressen
System\ Admin\ E-mail\ Address=E-Mail-Adresse des Systemadministrators
Jenkins\ URL=Jenkins URL
User\ Name=Benutzername
Password=Kennwort
Use\ SSL=SSL verwenden
Use\ SMTP\ Authentication=Verwende SMTP Authentifizierung
SMTP\ Port=SMTP-Port
Reply-To\ Address=Reply-To Adresse
Charset=Zeichensatz
17 changes: 7 additions & 10 deletions src/main/resources/hudson/tasks/Mailer/global_es.properties
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,15 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

E-mail\ Notification=Notificaci�n por correo electr�nico
E-mail\ Notification=Notificaci�n por correo electr�nico

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ndeloof Just looking at the diff on GitHub it looks like the encoding got broken. Could you double-check this (this and other properties files)?

SMTP\ server=Servidor de correo saliente (SMTP)
Default\ user\ e-mail\ suffix=sufijo de email por defecto
Jenkins\ URL=Direcci�n web de Jenkins
Use\ SMTP\ Authentication=Usar autenticaci�n SMTP
User\ Name=Nombre de usuario
Password=Contrase\u00F1a
Jenkins\ URL=Direcci�n web de Jenkins
Use\ SSL=Usar seguridad SSL
SMTP\ Port=Puerto de SMTP
Charset=Juego de caracteres
Test\ configuration\ by\ sending\ test\ e-mail=Probar la configuraci�n enviando un correo de prueba
Reply-To\ Address=Direcci�n para la respuesta
Test\ e-mail\ recipient=Direcci�n para el correo de prueba
Sender\ E-mail\ Address=Direcci�n de remitente
Test\ configuration=Configuraci�n de pruebas
Test\ configuration\ by\ sending\ test\ e-mail=Probar la configuraci�n enviando un correo de prueba
Reply-To\ Address=Direcci�n para la respuesta
Test\ e-mail\ recipient=Direcci�n para el correo de prueba
Sender\ E-mail\ Address=Direcci�n de remitente
Test\ configuration=Configuraci�n de pruebas
3 changes: 0 additions & 3 deletions src/main/resources/hudson/tasks/Mailer/global_fr.properties
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,5 @@ Default\ user\ e-mail\ suffix=Suffixe par d\u00E9faut des emails des utilisateur
Sender\ E-mail\ Address=Adresse e-mail de l''''exp\u00E9diteur

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In l''''exp\u00E9diteur there are two extra '' which should be removed.

System\ Admin\ E-mail\ Address=Adresse email de l''administrateur syst\u00cbme
Jenkins\ URL=URL de Jenkins
User\ Name=Nom d''utilisateur
Password=Mot de passe
Use\ SSL=Utiliser SSL
Use\ SMTP\ Authentication=Utiliser l''authentification par SMTP
SMTP\ Port=Port SMTP
Loading