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 javax.servlet.http.HttpServletRequest;
14  import javax.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   * Stops a web application.
30   */
31  @Controller
32  public class AjaxToggleContextController extends AbstractContextHandlerController {
33  
34    /** The Constant logger. */
35    private static final Logger logger = LoggerFactory.getLogger(AjaxToggleContextController.class);
36  
37    @RequestMapping(path = "/app/toggle.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          // Logging action
51          Authentication auth = SecurityContextHolder.getContext().getAuthentication();
52          // get username logger
53          String name = auth.getName();
54          if (context.getState().isAvailable()) {
55            logger.info("{} requested STOP of {}", request.getRemoteAddr(), contextName);
56            getContainerWrapper().getTomcatContainer().stop(contextName);
57            logger.info(getMessageSourceAccessor().getMessage("probe.src.log.stop"), name,
58                contextName);
59          } else {
60            logger.info("{} requested START of {}", request.getRemoteAddr(), contextName);
61            getContainerWrapper().getTomcatContainer().start(contextName);
62            logger.info(getMessageSourceAccessor().getMessage("probe.src.log.start"), name,
63                contextName);
64          }
65        } catch (Exception e) {
66          logger.error("Error during ajax request to START/STOP of '{}'", contextName, e);
67        }
68      }
69      return new ModelAndView(getViewName(), "available",
70          context != null && getContainerWrapper().getTomcatContainer().getAvailable(context));
71    }
72  
73    @Value("ajax/context_status")
74    @Override
75    public void setViewName(String viewName) {
76      super.setViewName(viewName);
77    }
78  
79  }