Ignore:
Timestamp:
10/09/12 15:02:43 (12 years ago)
Author:
vmipsl
Message:

clean

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ether_statistics/web/src/com/ether/ControllerEponge.java

    r569 r574  
    44import com.ether.annotation.ParamName; 
    55import com.ether.user.User; 
    6 import com.ether.user.UserRole; 
    7 import com.ether.user.UserState; 
    8 import com.medias.Constantes; 
    9 import com.medias.Context; 
    10 import com.medias.annuaire.Personne; 
    11 import com.medias.mail.Mail; 
    12 import com.medias.mail.MailFactory; 
    136import net.sf.json.JSONObject; 
    147import org.apache.commons.logging.Log; 
    158import org.apache.commons.logging.LogFactory; 
    169import org.jetbrains.annotations.NotNull; 
    17 import org.jetbrains.annotations.Nullable; 
     10import org.springframework.mail.javamail.JavaMailSenderImpl; 
     11import org.springframework.mail.javamail.MimeMessageHelper; 
    1812 
    1913import javax.mail.MessagingException; 
     14import javax.mail.internet.MimeMessage; 
    2015import javax.servlet.http.HttpServletRequest; 
    21 import java.security.DigestException; 
    22 import java.security.NoSuchAlgorithmException; 
    23 import java.util.ArrayList; 
    24 import java.util.Date; 
    2516import java.util.HashMap; 
    2617import java.util.Map; 
     
    2819/** 
    2920 * @author vmipsl 
    30  * @date 12 nov 2011 
     21 * @date 15 march 2012 
    3122 */ 
    3223public class ControllerEponge 
     
    3627    /** *********************** VIEWS ***************************** **/ 
    3728    /** *********************************************************** **/ 
    38     @ControllerMethod(view = VIEW_ERRORS) 
    39     public Map<String, Object> viewErrors() 
    40             throws ServiceException 
     29    /** 
     30     * Home method 
     31     * 
     32     * @return 
     33     * @throws WebException 
     34     */ 
     35    @ControllerMethod(view = VIEW_INIT) 
     36    public Map<String, Object> home() 
     37            throws WebException 
    4138    { 
    4239        return new HashMap<String, Object>(); 
    4340    } 
    4441 
    45     @ControllerMethod(view = VIEW_LOGIN) 
    46     public Map<String, Object> viewLogin() 
    47             throws ServiceException 
    48     { 
    49         return new HashMap<String, Object>(); 
    50     } 
    51  
    52     @ControllerMethod(view = VIEW_DATA_PROTOCOL_EN, requestMandatory = true) 
    53     public Map<String, Object> viewHomeDataProtocol( @NotNull final HttpServletRequest request ) 
    54             throws ServiceException 
    55     { 
    56         final MethodDescription methodDescription = getMethods().get( "viewHomeDataProtocol" ); 
    57  
    58         if( Context.getLangue( request ).equals( Constantes.french_language ) ) 
    59             methodDescription.setView( VIEW_DATA_PROTOCOL_FR ); 
    60         else 
    61             methodDescription.setView( VIEW_DATA_PROTOCOL_EN ); 
    62         return new HashMap<String, Object>(); 
    63     } 
    64  
    6542    /** *********************************************************** **/ 
    6643    /** *********************** CALLS ***************************** **/ 
    6744    /** *********************************************************** **/ 
     45    /** 
     46     * This method logs a user 
     47     * 
     48     * @param login 
     49     * @param password 
     50     * @param request 
     51     * @return 
     52     * @throws ServiceException 
     53     */ 
    6854    @ControllerMethod(requestMandatory = true, jsonResult = true) 
    6955    public JSONObject login( @ParamName(ParameterConstants.PARAMETER_LOGIN) final String login, 
     
    7258            throws ServiceException 
    7359    { 
    74         final JSONObject jSONPeople = new JSONObject(); 
     60        final JSONObject jSONUser = new JSONObject(); 
    7561        final JSONObject result = new JSONObject(); 
    7662 
     
    7965            try 
    8066            { 
    81                 final User user = getEtherService().getUserByEmail( login ); 
    82                 final String encryptedPassword = EtherHelper.encryptPassword( password ); 
    83                 if( user == null ) 
    84                     result.put( "errors", "login.error.notFound" ); 
    85                 else if( !encryptedPassword.equals( user.getPassword() ) ) 
    86                     result.put( "errors", "login.error.wrongPassword" ); 
    87                 else if( !UserState.ACCEPTED.equals( user.getState() ) ) 
    88                     result.put( "errors", "login.error.notAccepted" ); 
    89                 else 
    90                 { 
    91                     Personne person = getEtherService().getPeopleByEmail( user.getEmail() ); 
    92                     if( person == null ) 
    93                     { 
    94                         // This is a simple user with no set of data _ Personne in session is used for tests in .jsp 
    95                         person = new Personne(); 
    96                         person.setNom( user.getLastName() ); 
    97                         person.setPrenom( user.getFirstName() ); 
    98                         person.setMail( user.getEmail() ); 
    99                         person.setRoles( user.getRole().name() ); 
    100                         // Can't be null because of the tests in .jsp (upload.jsp by example) 
    101                         person.setJeux( new ArrayList<String>() ); 
    102                     } 
    103  
    104                     request.getSession().setAttribute( "SES_USER", person ); 
    105                     jSONPeople.put( "name", user.getLastName() ); 
    106                     jSONPeople.put( "firstName", user.getFirstName() ); 
    107                     jSONPeople.put( "role", user.getRole().name() ); 
    108                 } 
     67//                final User user = getTapasService().getUserByEmail( login ); 
     68//                final String encryptedPassword = EtherHelper.encryptPassword( password ); 
     69//                if( user == null ) 
     70//                    result.put( "errors", "login.error.notFound" ); 
     71//                else if( !encryptedPassword.equals( user.getPassword() ) ) 
     72//                    result.put( "errors", "login.error.wrongPassword" ); 
     73//                else 
     74//                { 
     75//                    request.getSession().setAttribute( "SES_USER", user ); 
     76//                    jSONUser.put( "name", user.getLastName() ); 
     77//                    jSONUser.put( "firstName", user.getFirstName() ); 
     78//                    jSONUser.put( "role", user.getRole().name() ); 
     79//                } 
    10980            } 
    11081            catch( Exception e ) 
     
    11485        } 
    11586 
    116         if( !jSONPeople.isEmpty() ) 
    117             result.put( "jSONPeople", jSONPeople ); 
     87        if( !jSONUser.isEmpty() ) 
     88            result.put( "jSONUser", jSONUser ); 
    11889        return result; 
    11990    } 
     
    129100    } 
    130101 
    131     @ControllerMethod(jsonResult = true) 
    132     public JSONObject createAccount( @NotNull @ParamName(ParameterConstants.PARAMETER_NAME) final String lastName, 
    133                                      @Nullable @ParamName(ParameterConstants.PARAMETER_FIRST_NAME) final String firstName, 
    134                                      @NotNull @ParamName(ParameterConstants.PARAMETER_EMAIL) final String email, 
    135                                      @NotNull @ParamName(ParameterConstants.PARAMETER_PWD) final String password ) 
    136             throws ServiceException, WebException 
    137     { 
    138         try 
    139         { 
    140             final User existingUser = getEtherService().getUserByEmail( email ); 
    141             if( null == existingUser ) 
    142             { 
    143                 final Date creationDate = new Date(); 
    144  
    145                 final String encryptedPassword = EtherHelper.encryptPassword( password ); 
    146                 final User user = new User( lastName, firstName, email, encryptedPassword, UserRole.COORDINATOR, UserState.WAITING, false, creationDate ); 
    147  
    148                 getEtherService().createUser( user ); 
    149  
    150                 // Send email to administrator to inform there is a new account 
    151                 sendEmailToAdministrator( user ); 
    152             } 
    153             else 
    154                 throw new WebException( WebException.WebCode.USER_ALREADY_EXISTS, "This email already corresponds to an User", WebException.getExceptionThrowable() ); 
    155         } 
    156         catch( DigestException e ) 
    157         { 
    158             throw new WebException( WebException.WebCode.ERROR_ENCRYPT_PASSWORD, "This password cannot be encrypted" ); 
    159         } 
    160         catch( NoSuchAlgorithmException e ) 
    161         { 
    162             throw new WebException( WebException.WebCode.ERROR_ENCRYPT_PASSWORD, "This password cannot be encrypted" ); 
    163         } 
    164  
    165         return new JSONObject(); 
    166     } 
     102 
     103//    @ControllerMethod(jsonResult = true) 
     104//    public JSONObject createAccount( @NotNull @ParamName(ParameterConstants.PARAMETER_NAME) final String lastName, 
     105//                                     @Nullable @ParamName(ParameterConstants.PARAMETER_FIRST_NAME) final String firstName, 
     106//                                     @NotNull @ParamName(ParameterConstants.PARAMETER_EMAIL) final String email, 
     107//                                     @NotNull @ParamName(ParameterConstants.PARAMETER_PWD) final String password, 
     108//                                     @Nullable @ParamName(ParameterConstants.PARAMETER_LABORATORY) final String laboratory, 
     109//                                     @Nullable @ParamName(ParameterConstants.PARAMETER_COUNTRY) final String country ) 
     110//            throws ServiceException, WebException 
     111//    { 
     112//        try 
     113//        { 
     114//            final User existingUser = getTapasService().getUserByEmail( email ); 
     115//            if( null == existingUser ) 
     116//            { 
     117//                final Date creationDate = new Date(); 
     118// 
     119//                final String encryptedPassword = EtherHelper.encryptPassword( password ); 
     120//                final User user = new User( lastName, firstName, email, encryptedPassword, UserRole.USER, false, laboratory, country, creationDate ); 
     121// 
     122//                getTapasService().createUser( user ); 
     123// 
     124//                // Send email to administrator to inform there is a new account 
     125//                sendEmailToAdministratorAndUser( user ); 
     126//            } 
     127//            else 
     128//                throw new WebException( WebException.WebCode.USER_ALREADY_EXISTS, "This email already corresponds to an User", WebException.getExceptionThrowable() ); 
     129//        } 
     130//        catch( DigestException e ) 
     131//        { 
     132//            throw new WebException( WebException.WebCode.ERROR_ENCRYPT_PASSWORD, "This password cannot be encrypted" ); 
     133//        } 
     134//        catch( NoSuchAlgorithmException e ) 
     135//        { 
     136//            throw new WebException( WebException.WebCode.ERROR_ENCRYPT_PASSWORD, "This password cannot be encrypted" ); 
     137//        } 
     138// 
     139//        return new JSONObject(); 
     140//    } 
    167141 
    168142    /** 
    169      * This method create and send an email to the administrator to inform there is a new account to validate 
     143     * This method create and send an email to : 
     144     * - the administrator to inform there is a new created account 
     145     * - the user to inform his account is created 
    170146     * 
    171147     * @param user 
    172148     * @throws WebException 
    173149     */ 
    174     private void sendEmailToAdministrator( @NotNull final User user ) 
     150    private void sendEmailToAdministratorAndUser( @NotNull final User user ) 
    175151            throws WebException 
    176152    { 
    177153        try 
    178154        { 
    179             final MailFactory mailFactory = (MailFactory) getServletContext().getAttribute( "APP_MAILFACTORY" ); 
    180             final String from = (String) getServletContext().getAttribute( "APP_WEBMASTER" ); 
    181             final String toPI = (String) getServletContext().getAttribute( "APP_PI" ); 
    182             final String subject = "[MEGAPOLI] Nouvelle demande de compte utilisateur"; 
    183             final String content = "Hello Matthias,\n\nUne nouvelle demande de compte vient d'arriver.\n\n" + 
     155            final String host = getProperty( "mail.host" ); 
     156            final String webMaster = getProperty( "mail.webmaster" ); 
     157            final JavaMailSenderImpl sender = new JavaMailSenderImpl(); 
     158            sender.setHost( host ); 
     159 
     160            final MimeMessage message = sender.createMimeMessage(); 
     161            final MimeMessageHelper helper = new MimeMessageHelper( message ); 
     162 
     163            // Mail Admin 
     164            final String subjectAdmin = "[TAPAS] Nouveau compte utilisateur"; 
     165            final String contentAdmin = "Nouvelle création de compte : \n\n" + 
    184166                    "   - Nom : " + user.getLastName() + '\n' + 
    185167                    "   - Prénom : " + user.getFirstName() + '\n' + 
    186168                    "   - Email : " + user.getEmail() + "\n\n" + 
    187                     "Tu peux accepter ou refuser son inscription via le BO : http://ether.ipsl.jussieu.fr/megapoli/backoffice\n\n" + 
    188                     "Bonne soirée,\nLe serveur masqué"; 
    189  
    190             final Mail mailAdministrator = new Mail( from, from, null, content, subject ); 
    191             mailFactory.sendMail( mailAdministrator ); 
    192             final Mail mailPI = new Mail( from, toPI, null, content, subject ); 
    193             mailFactory.sendMail( mailPI ); 
     169                    "Le serveur de mail masqué"; 
     170 
     171            helper.setFrom( webMaster ); 
     172            helper.setTo( webMaster ); 
     173            helper.setSubject( subjectAdmin ); 
     174            helper.setText( contentAdmin ); 
     175            sender.send( message ); 
     176 
     177            // Mail User 
     178            final String subjectUser = "[TAPAS] New account"; 
     179            final String contentUser = "Dear " + user.getLastName() + "\n\n" + 
     180                    "Your account on TAPAS is now created. You can access to the website with your email as login and your password.\n\n" + 
     181                    "The TAPAS Administrator"; 
     182 
     183            helper.setFrom( webMaster ); 
     184            helper.setTo( user.getEmail() ); 
     185            helper.setSubject( subjectUser ); 
     186            helper.setText( contentUser ); 
     187            sender.send( message ); 
    194188        } 
    195189        catch( MessagingException e ) 
    196190        { 
    197             throw new WebException( WebException.WebCode.ERROR_EMAIL_CANNOT_BE_SEND, "The email cannot be send to the megapoli administrator" ); 
     191            throw new WebException( WebException.WebCode.ERROR_EMAIL_CANNOT_BE_SEND, "The email cannot be send to the TAPAS administrator or USER" ); 
    198192        } 
    199193    } 
    200194 
     195//    @ControllerMethod(downloadFile = DOWNLOAD_DATA_PROTOCOL_EN) 
     196//    public void downloadFileDataProtocolEn() 
     197//    { 
     198//    } 
     199// 
     200//    @ControllerMethod(downloadFile = DOWNLOAD_DATA_PROTOCOL_FR) 
     201//    public void downloadFileDataProtocolFr() 
     202//    { 
     203//    } 
     204// 
    201205    private static final Log LOGGER = LogFactory.getLog( ControllerEponge.class ); 
    202206 
    203     private static final String VIEW_ERRORS = "project/errors"; 
    204     private static final String VIEW_LOGIN = "project/login"; 
     207    private static final String VIEW_INIT = "init"; 
    205208    private static final String VIEW_DATA_PROTOCOL_EN = "project/home_dataProtocol_en"; 
    206     private static final String VIEW_DATA_PROTOCOL_FR = "project/home_dataProtocol_fr"; 
     209    // TODO : mettre les bons fichiers de Dataprotocol 
     210    private static final String DOWNLOAD_DATA_PROTOCOL_EN = "bib.txt"; 
     211    private static final String DOWNLOAD_DATA_PROTOCOL_FR = "bib.txt"; 
    207212} 
Note: See TracChangeset for help on using the changeset viewer.