1
2
3
4
5
6
7
8
9
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
30
31 @Controller
32 public class AjaxToggleContextController extends AbstractContextHandlerController {
33
34
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
51 Authentication auth = SecurityContextHolder.getContext().getAuthentication();
52
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 }