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.web.servlet.ModelAndView;
20  import org.springframework.web.servlet.view.InternalResourceView;
21  import org.springframework.web.servlet.view.RedirectView;
22  
23  import psiprobe.controllers.AbstractContextHandlerController;
24  
25  /**
26   * Base class preventing "destructive" actions to be executed on the Probe's context.
27   */
28  public abstract class AbstractNoSelfContextHandlerController
29      extends AbstractContextHandlerController {
30  
31    /** The Constant logger. */
32    private static final Logger logger =
33        LoggerFactory.getLogger(AbstractNoSelfContextHandlerController.class);
34  
35    /** The pass query string. */
36    private boolean passQueryString;
37  
38    /**
39     * Checks if is pass query string.
40     *
41     * @return true, if is pass query string
42     */
43    public boolean isPassQueryString() {
44      return passQueryString;
45    }
46  
47    /**
48     * Sets the pass query string.
49     *
50     * @param passQueryString the new pass query string
51     */
52    public void setPassQueryString(boolean passQueryString) {
53      this.passQueryString = passQueryString;
54    }
55  
56    @Override
57    protected ModelAndView handleContext(String contextName, Context context,
58        HttpServletRequest request, HttpServletResponse response) throws Exception {
59  
60      try {
61        if (request.getContextPath().equals(contextName)) {
62          throw new IllegalStateException(
63              getMessageSourceAccessor().getMessage("probe.src.contextAction.cannotActOnSelf"));
64        }
65  
66        executeAction(contextName);
67      } catch (Exception e) {
68        request.setAttribute("errorMessage", e.getMessage());
69        logger.error("Error during invocation", e);
70        return new ModelAndView(new InternalResourceView(getViewName()));
71      }
72      return new ModelAndView(new RedirectView(request.getContextPath() + getViewName()
73          + (isPassQueryString() ? "?" + request.getQueryString() : "")));
74    }
75  
76    /**
77     * Execute action.
78     *
79     * @param contextName the context name
80     *
81     * @throws Exception the exception
82     */
83    protected abstract void executeAction(String contextName) throws Exception;
84  
85  }