14 Replies Latest reply on Apr 30, 2014 8:24 AM by Heiko Rupp

    How to extend the alert-email plugin for sending contents?

    Thomas Chang Newbie

      Hi,

       

      In JON there is a server-plugin "alert-email". The problem by this plugin is, you can just configure the email address, but not the title and even worse you can't send your own contents. The contents is fixed in a file called "email-template.txt". This means, no matter what an alert is, the content is always the same.

        • 1. Re: How to extend the alert-email plugin for sending contents?
          Thomas Chang Newbie

          The contend I mean here the body of the email.

          • 2. Re: How to extend the alert-email plugin for sending contents?
            Thomas Segismont Expert

            How about creating your own email sender plugin? You can start by copying the built-in plugin code, and enhance the email generation part. Remember that you need to update the plugin descriptor to let the server differentiate your plugin from the built-in one.

            • 3. Re: How to extend the alert-email plugin for sending contents?
              Stian Lund Expert

              Well, you can always just edit the template like a text file and make it look however you'd like. For instance the default one is really big, and we need to convert email to SMS messages, so I've trimmed out a lot leaving just the essential data.

               

              I think the email subject is there as well but been a while since I've looked at it.

              Just make sure to  make a backup as it does get overwritten on server update.

               

              Wish it was actually configurable in the Admin UI somewhere though, hint, hint

              • 4. Re: How to extend the alert-email plugin for sending contents?
                Thomas Chang Newbie

                I thing editing the template like a text file is not a good idea since this file is the only one template file. What I want is: when I define an alert, one can define his body.

                • 5. Re: How to extend the alert-email plugin for sending contents?
                  Thomas Chang Newbie

                  Hi,

                   

                  There are several plugin codes. I see their structures are different. Which one you thing the better one I should take?

                  • 6. Re: How to extend the alert-email plugin for sending contents?
                    Heiko Rupp Master

                    Am 11.04.2014 um 10:45 schrieb Stian Lund <do-not-reply@jboss.com>:

                    Wish it was actually configurable in the Admin UI somewhere though, hint, hint

                     

                    Which "it" ?

                     

                      Heiko

                     

                    P.S.: Patches are always welcome

                    • 7. Re: How to extend the alert-email plugin for sending contents?
                      Heiko Rupp Master

                      Hey,

                       

                      Am 16.04.2014 um 04:23 schrieb Thomas Chang <do-not-reply@jboss.com>:

                       

                      There are several plugin codes. I see their structures are different. Which one you thing the better one I should take?

                       

                      Directly look at the AlertEmail sender.

                       

                      The idea is I think that you should in the plugin descriptor add a field (long string) to the

                      alert-configuration, which is the field that is per alert

                       

                      Like

                          <!-- What can a user configure when defining an alert -->

                          <alert-configuration>

                              <c:simple-property name="emailAddress" displayName="Receiver Email Address(es)" type="longString"

                                      description="Email addresses (separated by comma) used for notifications.">

                               ...

                               

                              <c:simple-property name="template" type="longString" required="false" />  <<<<<<

                      • 8. Re: How to extend the alert-email plugin for sending contents?
                        Heiko Rupp Master

                        Crap, the email->forum gateway has cut off some stuff :-(

                         

                        The above continues:

                         

                        That template could either be a string that has the tokens like the original or a name of a file in a directory

                        (in this case one needs to be careful in HA scenarios to copy that template-file into all servers)

                         

                        This field is then accessible in the send() method like this:

                         

                           String templateName = alertParameters.getSimpleValue("template");

                         

                        Now the org.rhq.enterprise.server.plugins.alertEmail.EmailSender#send internally calls

                        org.rhq.enterprise.server.alert.AlertManagerBean#sendAlertNotificationEmails which unfortunately does

                        the sender plugin not make standalone. This internally calls org.rhq.enterprise.server.core.EmailManagerBean#getAlertEmailMessage

                        to read the template code.

                        So the alert sender would need to pass in the template path or template string from above property and

                        pass that through to org.rhq.enterprise.server.core.EmailManagerBean#getAlertEmailMessage to be used instead

                        of the default. If nothing is passed the solution can just fall back to what is already there.

                         

                        Hope that helps

                          Heiko

                        • 9. Re: How to extend the alert-email plugin for sending contents?
                          Thomas Chang Newbie

                          Hi,

                           

                          Thanks very much for the information and sorry for the late reply bcz. I have vacation. :-)

                           

                          I wonder how the variable 'templateName ' be used/called after it's defined so that it could be passed to the org.rhq.enterprise.server.core.EmailManagerBean#getAlertEmailMessage?

                          • 10. Re: Re: How to extend the alert-email plugin for sending contents?
                            Heiko Rupp Master

                            Hey,

                            you could use the variable to e.g. load a different template from the file system a little bit like this is done for the Alert-Scriptlang

                            sender to load themplates relative to $SERVER/alert-email-templates

                             

                                        String baseDir = System.getProperty("rhq.server.home") +

                                            File.separator +

                                            "alert-email-templates";

                                        templatePath = baseDir + File.separator + templateName;


                            If the templateName is non null. Otherwise just use the default.

                            Then load the respective template file and proceed as in the existing case.

                            • 11. Re: Re: How to extend the alert-email plugin for sending contents?
                              Thomas Chang Newbie

                              Hi,

                               

                              I understand the code here. But I still don't understand 100% how to "load the respective template file and proceed as in the existing case".

                               

                              Where and how can I load the variable 'templateName'? And what is the "existing case" which I should process?

                              • 12. Re: Re: Re: How to extend the alert-email plugin for sending contents?
                                Heiko Rupp Master

                                Add the template name or path to template to the signature of org.rhq.enterprise.server.core.EmailManagerBean#getAlertEmailMessage then in the code do something like

                                 

                                        if (templatePath!=null) {

                                            try {

                                                templateStream =new FileInputStream(templatePath);

                                          template = new String(StreamUtil.slurp(templateStream));

                                    } else {

                                      use existing code to load the default template

                                  }

                                 

                                Hth

                                 

                                (and send a pull-request when done

                                • 13. Re: Re: Re: How to extend the alert-email plugin for sending contents?
                                  Thomas Chang Newbie

                                  Hi,

                                   

                                  I thing I did what you told me. But it doesn't work perfectly. Here is my code. I still get the body from default file "alert-email-template.txt". Could you please have a look? It's based on your EmailSender class:

                                   

                                  EmailSender.java

                                  ++++++++++++++

                                  public class EmailSender extends AlertSender {

                                   

                                      private final Log log = LogFactory.getLog(EmailSender.class);

                                   

                                   

                                      @Override

                                      public SenderResult send(Alert alert) {

                                          String emailAddressString = alertParameters.getSimpleValue("emailAddress", null);

                                          String templateString = alertParameters.getSimpleValue("template");

                                          log.info("templateString = " + templateString);

                                  //        String baseDir = System.getProperty("rhq.server.home") + File.separator + "alert-email-templates";

                                          String baseDir = "/tmp" + File.separator + "test";

                                          log.info("baseDir = " + baseDir);

                                          String templatePath = baseDir + File.separator + templateString;

                                          log.info("templatePath = " + templatePath);

                                          String template = null;

                                         

                                          if (emailAddressString == null) {

                                              return SenderResult.getSimpleFailure("No email address given");

                                          }

                                   

                                   

                                          List<String> emails = AlertSender.unfence(emailAddressString, String.class, ",");

                                          try {

                                              Set<String> uniqueEmails = new HashSet<String>(emails);

                                              Collection<String> badEmails = LookupUtil.getAlertManager()

                                                  .sendAlertNotificationEmails(alert, uniqueEmails);

                                   

                                   

                                              List<String> goodEmails = new ArrayList<String>(uniqueEmails);

                                              goodEmails.removeAll(badEmails);

                                   

                                              SenderResult result = new SenderResult();

                                              result.setSummary("Target addresses were: " + uniqueEmails);

                                              if (goodEmails.size() > 0) {

                                                  result.addSuccessMessage("Successfully sent to: " + goodEmails);

                                              }

                                              if (badEmails.size() > 0) {

                                                  result.addFailureMessage("Failed to send to: " + badEmails);

                                              }

                                              if (templatePath != null) {

                                    try {

                                    FileInputStream templateStream = new FileInputStream(templatePath);

                                    template = new String(StreamUtil.slurp(templateStream));

                                    log.info("template = " + template);

                                    } catch (Exception e) {

                                    e.getStackTrace();

                                    log.error("templatePath = NULL");

                                    }

                                    }

                                             

                                              Map<String, String> message = new HashMap<String, String>(1);

                                              message.put(emailAddressString, template);

                                             

                                              return result;

                                          } catch (Throwable t) {

                                              return SenderResult.getSimpleFailure("Error sending email notifications to " + emails + ", cause: "

                                                  + t.getMessage());

                                          }

                                         

                                      }

                                   

                                   

                                      @Override

                                      public String previewConfiguration() {

                                          String emailAddressString = alertParameters.getSimpleValue("emailAddress", null);

                                          if (emailAddressString == null || emailAddressString.trim().length() == 0) {

                                              return "<empty>";

                                          }

                                          return emailAddressString;

                                      }

                                  }

                                   

                                   

                                  rhq-serverplugin.xml

                                  ++++++++++++++++

                                  <?xml version="1.0" encoding="UTF-8" ?>

                                   

                                   

                                  <alert-plugin

                                          name="alert-my-email"

                                          version="1.0"

                                          displayName="Alert:My Email"

                                          xmlns="urn:xmlns:rhq-serverplugin.alert"

                                          xmlns:c="urn:xmlns:rhq-configuration"

                                          xmlns:serverplugin="urn:xmlns:rhq-serverplugin"

                                          package="org.rhq.enterprise.server.plugins.alertEmail"

                                          description="Alert sender plugin that sends alert notifications via email"

                                          >

                                   

                                   

                                      <serverplugin:help>

                                          Used to send notifications to direct email addresses.

                                      </serverplugin:help>

                                   

                                   

                                   

                                   

                                      <!-- How does this sender show up in drop downs etc -->

                                      <short-name>My Direct Emails</short-name>

                                   

                                   

                                      <!-- Class that does the actual sending -->

                                      <plugin-class>EmailSender</plugin-class>

                                   

                                   

                                      <!-- What can a user configure when defining an alert -->

                                      <alert-configuration>

                                          <c:simple-property name="emailAddress" displayName="Receiver Email Address(es)" type="longString"

                                                  description="Email addresses (separated by comma) used for notifications.">

                                              <c:constraint>

                                                  <!-- a real email regex that validates all possible email address would be thousands of chars long -->

                                                  <!-- here we do just a very basic validation of comma-separated str1@str2 -->

                                                  <c:regex-constraint expression=".+@[^@, ]+(,.+@[^,@ ]+)?"/>

                                              </c:constraint>

                                          </c:simple-property>

                                          <c:simple-property name="template" type="longString" required="false" />

                                      </alert-configuration>

                                   

                                   

                                  </alert-plugin>

                                  • 14. Re: Re: Re: How to extend the alert-email plugin for sending contents?
                                    Heiko Rupp Master

                                    I above code you have

                                     

                                    Collection<String> badEmails = LookupUtil.getAlertManager()

                                                    .sendAlertNotificationEmails(alert, uniqueEmails);

                                     

                                    Which is calling into the server to do the real sending.

                                     

                                    You need to modify this to pass the template like

                                     

                                    Collection<String> badEmails = LookupUtil.getAlertManager()

                                                    .sendAlertNotificationEmails(alert, uniqueEmails, template);

                                     

                                    and move the template fetching code before this line.

                                     

                                    Then inside AlertManagerBean.sendAlertNotificationEmails() you need to pass the template to the call to the org.rhq.enterprise.server.core.EmailManagerBean#getAlertEmailMessage() and use your template instead of the default one that is defined in there.

                                    If your template is null, you should fall back to using the default though.