View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
10   */
11  package psiprobe.controllers.apps;
12  
13  import jakarta.servlet.http.HttpServletRequest;
14  import jakarta.servlet.http.HttpServletResponse;
15  
16  import org.apache.catalina.Context;
17  import org.slf4j.Logger;
18  import org.slf4j.LoggerFactory;
19  import org.springframework.beans.factory.annotation.Value;
20  import org.springframework.security.core.Authentication;
21  import org.springframework.security.core.context.SecurityContextHolder;
22  import org.springframework.stereotype.Controller;
23  import org.springframework.web.bind.annotation.RequestMapping;
24  import org.springframework.web.servlet.ModelAndView;
25  
26  import psiprobe.controllers.AbstractContextHandlerController;
27  
28  /**
29   * Reloads application context.
30   */
31  @Controller
32  public class AjaxReloadContextController extends AbstractContextHandlerController {
33  
34    /** The Constant logger. */
35    private static final Logger logger = LoggerFactory.getLogger(AjaxReloadContextController.class);
36  
37    @RequestMapping(path = "/app/reload.ajax")
38    @Override
39    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
40        throws Exception {
41      return super.handleRequest(request, response);
42    }
43  
44    @Override
45    protected ModelAndView handleContext(String contextName, Context context,
46        HttpServletRequest request, HttpServletResponse response) throws Exception {
47  
48      if (context != null && !request.getContextPath().equals(contextName)) {
49        try {
50          logger.info("{} requested RELOAD of {}", request.getRemoteAddr(), contextName);
51          context.reload();
52          // Logging action
53          Authentication auth = SecurityContextHolder.getContext().getAuthentication();
54          // get username logger
55          String name = auth.getName();
56          logger.info(getMessageSourceAccessor().getMessage("probe.src.log.reload"), name,
57              contextName);
58        } catch (Exception e) {
59          logger.error("Error during ajax request to RELOAD of '{}'", contextName, e);
60        }
61      }
62      return new ModelAndView(getViewName(), "available",
63          context != null && getContainerWrapper().getTomcatContainer().getAvailable(context));
64    }
65  
66    @Value("ajax/context_status")
67    @Override
68    public void setViewName(String viewName) {
69      super.setViewName(viewName);
70    }
71  
72  }