001package org.apache.wiki.tasks.auth; 002 003import java.util.Locale; 004 005import javax.mail.MessagingException; 006import javax.mail.internet.AddressException; 007 008import org.apache.log4j.Logger; 009import org.apache.wiki.WikiContext; 010import org.apache.wiki.WikiEngine; 011import org.apache.wiki.api.exceptions.WikiException; 012import org.apache.wiki.auth.user.UserProfile; 013import org.apache.wiki.i18n.InternationalizationManager; 014import org.apache.wiki.tasks.TasksManager; 015import org.apache.wiki.util.MailUtil; 016import org.apache.wiki.workflow.Outcome; 017import org.apache.wiki.workflow.Task; 018import org.apache.wiki.workflow.WorkflowManager; 019 020 021/** 022 * Handles the actual profile save action. 023 */ 024public class SaveUserProfileTask extends Task { 025 026 private static final long serialVersionUID = 6994297086560480285L; 027 private static final Logger LOG = Logger.getLogger( SaveUserProfileTask.class ); 028 private final WikiEngine m_engine; 029 private final Locale m_loc; 030 031 /** 032 * Constructs a new Task for saving a user profile. 033 * @param engine the wiki engine 034 */ 035 public SaveUserProfileTask( WikiEngine engine, Locale loc ) { 036 super( TasksManager.USER_PROFILE_SAVE_TASK_MESSAGE_KEY ); 037 m_engine = engine; 038 m_loc = loc; 039 } 040 041 /** 042 * Saves the user profile to the user database. 043 * @return {@link org.apache.wiki.workflow.Outcome#STEP_COMPLETE} if the 044 * task completed successfully 045 * @throws WikiException if the save did not complete for some reason 046 */ 047 @Override 048 public Outcome execute() throws WikiException { 049 // Retrieve user profile 050 final UserProfile profile = ( UserProfile )getWorkflow().getAttribute( WorkflowManager.WF_UP_CREATE_SAVE_ATTR_SAVED_PROFILE ); 051 052 // Save the profile (userdatabase will take care of timestamps for us) 053 m_engine.getUserManager().getUserDatabase().save( profile ); 054 055 // Send e-mail if user supplied an e-mail address 056 if ( profile.getEmail() != null ) { 057 try { 058 final InternationalizationManager i18n = m_engine.getInternationalizationManager(); 059 final String app = m_engine.getApplicationName(); 060 final String to = profile.getEmail(); 061 final String subject = i18n.get( InternationalizationManager.DEF_TEMPLATE, m_loc, 062 "notification.createUserProfile.accept.subject", app ); 063 064 final String content = i18n.get( InternationalizationManager.DEF_TEMPLATE, m_loc, 065 "notification.createUserProfile.accept.content", app, 066 profile.getLoginName(), 067 profile.getFullname(), 068 profile.getEmail(), 069 m_engine.getURL( WikiContext.LOGIN, null, null, true ) ); 070 MailUtil.sendMessage( m_engine.getWikiProperties(), to, subject, content); 071 } catch ( final AddressException e) { 072 LOG.debug( e.getMessage(), e ); 073 } catch ( final MessagingException me ) { 074 LOG.error( "Could not send registration confirmation e-mail. Is the e-mail server running?", me ); 075 } 076 } 077 078 return Outcome.STEP_COMPLETE; 079 } 080 081}