View Javadoc
1   /**
2    * This Source Code Form is subject to the terms of the Mozilla Public
3    * License, v. 2.0. If a copy of the MPL was not distributed with this
4    * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5    *
6    * If it is not possible or desirable to put the notice in a particular
7    * file, then You may include the notice in a location (such as a LICENSE
8    * file in a relevant directory) where a recipient would be likely to look
9    * for such a notice.
10  
11   * 
12   */
13   
14  /*  ---------------------------------------------------------------------------
15   *  U.S. Government, Department of the Army
16   *  Army Materiel Command
17   *  Research Development Engineering Command
18   *  Communications Electronics Research Development and Engineering Center
19   *  ---------------------------------------------------------------------------
20   */
21  
22  package org.miloss.fgsms.sla.actions;
23  
24  import java.util.ArrayList;
25  import java.util.List;
26  import java.util.Properties;
27  import java.util.concurrent.atomic.AtomicReference;
28  import javax.mail.Message;
29  import javax.mail.Session;
30  import javax.mail.Transport;
31  import javax.mail.internet.InternetAddress;
32  import javax.mail.internet.MimeMessage;
33  import org.apache.log4j.Level;
34  import org.miloss.fgsms.common.Logger;;
35  import org.miloss.fgsms.common.Utility;
36  import org.miloss.fgsms.plugins.sla.AlertContainer;
37  import org.miloss.fgsms.plugins.sla.SLAActionInterface;
38  import org.miloss.fgsms.services.interfaces.common.NameValuePair;
39  import org.miloss.fgsms.services.interfaces.common.PolicyType;
40  import org.miloss.fgsms.services.interfaces.policyconfiguration.SLAAction;
41  import org.miloss.fgsms.sla.SLACommon;
42  
43  /**
44   *
45   * @author AO
46   */
47  public class EmailAlerter implements SLAActionInterface {
48  
49      private static Logger log = Logger.getLogger("fgsms.SLAProcessor");
50  
51    //  @Override
52      public void ProcessAction(AlertContainer alert) {
53          SendMailAlert(alert.getFaultMsg(), alert.getSlaActionBaseType(), alert.getSLAID(), alert.isPooled(), alert.getModifiedurl());
54      }
55  
56      private static void SendMailAlert(String htmlencodedTest, SLAAction email, String slaguid, boolean pooled, String policyurl) {
57          Properties props = null;
58          if (pooled) {
59              props = SLACommon.LoadSLAPropertiesPooled();
60          } else {
61              props = SLACommon.LoadSLAPropertiesNotPooled();
62          }
63          InternetAddress[] subscribers = null;
64          if (pooled) {
65              subscribers = SLACommon.GetSubscribersPooled(slaguid);
66          } else {
67              subscribers = SLACommon.GetSubscribersNotPooled(slaguid);
68          }
69          if (subscribers != null && subscribers.length > 0) {
70              try {
71                  //get all subscribers
72  
73                  Session mailSession = Session.getDefaultInstance(props);
74                  Message simpleMessage = new MimeMessage(mailSession);
75                  InternetAddress from;
76                  NameValuePair nvfrom = Utility.getNameValuePairByName(email.getParameterNameValue(), "From");
77                  String sfrom = null;
78                  if (nvfrom != null) {
79                      if (nvfrom.isEncrypted()) {
80                          sfrom = Utility.DE(nvfrom.getValue());
81                      } else {
82                          sfrom = nvfrom.getValue();
83                      }
84                  }
85                  NameValuePair nvsubject = Utility.getNameValuePairByName(email.getParameterNameValue(), "Subject");
86                  String subject = nvsubject.getValue();
87                  if (nvsubject.isEncrypted()) {
88                      subject = Utility.DE(nvsubject.getValue());
89                  }
90  
91                  NameValuePair nvbody = Utility.getNameValuePairByName(email.getParameterNameValue(), "Body");
92                  String body = nvbody.getValue();
93                  if (nvbody.isEncrypted()) {
94                      body = Utility.DE(nvbody.getValue());
95                  }
96                  if (nvfrom == null || Utility.stringIsNullOrEmpty(sfrom)) {
97                      from = new InternetAddress(props.getProperty("defaultReplyAddress"));
98                  } else {
99                      from = new InternetAddress(sfrom);
100                 }
101                 for (int i2 = 0; i2 < subscribers.length; i2++) {
102                     try {
103                         simpleMessage.setFrom(from);
104                         simpleMessage.setRecipient(Message.RecipientType.TO, subscribers[i2]);
105                         simpleMessage.setSubject(subject);
106                         //TODO, it would be nice to have some context variables
107                         simpleMessage.setContent(Utility.encodeHTML(body) + "<br>" + htmlencodedTest + "<br>"
108                                 + SLACommon.GenerateManageLink(policyurl, props.getProperty("fgsms.GUI.URL")) + "<br><br>"
109                                 + SLACommon.GenerateSubscriptionLink(props.getProperty("fgsms.GUI.URL")), SLACommon.getBundleString("EmailEncodingType"));
110                         // simpleMessage.setText();
111                         Transport.send(simpleMessage);
112                     } catch (Exception ex) {
113                         log.log(Level.ERROR, SLACommon.getBundleString("ErrorSendingEmail") + subscribers[i2].getAddress(), ex);
114                     }
115                 }
116             } catch (Exception ex) {
117                 log.log(Level.ERROR, SLACommon.getBundleString("ErrorUncaughtException"), ex);
118             }
119         }
120     }
121 
122     @Override
123     public List<NameValuePair> GetRequiredParameters() {
124         List<NameValuePair> r = new ArrayList<NameValuePair>();
125         r.add(Utility.newNameValuePair("Subject", null, false, false));
126         r.add(Utility.newNameValuePair("Body", null, false, false));
127         return r;
128     }
129 
130     @Override
131     public boolean ValidateConfiguration(List<NameValuePair> params, AtomicReference<String> outError) {
132        if (outError == null) {
133             outError = new AtomicReference<String>();
134         }
135         if (params == null || params.isEmpty()) {
136             outError.set("The parameter 'Subject' and 'Body' is required. " + outError.get());
137         }
138         boolean foundSubject=false;
139         boolean foundBody=false;
140         for (int i = 0; i < params.size(); i++) {
141             if (params.get(i).getName().equals("Subject")) {
142                 foundSubject=true;
143                 if (Utility.stringIsNullOrEmpty(params.get(i).getValue())) {
144                     outError.set("A value must be specified for the parameter 'Subject'. " + outError.get());
145                 }
146             }
147               if (params.get(i).getName().equals("Body")) {
148                 foundBody=true;
149                 if (Utility.stringIsNullOrEmpty(params.get(i).getValue())) {
150                     outError.set("A value must be specified for the parameter 'Body'. " + outError.get());
151                 }
152             }
153         }
154         if (!foundBody || !foundSubject)
155             outError.set("The parameter 'Subject' and 'Body' is required. " + outError.get());
156         if (Utility.stringIsNullOrEmpty(outError.get())) {
157             return true;
158         } else {
159             return false;
160         }
161     }
162 
163     @Override
164     public String GetHtmlFormattedHelp() {
165         return "Send an Email Alert<Br>Use this SLA Action to define an email delivery action that users can then subscribe to. Configurable fields:"
166                 + "<ul><li>Subject</li><li>Body</li><li>From - optional. This will override the sending address</li></ul>"
167                 + "The remaining options are administrator defined in the <b>General Settings</b> page of fgsms and primarily deal with mail server"
168                 + "settings.";
169     }
170 
171     @Override
172     public String GetDisplayName() {
173         return "Email Alert";
174     }
175 
176     @Override
177     public List<NameValuePair> GetOptionalParameters() {
178         List<NameValuePair> r = new ArrayList<NameValuePair>();
179         r.add(Utility.newNameValuePair("From", null, false, false));
180         return r;
181     }
182 
183     @Override
184     public void ProcessAction(AlertContainer alert, List<NameValuePair> params) {
185           SendMailAlert(alert.getFaultMsg(), alert.getSlaActionBaseType(), alert.getSLAID(), alert.isPooled(), alert.getModifiedurl());
186     }
187 
188     @Override
189     public List<PolicyType> GetAppliesTo() {
190        return Utility.getAllPolicyTypes();
191     }
192 }