Open Kilda Java Documentation
BaseController.java
Go to the documentation of this file.
1 package org.openkilda.controller;
2 
3 import org.slf4j.Logger;
4 import org.slf4j.LoggerFactory;
5 
6 import org.springframework.beans.factory.annotation.Autowired;
7 import org.springframework.boot.autoconfigure.web.ErrorController;
8 import org.springframework.security.authentication.AnonymousAuthenticationToken;
9 import org.springframework.security.core.Authentication;
10 import org.springframework.security.core.context.SecurityContextHolder;
11 import org.springframework.ui.Model;
12 import org.springframework.web.bind.annotation.RequestMapping;
13 import org.springframework.web.servlet.ModelAndView;
14 
15 import javax.servlet.http.HttpServletRequest;
16 import javax.servlet.http.HttpSession;
17 
23 
24 public abstract class BaseController implements ErrorController {
25 
26  private static final Logger LOGGER = LoggerFactory.getLogger(BaseController.class);
27 
28  @Autowired
29  private UserRepository userRepository;
30 
45  public ModelAndView validateAndRedirect(final HttpServletRequest request,
46  final String viewName) {
47  ModelAndView modelAndView;
48  if (isUserLoggedIn()) {
49  UserInfo userInfo = getLoggedInUser(request);
50  LOGGER.info("[validateAndRedirect] Logged in user. view name: " + viewName + ", User name: " + userInfo.getName());
51 
52  modelAndView = new ModelAndView(viewName);
53  } else {
54  LOGGER.error("[validateAndRedirect] User in not logged in, redirected to login page. Requested view name: " + viewName);
55  modelAndView = new ModelAndView(IConstants.View.LOGIN);
56  }
57  return modelAndView;
58  }
59 
66  @RequestMapping("/403")
67  public ModelAndView error(final Model model) {
68  return new ModelAndView(IConstants.View.ERROR_403);
69  }
70 
71  /*
72  * (non-Javadoc)
73  *
74  * @see org.springframework.boot.autoconfigure.web.ErrorController#getErrorPath()
75  */
76  @Override
77  @RequestMapping("/error")
78  public String getErrorPath() {
79  return IConstants.View.ERROR;
80  }
81 
88  protected UserInfo getLoggedInUser(final HttpServletRequest request) {
89  HttpSession session = request.getSession();
90  UserInfo userInfo = null;
91  try {
92  userInfo = (UserInfo) session.getAttribute(IConstants.SESSION_OBJECT);
93  } catch (IllegalStateException ex) {
94  LOGGER.error(
95  "[getLoggedInUser] Exception while retrieving user information from session. Exception: "
96  + ex.getLocalizedMessage(),
97  ex);
98  } finally {
99  if (userInfo == null) {
100  session = request.getSession(false);
101  userInfo = new UserInfo();
102  session.setAttribute(IConstants.SESSION_OBJECT, userInfo);
103  }
104  }
105  return userInfo;
106  }
107 
108 
114  protected boolean isUserLoggedIn() {
115  Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
116  if (null != authentication) {
117  boolean isValid = (authentication.isAuthenticated()
118  && !(authentication instanceof AnonymousAuthenticationToken));
119  if(isValid) {
120  UserEntity userEntity = (UserEntity) authentication.getPrincipal();
121  userEntity = userRepository.findByUserId(userEntity.getUserId());
122  if(userEntity != null && userEntity.getStatusEntity().getStatusCode().equalsIgnoreCase(Status.ACTIVE.getCode())) {
123  isValid = true;
124  } else {
125  isValid = false;
126  }
127  }
128  return isValid;
129  } else {
130 
131  return false;
132  }
133  }
134 }
ModelAndView error(final Model model)
static final String SESSION_OBJECT
Definition: IConstants.java:15
ModelAndView validateAndRedirect(final HttpServletRequest request, final String viewName)
UserInfo getLoggedInUser(final HttpServletRequest request)