1 16 17 package org.apache.jetspeed.modules.actions.portlets; 18 19 import org.apache.turbine.util.RunData; 21 22 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 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 55 56 public class QuestionnaireAction extends JspPortletAction 57 { 58 59 62 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(QuestionnaireAction.class.getName()); 63 64 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 if (rundata.getRequest().getAttribute("email") == null) 93 { 94 resetTemplate(rundata); 96 } 97 98 } 99 100 106 public void doContinue(RunData rundata, Portlet portlet) 107 { 108 resetTemplate(rundata); 110 } 111 112 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 java.util.Properties properties = System.getProperties(); 151 properties.put("mail.smtp.host", emailSmtp); 152 Session emailSession = Session.getInstance(properties, null); 153 154 MimeMessage message = new MimeMessage (emailSession); 156 157 Address fromAddress = new InternetAddress (emailFrom); 159 message.setFrom(fromAddress); 160 161 Address [] toAddresses = InternetAddress.parse(emailTo); 163 message.setRecipients(Message.RecipientType.TO, toAddresses); 164 165 message.setSubject("Jetspeed Questionnaire from " + rundata.getUser().getEmail()); 167 message.setText(emailText); 168 169 if (emailAttachment != null) 171 { 172 File file = new File (emailAttachment); 173 if (file.exists()) 174 { 175 MimeBodyPart mbp1 = new MimeBodyPart (); 177 mbp1.setText(emailText); 178 179 MimeBodyPart mbp2 = new MimeBodyPart (); 181 182 FileDataSource fds = new FileDataSource (emailAttachment); 184 mbp2.setDataHandler(new DataHandler (fds)); 185 mbp2.setFileName(fds.getName()); 186 187 Multipart mp = new MimeMultipart (); 189 mp.addBodyPart(mbp1); 190 mp.addBodyPart(mbp2); 191 192 message.setContent(mp); 194 } 195 else 196 { 197 message.setText(emailBody.toString()); 198 } 199 } 200 201 Transport.send(message); 203 204 rundata.getRequest().setAttribute("email", emailBody.toString()); 206 String confirmTemplate = portlet.getPortletConfig().getInitParameter("confirm.template", 207 "JetspeedQuestionnaireConfirmation.jsp"); 208 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 221 } 222 223 224 } 225 226 | Popular Tags |