1 | package com.ether; |
---|
2 | |
---|
3 | import com.ether.annotation.ControllerMethod; |
---|
4 | import com.ether.annotation.ParamName; |
---|
5 | import com.ether.user.User; |
---|
6 | import net.sf.json.JSONObject; |
---|
7 | import org.apache.commons.logging.Log; |
---|
8 | import org.apache.commons.logging.LogFactory; |
---|
9 | import org.jetbrains.annotations.NotNull; |
---|
10 | import org.springframework.mail.javamail.JavaMailSenderImpl; |
---|
11 | import org.springframework.mail.javamail.MimeMessageHelper; |
---|
12 | |
---|
13 | import javax.mail.MessagingException; |
---|
14 | import javax.mail.internet.MimeMessage; |
---|
15 | import javax.servlet.http.HttpServletRequest; |
---|
16 | import java.util.HashMap; |
---|
17 | import java.util.Map; |
---|
18 | |
---|
19 | /** |
---|
20 | * @author vmipsl |
---|
21 | * @date 15 march 2012 |
---|
22 | */ |
---|
23 | public class ControllerEponge |
---|
24 | extends ControllerEther |
---|
25 | { |
---|
26 | /** *********************************************************** **/ |
---|
27 | /** *********************** VIEWS ***************************** **/ |
---|
28 | /** *********************************************************** **/ |
---|
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 |
---|
38 | { |
---|
39 | return new HashMap<String, Object>(); |
---|
40 | } |
---|
41 | |
---|
42 | /** *********************************************************** **/ |
---|
43 | /** *********************** CALLS ***************************** **/ |
---|
44 | /** *********************************************************** **/ |
---|
45 | /** |
---|
46 | * This method logs a user |
---|
47 | * |
---|
48 | * @param login |
---|
49 | * @param password |
---|
50 | * @param request |
---|
51 | * @return |
---|
52 | * @throws ServiceException |
---|
53 | */ |
---|
54 | @ControllerMethod(requestMandatory = true, jsonResult = true) |
---|
55 | public JSONObject login( @ParamName(ParameterConstants.PARAMETER_LOGIN) final String login, |
---|
56 | @ParamName(ParameterConstants.PARAMETER_PWD) final String password, |
---|
57 | @NotNull final HttpServletRequest request ) |
---|
58 | throws ServiceException |
---|
59 | { |
---|
60 | final JSONObject jSONUser = new JSONObject(); |
---|
61 | final JSONObject result = new JSONObject(); |
---|
62 | |
---|
63 | if( login != null && password != null ) |
---|
64 | { |
---|
65 | try |
---|
66 | { |
---|
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 | // } |
---|
80 | } |
---|
81 | catch( Exception e ) |
---|
82 | { |
---|
83 | result.put( "errors", "login.error.failed" ); |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | if( !jSONUser.isEmpty() ) |
---|
88 | result.put( "jSONUser", jSONUser ); |
---|
89 | return result; |
---|
90 | } |
---|
91 | |
---|
92 | @ControllerMethod(requestMandatory = true, jsonResult = true) |
---|
93 | public JSONObject logout( @NotNull final HttpServletRequest request ) |
---|
94 | throws ServiceException |
---|
95 | { |
---|
96 | request.getSession().setAttribute( "SES_USER", null ); |
---|
97 | request.getSession().invalidate(); |
---|
98 | |
---|
99 | return new JSONObject(); |
---|
100 | } |
---|
101 | |
---|
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 | // } |
---|
141 | |
---|
142 | /** |
---|
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 |
---|
146 | * |
---|
147 | * @param user |
---|
148 | * @throws WebException |
---|
149 | */ |
---|
150 | private void sendEmailToAdministratorAndUser( @NotNull final User user ) |
---|
151 | throws WebException |
---|
152 | { |
---|
153 | try |
---|
154 | { |
---|
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" + |
---|
166 | " - Nom : " + user.getLastName() + '\n' + |
---|
167 | " - Prénom : " + user.getFirstName() + '\n' + |
---|
168 | " - Email : " + user.getEmail() + "\n\n" + |
---|
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 ); |
---|
188 | } |
---|
189 | catch( MessagingException e ) |
---|
190 | { |
---|
191 | throw new WebException( WebException.WebCode.ERROR_EMAIL_CANNOT_BE_SEND, "The email cannot be send to the TAPAS administrator or USER" ); |
---|
192 | } |
---|
193 | } |
---|
194 | |
---|
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 | // |
---|
205 | private static final Log LOGGER = LogFactory.getLog( ControllerEponge.class ); |
---|
206 | |
---|
207 | private static final String VIEW_INIT = "init"; |
---|
208 | private static final String VIEW_DATA_PROTOCOL_EN = "project/home_dataProtocol_en"; |
---|
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"; |
---|
212 | } |
---|