1 package com.instantbank.collections.security.web; 2 3 import java.io.IOException; 4 import java.io.OutputStreamWriter; 5 import java.io.PrintWriter; 6 import javax.servlet.RequestDispatcher; 7 import javax.servlet.ServletConfig; 8 import javax.servlet.ServletContext; 9 import javax.servlet.ServletException; 10 import javax.servlet.http.HttpServlet; 11 import javax.servlet.http.HttpServletRequest; 12 import javax.servlet.http.HttpServletResponse; 13 import javax.servlet.http.HttpSession; 14 import com.instantbank.collections.security.ejb.SecurityProfilesServices; 15 import com.instantbank.collections.security.ejb.SecurityProfilesServicesHome; 16 import com.instantbank.collections.security.ejb.SecurityServices; 17 import com.instantbank.collections.security.ejb.SecurityServicesHome; 18 import com.instantbank.collections.util.FilterChain; 19 import com.instantbank.collections.util.InstantbankException; 20 import com.instantbank.collections.util.ServiceLocator; 21 22 23 public class ChangePasswordController extends HttpServlet { 24 private Long companyId; 25 private String debug; 26 private Long maximunRows; 27 private SecurityProfilesServices PrfServices; 28 private SecurityServices SecServices; 29 private HttpSession session; 30 31 32 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 33 doPost(request, response); 34 } 35 36 37 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 38 String action = ""; 39 com.instantbank.collections.util.FilterChain chain; 40 41 debug = "set action"; 42 try { 43 action = request.getParameter("action"); 44 session = request.getSession(false); 45 session.setAttribute("hasException", "0"); 46 try { 47 chain = new com.instantbank.collections.util.FilterChain(true, getServletConfig().getServletContext()); 48 if(chain.processFilter(request, response)) { 49 return; 50 } 51 if(action.equals("initialize")) { 52 initialize(request, response); 53 } 54 else if(action.equals("ChangePassword")) { 55 changePassword(request, response); 56 } 57 else { 58 throw new InstantbankException("132001", "Action " + action + " not supported"); 59 } 60 } 61 catch(Exception e) { 62 throw new InstantbankException(e, "132002", "Failed to execute controller action " + action); 63 } 64 } 65 catch(InstantbankException e) { 66 session.setAttribute("hasException", "1"); 67 session.setAttribute("Exception", e); 68 if(response.isCommitted()) { 69 return; 70 } 71 response.sendRedirect("../main_web/ControllerError.jsp?showTechnical=0"); 72 } 73 } 74 75 76 public String getServletInfo() { 77 return "com.instantbank.collections.security.web.SecurityReportController Information"; 78 } 79 80 81 public void init(ServletConfig config) throws ServletException { 82 super.init(config); 83 try { 84 SecurityServicesHome SecHome = (SecurityServicesHome) 85 ServiceLocator.instance().createEJB("SecurityServicesHome", SecurityServicesHome.class, false); 86 SecServices = SecHome.create(); 87 SecurityProfilesServicesHome PrfHome = (SecurityProfilesServicesHome) 88 ServiceLocator.instance().createEJB("SecurityProfilesServicesHome", SecurityProfilesServicesHome.class, false); 89 PrfServices = PrfHome.create(); 90 } 91 catch(Exception e) { 92 throw new ServletException(e); 93 } 94 } 95 96 97 private void changePassword(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InstantbankException { 98 Long userId; 99 String newPassword; 100 String message; 101 companyId = (Long)session.getAttribute("companyId"); 102 userId = (Long)session.getAttribute("userId"); 103 newPassword = request.getParameter("newPassword"); 104 message = SecServices.updateUserPassword(userId, newPassword, companyId); 105 OutputStreamWriter osw = new OutputStreamWriter(response.getOutputStream()); 106 PrintWriter out = new PrintWriter(osw); 107 out.println("<script>"); 108 out.print("alert ('" + message + "');"); 109 out.print("top.navigate('../security_web/SecurityDispatcher.jsp?useCase=Home');"); 110 out.println("</script>"); 111 out.close(); 112 } 113 114 115 private void initialize(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, InstantbankException { 116 Long userId; 117 String oldPassword; 118 119 userId = (Long)session.getAttribute("userId"); 120 oldPassword = SecServices.getUserPassword(userId); 121 request.setAttribute("oldPassword", oldPassword); 122 ServletContext sc = getServletConfig().getServletContext(); 123 RequestDispatcher rd = sc.getRequestDispatcher("/security_web/ChangePasswordView.jsp"); 124 rd.forward(request, response); 125 } 126 } 127