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  import java.util.Locale;
23  
24  // Jetspeed Stuff
25  import org.apache.jetspeed.services.resources.JetspeedResources;
26  import org.apache.jetspeed.services.TemplateLocator;
27  import org.apache.jetspeed.services.JetspeedSecurity;
28  import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
29  import org.apache.jetspeed.services.logging.JetspeedLogger;
30  import org.apache.jetspeed.om.security.JetspeedUser;
31  
32  // Turbine Stuff
33  import org.apache.turbine.modules.Action;
34  import org.apache.turbine.services.localization.Localization;
35  import org.apache.turbine.services.velocity.TurbineVelocity;
36  import org.apache.turbine.util.RunData;
37  import org.apache.turbine.util.DynamicURI;
38  import org.apache.turbine.util.mail.SimpleEmail;
39  
40  // Velocity Stuff
41  import org.apache.velocity.context.Context;
42  
43  /***
44      This action will send a notification email to the notification user.
45  
46  TODO - does this apply to the notify emails?
47      This class is used in two places, the first one is for new users.
48      The second is where a user is updating their information after they
49      have already created their account. If they are updating and they change
50      their email address, then we want to re-confirm it to prevent people from
51      screwing up their email address.
52  */
53  public class SendNewUserNotificationEmail extends Action
54  {
55      /***
56       * Static initialization of the logger for this class
57       */    
58      private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(SendNewUserNotificationEmail.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("pane0", "Security" )
65                               .addPathInfo("select-panel2", "User")
66                               .addPathInfo("entityid", user.getUserName() )
67                               .addQueryData("mode","update");
68          try
69          {
70              //build body via template
71              StringWriter email_body = new StringWriter();
72              SimpleEmail se = new SimpleEmail();
73              Context emailContext = TurbineVelocity.getContext();
74              emailContext.put( "data", data );
75              emailContext.put( "user", user );
76              emailContext.put( "config",new JetspeedResources());
77              emailContext.put( "userurl",url);
78              emailContext.put( "email",se);
79  
80              //determine the language to be used for the notification email
81              String language = JetspeedResources.getString("newuser.notification.language","en");
82              String country = JetspeedResources.getString("newuser.notification.country","US");
83              Locale locale = new Locale(language,country);
84  
85              String templateFile = JetspeedResources.getString("newuser.notification.email.template");
86              String templatePath = TemplateLocator.locateEmailTemplate(data, templateFile, locale);
87              TurbineVelocity.handleRequest(emailContext, templatePath, email_body);
88  
89              se.setMsg(email_body.toString());
90  
91              Properties props = System.getProperties();
92              String mailServerMachine = JetspeedResources.getString( "mail.server" );
93              props.put ( "mail.host", mailServerMachine );
94              props.put("mail.smtp.host", mailServerMachine);
95  
96              se.send();
97  
98              data.setMessage (Localization.getString(data, "SENDCONFIRMATIONEMAIL_SENT"));
99          } catch ( Exception e )
100         {
101             String errorTitle = Localization.getString(data, "SENDCONFIRMATIONEMAIL_ERROR") ;
102             String errorMessage = errorTitle + e.getMessage();
103 
104             logger.error( errorMessage, e );
105             data.setMessage ( errorTitle + errorMessage );
106         }
107     }
108 }