View Javadoc

1   /**
2    * This file Copyright (c) 2003-2012 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.mail.commands;
35  
36  import info.magnolia.context.WebContext;
37  import info.magnolia.cms.util.AlertUtil;
38  import info.magnolia.module.mail.MailModule;
39  import info.magnolia.module.mail.MailTemplate;
40  import info.magnolia.module.mail.MgnlMailFactory;
41  import info.magnolia.module.mail.templates.MailAttachment;
42  import info.magnolia.module.mail.templates.MgnlEmail;
43  import info.magnolia.module.mail.util.MailUtil;
44  
45  import java.util.Arrays;
46  import java.util.List;
47  import java.util.Map;
48  
49  import org.apache.commons.chain.Command;
50  import org.apache.commons.chain.Context;
51  import org.apache.commons.lang.StringUtils;
52  import org.slf4j.Logger;
53  import org.slf4j.LoggerFactory;
54  
55  
56  /**
57   * the command for sending mail.
58   * @author jackie
59   * @author niko
60   */
61  public class MailCommand implements Command {
62  
63      public static Logger log = LoggerFactory.getLogger(MailCommand.class);
64  
65      @Override
66      public boolean execute(Context ctx) {
67          if (log.isDebugEnabled()) {
68              log.debug("starting sending mail");
69          }
70  
71          try {
72              MgnlMailFactory factory = MailModule.getInstance().getFactory();
73              MgnlEmail email;
74              if (log.isDebugEnabled()) {
75                  log.debug(Arrays.asList(ctx.entrySet().toArray()).toString());
76              }
77  
78              String template = (String) ctx.get("mailTemplate");
79              //get parameters from mail page parameter text area
80              if(ctx.containsKey(MailTemplate.MAIL_PARAMETERS)) {
81                  Map<String, String> temp = MailUtil.convertToMap((String)ctx.get(MailTemplate.MAIL_PARAMETERS));
82                  ctx.putAll(temp);
83              }
84  
85              //find attachments in parameters or form if we are using one
86              List<MailAttachment> attachments = null;
87  
88              if(ctx instanceof WebContext) {
89                  attachments = MailUtil.createAttachmentList(((WebContext)ctx).getParameters());
90              }
91  
92              if (StringUtils.isNotEmpty(template)) {
93                  log.debug("Command using mail template: " + template);
94  
95                  email = factory.getEmailFromTemplate(template, attachments, ctx);
96                  email.setBodyFromResourceFile();
97              }
98              else {
99                  log.debug("command using static parameters");
100 
101                 email = factory.getEmail(ctx, attachments);
102                 if(StringUtils.isEmpty(email.getTemplate().getTemplateFile())) {
103                     email.setBody();
104                 } else {
105                     email.setBodyFromResourceFile();
106                 }
107 
108             }
109             factory.getEmailHandler().sendMail(email);
110 
111             log.info("send mail successfully to:" + email.getTemplate().getTo());
112         }
113         catch (Exception e) {
114             log.debug("Could not send email:" + e.getMessage(), e);
115             log.error("Could not send email:" + e.getMessage());
116             AlertUtil.setMessage("Error: " + e.getMessage());
117             return false;
118         }
119 
120         return true;
121     }
122 
123 }