| 396 |
ashish |
1 |
package in.shop2020.serving.controllers;
|
|
|
2 |
|
| 416 |
rajveer |
3 |
import java.util.Date;
|
|
|
4 |
|
|
|
5 |
import javax.servlet.http.Cookie;
|
|
|
6 |
|
| 545 |
rajveer |
7 |
import org.apache.juli.logging.Log;
|
|
|
8 |
import org.apache.juli.logging.LogFactory;
|
| 416 |
rajveer |
9 |
import org.apache.struts2.convention.annotation.Result;
|
|
|
10 |
import org.apache.struts2.convention.annotation.Results;
|
| 396 |
ashish |
11 |
import org.apache.struts2.rest.DefaultHttpHeaders;
|
|
|
12 |
import org.apache.struts2.rest.HttpHeaders;
|
|
|
13 |
import org.apache.thrift.TException;
|
|
|
14 |
|
|
|
15 |
import in.shop2020.model.v1.user.AuthenticationException;
|
| 416 |
rajveer |
16 |
import in.shop2020.model.v1.user.UserContextException;
|
| 396 |
ashish |
17 |
import in.shop2020.model.v1.user.UserContextService.Client;
|
| 419 |
rajveer |
18 |
import in.shop2020.serving.utils.Utils;
|
| 396 |
ashish |
19 |
import in.shop2020.thrift.clients.UserContextServiceClient;
|
|
|
20 |
|
|
|
21 |
import com.opensymphony.xwork2.ModelDriven;
|
|
|
22 |
|
| 545 |
rajveer |
23 |
/**
|
|
|
24 |
* Authantication class to authenticate the user.
|
|
|
25 |
*
|
|
|
26 |
* @author rajveer
|
|
|
27 |
*
|
|
|
28 |
*/
|
|
|
29 |
|
| 416 |
rajveer |
30 |
public class AuthController extends BaseController implements ModelDriven<Object>{
|
| 396 |
ashish |
31 |
|
|
|
32 |
private Auth auth = new Auth();
|
|
|
33 |
|
|
|
34 |
private UserContextServiceClient client = null;
|
|
|
35 |
|
|
|
36 |
private int errorCode = 0;
|
|
|
37 |
private String errorMessage;
|
|
|
38 |
|
| 398 |
ashish |
39 |
private String id;
|
|
|
40 |
|
| 517 |
rajveer |
41 |
private String nameOfUser;
|
| 416 |
rajveer |
42 |
|
| 545 |
rajveer |
43 |
private static Log log = LogFactory.getLog(AuthController.class);
|
| 517 |
rajveer |
44 |
|
| 396 |
ashish |
45 |
public AuthController(){
|
|
|
46 |
try {
|
| 416 |
rajveer |
47 |
client = new UserContextServiceClient();
|
| 396 |
ashish |
48 |
} catch (Exception e) {
|
|
|
49 |
e.printStackTrace();
|
|
|
50 |
}
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
@Override
|
|
|
54 |
public Object getModel() {
|
| 399 |
rajveer |
55 |
return this.auth;
|
| 396 |
ashish |
56 |
}
|
|
|
57 |
|
| 545 |
rajveer |
58 |
// POST /auth
|
| 396 |
ashish |
59 |
public HttpHeaders create(){
|
| 545 |
rajveer |
60 |
log.info(auth);
|
| 396 |
ashish |
61 |
Client c = client.getClient();
|
|
|
62 |
try {
|
|
|
63 |
if (c.authenticateUser(auth.getUserName(), auth.getPassword(), true)){
|
| 416 |
rajveer |
64 |
long userId = c.getContext(auth.getUserName(), auth.getPassword()).getId();
|
|
|
65 |
c.setUserAsLoggedIn(userId, (new Date()).getTime());
|
|
|
66 |
|
| 419 |
rajveer |
67 |
userinfo.setUserId(userId);
|
|
|
68 |
userinfo.setLoggedIn(true);
|
| 424 |
rajveer |
69 |
userinfo.setEmail(auth.getUserName());
|
| 458 |
rajveer |
70 |
userinfo.setNameOfUser(Utils.getNameOfUser(userId));
|
| 517 |
rajveer |
71 |
this.nameOfUser = Utils.getNameOfUser(userId);
|
| 536 |
rajveer |
72 |
if(userinfo.getCartId() == -1){
|
|
|
73 |
userinfo.setCartId(Utils.getCartId(userId));
|
|
|
74 |
userinfo.setTotalItems(Utils.getNumberOfItemsInCart(userinfo.getCartId()));
|
|
|
75 |
}else{
|
|
|
76 |
Utils.mergeCarts(userinfo.getCartId(), Utils.getCartId(userId));
|
|
|
77 |
userinfo.setCartId(Utils.getCartId(userId));
|
|
|
78 |
userinfo.setTotalItems(Utils.getNumberOfItemsInCart(userinfo.getCartId()));
|
|
|
79 |
}
|
| 419 |
rajveer |
80 |
this.session.setAttribute("userinfo", userinfo);
|
|
|
81 |
|
| 416 |
rajveer |
82 |
Cookie cookie1 = new Cookie("userid", userId+"");
|
|
|
83 |
this.response.addCookie(cookie1);
|
| 396 |
ashish |
84 |
return new DefaultHttpHeaders("lsuccess");
|
|
|
85 |
}
|
|
|
86 |
} catch (AuthenticationException e) {
|
|
|
87 |
|
|
|
88 |
errorCode = e.getErrorCode();
|
|
|
89 |
errorMessage = e.getMessage();
|
| 545 |
rajveer |
90 |
log.error("Exception number is: " + errorCode + "Exception message is:" + errorMessage);
|
| 396 |
ashish |
91 |
} catch (TException e) {
|
|
|
92 |
errorCode = -1;
|
|
|
93 |
e.printStackTrace();
|
| 545 |
rajveer |
94 |
log.error("Exception number is: " + errorCode + "Exception message is:" + errorMessage);
|
| 416 |
rajveer |
95 |
} catch (UserContextException e) {
|
| 545 |
rajveer |
96 |
errorCode = e.getErrorCode();
|
| 416 |
rajveer |
97 |
e.printStackTrace();
|
| 545 |
rajveer |
98 |
log.error("Exception number is: " + errorCode + "Exception message is:" + errorMessage);
|
| 396 |
ashish |
99 |
}
|
|
|
100 |
return new DefaultHttpHeaders("lfail");
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
public int getErrorCode() {
|
|
|
104 |
return errorCode;
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
public String getErrorMessage() {
|
|
|
108 |
return errorMessage;
|
|
|
109 |
}
|
| 398 |
ashish |
110 |
|
|
|
111 |
public String getId(){
|
|
|
112 |
return id;
|
|
|
113 |
}
|
|
|
114 |
|
| 399 |
rajveer |
115 |
public void setId(String id){
|
| 398 |
ashish |
116 |
this.id = id;
|
|
|
117 |
}
|
| 401 |
ashish |
118 |
|
|
|
119 |
public Auth getAuth() {
|
|
|
120 |
return auth;
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
public void setAuth(Auth auth) {
|
|
|
124 |
this.auth = auth;
|
|
|
125 |
}
|
| 449 |
rajveer |
126 |
|
|
|
127 |
public String getUserName(){
|
| 517 |
rajveer |
128 |
return nameOfUser;
|
| 449 |
rajveer |
129 |
}
|
| 396 |
ashish |
130 |
}
|