1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.modules.actions.portlets;
18
19
20 import org.apache.turbine.util.RunData;
21
22
23 import org.apache.jetspeed.portal.Portlet;
24 import org.apache.jetspeed.om.registry.Parameter;
25 import org.apache.jetspeed.services.Registry;
26 import org.apache.jetspeed.om.registry.PortletEntry;
27 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
28 import org.apache.jetspeed.services.logging.JetspeedLogger;
29 import org.apache.jetspeed.services.resources.JetspeedResources;
30
31
32 import java.util.Hashtable;
33 import java.util.Iterator;
34 import java.io.File;
35
36 import javax.mail.Session;
37 import javax.mail.Address;
38 import javax.mail.Message;
39 import javax.mail.Multipart;
40 import javax.mail.Transport;
41 import javax.mail.internet.MimeMessage;
42 import javax.mail.internet.InternetAddress;
43 import javax.mail.internet.MimeBodyPart;
44 import javax.mail.internet.MimeMultipart;
45 import javax.activation.FileDataSource;
46 import javax.activation.DataHandler;
47
48 /***
49 * This action sets up the template context for retrieving stock quotes.
50 *
51 * @author <a href="mailto:morciuch@apache.org">Mark Orciuch</a>
52 * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
53 * @version $Id: QuestionnaireAction.java,v 1.7 2004/02/23 02:56:58 jford Exp $
54 */
55
56 public class QuestionnaireAction extends JspPortletAction
57 {
58
59 /***
60 * Static initialization of the logger for this class
61 */
62 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(QuestionnaireAction.class.getName());
63
64 /***
65 * Build the normal state content for this portlet.
66 *
67 * @param portlet The jsp-based portlet that is being built.
68 * @param rundata The turbine rundata context for this request.
69 */
70 protected void buildNormalContext(Portlet portlet, RunData rundata)
71 {
72 PortletEntry entry = (PortletEntry) Registry.getEntry(Registry.PORTLET, portlet.getName());
73 Iterator i = entry.getParameterNames();
74 Hashtable qa = new Hashtable();
75
76 while (i.hasNext())
77 {
78 String name = (String) i.next();
79 Parameter param = entry.getParameter(name);
80 if (param.isHidden() == false)
81 {
82 String title = param.getTitle();
83 String value = portlet.getPortletConfig().getInitParameter(name);
84 qa.put(title, value);
85 }
86 }
87
88 rundata.getRequest().setAttribute("questions", qa);
89
90
91
92 if (rundata.getRequest().getAttribute("email") == null)
93 {
94
95 resetTemplate(rundata);
96 }
97
98 }
99
100 /***
101 * Continue event handler.
102 *
103 * @param portlet The jsp-based portlet that is being built.
104 * @param rundata The turbine rundata context for this request.
105 */
106 public void doContinue(RunData rundata, Portlet portlet)
107 {
108
109 resetTemplate(rundata);
110 }
111
112 /***
113 * Sort the quotes.
114 *
115 * @param portlet The jsp-based portlet that is being built.
116 * @param rundata The turbine rundata context for this request.
117 */
118 public void doEmail(RunData rundata, Portlet portlet)
119 {
120 StringBuffer emailBody = new StringBuffer();
121 PortletEntry entry = (PortletEntry) Registry.getEntry(Registry.PORTLET, portlet.getName());
122 Iterator i = entry.getParameterNames();
123
124 while (i.hasNext())
125 {
126 String name = (String) i.next();
127 Parameter param = entry.getParameter(name);
128 if (param.isHidden() == false)
129 {
130 String title = param.getTitle();
131 String value = portlet.getPortletConfig().getInitParameter(name);
132 value = value == null || value.length() == 0 ? "NOT PROVIDED" : value;
133 emailBody.append(title);
134 emailBody.append(" ===> ");
135 emailBody.append(value);
136 emailBody.append("\n\n");
137
138 }
139 }
140
141 String emailSmtp = JetspeedResources.getString(JetspeedResources.MAIL_SERVER_KEY);
142 String emailFrom = JetspeedResources.getString("mail.support", "david@bluesunrise.com");
143 String emailTo = rundata.getParameters().getString("emailTo", "jetspeed-dev@jakarta.apache.org");
144 String emailAttachment = rundata.getRequest().getParameter("emailAttachment");
145 try
146 {
147 String emailText = emailBody.toString();
148
149
150 java.util.Properties properties = System.getProperties();
151 properties.put("mail.smtp.host", emailSmtp);
152 Session emailSession = Session.getInstance(properties, null);
153
154
155 MimeMessage message = new MimeMessage(emailSession);
156
157
158 Address fromAddress = new InternetAddress(emailFrom);
159 message.setFrom(fromAddress);
160
161
162 Address[] toAddresses = InternetAddress.parse(emailTo);
163 message.setRecipients(Message.RecipientType.TO, toAddresses);
164
165
166 message.setSubject("Jetspeed Questionnaire from " + rundata.getUser().getEmail());
167 message.setText(emailText);
168
169
170 if (emailAttachment != null)
171 {
172 File file = new File(emailAttachment);
173 if (file.exists())
174 {
175
176 MimeBodyPart mbp1 = new MimeBodyPart();
177 mbp1.setText(emailText);
178
179
180 MimeBodyPart mbp2 = new MimeBodyPart();
181
182
183 FileDataSource fds = new FileDataSource(emailAttachment);
184 mbp2.setDataHandler(new DataHandler(fds));
185 mbp2.setFileName(fds.getName());
186
187
188 Multipart mp = new MimeMultipart();
189 mp.addBodyPart(mbp1);
190 mp.addBodyPart(mbp2);
191
192
193 message.setContent(mp);
194 }
195 else
196 {
197 message.setText(emailBody.toString());
198 }
199 }
200
201
202 Transport.send(message);
203
204
205 rundata.getRequest().setAttribute("email", emailBody.toString());
206 String confirmTemplate = portlet.getPortletConfig().getInitParameter("confirm.template",
207 "JetspeedQuestionnaireConfirmation.jsp");
208
209 setTemplate(rundata, confirmTemplate, true);
210
211 rundata.setMessage("Email successfully sent");
212 }
213 catch (Exception e)
214 {
215 logger.error("Exception", e);
216 rundata.setMessage("Error sending email: " + e);
217 }
218
219
220
221 }
222
223
224 }
225