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;
555 chandransh 16
import in.shop2020.model.v1.user.User;
416 rajveer 17
import in.shop2020.model.v1.user.UserContextException;
396 ashish 18
import in.shop2020.model.v1.user.UserContextService.Client;
419 rajveer 19
import in.shop2020.serving.utils.Utils;
396 ashish 20
import in.shop2020.thrift.clients.UserContextServiceClient;
21
 
22
import com.opensymphony.xwork2.ModelDriven;
23
 
545 rajveer 24
/**
25
 * Authantication class to authenticate the user.
26
 * 
27
 * @author rajveer
28
 *
29
 */
30
 
416 rajveer 31
public class AuthController extends BaseController implements ModelDriven<Object>{
396 ashish 32
 
33
	private Auth auth = new Auth();
34
 
35
	private UserContextServiceClient client = null;
36
 
37
	private int errorCode = 0;
38
	private String errorMessage;
39
 
398 ashish 40
	private String id;
41
 
517 rajveer 42
	private String nameOfUser;
416 rajveer 43
 
545 rajveer 44
	private static Log log = LogFactory.getLog(AuthController.class);
517 rajveer 45
 
396 ashish 46
	public AuthController(){
47
		try {
416 rajveer 48
			client = new UserContextServiceClient();
396 ashish 49
		} catch (Exception e) {
50
			e.printStackTrace();
51
		}
52
	}
53
 
54
	@Override
55
	public Object getModel() {
399 rajveer 56
		return this.auth;
396 ashish 57
	}
58
 
545 rajveer 59
	// POST /auth
396 ashish 60
	public HttpHeaders create(){
545 rajveer 61
		log.info(auth);
396 ashish 62
		Client c = client.getClient();
555 chandransh 63
		String userEmail = auth.getUserName();
64
		String password = auth.getPassword();
396 ashish 65
		try {
555 chandransh 66
			User user = c.authenticateUser(userEmail, password);
67
			if (user != null){
68
				long userId = user.getUserId();
416 rajveer 69
				c.setUserAsLoggedIn(userId, (new Date()).getTime());
70
 
419 rajveer 71
				userinfo.setUserId(userId);
72
				userinfo.setLoggedIn(true);
555 chandransh 73
				userinfo.setEmail(userEmail);
74
				userinfo.setNameOfUser(user.getName());
75
				this.nameOfUser = user.getName();
76
				c.mergeCart(userinfo.getCartId(), user.getActiveCartId());
77
				userinfo.setCartId(user.getActiveCartId());
78
				userinfo.setTotalItems(Utils.getNumberOfItemsInCart(userinfo.getCartId()));
79
 
419 rajveer 80
				this.session.setAttribute("userinfo", userinfo);
81
 
416 rajveer 82
				Cookie cookie1 = new Cookie("userid", userId+"");
555 chandransh 83
			    	this.response.addCookie(cookie1);
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);
396 ashish 99
		}
100
		return new DefaultHttpHeaders("lfail");
101
	}
102
 
103
	public int getErrorCode() {
104
		return errorCode;
105
	}
106
 
107
	public String getErrorMessage() {
108
		return errorMessage;
109
	}
398 ashish 110
 
111
	public String getId(){
112
		return id;
113
	}
114
 
399 rajveer 115
	public void setId(String id){
398 ashish 116
		this.id = id;
117
	}
401 ashish 118
 
119
	public Auth getAuth() {
120
		return auth;
121
	}
122
 
123
	public void setAuth(Auth auth) {
124
		this.auth = auth;
125
	}
449 rajveer 126
 
127
	public String getUserName(){
517 rajveer 128
		return nameOfUser;
449 rajveer 129
	}
396 ashish 130
}