Subversion Repositories SmartDukaan

Rev

Rev 555 | 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.User;
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();
                String userEmail = auth.getUserName();
                String password = auth.getPassword();
                try {
                        User user = c.authenticateUser(userEmail, password);
                        if (user != null){
                                long userId = user.getUserId();
                                c.setUserAsLoggedIn(userId, (new Date()).getTime());

                                userinfo.setUserId(userId);
                                userinfo.setLoggedIn(true);
                                userinfo.setEmail(userEmail);
                                userinfo.setNameOfUser(user.getName());
                                this.nameOfUser = user.getName();
                                c.mergeCart(userinfo.getCartId(), user.getActiveCartId());
                                userinfo.setCartId(user.getActiveCartId());
                                userinfo.setTotalItems(Utils.getNumberOfItemsInCart(userinfo.getCartId()));

                                this.session.setAttribute("userinfo", userinfo);

                                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;
        }
}