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