Rev 572 | Blame | Last modification | View Log | RSS feed
package in.shop2020.serving.controllers;import in.shop2020.serving.services.UserSessionInfo;import in.shop2020.serving.utils.DesEncrypter;import in.shop2020.serving.utils.Utils;import java.util.HashMap;import java.util.Map;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import org.apache.juli.logging.Log;import org.apache.juli.logging.LogFactory;import org.apache.struts2.interceptor.CookiesAware;import org.apache.struts2.interceptor.ServletRequestAware;import org.apache.struts2.interceptor.ServletResponseAware;import com.opensymphony.xwork2.ValidationAware;import com.opensymphony.xwork2.ValidationAwareSupport;/*** Base class for all user action handlers i.e. controllers** @author rajveer*/public abstract class BaseController extends ValidationAwareSupport implements CookiesAware, ServletResponseAware, ServletRequestAware {/****/private static final long serialVersionUID = 1L;protected Map<String, Cookie> cookiesMap = null;protected HttpServletResponse response;protected HttpServletRequest request;protected HttpSession session;protected UserSessionInfo userinfo = null;private static Log log = LogFactory.getLog(BaseController.class);private DesEncrypter desEncrypter = new DesEncrypter("shop2020");protected Cookie userCookie = null;public BaseController() {}public Map getCookiesMap() {return cookiesMap;}@Overridepublic void setCookiesMap(Map cookiesMap) {log.info("Received cookiesMap and it is " + cookiesMap);this.cookiesMap = cookiesMap;}@Overridepublic void setServletResponse(HttpServletResponse response){this.response = response;if(userCookie!=null)response.addCookie(userCookie);}@Overridepublic void setServletRequest(HttpServletRequest request){this.request = request;this.session = request.getSession(); // Get the existing session or create a new onegetCookiesMap(request);String requestedSessionId = request.getRequestedSessionId();// Check if this is a brand new request with no prior cookies set; OR// If the request is for an active session.if(requestedSessionId == null || request.isRequestedSessionIdValid()){log.info("Request received for valid session: " + requestedSessionId);// Set the userinfo and the uid cookie if they're not already set.this.session = request.getSession();setUserSessionInfo(this.session.getId());createUserCookie(this.userinfo.getUserId(), false);} else {log.info("Request received for invalid session: " + requestedSessionId);// If the requested session is inactive, do the following:// 1. Retrieve the user for the requested session from the user cookie// 2. Add the retrieved user to the newly created session above.// 3. Update the uid cookie to ensure that a valid user is set in the sessionrecreateSessionFromUIDCookie(this.session.getId());createUserCookie(this.userinfo.getUserId(), true);}}private void getCookiesMap(HttpServletRequest request) {cookiesMap = new HashMap<String, Cookie>();Cookie[] cookies = request.getCookies();// This check is necessary for the first request when no cookies are// sent.if(cookies==null)return;for (Cookie cookie : cookies)cookiesMap.put(cookie.getName(), cookie);}private void setUserSessionInfo(String jsessionid){this.userinfo = (UserSessionInfo) this.session.getAttribute("userinfo");if(this.userinfo == null || this.userinfo.getUserId() == -1){this.userinfo = new UserSessionInfo(jsessionid);this.session.setAttribute("userinfo", this.userinfo);}}protected void createUserCookie(long userId, boolean force) {userCookie = (Cookie) cookiesMap.get("uid");if(force || userCookie == null || !(userId + "").equals(userCookie.getValue())){String encryptedUserId = desEncrypter.encrypt(userId + "");userCookie = new Cookie("uid", encryptedUserId);}}private void recreateSessionFromUIDCookie(String jsessionid) {Cookie userCookie = (Cookie) cookiesMap.get("uid");if(userCookie != null){String uidString = userCookie.getValue();if(uidString != null){try {Long receivedUID = Long.parseLong(desEncrypter.decrypt(uidString));this.userinfo = new UserSessionInfo(receivedUID, jsessionid);this.session.setAttribute("userinfo", this.userinfo);} catch (NumberFormatException nfe) {log.error("The UID cookie contains an unparseable userID");}}}if(this.userinfo==null)setUserSessionInfo(jsessionid);}// private void processCookiesInfo(){// Cookie[] cookies = this.request.getCookies();// boolean foundUserIdCookie = false;// boolean foundSessionIdCookie = false;// long userId = 0 ;// long sessionId = 0;//// if(cookies != null){// for(int loopIndex = 0; loopIndex < cookies.length; loopIndex++) {// Cookie cookie1 = cookies[loopIndex];// if (cookie1.getName().equals("userid")) {// System.out.println("User Id is = " + cookie1.getValue());// userId = Long.parseLong(cookie1.getValue());// foundUserIdCookie = true;// }// if (cookie1.getName().equals("sessionid")) {// System.out.println("Session Id is = " + cookie1.getValue());// sessionId = Long.parseLong(cookie1.getValue());// foundSessionIdCookie = true;// }// }// }//// if(foundUserIdCookie){// if(Utils.isUserLoggedIn(userId)){// userinfo = new UserSessionInfo(userId, false);//// }// else{// if(foundSessionIdCookie){// userinfo = new UserSessionInfo(sessionId, true);// }else{// userinfo = new UserSessionInfo();// for(int loopIndex = 0; loopIndex < cookies.length; loopIndex++) {// Cookie cookie1 = cookies[loopIndex];// if (cookie1.getName().equals("userid")) {// cookie1.setMaxAge(0);// //cookie1.setPath(cookie1.getPath());// //cookie1.setDomain(cookie1.getDomain());// tempCookie = cookie1;// }// }// }// }// }// else{// if(foundSessionIdCookie){// userinfo = new UserSessionInfo(sessionId, true);// }// else{// userinfo = new UserSessionInfo();// //Cookie cookie1 = new Cookie("sessionid", userinfo.getSessionId()+"");// //tempCookie = cookie1;// }// }// }private String getEmail(){return userinfo.getEmail();}private String getNameOfUser(){return userinfo.getNameOfUser();}}