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" , 
25
    		"entity"})
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);
124
				userinfo.setEmail(Utils.getEmailId(userId));
125
 
126
				this.session.setAttribute("userinfo", userinfo);
127
 
416 rajveer 128
				Cookie cookie1 = new Cookie("userid", userId+"");
129
		    	this.response.addCookie(cookie1);
396 ashish 130
				return new DefaultHttpHeaders("lsuccess");
131
			}
132
		} catch (AuthenticationException e) {
133
 
134
			errorCode = e.getErrorCode();
135
			errorMessage = e.getMessage();
136
 
137
		} catch (TException e) {
138
			// TODO Auto-generated catch block
139
			errorCode = -1;
140
			e.printStackTrace();
416 rajveer 141
		} catch (UserContextException e) {
142
			// TODO Auto-generated catch block
143
			errorCode = -1;
144
			e.printStackTrace();
396 ashish 145
		}
146
		return new DefaultHttpHeaders("lfail");
416 rajveer 147
 
396 ashish 148
	}
149
 
150
	public int getErrorCode() {
151
		return errorCode;
152
	}
153
 
154
	public String getErrorMessage() {
155
		return errorMessage;
156
	}
398 ashish 157
 
158
	public String getId(){
159
		return id;
160
	}
161
 
399 rajveer 162
	public void setId(String id){
398 ashish 163
		this.id = id;
164
	}
401 ashish 165
 
166
	public Auth getAuth() {
167
		return auth;
168
	}
169
 
170
	public void setAuth(Auth auth) {
171
		this.auth = auth;
172
	}
396 ashish 173
}