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