1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.modules.actions;
18
19
20 import java.io.StringWriter;
21 import java.util.Properties;
22
23
24 import org.apache.jetspeed.services.TemplateLocator;
25 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
26 import org.apache.jetspeed.services.logging.JetspeedLogger;
27 import org.apache.jetspeed.services.resources.JetspeedResources;
28 import org.apache.jetspeed.services.JetspeedSecurity;
29 import org.apache.jetspeed.om.security.JetspeedUser;
30
31
32 import org.apache.turbine.modules.Action;
33 import org.apache.turbine.services.localization.Localization;
34 import org.apache.turbine.services.velocity.TurbineVelocity;
35 import org.apache.turbine.util.RunData;
36 import org.apache.turbine.util.DynamicURI;
37 import org.apache.turbine.util.mail.SimpleEmail;
38
39
40 import org.apache.velocity.context.Context;
41
42 /***
43 * This action will attempt to send a confirmation email to the user.
44 * This class is used in two places, the first one is for new users.
45 * The second is where a user is updating their information after they
46 * have already created their account. If they are updating and they change
47 * their email address, then we want to re-confirm it to prevent people from
48 * screwing up their email address.
49 *
50 *@author <a href="mailto:paulsp@apache.org">Paul Spencer</a>
51 */
52 public class SendConfirmationEmail extends Action
53 {
54
55 /***
56 * Static initialization of the logger for this class
57 */
58 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(SendConfirmationEmail.class.getName());
59
60 public void doPerform( RunData data ) throws Exception
61 {
62 JetspeedUser user = JetspeedSecurity.getUser(data.getParameters().getString("username", ""));
63 DynamicURI url = new DynamicURI(data)
64 .addPathInfo(JetspeedResources.PATH_TEMPLATE_KEY, "ConfirmRegistration")
65 .addPathInfo("username", user.getUserName())
66 .addPathInfo("secretkey", user.getConfirmed())
67 .addPathInfo("password", user.getPassword());
68 try
69 {
70
71 StringWriter email_body = new StringWriter();
72 Context emailContext = TurbineVelocity.getContext(data);
73 SimpleEmail se = new SimpleEmail();
74 emailContext.put( "data", data );
75 emailContext.put( "user", user );
76 emailContext.put("config",new JetspeedResources());
77 emailContext.put("urltojetspeed",url);
78 emailContext.put("email",se);
79 String templateFile = JetspeedResources.getString("newuser.confirm.email.template");
80 String templatePath = TemplateLocator.locateEmailTemplate(data, templateFile);
81 TurbineVelocity.handleRequest(emailContext, templatePath, email_body);
82
83 se.setMsg( email_body.toString() );
84
85 Properties props = System.getProperties();
86 String mailServerMachine = JetspeedResources.getString( "mail.server" );
87 props.put ( "mail.host", mailServerMachine );
88 props.put("mail.smtp.host", mailServerMachine);
89
90 se.send();
91
92 data.setMessage (Localization.getString(data, "SENDCONFIRMATIONEMAIL_SENT"));
93 }
94 catch ( Exception e )
95 {
96 String errorTitle = Localization.getString("SENDCONFIRMATIONEMAIL_ERROR") ;
97 String errorMessage = errorTitle + e.getMessage();
98
99 logger.error( errorMessage, e );
100 data.setMessage ( errorTitle + errorMessage );
101 }
102 }
103 }