Subversion Repositories SmartDukaan

Rev

Rev 2949 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.serving.controllers;


import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import in.shop2020.model.v1.user.UserContextService.Client;
import in.shop2020.serving.controllers.BaseController;
import in.shop2020.serving.utils.DesEncrypter;
import in.shop2020.thrift.clients.HelperClient;
import in.shop2020.thrift.clients.UserClient;
import in.shop2020.utils.Mail;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.rest.DefaultHttpHeaders;
import org.apache.struts2.rest.HttpHeaders;

@Results({
    @Result(name="failure", type="redirectAction", 
                params = {"actionName" , "forgot-password"})
})
public class ForgotPasswordController extends BaseController{
        
        private static final long serialVersionUID = 1L;
        private static final String chars = "abcdefghijklmonpqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    private static final Random random = new Random();
    private static final int LENGTH = 10;
    
        private static Logger log = Logger.getLogger(Class.class);
        private DesEncrypter desEncrypter = new DesEncrypter("saholic");
        
    
                public ForgotPasswordController(){
                        super();        
                }
                
                 // GET /Forgot password page
                 public HttpHeaders index() {           
                        return new DefaultHttpHeaders("index").disableCaching();
                 }

                // POST /Forgot password
                public String create() {
                log.info("ForgotPasswordController.create");
                String emailId = request.getParameter("emailId");
                if(emailId != null){
                        UserClient userContextServiceClient;
                                try {
                                        userContextServiceClient = new UserClient();
                                Client client = userContextServiceClient.getClient();
                                if(!client.userExists(emailId)){
                                        addActionError("Email address is not registered with us.");
                                        return "failure";
                                }
                                String newPassword = generateNewPassword();
                                String encryptedPassword =   desEncrypter.encrypt(newPassword);
                                if(client.forgotPassword(emailId, encryptedPassword)){
                                        if(mailNewPassword(emailId, newPassword)){
                                                return "success";
                                        }else{
                                                return "failure";
                                        }
                                        
                                }else{
                                        addActionError("Email address is not registered with us.");
                                        return "failure";
                                }
                                } catch (Exception e) {
                                        log.error("Unexpected error while processing forgot password request", e);
                                        addActionError("Something went wrong. Try again.");
                                }
                }
                return "failure";
            }
                
                private boolean mailNewPassword(String emailId, String newPassword) {
                        List<String> toList = new ArrayList<String>();
                        toList.add(emailId);
                        
                        HelperClient helperServiceClient = null;
                        try {
                                helperServiceClient = new HelperClient();
                                in.shop2020.utils.HelperService.Client client = helperServiceClient.getClient();
                                Mail mail = new Mail();
                                mail.setSubject("Password reset request");
                                mail.setTo(toList);
                                mail.setData("Your new password is: " + newPassword);
                                client.sendMail(mail);
                        } catch (Exception e) {
                                log.error("Unexpected error while mailing the new password");
                                return false;
                        }
                        return true;
                }
                
                private static String generateNewPassword() {
                    char[] buf = new char[LENGTH];
                for (int i = 0; i < buf.length; i++) {
                    buf[i] = chars.charAt(random.nextInt(chars.length()));
                }
                return new String(buf);
            }
                 
}