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);
424 rajveer 124
				userinfo.setEmail(auth.getUserName());
125
				//userinfo.setNameOfUser(Utils.getNameOfUser(userId));
419 rajveer 126
 
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
	}
396 ashish 174
}