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
 
396 ashish 82
				return new DefaultHttpHeaders("lsuccess");
83
			}
84
		} catch (AuthenticationException e) {
85
 
86
			errorCode = e.getErrorCode();
87
			errorMessage = e.getMessage();
545 rajveer 88
			log.error("Exception number is: " +  errorCode + "Exception message is:" + errorMessage);
396 ashish 89
		} catch (TException e) {
90
			errorCode = -1;
91
			e.printStackTrace();
545 rajveer 92
			log.error("Exception number is: " +  errorCode + "Exception message is:" + errorMessage);
416 rajveer 93
		} catch (UserContextException e) {
545 rajveer 94
			errorCode = e.getErrorCode();
416 rajveer 95
			e.printStackTrace();
545 rajveer 96
			log.error("Exception number is: " +  errorCode + "Exception message is:" + errorMessage);
396 ashish 97
		}
98
		return new DefaultHttpHeaders("lfail");
99
	}
100
 
101
	public int getErrorCode() {
102
		return errorCode;
103
	}
104
 
105
	public String getErrorMessage() {
106
		return errorMessage;
107
	}
398 ashish 108
 
109
	public String getId(){
110
		return id;
111
	}
112
 
399 rajveer 113
	public void setId(String id){
398 ashish 114
		this.id = id;
115
	}
401 ashish 116
 
117
	public Auth getAuth() {
118
		return auth;
119
	}
120
 
121
	public void setAuth(Auth auth) {
122
		this.auth = auth;
123
	}
449 rajveer 124
 
125
	public String getUserName(){
517 rajveer 126
		return nameOfUser;
449 rajveer 127
	}
396 ashish 128
}