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.deploy;
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.security.core.Authentication;
20  import org.springframework.security.core.context.SecurityContextHolder;
21  import org.springframework.web.servlet.ModelAndView;
22  import org.springframework.web.servlet.view.InternalResourceView;
23  import org.springframework.web.servlet.view.RedirectView;
24  
25  import psiprobe.controllers.AbstractContextHandlerController;
26  
27  /**
28   * Undeploys a web application.
29   */
30  public class BaseUndeployContextController extends AbstractContextHandlerController {
31  
32    /** The Constant logger. */
33    private static final Logger logger = LoggerFactory.getLogger(BaseUndeployContextController.class);
34  
35    /** The failure view name. */
36    private String failureViewName;
37  
38    /**
39     * Gets the failure view name.
40     *
41     * @return the failure view name
42     */
43    public String getFailureViewName() {
44      return failureViewName;
45    }
46  
47    /**
48     * Sets the failure view name.
49     *
50     * @param failureViewName the new failure view name
51     */
52    public void setFailureViewName(String failureViewName) {
53      this.failureViewName = failureViewName;
54    }
55  
56    @Override
57    protected ModelAndView handleContext(String contextName, Context context,
58        HttpServletRequest request, HttpServletResponse response) throws Exception {
59      try {
60        if (request.getContextPath().equals(contextName)) {
61          throw new IllegalStateException(
62              getMessageSourceAccessor().getMessage("probe.src.contextAction.cannotActOnSelf"));
63        }
64  
65        getContainerWrapper().getTomcatContainer().remove(contextName);
66        // Logging action
67        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
68        // get username logger
69        String name = auth.getName();
70        logger.info(getMessageSourceAccessor().getMessage("probe.src.log.undeploy"), name,
71            contextName);
72  
73      } catch (Exception e) {
74        request.setAttribute("errorMessage", e.getMessage());
75        logger.error("Error during undeploy of '{}'", contextName, e);
76        return new ModelAndView(new InternalResourceView(
77            getFailureViewName() == null ? getViewName() : getFailureViewName()));
78      }
79      return new ModelAndView(new RedirectView(request.getContextPath() + getViewName()));
80    }
81  
82    /**
83     * Execute action.
84     *
85     * @param contextName the context name
86     *
87     * @throws Exception the exception
88     */
89    protected void executeAction(String contextName) throws Exception {
90      // Not Implemented
91    }
92  
93  }