| 317 |
ashish |
1 |
/**
|
|
|
2 |
*
|
|
|
3 |
*/
|
|
|
4 |
|
|
|
5 |
|
|
|
6 |
package in.shop2020.serving.controllers;
|
| 410 |
rajveer |
7 |
|
| 416 |
rajveer |
8 |
import in.shop2020.model.v1.user.UserContext;
|
|
|
9 |
import in.shop2020.model.v1.user.UserContextException;
|
|
|
10 |
import in.shop2020.thrift.clients.ShoppingCartClient;
|
|
|
11 |
import in.shop2020.thrift.clients.UserContextServiceClient;
|
|
|
12 |
|
| 410 |
rajveer |
13 |
import java.util.Map;
|
|
|
14 |
|
| 416 |
rajveer |
15 |
import javax.servlet.http.Cookie;
|
|
|
16 |
import javax.servlet.http.HttpServletRequest;
|
| 410 |
rajveer |
17 |
import javax.servlet.http.HttpServletResponse;
|
| 416 |
rajveer |
18 |
import javax.servlet.http.HttpSession;
|
| 410 |
rajveer |
19 |
|
| 416 |
rajveer |
20 |
import org.apache.juli.logging.Log;
|
|
|
21 |
import org.apache.juli.logging.LogFactory;
|
| 410 |
rajveer |
22 |
import org.apache.struts2.interceptor.CookiesAware;
|
| 416 |
rajveer |
23 |
import org.apache.struts2.interceptor.ServletRequestAware;
|
| 410 |
rajveer |
24 |
import org.apache.struts2.interceptor.ServletResponseAware;
|
| 416 |
rajveer |
25 |
import org.apache.struts2.interceptor.SessionAware;
|
|
|
26 |
import org.apache.thrift.TException;
|
| 410 |
rajveer |
27 |
|
| 317 |
ashish |
28 |
/**
|
|
|
29 |
* Base class for all user action handlers i.e. controllers
|
|
|
30 |
*
|
|
|
31 |
* @author naveen
|
|
|
32 |
*
|
|
|
33 |
*/
|
| 416 |
rajveer |
34 |
public abstract class BaseController implements CookiesAware, ServletResponseAware, ServletRequestAware {
|
| 410 |
rajveer |
35 |
private Map cookiesMap;
|
| 416 |
rajveer |
36 |
protected HttpServletResponse response;
|
|
|
37 |
protected HttpServletRequest request;
|
|
|
38 |
protected HttpSession session;
|
|
|
39 |
// private boolean isLoggedIn = ;
|
|
|
40 |
// private boolean isSessionId = true;
|
|
|
41 |
// long userId = 0;
|
|
|
42 |
Cookie tempCookie = null;
|
|
|
43 |
|
|
|
44 |
private static Log log = LogFactory.getLog(BaseController.class);
|
|
|
45 |
|
|
|
46 |
public BaseController() {
|
|
|
47 |
//setSessionAndCookies();
|
|
|
48 |
// TODO Auto-generated constructor stub
|
|
|
49 |
}
|
| 410 |
rajveer |
50 |
public Map getCookiesMap() {
|
|
|
51 |
return cookiesMap;
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
@Override
|
|
|
55 |
public void setCookiesMap(Map cookiesMap) {
|
|
|
56 |
this.cookiesMap = cookiesMap;
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
@Override
|
|
|
60 |
public void setServletResponse(HttpServletResponse response)
|
|
|
61 |
{
|
|
|
62 |
this.response = response;
|
| 416 |
rajveer |
63 |
if(tempCookie != null){
|
|
|
64 |
this.response.addCookie(tempCookie);
|
|
|
65 |
}
|
|
|
66 |
//removeSessionOnLogout();
|
| 410 |
rajveer |
67 |
}
|
| 416 |
rajveer |
68 |
|
|
|
69 |
@Override
|
|
|
70 |
public void setServletRequest(HttpServletRequest request){
|
|
|
71 |
this.request = request;
|
|
|
72 |
this.session = request.getSession();
|
|
|
73 |
|
|
|
74 |
setSessionAndCookies();
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
public void removeSessionOnLogout(){
|
|
|
78 |
if(this.session.getAttribute("loggedin") != null ){
|
|
|
79 |
this.session.removeAttribute("loggedin");
|
|
|
80 |
}
|
|
|
81 |
if(this.session.getAttribute("userid") != null){
|
|
|
82 |
this.session.removeAttribute("userid");
|
|
|
83 |
}
|
|
|
84 |
if( this.session.getAttribute("issessionid") != null){
|
|
|
85 |
this.session.removeAttribute("issessionid");
|
|
|
86 |
}
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
public void setSessionAndCookies(){
|
|
|
90 |
|
|
|
91 |
/*Following steps will be executed on each of the requests
|
|
|
92 |
* Check Session:
|
|
|
93 |
* IF user is loggedin
|
|
|
94 |
* - OK. Nothing to do. Allow access to the resources
|
|
|
95 |
* ELSE check cookies
|
|
|
96 |
* IF UserID is set
|
|
|
97 |
* IF User is logged into backend.
|
|
|
98 |
* Set user as loogedin in Session.
|
|
|
99 |
* Set SessionID in session
|
|
|
100 |
* ELSE
|
|
|
101 |
* Remove UserID from cookies.
|
|
|
102 |
* ELSE
|
|
|
103 |
* IF SessionID is set
|
|
|
104 |
* OK....set session id in session also
|
|
|
105 |
* ELSE Create SessionID and Set SessionID in cookies and Session
|
|
|
106 |
* ELSE IF SessionID is set in session
|
|
|
107 |
* - OK.
|
|
|
108 |
*
|
|
|
109 |
*/
|
|
|
110 |
|
|
|
111 |
String loggedIn = null;
|
|
|
112 |
String userId = null;
|
|
|
113 |
String isSessionId = null;
|
|
|
114 |
|
|
|
115 |
|
|
|
116 |
if(this.session.getAttribute("loggedin") != null ){
|
|
|
117 |
loggedIn = this.session.getAttribute("loggedin").toString();
|
|
|
118 |
}
|
|
|
119 |
if(this.session.getAttribute("userid") != null){
|
|
|
120 |
userId = this.session.getAttribute("userid").toString();
|
|
|
121 |
}
|
|
|
122 |
if( this.session.getAttribute("issessionid") != null){
|
|
|
123 |
isSessionId = this.session.getAttribute("issessionid").toString();
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
|
|
|
127 |
|
|
|
128 |
|
|
|
129 |
if(loggedIn != null && isSessionId != null && userId != null ){
|
|
|
130 |
if(loggedIn.equals("true")){
|
|
|
131 |
if(isSessionId.equals("false")){
|
|
|
132 |
System.out.println("Logged on user id is :" + userId );
|
|
|
133 |
}else{
|
|
|
134 |
// if we reached here something is wrong.
|
|
|
135 |
System.out.println("Something went wrong... Dude !!!");
|
|
|
136 |
}
|
|
|
137 |
}else{
|
|
|
138 |
if(isSessionId.equals("true")){
|
|
|
139 |
System.out.println("Session is already set with sessionid :" + userId );
|
|
|
140 |
}else{
|
|
|
141 |
// if we reached here something is wrong.
|
|
|
142 |
System.out.println("Something went wrong... Dude !!!");
|
|
|
143 |
}
|
|
|
144 |
}
|
|
|
145 |
}
|
|
|
146 |
else if(!getLoginSessionInfoFromCookies()){
|
|
|
147 |
System.out.println("Cookies are not set. Lookes like new user. Setting all the things.");
|
|
|
148 |
}
|
|
|
149 |
// else if(isSessionId != null && userId != null){
|
|
|
150 |
// System.out.println("Session is already set with sessionid :" + userId );
|
|
|
151 |
// }
|
|
|
152 |
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
public boolean isUserLoggedIn(long userId){
|
|
|
156 |
UserContextServiceClient userContextServiceClient = null;
|
|
|
157 |
in.shop2020.model.v1.user.UserContextService.Client userClient = null;
|
|
|
158 |
boolean isLoggedIn = false;
|
|
|
159 |
|
|
|
160 |
try {
|
|
|
161 |
userContextServiceClient = new UserContextServiceClient();
|
|
|
162 |
userClient = userContextServiceClient.getClient();
|
|
|
163 |
userClient.getState(userId, false).isIsLoggedIn();
|
|
|
164 |
} catch (UserContextException e) {
|
|
|
165 |
// TODO Auto-generated catch block
|
|
|
166 |
e.printStackTrace();
|
|
|
167 |
} catch (TException e) {
|
|
|
168 |
// TODO Auto-generated catch block
|
|
|
169 |
e.printStackTrace();
|
|
|
170 |
} catch (Exception e) {
|
|
|
171 |
// TODO Auto-generated catch block
|
|
|
172 |
e.printStackTrace();
|
|
|
173 |
}
|
|
|
174 |
|
|
|
175 |
return isLoggedIn;
|
|
|
176 |
}
|
|
|
177 |
|
|
|
178 |
|
|
|
179 |
public long getNewUserSessionId(){
|
|
|
180 |
// I feel we dont need to create the context right now. need to discuss
|
|
|
181 |
// UserContextServiceClient userContextServiceClient = null;
|
|
|
182 |
// in.shop2020.model.v1.user.UserContextService.Client userClient = null;
|
|
|
183 |
//
|
|
|
184 |
// userContextServiceClient = new UserContextServiceClient();
|
|
|
185 |
// userClient = userContextServiceClient.getClient();
|
|
|
186 |
//
|
|
|
187 |
// UserContext context = new UserContext();
|
|
|
188 |
// context.setSessionid(1000);
|
|
|
189 |
// context = userClient.createContext(context, false);
|
|
|
190 |
//
|
|
|
191 |
long sessionId = 12;
|
|
|
192 |
// sessionId = context.getSessionid();
|
|
|
193 |
return sessionId;
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
public boolean getLoginSessionInfoFromCookies(){
|
|
|
197 |
Cookie[] cookies = this.request.getCookies();
|
|
|
198 |
boolean foundUserIdCookie = false;
|
|
|
199 |
boolean foundSessionIdCookie = false;
|
|
|
200 |
|
|
|
201 |
long sessionId = 0;
|
|
|
202 |
long userId = 0;
|
|
|
203 |
|
|
|
204 |
if(cookies != null){
|
|
|
205 |
for(int loopIndex = 0; loopIndex < cookies.length; loopIndex++) {
|
|
|
206 |
Cookie cookie1 = cookies[loopIndex];
|
|
|
207 |
if (cookie1.getName().equals("userid")) {
|
|
|
208 |
System.out.println("User Id is = " + cookie1.getValue());
|
|
|
209 |
userId = Long.parseLong(cookie1.getValue());
|
|
|
210 |
foundUserIdCookie = true;
|
|
|
211 |
}
|
|
|
212 |
if (cookie1.getName().equals("sessionid")) {
|
|
|
213 |
System.out.println("Session Id is = " + cookie1.getValue());
|
|
|
214 |
sessionId = Long.parseLong(cookie1.getValue());
|
|
|
215 |
foundSessionIdCookie = true;
|
|
|
216 |
}
|
|
|
217 |
}
|
|
|
218 |
}
|
|
|
219 |
|
|
|
220 |
if(foundUserIdCookie){
|
|
|
221 |
if(isUserLoggedIn(userId)){
|
|
|
222 |
this.session.setAttribute("userid", userId+"");
|
|
|
223 |
this.session.setAttribute("loggedin", "true");
|
|
|
224 |
this.session.setAttribute("issessionid", "false");
|
|
|
225 |
}
|
|
|
226 |
else{
|
|
|
227 |
/* may be something different can be done as commented
|
|
|
228 |
* this.session.setAttribute("userid", userId+"");
|
|
|
229 |
* this.session.setAttribute("loggedin", "false");
|
|
|
230 |
* this.session.setAttribute("issessionid", "false");
|
|
|
231 |
*/
|
|
|
232 |
for(int loopIndex = 0; loopIndex < cookies.length; loopIndex++) {
|
|
|
233 |
Cookie cookie1 = cookies[loopIndex];
|
|
|
234 |
if (cookie1.getName().equals("userid")) {
|
|
|
235 |
cookie1.setMaxAge(0);
|
|
|
236 |
//cookie1.setPath(cookie1.getPath());
|
|
|
237 |
//cookie1.setDomain(cookie1.getDomain());
|
|
|
238 |
tempCookie = cookie1;
|
|
|
239 |
}
|
|
|
240 |
}
|
|
|
241 |
}
|
|
|
242 |
}
|
|
|
243 |
else{
|
|
|
244 |
if(foundSessionIdCookie){
|
|
|
245 |
this.session.setAttribute("userid", sessionId+"");
|
|
|
246 |
this.session.setAttribute("loggedin", "false");
|
|
|
247 |
this.session.setAttribute("issessionid", "true");
|
|
|
248 |
}
|
|
|
249 |
else{
|
|
|
250 |
sessionId = getNewUserSessionId();
|
|
|
251 |
this.session.setAttribute("userid", sessionId+"");
|
|
|
252 |
this.session.setAttribute("loggedin", "false");
|
|
|
253 |
this.session.setAttribute("issessionid", "true");
|
|
|
254 |
|
|
|
255 |
Cookie cookie1 = new Cookie("sessionid", sessionId+"");
|
|
|
256 |
tempCookie = cookie1;
|
|
|
257 |
|
|
|
258 |
}
|
|
|
259 |
}
|
|
|
260 |
return false;
|
|
|
261 |
}
|
|
|
262 |
|
|
|
263 |
|
| 317 |
ashish |
264 |
}
|
| 416 |
rajveer |
265 |
|
|
|
266 |
|
|
|
267 |
|
|
|
268 |
/*
|
|
|
269 |
}
|
|
|
270 |
}
|
|
|
271 |
|
|
|
272 |
else if(getCookiesMap() != null){
|
|
|
273 |
Cookie loginCookie = (Cookie)getCookiesMap().get("USER_ID");
|
|
|
274 |
log.info("login cookie name is " + loginCookie.getName() );
|
|
|
275 |
log.info("login cookie value is " + loginCookie.getValue());
|
|
|
276 |
}
|
|
|
277 |
|
|
|
278 |
|
|
|
279 |
|
|
|
280 |
if(loginInfo != null && loginInfo.equals("true")){
|
|
|
281 |
System.out.println("user id is :" + this.session.getAttribute("userid").toString());
|
|
|
282 |
}
|
|
|
283 |
|
|
|
284 |
|
|
|
285 |
|
|
|
286 |
this.session.setAttribute("loggedin", "true");
|
|
|
287 |
this.session.setAttribute("userid", "rajveer");
|
|
|
288 |
System.out.println("setting user as logged in ");
|
|
|
289 |
}
|
|
|
290 |
*/
|
|
|
291 |
|
|
|
292 |
|
|
|
293 |
|