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
 
7
import org.apache.struts2.convention.annotation.Result;
8
import org.apache.struts2.convention.annotation.Results;
396 ashish 9
import org.apache.struts2.rest.DefaultHttpHeaders;
10
import org.apache.struts2.rest.HttpHeaders;
11
import org.apache.thrift.TException;
12
 
13
import in.shop2020.model.v1.user.AuthenticationException;
416 rajveer 14
import in.shop2020.model.v1.user.UserContextException;
396 ashish 15
import in.shop2020.model.v1.user.UserContextService.Client;
419 rajveer 16
import in.shop2020.serving.utils.Utils;
396 ashish 17
import in.shop2020.thrift.clients.UserContextServiceClient;
18
 
19
import com.opensymphony.xwork2.ModelDriven;
20
 
416 rajveer 21
@Results({
22
    @Result(name="success", type="redirectAction", params = {"actionName" , 
458 rajveer 23
    		"entity/1000111"})
416 rajveer 24
})
25
public class AuthController extends BaseController implements ModelDriven<Object>{
396 ashish 26
 
27
	private Auth auth = new Auth();
28
 
29
	private UserContextServiceClient client = null;
30
 
31
	private int errorCode = 0;
32
	private String errorMessage;
33
 
398 ashish 34
	private String id;
35
 
416 rajveer 36
 
396 ashish 37
	public AuthController(){
38
		try {
416 rajveer 39
			client = new UserContextServiceClient();
396 ashish 40
		} catch (Exception e) {
41
			e.printStackTrace();
42
		}
43
	}
44
 
45
	@Override
46
	public Object getModel() {
399 rajveer 47
		return this.auth;
396 ashish 48
	}
49
 
419 rajveer 50
	public HttpHeaders show(){
51
		Client c = client.getClient();
52
 
53
		/*
54
		if(this.getId().equals("logout")){
55
			if(this.session.getAttribute("userid") != null 
56
					&& this.session.getAttribute("loggedin") != null
57
					&& this.session.getAttribute("loggedin").toString().equals("true")){
58
 
59
				long userId = Long.parseLong(this.session.getAttribute("userid").toString());
60
				try {
61
					c.setUserAsLoggedOut(userId, (new Date()).getTime());
62
				} catch (UserContextException e) {
63
					// TODO Auto-generated catch block
64
					e.printStackTrace();
65
				} catch (TException e) {
66
					// TODO Auto-generated catch block
67
					e.printStackTrace();
68
				}
69
 
70
				this.session.removeAttribute("userid");
71
				this.session.removeAttribute("loggedin");
72
				this.session.removeAttribute("issessionid");
73
				this.session.removeAttribute("email");
74
				return new DefaultHttpHeaders("lsuccess");
75
			}else{
76
				return new DefaultHttpHeaders("lfail");
77
			}
78
		}*/
79
 
80
		if(this.getId().equals("logout")){
81
			if(userinfo.isLoggedIn()){
82
				long userId = userinfo.getUserId();
83
 
84
				try {
85
					c.setUserAsLoggedOut(userId, (new Date()).getTime());
86
					this.session.removeAttribute("userinfo");
87
				} catch (UserContextException e) {
88
					// TODO Auto-generated catch block
89
					e.printStackTrace();
90
				} catch (TException e) {
91
					// TODO Auto-generated catch block
92
					e.printStackTrace();
93
				}
94
 
95
				return new DefaultHttpHeaders("lsuccess");
96
			}else{
97
				return new DefaultHttpHeaders("lfail");
98
			}
99
		}
100
 
101
 
102
		return new DefaultHttpHeaders("lfail");
103
 
104
	}
105
 
396 ashish 106
	public HttpHeaders create(){
398 ashish 107
		System.out.println(auth);
416 rajveer 108
		//return new DefaultHttpHeaders("lsuccess");
396 ashish 109
		//AUTH service to be invoked from here
416 rajveer 110
 
396 ashish 111
		Client c = client.getClient();
112
		try {
398 ashish 113
 
396 ashish 114
			if (c.authenticateUser(auth.getUserName(), auth.getPassword(), true)){
416 rajveer 115
				long userId = c.getContext(auth.getUserName(), auth.getPassword()).getId();
116
				c.setUserAsLoggedIn(userId, (new Date()).getTime());
117
 
419 rajveer 118
//				userinfo = (UserSessionInfo) this.session.getAttribute("userinfo");
416 rajveer 119
 
419 rajveer 120
				userinfo.setUserId(userId);
121
				userinfo.setLoggedIn(true);
424 rajveer 122
				userinfo.setEmail(auth.getUserName());
458 rajveer 123
				userinfo.setNameOfUser(Utils.getNameOfUser(userId));
124
//				if(userinfo.getCartId() == -1){
125
//					userinfo.setCartId(Utils.getCartId(userId));
126
//				}
419 rajveer 127
				this.session.setAttribute("userinfo", userinfo);
128
 
416 rajveer 129
				Cookie cookie1 = new Cookie("userid", userId+"");
130
		    	this.response.addCookie(cookie1);
396 ashish 131
				return new DefaultHttpHeaders("lsuccess");
132
			}
133
		} catch (AuthenticationException e) {
134
 
135
			errorCode = e.getErrorCode();
136
			errorMessage = e.getMessage();
137
 
138
		} catch (TException e) {
139
			// TODO Auto-generated catch block
140
			errorCode = -1;
141
			e.printStackTrace();
416 rajveer 142
		} catch (UserContextException e) {
143
			// TODO Auto-generated catch block
144
			errorCode = -1;
145
			e.printStackTrace();
396 ashish 146
		}
147
		return new DefaultHttpHeaders("lfail");
416 rajveer 148
 
396 ashish 149
	}
150
 
151
	public int getErrorCode() {
152
		return errorCode;
153
	}
154
 
155
	public String getErrorMessage() {
156
		return errorMessage;
157
	}
398 ashish 158
 
159
	public String getId(){
160
		return id;
161
	}
162
 
399 rajveer 163
	public void setId(String id){
398 ashish 164
		this.id = id;
165
	}
401 ashish 166
 
167
	public Auth getAuth() {
168
		return auth;
169
	}
170
 
171
	public void setAuth(Auth auth) {
172
		this.auth = auth;
173
	}
449 rajveer 174
 
175
	public String getUserName(){
176
		return userinfo.getNameOfUser();
177
	}
396 ashish 178
}