github.com/ismailbayram/bigpicture@v0.0.0-20231225173155-e4b21f5efcff/internal/browser/javaproject/src/main/com/shashi/utility/TrainUtil.java (about) 1 package com.shashi.utility; 2 3 import java.util.Arrays; 4 import java.util.Optional; 5 import java.util.UUID; 6 7 import javax.servlet.http.Cookie; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 import com.shashi.beans.TrainException; 12 import com.shashi.beans.UserBean; 13 import com.shashi.constant.ResponseCode; 14 import com.shashi.constant.UserRole; 15 import com.shashi.service.UserService; 16 import com.shashi.service.impl.UserServiceImpl; 17 18 public class TrainUtil { 19 20 public static Optional<String> readCookie(HttpServletRequest request, String key) { 21 Cookie[] cookies = request.getCookies(); 22 if (cookies == null) { 23 return Optional.empty(); 24 } 25 return Arrays.stream(cookies).filter(c -> key.equals(c.getName())).map(Cookie::getValue).findAny(); 26 } 27 28 public static String login(HttpServletRequest request, HttpServletResponse response, UserRole userRole, 29 String username, String password) { 30 UserService userService = new UserServiceImpl(userRole); 31 String responseCode = ResponseCode.UNAUTHORIZED.toString(); 32 try { 33 UserBean user = userService.loginUser(username, password); 34 35 // Add the user details to the ServletContext with key as role name 36 request.getServletContext().setAttribute(userRole.toString(), user); 37 38 // Store the user firstName and mailId in the http session 39 request.getSession().setAttribute("uName", user.getFName()); 40 request.getSession().setAttribute("mailid", user.getMailId()); 41 42 // Add the sessionId to the cookie with key as sessionId 43 Cookie cookie = new Cookie("sessionIdFor" + userRole.toString(), UUID.randomUUID().toString()); 44 45 // set the max age for the cookie 46 cookie.setMaxAge(600); // Expires after 10 MIN 47 48 // add the cookie to the response 49 response.addCookie(cookie); 50 51 // set the responseCode to success 52 responseCode = ResponseCode.SUCCESS.toString(); 53 54 } catch (TrainException e) { 55 responseCode += " : " + e.getMessage(); 56 } 57 58 return responseCode; 59 } 60 61 public static boolean isLoggedIn(HttpServletRequest request, UserRole userRole) { 62 Optional<String> sessionId = readCookie(request, "sessionIdFor" + userRole.toString()); 63 return sessionId != null && sessionId.isPresent(); 64 } 65 66 public static void validateUserAuthorization(HttpServletRequest request, UserRole userRole) throws TrainException { 67 if (!isLoggedIn(request, userRole)) { 68 throw new TrainException(ResponseCode.SESSION_EXPIRED); 69 } 70 } 71 72 public static boolean logout(HttpServletResponse response) { 73 74 // Set the max age to 0 for the admin and customer cookies 75 Cookie cookie = new Cookie("sessionIdFor" + UserRole.ADMIN.toString(), UUID.randomUUID().toString()); 76 cookie.setMaxAge(0); 77 78 Cookie cookie2 = new Cookie("sessionIdFor" + UserRole.CUSTOMER.toString(), UUID.randomUUID().toString()); 79 cookie2.setMaxAge(0); 80 81 response.addCookie(cookie); 82 response.addCookie(cookie2); 83 84 return true; 85 } 86 87 public static String getCurrentUserName(HttpServletRequest req) { 88 return (String) req.getSession().getAttribute("uName"); 89 } 90 91 public static String getCurrentUserEmail(HttpServletRequest req) { 92 return (String) req.getSession().getAttribute("mailid"); 93 } 94 95 public static UserBean getCurrentCustomer(HttpServletRequest req) { 96 return (UserBean) req.getServletContext().getAttribute(UserRole.CUSTOMER.toString()); 97 } 98 }