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.services.UserSessionInfo;
17
import in.shop2020.serving.utils.Utils;
396 ashish 18
import in.shop2020.thrift.clients.UserContextServiceClient;
19
import in.shop2020.thrift.clients.UserProfileClient;
20
 
21
import com.opensymphony.xwork2.ModelDriven;
22
 
416 rajveer 23
@Results({
24
    @Result(name="success", type="redirectAction", params = {"actionName" , 
458 rajveer 25
    		"entity/1000111"})
416 rajveer 26
})
27
public class AuthController extends BaseController implements ModelDriven<Object>{
396 ashish 28
 
29
	private Auth auth = new Auth();
30
 
31
	private UserContextServiceClient client = null;
32
 
33
	private int errorCode = 0;
34
	private String errorMessage;
35
 
398 ashish 36
	private String id;
37
 
416 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));
126
//				if(userinfo.getCartId() == -1){
127
//					userinfo.setCartId(Utils.getCartId(userId));
128
//				}
419 rajveer 129
				this.session.setAttribute("userinfo", userinfo);
130
 
416 rajveer 131
				Cookie cookie1 = new Cookie("userid", userId+"");
132
		    	this.response.addCookie(cookie1);
396 ashish 133
				return new DefaultHttpHeaders("lsuccess");
134
			}
135
		} catch (AuthenticationException e) {
136
 
137
			errorCode = e.getErrorCode();
138
			errorMessage = e.getMessage();
139
 
140
		} catch (TException e) {
141
			// TODO Auto-generated catch block
142
			errorCode = -1;
143
			e.printStackTrace();
416 rajveer 144
		} catch (UserContextException e) {
145
			// TODO Auto-generated catch block
146
			errorCode = -1;
147
			e.printStackTrace();
396 ashish 148
		}
149
		return new DefaultHttpHeaders("lfail");
416 rajveer 150
 
396 ashish 151
	}
152
 
153
	public int getErrorCode() {
154
		return errorCode;
155
	}
156
 
157
	public String getErrorMessage() {
158
		return errorMessage;
159
	}
398 ashish 160
 
161
	public String getId(){
162
		return id;
163
	}
164
 
399 rajveer 165
	public void setId(String id){
398 ashish 166
		this.id = id;
167
	}
401 ashish 168
 
169
	public Auth getAuth() {
170
		return auth;
171
	}
172
 
173
	public void setAuth(Auth auth) {
174
		this.auth = auth;
175
	}
449 rajveer 176
 
177
	public String getUserName(){
178
		return userinfo.getNameOfUser();
179
	}
396 ashish 180
}