Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
396 ashish 1
package in.shop2020.serving.controllers;
2
 
416 rajveer 3
import java.util.Date;
4
 
5
import javax.servlet.http.Cookie;
6
 
545 rajveer 7
import org.apache.juli.logging.Log;
8
import org.apache.juli.logging.LogFactory;
416 rajveer 9
import org.apache.struts2.convention.annotation.Result;
10
import org.apache.struts2.convention.annotation.Results;
396 ashish 11
import org.apache.struts2.rest.DefaultHttpHeaders;
12
import org.apache.struts2.rest.HttpHeaders;
13
import org.apache.thrift.TException;
14
 
15
import in.shop2020.model.v1.user.AuthenticationException;
762 rajveer 16
import in.shop2020.model.v1.user.ShoppingCartException;
555 chandransh 17
import in.shop2020.model.v1.user.User;
416 rajveer 18
import in.shop2020.model.v1.user.UserContextException;
396 ashish 19
import in.shop2020.model.v1.user.UserContextService.Client;
419 rajveer 20
import in.shop2020.serving.utils.Utils;
396 ashish 21
import in.shop2020.thrift.clients.UserContextServiceClient;
22
 
23
import com.opensymphony.xwork2.ModelDriven;
24
 
545 rajveer 25
/**
26
 * Authantication class to authenticate the user.
27
 * 
28
 * @author rajveer
29
 *
30
 */
31
 
416 rajveer 32
public class AuthController extends BaseController implements ModelDriven<Object>{
396 ashish 33
 
34
	private Auth auth = new Auth();
35
 
36
	private UserContextServiceClient client = null;
37
 
38
	private int errorCode = 0;
39
	private String errorMessage;
40
 
398 ashish 41
	private String id;
42
 
517 rajveer 43
	private String nameOfUser;
416 rajveer 44
 
545 rajveer 45
	private static Log log = LogFactory.getLog(AuthController.class);
517 rajveer 46
 
396 ashish 47
	public AuthController(){
48
		try {
416 rajveer 49
			client = new UserContextServiceClient();
396 ashish 50
		} catch (Exception e) {
51
			e.printStackTrace();
52
		}
53
	}
54
 
55
	@Override
56
	public Object getModel() {
399 rajveer 57
		return this.auth;
396 ashish 58
	}
59
 
545 rajveer 60
	// POST /auth
396 ashish 61
	public HttpHeaders create(){
545 rajveer 62
		log.info(auth);
396 ashish 63
		Client c = client.getClient();
555 chandransh 64
		String userEmail = auth.getUserName();
65
		String password = auth.getPassword();
396 ashish 66
		try {
555 chandransh 67
			User user = c.authenticateUser(userEmail, password);
68
			if (user != null){
69
				long userId = user.getUserId();
416 rajveer 70
				c.setUserAsLoggedIn(userId, (new Date()).getTime());
71
 
419 rajveer 72
				userinfo.setUserId(userId);
73
				userinfo.setLoggedIn(true);
555 chandransh 74
				userinfo.setEmail(userEmail);
75
				userinfo.setNameOfUser(user.getName());
76
				this.nameOfUser = user.getName();
77
				c.mergeCart(userinfo.getCartId(), user.getActiveCartId());
78
				userinfo.setCartId(user.getActiveCartId());
762 rajveer 79
				int totalItems = c.getCart(user.getActiveCartId()).getLinesSize();
80
				userinfo.setTotalItems(totalItems);
555 chandransh 81
 
419 rajveer 82
				this.session.setAttribute("userinfo", userinfo);
83
 
396 ashish 84
				return new DefaultHttpHeaders("lsuccess");
85
			}
86
		} catch (AuthenticationException e) {
87
 
88
			errorCode = e.getErrorCode();
89
			errorMessage = e.getMessage();
545 rajveer 90
			log.error("Exception number is: " +  errorCode + "Exception message is:" + errorMessage);
396 ashish 91
		} catch (TException e) {
92
			errorCode = -1;
93
			e.printStackTrace();
545 rajveer 94
			log.error("Exception number is: " +  errorCode + "Exception message is:" + errorMessage);
416 rajveer 95
		} catch (UserContextException e) {
545 rajveer 96
			errorCode = e.getErrorCode();
416 rajveer 97
			e.printStackTrace();
545 rajveer 98
			log.error("Exception number is: " +  errorCode + "Exception message is:" + errorMessage);
762 rajveer 99
		} catch (ShoppingCartException e) {
100
			// TODO Auto-generated catch block
101
			e.printStackTrace();
396 ashish 102
		}
103
		return new DefaultHttpHeaders("lfail");
104
	}
105
 
106
	public int getErrorCode() {
107
		return errorCode;
108
	}
109
 
110
	public String getErrorMessage() {
111
		return errorMessage;
112
	}
398 ashish 113
 
114
	public String getId(){
115
		return id;
116
	}
117
 
399 rajveer 118
	public void setId(String id){
398 ashish 119
		this.id = id;
120
	}
401 ashish 121
 
122
	public Auth getAuth() {
123
		return auth;
124
	}
125
 
126
	public void setAuth(Auth auth) {
127
		this.auth = auth;
128
	}
449 rajveer 129
 
130
	public String getUserName(){
517 rajveer 131
		return nameOfUser;
449 rajveer 132
	}
396 ashish 133
}