Changeset 5442


Ignore:
Timestamp:
Oct 7, 2010, 8:48:16 AM (11 years ago)
Author:
Nicklas Nordborg
Message:

Fixes #1524: Send messages as emails

Location:
trunk
Files:
5 added
2 deleted
18 edited

Legend:

Unmodified
Added
Removed
  • trunk/config/dist/base.config

    r5416 r5442  
    154154# that has been updated (default: false)
    155155# changelog.dblogger.detailed-properties = true
     156
     157# ==========================
     158# SMTP server section
     159# ==========================
     160
     161# SMTP server for outgoing mail. If not specified,
     162# mail sending will be disabled
     163# mail.server.host
     164
     165# The port number the SMTP server is listening on. If not
     166# specified the default port is used (25 or 465 if ssl is enabled)
     167# mail.server.port = 25
     168
     169# A flag to enable SSL encryption
     170# mail.server.ssl = 0
     171
     172# A flag to enable TLS encryption
     173# mail.server.tls = 0
     174
     175# Email to use in the "From" address. If not specified,
     176# mail sending will be disabled
     177# mail.from.email = noreply@mail.yourdomain
     178
     179# Name to use in the "From" address. If not specified,
     180# a default value is created automatically using the
     181# host name of the BASE server.
     182# mail.from.name
     183
    156184
    157185# ===============
  • trunk/doc/3rd-party-components.txt

    r5428 r5442  
    174174Jar files : httpclient-4.0.3.jar, httpcore-4.0.1.jar, commons-codec-1.3.jar,
    175175            commons-logging-1.1.1.jar
     176
     177Apache Commons Email
     178---------------------
     179Package for sending email. Depends on JavaMail 1.4.
     180
     181More info : http://commons.apache.org/email/
     182Version   : 1.2
     183License   : Apache License 2.0 (apache.license-2.0.txt)
     184Jar files : commons-email-1.2.jar
     185
     186
     187JavaMail
     188---------
     189Low-level package for sending email. Used by Apache commons email.
     190
     191More info : http://www.oracle.com/technetwork/java/index-jsp-139225.html
     192Version   : 1.4.3
     193License   : Sun (mail-LICENSE.txt)
     194Jar files : mail-1.4.3.jar
    176195
    177196
  • trunk/doc/src/docbook/appendix/base.config.xml

    r5409 r5442  
    628628  </simplesect>
    629629 
     630    <simplesect id="appendix.base.config.smtp">
     631    <title>SMTP server section</title>
     632   
     633    <para>
     634      This section contains settings for the SMTP server used for
     635      outgoing mail. This is optional, and if not configured outgoing
     636      mail will be disabled.
     637    </para>
     638   
     639    <variablelist>
     640    <varlistentry>
     641      <term><property>mail.server.host</property></term>
     642      <listitem>
     643        <para>
     644        The host name of the SMTP server to use for outgoing mail.
     645        If not configured mailing functions will be disabled.
     646          </para>
     647      </listitem>
     648    </varlistentry>
     649    <varlistentry>
     650      <term><property>mail.server.port</property></term>
     651      <listitem>
     652        <para>
     653        The port the SMTP server is listening on. If not configured a
     654        default port is used. Eg. 25 for regular mail server, 465 for
     655        SSL mail server.
     656          </para>
     657      </listitem>
     658    </varlistentry>
     659    <varlistentry>
     660      <term><property>mail.server.ssl</property></term>
     661      <listitem>
     662        <para>
     663        A boolean value that specifies if the SMTP server is using
     664        SSL or not.
     665        </para>
     666      </listitem>
     667    </varlistentry>
     668    <varlistentry>
     669      <term><property>mail.server.tls</property></term>
     670      <listitem>
     671        <para>
     672        A boolean value that specifies if the SMTP server is using
     673        TLS or not.
     674        </para>
     675      </listitem>
     676    </varlistentry>
     677    <varlistentry>
     678      <term><property>mail.from.email</property></term>
     679      <listitem>
     680        <para>
     681        The email address that will be used as the sender of outgoing
     682        emails. If not configured mailing functions will be disabled.
     683        </para>
     684      </listitem>
     685    </varlistentry>
     686    <varlistentry>
     687      <term><property>mail.from.name</property></term>
     688      <listitem>
     689        <para>
     690        Thename that will be used as the sender of outgoing
     691        emails. If not configured, a default name is automatically
     692        generated using the host name of the BASE server.
     693        </para>
     694      </listitem>
     695    </varlistentry>
     696    </variablelist>
     697   
     698  </simplesect>
     699 
     700 
    630701  <simplesect id="appendix.base.config.secondary.other">
    631702    <title>Other settings</title>
  • trunk/doc/src/docbook/userdoc/webclient.xml

    r5370 r5442  
    410410              <listitem>
    411411                <para>
    412                 Your email address (optional).
     412                Your email address (optional). If an email has been specified and if the
     413                server administrator has enabled email notifications, you also have the
     414                option to select if messages should be sent as emails. This can be useful
     415                to keep track of jobs that take a long time to complete.
    413416                </para>
    414417              </listitem>
  • trunk/src/clients/web/net/sf/basedb/clients/web/util/HTML.java

    r5326 r5442  
    2424
    2525import java.util.regex.*;
     26
     27import net.sf.basedb.util.EmailUtil;
    2628
    2729/**
     
    6062    not check that the domain or email address actually exists.
    6163  */
    62   public static final Pattern EMAIL_REGEXP = Pattern.compile("[\\p{L}\\d_-]+(\\.[\\p{L}\\d_-]+)*@[\\p{L}\\d_-]+(\\.[\\p{L}\\d_-]+)+");
     64  public static final Pattern EMAIL_REGEXP = EmailUtil.EMAIL_REGEXP;
    6365
    6466  /**
     
    108110  public static boolean isValidEmail(String email)
    109111  {
    110     if (email == null) return false;
    111     Matcher m = EMAIL_REGEXP.matcher(email);
    112     return m.matches();
     112    return EmailUtil.isValidEmail(email);
    113113  }
    114114
  • trunk/src/core/common-queries.xml

    r5434 r5442  
    38973897  </query>
    38983898
     3899  <query id="SET_SEND_MESSAGES_AS_EMAIL_FOR_USERS" type="HQL">
     3900    <sql>
     3901      UPDATE UserData usr
     3902      SET usr.sendMessagesAsEmail = :sendMessagesAsEmail
     3903      WHERE usr.sendMessagesAsEmail IS NULL
     3904    </sql>
     3905    <description>
     3906      An HQL update-query that sets the 'sendMessagesAsEmail' flag
     3907      for all users where it is null.
     3908    </description>
     3909  </query>
     3910
     3911
    38993912</predefined-queries>
  • trunk/src/core/net/sf/basedb/core/Application.java

    r5384 r5442  
    3030import net.sf.basedb.core.authentication.Authenticator;
    3131import net.sf.basedb.util.ClassUtil;
     32import net.sf.basedb.util.EmailUtil;
    3233import net.sf.basedb.util.FileUtil;
    3334import net.sf.basedb.util.RegexpFileFilter;
     
    469470      BatchUtil.init();
    470471      Metadata.init();
     472      EmailUtil.init();
    471473 
    472474      if (verifySchemaVersion)
     
    642644    SystemItems.unload();
    643645    Metadata.unload();
     646    EmailUtil.unload();
    644647    BatchUtil.unload();
    645648    PredefinedQuery.unload();
  • trunk/src/core/net/sf/basedb/core/Install.java

    r5434 r5442  
    115115    method.
    116116  */
    117   public static final int NEW_SCHEMA_VERSION = Integer.valueOf(86).intValue();
     117  public static final int NEW_SCHEMA_VERSION = Integer.valueOf(87).intValue();
    118118 
    119119  public static synchronized void createTables(boolean update, final ProgressReporter progress)
  • trunk/src/core/net/sf/basedb/core/Job.java

    r5406 r5442  
    2626import net.sf.basedb.core.data.ExperimentData;
    2727import net.sf.basedb.core.data.JobData;
    28 import net.sf.basedb.core.data.MessageData;
    2928import net.sf.basedb.core.data.ParameterValueData;
    3029import net.sf.basedb.core.data.PluginConfigurationData;
     
    15431542  private void sendMessage()
    15441543  {
    1545     MessageData message = new MessageData();
    1546     JobData data = getData();
    1547     message.setTo(data.getOwner());
    1548     message.setFrom("SYSTEM");
    1549     message.setTimeSent(new Date());
    1550     message.setJob(data);
    1551     message.setDescription(data.getStatusMessage());
    1552    
     1544    DbControl dc = getDbControl();
     1545    Message m = Message.getNew(dc, getOwner(), "SYSTEM", null, this);
     1546    m.setDescription(getStatusMessage());
     1547    String jobName = net.sf.basedb.util.Values.trim(getName(), Message.MAX_NAME_LENGTH - 20);
    15531548    if (getStatus() == Job.Status.ERROR)
    15541549    {
    1555       message.setName(net.sf.basedb.util.Values.trim("Error: " + getName(),
    1556         Message.MAX_NAME_LENGTH));
     1550      m.setName("Job failed: '" + jobName + "'");
    15571551    }
    15581552    else
    15591553    {
    1560       message.setName(net.sf.basedb.util.Values.trim("Done: " + getName(),
    1561         Message.MAX_NAME_LENGTH));
    1562     }
    1563     HibernateUtil.saveData(getDbControl().getHibernateSession(), message);
     1554      m.setName("Job done: '" + jobName + "'");
     1555    }
     1556    dc.saveItem(m);
    15641557  }
    15651558 
  • trunk/src/core/net/sf/basedb/core/Message.java

    r4889 r5442  
    2424
    2525import net.sf.basedb.core.data.MessageData;
     26import net.sf.basedb.core.data.UserData;
    2627import net.sf.basedb.core.query.Restrictions;
    2728import net.sf.basedb.core.query.Hql;
     29import net.sf.basedb.util.EmailUtil;
    2830
    2931import java.util.Date;
     32
     33import org.apache.commons.mail.Email;
     34import org.apache.commons.mail.EmailException;
     35
    3036
    3137/**
     
    150156  }
    151157
     158  private boolean sendAsEmail;
     159 
    152160  Message(MessageData messageData)
    153161  {
     
    223231  }
    224232  // -------------------------------------------
     233
     234  /**
     235    Send the message as an email if requested by the user.
     236  */
     237  @Override
     238  void onAfterCommit(Transactional.Action action)
     239  {
     240    super.onAfterCommit(action);
     241   
     242    if (action == Transactional.Action.CREATE)
     243    {
     244      if (sendAsEmail)
     245      {
     246        try
     247        {
     248          this.sendAsEmail();
     249        }
     250        catch (RuntimeException ex)
     251        {
     252          Application.getLogger().warn(ex.getMessage(), ex);
     253        }
     254      }
     255    }
     256   
     257  }
     258
    225259
    226260  /**
     
    246280    if (to == null) throw new InvalidUseOfNullException("to");
    247281    getData().setTo(to.getData());
     282    sendAsEmail = EmailUtil.isEnabled(to);
    248283  }
    249284 
     
    353388  }
    354389 
     390  /**
     391    Send this message as an email. This method call is ignored
     392    if the server admin has not configured an outgoing SMTP
     393    server.
     394    @since 2.16
     395    @see EmailUtil#isEnabled()
     396  */
     397  public void sendAsEmail()
     398  {
     399    if (!EmailUtil.isEnabled()) return;
     400    try
     401    {
     402      Email email = EmailUtil.createSimpleEmail();
     403      UserData user = getData().getTo();
     404      email.addTo(user.getEmail(), user.getName(), "UTF-8");
     405      email.setSubject(getName());
     406      String message = getDescription();
     407      if (message != null && message.length() > 0)
     408      {
     409        email.setMsg(message);
     410      }
     411      email.send();
     412    }
     413    catch (EmailException ex)
     414    {
     415      throw new BaseException("Could not send email for message:" + this, ex);
     416    }
     417  }
     418 
    355419}
  • trunk/src/core/net/sf/basedb/core/Update.java

    r5434 r5442  
    6565import net.sf.basedb.core.data.TimestampParameterValueData;
    6666import net.sf.basedb.core.data.TransformationData;
     67import net.sf.basedb.core.data.UserData;
    6768import net.sf.basedb.core.data.keyring.PluginKeys;
    6869import net.sf.basedb.core.dbengine.DbEngine;
     
    931932      all settings ending with '.recent' that contains at least one colon (:).
    932933      See http://base.thep.lu.se/ticket/1517 for more information.
     934    </td>
     935  </tr>
     936  <tr>
     937    <td>87</td>
     938    <td>
     939      Adds {@link UserData#getSendMessagesAsEmail()}. The update will set
     940      the value to FALSE for all existing users.
    933941    </td>
    934942  </tr>
     
    12831291            "--Updating schema version: " + schemaVersion + " -> 86...");
    12841292        schemaVersion = updateToSchemaVersion86(session);
     1293      }
     1294     
     1295      if (schemaVersion < 87)
     1296      {
     1297        if (progress != null) progress.display((int)(86*progress_factor),
     1298            "--Updating schema version: " + schemaVersion + " -> 87...");
     1299        schemaVersion = setSchemaVersionInTransaction(session, 87);
    12851300      }
    12861301
     
    35683583        }
    35693584       
     3585        if (schemaVersion < 87)
     3586        {
     3587          // Set sendMessagesAsEmail=false on all users
     3588          org.hibernate.Query query = HibernateUtil.getPredefinedQuery(session, "SET_SEND_MESSAGES_AS_EMAIL_FOR_USERS");
     3589          /*
     3590            UPDATE UserData usr
     3591            SET usr.sendMessagesAsEmail = :sendMessagesAsEmail
     3592            WHERE usr.sendMessagesAsEmail IS NULL
     3593          */
     3594          query.setBoolean("sendMessagesAsEmail", false);
     3595          query.executeUpdate();
     3596        }
     3597       
    35703598        //  Commit the changes
    35713599        HibernateUtil.commit(tx);
  • trunk/src/core/net/sf/basedb/core/User.java

    r5345 r5442  
    728728  }
    729729
     730  /**
     731    If this flag has been set, messges to the user is also sent as an
     732    email. This requires that the server admin has configured an outgoing
     733    smtp server and that this user has specified a valid email address.
     734    @return TRUE to send messages as email, FALSE if not
     735    @since 2.16
     736  */
     737  public boolean getSendMessagesAsEmail()
     738  {
     739    return getData().getSendMessagesAsEmail();
     740  }
     741  /**
     742    Set a flag indicating that all messages should also be sent as
     743    email. See {@link #getSendMessagesAsEmail()} for information
     744    about restrictions.
     745    @throws PermissionDeniedException If the logged in user doesn't have
     746      write permission
     747    @since 2.16
     748  */
     749  public void setSendMessagesAsEmail(boolean sendMessagesAsEmail)
     750  {
     751    checkPermission(Permission.RESTRICTED_WRITE);
     752    getData().setSendMessagesAsEmail(sendMessagesAsEmail);
     753  }
     754 
    730755  /**
    731756    The maximum length of the url that can be stored in the database.
  • trunk/src/core/net/sf/basedb/core/data/UserData.java

    r4889 r5442  
    349349  }
    350350
     351  private boolean sendMessagesAsEmail;
     352  /**
     353    If this flag is set, all messages to this user are sent as an email
     354    also, but only if: the server admin has configured an outgoing smtp
     355    server and the user has supplied a valid email address.
     356   
     357    NOTE! This property is mapped in hibernate-properties-UserData.xml since
     358    'default' is not supported by XDoclet
     359    //hibernate.property type="boolean"
     360    //hibernate.column name="`send_messages_as_email`" not-null="true" default="false"
     361    @since 2.16
     362  */
     363  public boolean getSendMessagesAsEmail()
     364  {
     365    return sendMessagesAsEmail;
     366  }
     367  public void setSendMessagesAsEmail(boolean sendMessagesAsEmail)
     368  {
     369    this.sendMessagesAsEmail = sendMessagesAsEmail;
     370  }
     371 
    351372  /**
    352373    The maximum length of the url that can be stored in the database.
  • trunk/src/test/TestMessage.java

    r5340 r5442  
    2222*/
    2323import net.sf.basedb.core.*;
     24import net.sf.basedb.util.EmailUtil;
    2425
    2526import java.util.Date;
     
    6768      dc = TestUtil.getDbControl();
    6869      User user = User.getById(dc, toUserId);
     70      if (EmailUtil.isEnabled())
     71      {
     72        user.setEmail(Config.getString("mail.from.email"));
     73        user.setSendMessagesAsEmail(true);
     74      }
    6975      Message m = Message.getNew(dc, user, from, null, null);
    7076      if (setAll)
  • trunk/www/common/plugin/finish_job.jsp

    r5426 r5442  
    3737  import="net.sf.basedb.core.Job"
    3838  import="net.sf.basedb.core.JobAgent"
     39  import="net.sf.basedb.core.User"
    3940  import="net.sf.basedb.core.PluginResponse"
    4041  import="net.sf.basedb.core.query.Hql"
     
    4748  import="net.sf.basedb.core.plugin.InteractivePlugin"
    4849  import="net.sf.basedb.core.plugin.AutoDetectingImporter"
     50  import="net.sf.basedb.util.EmailUtil"
     51  import="net.sf.basedb.util.Values"
    4952  import="net.sf.basedb.clients.web.Base"
    50   import="net.sf.basedb.util.Values"
    5153  import="net.sf.basedb.clients.web.util.HTML"
    5254  import="java.util.Set"
     
    6870  PluginConfiguration pluginConfig = job.getPluginConfiguration();
    6971  boolean sendMessage = Values.getBoolean(sc.getUserClientSetting("plugins.sendmessage"), true);
     72  boolean sendMessageAsEmail = EmailUtil.isEnabled(User.getById(dc, sc.getLoggedInUserId()));
    7073  boolean removeJobWhenFinished = Values.getBoolean(sc.getUserClientSetting("plugins.removejob"), false);
    7174
     
    168171        <td>
    169172          <input type="checkbox" name="send_message" value="1" <%=sendMessage ? "checked" : "" %>>
    170           <a href="javascript:document.forms['plugin'].send_message.click();">Send a message when the job is completed</a>
     173          <a href="javascript:document.forms['plugin'].send_message.click();">Send a
     174          message<%=sendMessageAsEmail ? "+email" : "" %> when the job is completed</a>
    171175        </td>
    172176      </tr>
  • trunk/www/include/menu.jsp

    r5426 r5442  
    537537      <m:menuitem
    538538        title="<%=menu.getString("contactinfo.title") + "…"%>"
    539         onclick="<%="Main.openPopup('"+root+"my_base/user/settings.jsp?ID="+ID+"&page=contact', 'Settings', 500, 360);"%>"
     539        onclick="<%="Main.openPopup('"+root+"my_base/user/settings.jsp?ID="+ID+"&page=contact', 'Settings', 600, 400);"%>"
    540540        tooltip="<%=menu.getString("contactinfo.tooltip")%>"
    541541      />
    542542      <m:menuitem
    543543        title="<%=menu.getString("password.title") + "…"%>"
    544         onclick="<%="Main.openPopup('"+root+"my_base/user/settings.jsp?ID="+ID+"&page=password', 'Settings', 500, 360);"%>"
     544        onclick="<%="Main.openPopup('"+root+"my_base/user/settings.jsp?ID="+ID+"&page=password', 'Settings', 600, 400);"%>"
    545545        tooltip="<%=menu.getString("password.tooltip")%>"
    546546      />
    547547      <m:menuitem
    548548        title="<%=menu.getString("other.title") + "…"%>" 
    549         onclick="<%="Main.openPopup('"+root+"my_base/user/settings.jsp?ID="+ID+"&page=other', 'Settings', 500, 360);"%>"
     549        onclick="<%="Main.openPopup('"+root+"my_base/user/settings.jsp?ID="+ID+"&page=other', 'Settings', 600, 400);"%>"
    550550        tooltip="<%=menu.getString("other.tooltip")%>"
    551551      />
  • trunk/www/my_base/user/settings.jsp

    r5426 r5442  
    3838<%@ page pageEncoding="UTF-8" session="false"
    3939  import="net.sf.basedb.core.*"
     40  import="net.sf.basedb.util.EmailUtil"
    4041  import="net.sf.basedb.clients.web.*"
    4142  import="net.sf.basedb.clients.web.util.HTML"
     
    7374  String htmlDateFormat = HTML.encodeTags(dateFormat);
    7475  %>
    75 
    7676  <base:page type="popup" title="<%="Information for "+HTML.encodeTags(user.getName())%>">
    7777  <base:head scripts="tabcontrol.js" styles="tabcontrol.css">
     
    183183      <td><input <%=clazz%> type="text" name="email" value="<%=HTML.encodeTags(user.getEmail())%>" size="40" maxlength="<%=User.MAX_EMAIL_LENGTH%>"></td>
    184184    </tr>
     185    <%
     186    if (EmailUtil.isEnabled() && writePermission)
     187    {
     188      %>
     189      <tr>
     190        <td class="prompt"></td>
     191        <td>
     192          <%
     193          boolean sendMessagesAsEmail = user.getSendMessagesAsEmail();
     194          %>
     195          <input type=checkbox name="sendMessagesAsEmail" value="1" <%=sendMessagesAsEmail ? "checked" : ""%>
     196            ><a href="javascript:document.forms['user'].sendMessagesAsEmail.click()">Send system messages as email</a>
     197        </td>
     198      </tr>
     199      <%
     200    }
     201    %>
    185202    <tr>
    186203      <td class="prompt">Organisation</td>
  • trunk/www/my_base/user/submit_user.jsp

    r5426 r5442  
    3737  import="net.sf.basedb.core.User"
    3838  import="net.sf.basedb.core.ItemContext"
     39  import="net.sf.basedb.util.EmailUtil"
    3940  import="net.sf.basedb.clients.web.Base"
    4041  import="net.sf.basedb.clients.web.WebException"
     
    7071    // Contact information tab
    7172    user.setEmail(email);
     73    if (EmailUtil.isEnabled())
     74    {
     75      user.setSendMessagesAsEmail(email != null && Values.getBoolean(request.getParameter("sendMessagesAsEmail")));
     76    }
    7277    user.setOrganisation(Values.getStringOrNull(request.getParameter("organisation")));
    7378    user.setAddress(Values.getStringOrNull(request.getParameter("address")));
Note: See TracChangeset for help on using the changeset viewer.