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