1
22
23 package com.liferay.portlet.webform.action;
24
25 import com.liferay.mail.service.MailServiceUtil;
26 import com.liferay.portal.captcha.CaptchaTextException;
27 import com.liferay.portal.captcha.CaptchaUtil;
28 import com.liferay.portal.kernel.mail.MailMessage;
29 import com.liferay.portal.kernel.util.GetterUtil;
30 import com.liferay.portal.kernel.util.StringMaker;
31 import com.liferay.portal.kernel.util.StringPool;
32 import com.liferay.portal.kernel.util.StringUtil;
33 import com.liferay.portal.kernel.util.Validator;
34 import com.liferay.portal.struts.PortletAction;
35 import com.liferay.portal.util.PropsUtil;
36 import com.liferay.portlet.PortletConfigImpl;
37 import com.liferay.portlet.PortletPreferencesFactoryUtil;
38 import com.liferay.util.FileUtil;
39 import com.liferay.util.servlet.SessionErrors;
40 import com.liferay.util.servlet.SessionMessages;
41
42 import java.util.ArrayList;
43 import java.util.Iterator;
44 import java.util.List;
45
46 import javax.mail.internet.InternetAddress;
47
48 import javax.portlet.ActionRequest;
49 import javax.portlet.ActionResponse;
50 import javax.portlet.PortletConfig;
51 import javax.portlet.PortletPreferences;
52 import javax.portlet.RenderRequest;
53 import javax.portlet.RenderResponse;
54
55 import org.apache.commons.logging.Log;
56 import org.apache.commons.logging.LogFactory;
57 import org.apache.struts.action.ActionForm;
58 import org.apache.struts.action.ActionForward;
59 import org.apache.struts.action.ActionMapping;
60
61
68 public class ViewAction extends PortletAction {
69
70 public void processAction(
71 ActionMapping mapping, ActionForm form, PortletConfig config,
72 ActionRequest req, ActionResponse res)
73 throws Exception {
74
75 PortletConfigImpl configImpl = (PortletConfigImpl)config;
76
77 String portletId = configImpl.getPortletId();
78
79 PortletPreferences prefs =
80 PortletPreferencesFactoryUtil.getPortletSetup(req, portletId);
81
82 boolean requireCaptcha = GetterUtil.getBoolean(
83 prefs.getValue("requireCaptcha", StringPool.BLANK));
84 String successURL = GetterUtil.getString(
85 prefs.getValue("successURL", StringPool.BLANK));
86 boolean sendAsEmail = GetterUtil.getBoolean(
87 prefs.getValue("sendAsEmail", StringPool.BLANK), true);
88 boolean saveToFile = GetterUtil.getBoolean(
89 prefs.getValue("saveToFile", StringPool.BLANK));
90 String fileName = GetterUtil.getString(
91 prefs.getValue("fileName", StringPool.BLANK));
92
93 if (requireCaptcha) {
94 try {
95 CaptchaUtil.check(req);
96 }
97 catch (CaptchaTextException cte) {
98 SessionErrors.add(req, CaptchaTextException.class.getName());
99 }
100 }
101
102 List<String> fieldValues = new ArrayList<String>();
103
104 for (int i = 1; i <= _MAX_FIELDS; i++) {
105 fieldValues.add(req.getParameter("field" + i));
106 }
107
108 if (validate(fieldValues, prefs)) {
109 boolean emailSent = false;
110 boolean fileSaved = false;
111
112 if (sendAsEmail) {
113 emailSent = sendEmail(fieldValues, prefs);
114 }
115
116 if (saveToFile) {
117 fileSaved = saveFile(fieldValues, prefs, fileName);
118 }
119
120 if ((sendAsEmail == emailSent) && (saveToFile == fileSaved)) {
121 SessionMessages.add(req, "emailSent");
122 }
123 else {
124 SessionErrors.add(req, "emailNotSent");
125 }
126 }
127 else {
128 SessionErrors.add(req, "allFieldsRequired");
129 }
130
131 if (SessionErrors.isEmpty(req) && Validator.isNotNull(successURL)) {
132 res.sendRedirect(successURL);
133 }
134 }
135
136 public ActionForward render(
137 ActionMapping mapping, ActionForm form, PortletConfig config,
138 RenderRequest req, RenderResponse res)
139 throws Exception {
140
141 return mapping.findForward("portlet.web_form.view");
142 }
143
144 protected String getMailBody(
145 List<String> fieldValues, PortletPreferences prefs) {
146
147 StringMaker sm = new StringMaker();
148
149 Iterator<String> itr = fieldValues.iterator();
150
151 for (int i = 1; itr.hasNext(); i++) {
152 String fieldValue = itr.next();
153
154 String fieldLabel = prefs.getValue(
155 "fieldLabel" + i, StringPool.BLANK);
156
157 if (Validator.isNotNull(fieldLabel)) {
158 sm.append(fieldLabel);
159 sm.append(" : ");
160 sm.append(fieldValue);
161 sm.append("\n");
162 }
163 }
164
165 return sm.toString();
166 }
167
168 protected boolean saveFile(
169 List<String> fieldValues, PortletPreferences prefs, String fileName) {
170
171
175 StringMaker sm = new StringMaker();
176
177 Iterator<String> itr = fieldValues.iterator();
178
179 for (int i = 1; itr.hasNext(); i++) {
180 String fieldValue = itr.next();
181
182 String fieldLabel = prefs.getValue(
183 "fieldLabel" + i, StringPool.BLANK);
184
185 if (Validator.isNotNull(fieldLabel)) {
186 sm.append("\"");
187 sm.append(StringUtil.replace(fieldValue, "\"", "\"\""));
188 sm.append("\";");
189 }
190 }
191
192 String s = sm.substring(0, sm.length() - 1) + "\n";
193
194 try {
195 FileUtil.write(fileName, s, false, true);
196
197 return true;
198 }
199 catch (Exception e) {
200 _log.error("The web form email could not be saved", e);
201
202 return false;
203 }
204 }
205
206 protected boolean sendEmail(
207 List<String> fieldValues, PortletPreferences prefs) {
208
209 try {
210 String subject = prefs.getValue("subject", StringPool.BLANK);
211 String emailAddress = prefs.getValue(
212 "emailAddress", StringPool.BLANK);
213
214 if (Validator.isNull(emailAddress)) {
215 _log.error(
216 "The web form email cannot be sent because no email " +
217 "address is configured");
218
219 return false;
220 }
221
222 String body = getMailBody(fieldValues, prefs);
223
224 InternetAddress fromAddress = new InternetAddress(emailAddress);
225 InternetAddress toAddress = new InternetAddress(emailAddress);
226
227 MailMessage mailMessage = new MailMessage(
228 fromAddress, toAddress, subject, body);
229
230 MailServiceUtil.sendEmail(mailMessage);
231
232 return true;
233 }
234 catch (Exception e) {
235 _log.error("The web form email could not be sent", e);
236
237 return false;
238 }
239 }
240
241 protected boolean validate(
242 List<String> fieldValues, PortletPreferences prefs) {
243
244 for (int i = 1; i < _MAX_FIELDS; i++) {
245 String fieldValue = fieldValues.get(i - 1);
246
247 String fieldLabel = prefs.getValue(
248 "fieldLabel" + i, StringPool.BLANK);
249 boolean fieldOptional = GetterUtil.getBoolean(
250 prefs.getValue("fieldOptional" + i, StringPool.BLANK));
251
252 if (!fieldOptional && Validator.isNotNull(fieldLabel) &&
253 Validator.isNull(fieldValue)) {
254
255 return false;
256 }
257 }
258
259 return true;
260 }
261
262 private static final int _MAX_FIELDS = GetterUtil.getInteger(
263 PropsUtil.get(PropsUtil.WEB_FORM_PORTLET_MAX_FIELDS));
264
265 private static Log _log = LogFactory.getLog(ViewAction.class);
266
267 }