| 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;
|
| 21577 |
ashik.ali |
11 |
import org.slf4j.Logger;
|
|
|
12 |
import org.slf4j.LoggerFactory;
|
|
|
13 |
import org.springframework.stereotype.Component;
|
|
|
14 |
|
|
|
15 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
16 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
| 22111 |
ashik.ali |
17 |
import com.spice.profitmandi.dao.enumuration.dtr.RoleType;
|
| 22139 |
amit.gupta |
18 |
import com.spice.profitmandi.web.model.LoginDetails;
|
| 21577 |
ashik.ali |
19 |
|
|
|
20 |
@Component
|
| 22069 |
ashik.ali |
21 |
public class CookiesProcessor {
|
| 21577 |
ashik.ali |
22 |
|
| 22069 |
ashik.ali |
23 |
private static final Logger LOGGER = LoggerFactory.getLogger(CookiesProcessor.class);
|
| 21577 |
ashik.ali |
24 |
|
| 22139 |
amit.gupta |
25 |
public LoginDetails getCookiesObject(HttpServletRequest request) throws ProfitMandiBusinessException{
|
| 21577 |
ashik.ali |
26 |
Cookie[] cookies = request.getCookies();
|
| 21626 |
kshitij.so |
27 |
if (cookies == null){
|
| 21598 |
ashik.ali |
28 |
throw new ProfitMandiBusinessException("", "", "");
|
|
|
29 |
}
|
| 22533 |
ashik.ali |
30 |
String fofoIdFound = null, emailIdFound = null, roleNamesString = null;
|
| 21583 |
ashik.ali |
31 |
for(Cookie cookie : cookies){
|
| 22111 |
ashik.ali |
32 |
LOGGER.info("Requested Cookie {}={}", cookie.getName(), cookie.getValue());
|
| 21583 |
ashik.ali |
33 |
if(cookie.getName().equals(ProfitMandiConstants.FOFO_ID) && cookie.getValue() != null && !cookie.getValue().isEmpty()){
|
|
|
34 |
fofoIdFound = cookie.getValue();
|
|
|
35 |
}
|
| 21626 |
kshitij.so |
36 |
if(cookie.getName().equals(ProfitMandiConstants.EMAIL_ID) && cookie.getValue() != null && !cookie.getValue().isEmpty()){
|
| 21583 |
ashik.ali |
37 |
emailIdFound = cookie.getValue();
|
|
|
38 |
}
|
| 22148 |
amit.gupta |
39 |
if(cookie.getName().equals(ProfitMandiConstants.ROLE_NAMES) && cookie.getValue() != null){
|
| 22111 |
ashik.ali |
40 |
roleNamesString = cookie.getValue();
|
| 22217 |
ashik.ali |
41 |
//LOGGER.info("roleNameString is {}", roleNamesString);
|
| 22111 |
ashik.ali |
42 |
}
|
| 22533 |
ashik.ali |
43 |
if(fofoIdFound != null && emailIdFound != null && roleNamesString != null){
|
| 21583 |
ashik.ali |
44 |
break;
|
|
|
45 |
}
|
|
|
46 |
}
|
|
|
47 |
|
| 22533 |
ashik.ali |
48 |
if(fofoIdFound == null || emailIdFound == null || roleNamesString == null){
|
| 22150 |
amit.gupta |
49 |
LOGGER.info("roleNameString is {}", roleNamesString);
|
| 22217 |
ashik.ali |
50 |
LOGGER.info("fofoIdFound is {}", fofoIdFound);
|
|
|
51 |
LOGGER.info("emailIdFound is {}", emailIdFound);
|
| 21577 |
ashik.ali |
52 |
LOGGER.error("Requested session is not valid");
|
|
|
53 |
throw new ProfitMandiBusinessException("", "", "");
|
|
|
54 |
}else{
|
| 22139 |
amit.gupta |
55 |
LoginDetails fofoDetails = new LoginDetails();
|
| 21583 |
ashik.ali |
56 |
fofoDetails.setFofoId(Integer.parseInt(fofoIdFound));
|
|
|
57 |
fofoDetails.setEmailId(emailIdFound);
|
| 22148 |
amit.gupta |
58 |
String[] roleNames = StringUtils.split(roleNamesString, ",");
|
| 22111 |
ashik.ali |
59 |
Set<RoleType> roleTypes = new HashSet<>();
|
|
|
60 |
for(String roleName : roleNames){
|
|
|
61 |
RoleType roleType = RoleType.valueOf(roleName);
|
|
|
62 |
roleTypes.add(roleType);
|
|
|
63 |
}
|
|
|
64 |
fofoDetails.setRoleTypes(roleTypes);
|
|
|
65 |
return fofoDetails;
|
| 21577 |
ashik.ali |
66 |
}
|
| 21583 |
ashik.ali |
67 |
|
| 21577 |
ashik.ali |
68 |
}
|
| 22069 |
ashik.ali |
69 |
|
|
|
70 |
public void removeCookies(HttpServletRequest request, HttpServletResponse response) throws ProfitMandiBusinessException{
|
|
|
71 |
Cookie[] cookies = request.getCookies();
|
|
|
72 |
if (cookies == null){
|
|
|
73 |
throw new ProfitMandiBusinessException("", "", "");
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
Cookie cookieFofoId = new Cookie(ProfitMandiConstants.FOFO_ID, "");
|
|
|
77 |
cookieFofoId.setMaxAge(0);
|
| 22093 |
amit.gupta |
78 |
cookieFofoId.setPath(request.getContextPath());
|
| 22094 |
amit.gupta |
79 |
cookieFofoId.setDomain(request.getServerName());
|
|
|
80 |
|
|
|
81 |
Cookie cookieEmailId = new Cookie(ProfitMandiConstants.EMAIL_ID, "");
|
| 22069 |
ashik.ali |
82 |
cookieEmailId.setMaxAge(0);
|
| 22094 |
amit.gupta |
83 |
cookieEmailId.setDomain(request.getServerName());
|
| 22093 |
amit.gupta |
84 |
cookieEmailId.setPath(request.getContextPath());
|
| 22094 |
amit.gupta |
85 |
|
| 22111 |
ashik.ali |
86 |
Cookie cookieRoleNames = new Cookie(ProfitMandiConstants.ROLE_NAMES, "");
|
|
|
87 |
cookieRoleNames.setMaxAge(0);
|
|
|
88 |
cookieRoleNames.setDomain(request.getServerName());
|
|
|
89 |
cookieRoleNames.setPath(request.getContextPath());
|
| 22139 |
amit.gupta |
90 |
|
|
|
91 |
Cookie cookieFofoFlag = new Cookie(ProfitMandiConstants.FOFO_FLAG, "");
|
|
|
92 |
cookieFofoFlag.setMaxAge(0);
|
|
|
93 |
cookieFofoFlag.setDomain(request.getServerName());
|
|
|
94 |
cookieFofoFlag.setPath(request.getContextPath());
|
| 22111 |
ashik.ali |
95 |
|
| 22069 |
ashik.ali |
96 |
response.addCookie(cookieFofoId);
|
|
|
97 |
response.addCookie(cookieEmailId);
|
| 22111 |
ashik.ali |
98 |
response.addCookie(cookieRoleNames);
|
| 22139 |
amit.gupta |
99 |
response.addCookie(cookieFofoFlag);
|
| 22069 |
ashik.ali |
100 |
}
|
| 21577 |
ashik.ali |
101 |
}
|