1 |
49196
|
katerina.i
|
package eu.dnetlib.openaire.usermanagement;
|
2 |
|
|
|
3 |
|
|
import com.unboundid.ldap.sdk.LDAPException;
|
4 |
49406
|
sofia.balt
|
import eu.dnetlib.openaire.user.utils.EmailSender;
|
5 |
49196
|
katerina.i
|
import eu.dnetlib.openaire.user.utils.LDAPActions;
|
6 |
|
|
import org.apache.log4j.Logger;
|
7 |
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
8 |
|
|
import org.springframework.stereotype.Component;
|
9 |
|
|
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
|
10 |
|
|
|
11 |
|
|
import javax.servlet.ServletConfig;
|
12 |
|
|
import javax.servlet.ServletException;
|
13 |
|
|
import javax.servlet.http.HttpServlet;
|
14 |
|
|
import javax.servlet.http.HttpServletRequest;
|
15 |
|
|
import javax.servlet.http.HttpServletResponse;
|
16 |
|
|
import java.io.IOException;
|
17 |
|
|
|
18 |
|
|
/**
|
19 |
|
|
* Created by kiatrop on 2/10/2017.
|
20 |
|
|
*/
|
21 |
|
|
@Component
|
22 |
|
|
public class RemindUsernameServlet extends HttpServlet {
|
23 |
|
|
|
24 |
|
|
public void init(ServletConfig config) throws ServletException {
|
25 |
|
|
super.init(config);
|
26 |
|
|
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,
|
27 |
|
|
config.getServletContext());
|
28 |
|
|
}
|
29 |
|
|
|
30 |
|
|
@Autowired
|
31 |
|
|
private LDAPActions ldapActions;
|
32 |
|
|
|
33 |
49406
|
sofia.balt
|
@Autowired
|
34 |
|
|
private EmailSender emailSender;
|
35 |
49196
|
katerina.i
|
|
36 |
|
|
private Logger logger = Logger.getLogger(RemindUsernameServlet.class);
|
37 |
|
|
|
38 |
|
|
@Override
|
39 |
|
|
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
40 |
|
|
String formEmail = request.getParameter("email");
|
41 |
|
|
|
42 |
|
|
if (formEmail == null){
|
43 |
|
|
request.getSession().setAttribute("message", "Error reading email.");
|
44 |
|
|
response.sendRedirect("./remindUsername.jsp");
|
45 |
|
|
}
|
46 |
|
|
|
47 |
|
|
try{
|
48 |
|
|
String username = ldapActions.getUsername(formEmail);
|
49 |
49406
|
sofia.balt
|
if (username != null && !username.isEmpty()) {
|
50 |
49196
|
katerina.i
|
|
51 |
49406
|
sofia.balt
|
String verificationCodeMsg = "Hello,\n" +
|
52 |
|
|
"\n" + "A username reminder has been requested for your OpenAIRE account.\n" +
|
53 |
|
|
" Your username is "+ username + ". Thank you. ";
|
54 |
|
|
|
55 |
|
|
String verificationCodeSubject = "Your OpenAIRE username";
|
56 |
|
|
|
57 |
|
|
emailSender.sendEmail(formEmail, verificationCodeSubject, verificationCodeMsg);
|
58 |
|
|
|
59 |
49196
|
katerina.i
|
} else {
|
60 |
|
|
request.getSession().setAttribute("message", "User not found");
|
61 |
|
|
response.sendRedirect("./remindUsername.jsp");
|
62 |
|
|
}
|
63 |
|
|
|
64 |
|
|
} catch (LDAPException ldape) {
|
65 |
|
|
//TODO create error page
|
66 |
|
|
logger.error("Could not find user with email " + formEmail, ldape);
|
67 |
|
|
response.sendRedirect("./error.jsp");
|
68 |
|
|
}
|
69 |
|
|
}
|
70 |
|
|
|
71 |
|
|
}
|