KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > website > mail > SimpleMailFormController


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.website.mail;
25
26 import java.io.StringWriter JavaDoc;
27
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29
30 import org.springframework.beans.factory.InitializingBean;
31 import org.springframework.mail.MailSender;
32 import org.springframework.mail.SimpleMailMessage;
33 import org.springframework.util.Assert;
34 import org.springframework.web.bind.ServletRequestDataBinder;
35 import org.springframework.web.servlet.mvc.SimpleFormController;
36
37 import freemarker.template.Configuration;
38 import freemarker.template.Template;
39
40 /**
41  * Simple controller that sends data collected from a from via email.
42  * A FreeMarker template is used to format the mail body.
43  */

44 public class SimpleMailFormController extends SimpleFormController
45         implements InitializingBean {
46
47     private String JavaDoc to;
48     
49     private String JavaDoc[] bcc;
50     
51     private String JavaDoc from;
52     
53     private String JavaDoc subject;
54     
55     private MailSender mailSender;
56     
57     private Configuration freemarkerConfig;
58     
59     private String JavaDoc mailTemplateName;
60     
61     private String JavaDoc[] requiredFields;
62     
63     
64     public SimpleMailFormController() {
65         setCommandClass(MailForm.class);
66     }
67
68     public void setTo(String JavaDoc to) {
69         this.to = to;
70     }
71
72     public void setBcc(String JavaDoc[] bcc) {
73         this.bcc = bcc;
74     }
75
76     public void setFrom(String JavaDoc from) {
77         this.from = from;
78     }
79
80     public void setSubject(String JavaDoc subject) {
81         this.subject = subject;
82     }
83
84     public void setMailSender(MailSender mailSender) {
85         this.mailSender = mailSender;
86     }
87
88     public void setFreemarkerConfig(Configuration freemarkerConfig) {
89         this.freemarkerConfig = freemarkerConfig;
90     }
91
92     public void setMailTemplateName(String JavaDoc mailTemplateName) {
93         this.mailTemplateName = mailTemplateName;
94     }
95
96     public void setRequiredFields(String JavaDoc[] requiredFields) {
97         this.requiredFields = requiredFields;
98     }
99
100     public void afterPropertiesSet() throws Exception JavaDoc {
101         Assert.notNull(to);
102         Assert.notNull(mailSender);
103         Assert.notNull(freemarkerConfig);
104         Assert.notNull(mailTemplateName);
105     }
106     
107     protected void initBinder(HttpServletRequest JavaDoc request,
108             ServletRequestDataBinder binder) throws Exception JavaDoc {
109         
110         binder.setRequiredFields(requiredFields);
111     }
112     
113     protected void doSubmitAction(Object JavaDoc command) throws Exception JavaDoc {
114         Template template = freemarkerConfig.getTemplate(mailTemplateName);
115         
116         StringWriter JavaDoc mailTextWriter = new StringWriter JavaDoc();
117         template.process(command, mailTextWriter);
118     
119         SimpleMailMessage mail = new SimpleMailMessage();
120         mail.setTo(to);
121         mail.setBcc(bcc);
122         mail.setFrom(from);
123         mail.setSubject(subject);
124         mail.setText(mailTextWriter.toString());
125         prepareMail(mail, command);
126         mailSender.send(mail);
127     }
128
129     /**
130      * This method can be overriden in order to manipulate the mail before it
131      * is being sent.
132      */

133     protected void prepareMail(SimpleMailMessage mail, Object JavaDoc command) {
134     }
135     
136 }
137
Popular Tags