View Javadoc

1   /*
2    * Copyright 2000-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.jetspeed.modules.actions;
18  
19  // Java stuff
20  import java.io.StringWriter;
21  import java.util.Properties;
22    
23  // Jetspeed Stuff
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  // Turbine Stuff
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  // Velocity Stuff
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              //build body via template
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 }