Rev 650 | Blame | Last modification | View Log | RSS feed
package in.shop2020.serving.controllers;import in.shop2020.serving.services.PageLoaderHandler;import in.shop2020.serving.services.UserSessionInfo;import in.shop2020.serving.utils.DesEncrypter;import in.shop2020.serving.utils.Utils;import java.util.Enumeration;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 javax.servlet.http.HttpUtils;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 org.apache.velocity.VelocityContext;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;protected Map<String,String> htmlSnippets;PageLoaderHandler pageLoader = null;public BaseController() {pageLoader = new PageLoaderHandler();htmlSnippets = new HashMap<String, String>();}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;log.info("REQUESTED URL: " + request.getRequestURL().toString());log.info("Remote host "+ request.getRemoteHost());log.info("requested Session Id "+ request.getRequestedSessionId());log.info("Session Id "+ request.getSession().getId());log.info("QUERY STRING IS: " + this.request.getQueryString());Enumeration names = request.getHeaderNames();while (names.hasMoreElements()) {String name = (String) names.nextElement();Enumeration values = request.getHeaders(name); // support multiple valuesif (values != null) {while (values.hasMoreElements()) {String value = (String) values.nextElement();log.info(name + ": " + value);}}}for(Object param: request.getParameterMap().keySet()){log.info("PARAMS: " + param + " = "+ request.getParameter((String)param));}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){log.info("Inside SetUserSessionInfo 1");this.userinfo = (UserSessionInfo) this.session.getAttribute("userinfo");if(this.userinfo == null || this.userinfo.getUserId() == -1){log.info("Inside SetUserSessionInfo 2");this.userinfo = new UserSessionInfo(jsessionid);this.session.setAttribute("userinfo", this.userinfo);}}protected void createUserCookie(long userId, boolean force) {log.info("Inside CreateUserCookie 1");userCookie = (Cookie) cookiesMap.get("uid");if(force || userCookie == null || !(userId + "").equals(userCookie.getValue())){log.info("Inside CreateUserCookie 2");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;// }// }// }public UserSessionInfo getUserInfo(){return this.userinfo;}public String getHeaderSnippet(){return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getNameOfUser());}public String getMainMenuSnippet(){return pageLoader.getMainMenuHtml();}public String getSearchBarSnippet(){return pageLoader.getSearchBarHtml(userinfo.getTotalItems(), 10000);}public String getCustomerServiceSnippet(){return pageLoader.getCustomerServiceHtml();}public String getMyResearchSnippet(){return pageLoader.getMyResearchHtml(userinfo.getUserId(), userinfo.isLoggedIn());}public String getBrowseHistorySnippet(){return pageLoader.getBrowseHistoryHtml(userinfo.getUserId(), userinfo.isLoggedIn());}public String getFooterSnippet(){return pageLoader.getFooterHtml();}public String getRedirectUrl(){return (String)this.request.getSession().getAttribute("REDIRECT_URL");}public void setRedirectUrl(){String queryString = this.request.getQueryString();log.info("Query String is: "+queryString);if(queryString==null){queryString="";}else{queryString = "?" + queryString;}this.request.getSession().setAttribute("REDIRECT_URL", this.request.getRequestURI() + queryString);}public void resetRedirectUrl(){this.request.getSession().removeAttribute("REDIRECT_URL");}}