| 21577 |
ashik.ali |
1 |
package com.spice.profitmandi.web.util;
|
|
|
2 |
|
| 22111 |
ashik.ali |
3 |
import java.util.HashSet;
|
|
|
4 |
import java.util.Set;
|
|
|
5 |
|
| 21577 |
ashik.ali |
6 |
import javax.servlet.http.Cookie;
|
|
|
7 |
import javax.servlet.http.HttpServletRequest;
|
| 22069 |
ashik.ali |
8 |
import javax.servlet.http.HttpServletResponse;
|
| 21577 |
ashik.ali |
9 |
|
| 22148 |
amit.gupta |
10 |
import org.apache.commons.lang3.StringUtils;
|
| 23784 |
ashik.ali |
11 |
import org.apache.logging.log4j.LogManager;
|
| 23568 |
govind |
12 |
import org.apache.logging.log4j.Logger;
|
| 21577 |
ashik.ali |
13 |
import org.springframework.stereotype.Component;
|
|
|
14 |
|
|
|
15 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
16 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
| 22139 |
amit.gupta |
17 |
import com.spice.profitmandi.web.model.LoginDetails;
|
| 21577 |
ashik.ali |
18 |
|
|
|
19 |
@Component
|
| 22069 |
ashik.ali |
20 |
public class CookiesProcessor {
|
| 21577 |
ashik.ali |
21 |
|
| 23568 |
govind |
22 |
private static final Logger LOGGER = LogManager.getLogger(CookiesProcessor.class);
|
| 21577 |
ashik.ali |
23 |
|
| 22139 |
amit.gupta |
24 |
public LoginDetails getCookiesObject(HttpServletRequest request) throws ProfitMandiBusinessException{
|
| 21577 |
ashik.ali |
25 |
Cookie[] cookies = request.getCookies();
|
| 21626 |
kshitij.so |
26 |
if (cookies == null){
|
| 23784 |
ashik.ali |
27 |
throw new ProfitMandiBusinessException("cookies", "", "GE_1008");
|
| 21598 |
ashik.ali |
28 |
}
|
| 23784 |
ashik.ali |
29 |
String fofoIdFound = null, emailIdFound = null, roleIdsString = null;
|
| 21583 |
ashik.ali |
30 |
for(Cookie cookie : cookies){
|
| 22111 |
ashik.ali |
31 |
LOGGER.info("Requested Cookie {}={}", cookie.getName(), cookie.getValue());
|
| 21583 |
ashik.ali |
32 |
if(cookie.getName().equals(ProfitMandiConstants.FOFO_ID) && cookie.getValue() != null && !cookie.getValue().isEmpty()){
|
|
|
33 |
fofoIdFound = cookie.getValue();
|
|
|
34 |
}
|
| 21626 |
kshitij.so |
35 |
if(cookie.getName().equals(ProfitMandiConstants.EMAIL_ID) && cookie.getValue() != null && !cookie.getValue().isEmpty()){
|
| 21583 |
ashik.ali |
36 |
emailIdFound = cookie.getValue();
|
|
|
37 |
}
|
| 23784 |
ashik.ali |
38 |
if(cookie.getName().equals(ProfitMandiConstants.ROLE_IDS) && cookie.getValue() != null){
|
|
|
39 |
roleIdsString = cookie.getValue();
|
| 22217 |
ashik.ali |
40 |
//LOGGER.info("roleNameString is {}", roleNamesString);
|
| 22111 |
ashik.ali |
41 |
}
|
| 23784 |
ashik.ali |
42 |
if(fofoIdFound != null && emailIdFound != null && roleIdsString != null){
|
| 21583 |
ashik.ali |
43 |
break;
|
|
|
44 |
}
|
|
|
45 |
}
|
|
|
46 |
|
| 23784 |
ashik.ali |
47 |
if(fofoIdFound == null || emailIdFound == null || roleIdsString == null){
|
|
|
48 |
LOGGER.info("roleIdString is {}", roleIdsString);
|
| 22217 |
ashik.ali |
49 |
LOGGER.info("fofoIdFound is {}", fofoIdFound);
|
|
|
50 |
LOGGER.info("emailIdFound is {}", emailIdFound);
|
| 21577 |
ashik.ali |
51 |
LOGGER.error("Requested session is not valid");
|
| 23784 |
ashik.ali |
52 |
throw new ProfitMandiBusinessException("cookies", "", "GE_1008");
|
| 23510 |
amit.gupta |
53 |
}else {
|
| 22139 |
amit.gupta |
54 |
LoginDetails fofoDetails = new LoginDetails();
|
| 21583 |
ashik.ali |
55 |
fofoDetails.setFofoId(Integer.parseInt(fofoIdFound));
|
|
|
56 |
fofoDetails.setEmailId(emailIdFound);
|
| 23784 |
ashik.ali |
57 |
String[] roleIdStrings = StringUtils.split(roleIdsString, ",");
|
|
|
58 |
Set<Integer> roleIds = new HashSet<>();
|
|
|
59 |
for(String roleId : roleIdStrings){
|
|
|
60 |
roleIds.add(Integer.valueOf(roleId));
|
| 22111 |
ashik.ali |
61 |
}
|
| 23784 |
ashik.ali |
62 |
fofoDetails.setRoleIds(roleIds);
|
| 23506 |
amit.gupta |
63 |
return fofoDetails;
|
| 21577 |
ashik.ali |
64 |
}
|
| 21583 |
ashik.ali |
65 |
|
| 21577 |
ashik.ali |
66 |
}
|
| 22069 |
ashik.ali |
67 |
|
|
|
68 |
public void removeCookies(HttpServletRequest request, HttpServletResponse response) throws ProfitMandiBusinessException{
|
|
|
69 |
Cookie[] cookies = request.getCookies();
|
|
|
70 |
if (cookies == null){
|
|
|
71 |
throw new ProfitMandiBusinessException("", "", "");
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
Cookie cookieFofoId = new Cookie(ProfitMandiConstants.FOFO_ID, "");
|
|
|
75 |
cookieFofoId.setMaxAge(0);
|
| 22093 |
amit.gupta |
76 |
cookieFofoId.setPath(request.getContextPath());
|
| 22094 |
amit.gupta |
77 |
cookieFofoId.setDomain(request.getServerName());
|
|
|
78 |
|
|
|
79 |
Cookie cookieEmailId = new Cookie(ProfitMandiConstants.EMAIL_ID, "");
|
| 22069 |
ashik.ali |
80 |
cookieEmailId.setMaxAge(0);
|
| 22094 |
amit.gupta |
81 |
cookieEmailId.setDomain(request.getServerName());
|
| 22093 |
amit.gupta |
82 |
cookieEmailId.setPath(request.getContextPath());
|
| 22094 |
amit.gupta |
83 |
|
| 23784 |
ashik.ali |
84 |
Cookie cookieRoleNames = new Cookie(ProfitMandiConstants.ROLE_IDS, "");
|
| 22111 |
ashik.ali |
85 |
cookieRoleNames.setMaxAge(0);
|
|
|
86 |
cookieRoleNames.setDomain(request.getServerName());
|
|
|
87 |
cookieRoleNames.setPath(request.getContextPath());
|
| 22139 |
amit.gupta |
88 |
|
|
|
89 |
Cookie cookieFofoFlag = new Cookie(ProfitMandiConstants.FOFO_FLAG, "");
|
|
|
90 |
cookieFofoFlag.setMaxAge(0);
|
|
|
91 |
cookieFofoFlag.setDomain(request.getServerName());
|
|
|
92 |
cookieFofoFlag.setPath(request.getContextPath());
|
| 22111 |
ashik.ali |
93 |
|
| 22069 |
ashik.ali |
94 |
response.addCookie(cookieFofoId);
|
|
|
95 |
response.addCookie(cookieEmailId);
|
| 22111 |
ashik.ali |
96 |
response.addCookie(cookieRoleNames);
|
| 22139 |
amit.gupta |
97 |
response.addCookie(cookieFofoFlag);
|
| 22069 |
ashik.ali |
98 |
}
|
| 21577 |
ashik.ali |
99 |
}
|