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;
16
import in.shop2020.thrift.clients.UserContextServiceClient;
17
import in.shop2020.thrift.clients.UserProfileClient;
18
 
19
import com.opensymphony.xwork2.ModelDriven;
20
 
416 rajveer 21
@Results({
22
    @Result(name="success", type="redirectAction", params = {"actionName" , 
23
    		"entity"})
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
 
50
	public HttpHeaders create(){
398 ashish 51
		System.out.println(auth);
416 rajveer 52
		//return new DefaultHttpHeaders("lsuccess");
396 ashish 53
		//AUTH service to be invoked from here
416 rajveer 54
 
396 ashish 55
		Client c = client.getClient();
56
		try {
398 ashish 57
 
396 ashish 58
			if (c.authenticateUser(auth.getUserName(), auth.getPassword(), true)){
416 rajveer 59
				long userId = c.getContext(auth.getUserName(), auth.getPassword()).getId();
60
				c.setUserAsLoggedIn(userId, (new Date()).getTime());
61
				this.session.removeAttribute("useid");
62
				this.session.removeAttribute("loggedin");
63
				this.session.removeAttribute("issessionid");
64
 
65
				this.session.setAttribute("userid", userId+"");
66
				this.session.setAttribute("loggedin", "true");
67
				this.session.setAttribute("issessionid", "false");
68
 
69
				Cookie cookie1 = new Cookie("userid", userId+"");
70
		    	this.response.addCookie(cookie1);
71
 
396 ashish 72
				return new DefaultHttpHeaders("lsuccess");
73
			}
74
		} catch (AuthenticationException e) {
75
 
76
			errorCode = e.getErrorCode();
77
			errorMessage = e.getMessage();
78
 
79
		} catch (TException e) {
80
			// TODO Auto-generated catch block
81
			errorCode = -1;
82
			e.printStackTrace();
416 rajveer 83
		} catch (UserContextException e) {
84
			// TODO Auto-generated catch block
85
			errorCode = -1;
86
			e.printStackTrace();
396 ashish 87
		}
88
		return new DefaultHttpHeaders("lfail");
416 rajveer 89
 
396 ashish 90
	}
91
 
92
	public int getErrorCode() {
93
		return errorCode;
94
	}
95
 
96
	public String getErrorMessage() {
97
		return errorMessage;
98
	}
398 ashish 99
 
100
	public String getId(){
101
		return id;
102
	}
103
 
399 rajveer 104
	public void setId(String id){
398 ashish 105
		this.id = id;
106
	}
401 ashish 107
 
108
	public Auth getAuth() {
109
		return auth;
110
	}
111
 
112
	public void setAuth(Auth auth) {
113
		this.auth = auth;
114
	}
396 ashish 115
}