Subversion Repositories SmartDukaan

Rev

Rev 536 | Blame | Last modification | View Log | RSS feed

package in.shop2020.serving.controllers;

import java.util.Date;

import javax.servlet.http.Cookie;

import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
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;
import org.apache.thrift.TException;

import in.shop2020.model.v1.user.AuthenticationException;
import in.shop2020.model.v1.user.UserContextException;
import in.shop2020.model.v1.user.UserContextService.Client;
import in.shop2020.serving.utils.Utils;
import in.shop2020.thrift.clients.UserContextServiceClient;

import com.opensymphony.xwork2.ModelDriven;

/**
 * Authantication class to authenticate the user.
 * 
 * @author rajveer
 *
 */

public class AuthController extends BaseController implements ModelDriven<Object>{

        private Auth auth = new Auth();
        
        private UserContextServiceClient client = null;
        
        private int errorCode = 0;
        private String errorMessage;
        
        private String id;
        
        private String nameOfUser;
        
        private static Log log = LogFactory.getLog(AuthController.class);
        
        public AuthController(){
                try {
                        client = new UserContextServiceClient();
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
        
        @Override
        public Object getModel() {
                return this.auth;
        }
        
        // POST /auth
        public HttpHeaders create(){
                log.info(auth);
                Client c = client.getClient();
                try {
                        if (c.authenticateUser(auth.getUserName(), auth.getPassword(), true)){
                                long userId = c.getContext(auth.getUserName(), auth.getPassword()).getId();
                                c.setUserAsLoggedIn(userId, (new Date()).getTime());

                                userinfo.setUserId(userId);
                                userinfo.setLoggedIn(true);
                                userinfo.setEmail(auth.getUserName());
                                userinfo.setNameOfUser(Utils.getNameOfUser(userId));
                                this.nameOfUser = Utils.getNameOfUser(userId);
                                if(userinfo.getCartId() == -1){
                                        userinfo.setCartId(Utils.getCartId(userId));
                                        userinfo.setTotalItems(Utils.getNumberOfItemsInCart(userinfo.getCartId()));
                                }else{
                                        Utils.mergeCarts(userinfo.getCartId(), Utils.getCartId(userId));
                                        userinfo.setCartId(Utils.getCartId(userId));
                                        userinfo.setTotalItems(Utils.getNumberOfItemsInCart(userinfo.getCartId()));
                                }
                                this.session.setAttribute("userinfo", userinfo);

                                Cookie cookie1 = new Cookie("userid", userId+"");
                        this.response.addCookie(cookie1);
                                return new DefaultHttpHeaders("lsuccess");
                        }
                } catch (AuthenticationException e) {
                        
                        errorCode = e.getErrorCode();
                        errorMessage = e.getMessage();
                        log.error("Exception number is: " +  errorCode + "Exception message is:" + errorMessage);
                } catch (TException e) {
                        errorCode = -1;
                        e.printStackTrace();
                        log.error("Exception number is: " +  errorCode + "Exception message is:" + errorMessage);
                } catch (UserContextException e) {
                        errorCode = e.getErrorCode();
                        e.printStackTrace();
                        log.error("Exception number is: " +  errorCode + "Exception message is:" + errorMessage);
                }
                return new DefaultHttpHeaders("lfail");
        }

        public int getErrorCode() {
                return errorCode;
        }

        public String getErrorMessage() {
                return errorMessage;
        }
        
        public String getId(){
                return id;
        }
        
        public void setId(String id){
                this.id = id;
        }

        public Auth getAuth() {
                return auth;
        }

        public void setAuth(Auth auth) {
                this.auth = auth;
        }
        
        public String getUserName(){
                return nameOfUser;
        }
}