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.datasources;
12  
13  import jakarta.servlet.http.HttpServletRequest;
14  import jakarta.servlet.http.HttpServletResponse;
15  
16  import javax.naming.NamingException;
17  
18  import org.apache.catalina.Context;
19  import org.slf4j.Logger;
20  import org.slf4j.LoggerFactory;
21  import org.springframework.beans.factory.annotation.Value;
22  import org.springframework.stereotype.Controller;
23  import org.springframework.web.bind.ServletRequestUtils;
24  import org.springframework.web.bind.annotation.RequestMapping;
25  import org.springframework.web.servlet.ModelAndView;
26  import org.springframework.web.servlet.view.RedirectView;
27  
28  import psiprobe.controllers.AbstractContextHandlerController;
29  
30  /**
31   * Resets datasource if the datasource supports it.
32   */
33  @Controller
34  public class ResetDataSourceController extends AbstractContextHandlerController {
35  
36    /** The Constant logger. */
37    private static final Logger logger = LoggerFactory.getLogger(ResetDataSourceController.class);
38  
39    /** The replace pattern. */
40    private String replacePattern;
41  
42    /**
43     * Gets the replace pattern.
44     *
45     * @return the replace pattern
46     */
47    public String getReplacePattern() {
48      return replacePattern;
49    }
50  
51    /**
52     * Sets the replace pattern.
53     *
54     * @param replacePattern the new replace pattern
55     */
56    @Value("^http(s)?://[a-zA-Z\\-\\.0-9]+(:[0-9]+)?")
57    public void setReplacePattern(String replacePattern) {
58      this.replacePattern = replacePattern;
59    }
60  
61    @RequestMapping(path = "/app/resetds.htm")
62    @Override
63    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
64        throws Exception {
65      return super.handleRequest(request, response);
66    }
67  
68    @Override
69    protected ModelAndView handleContext(String contextName, Context context,
70        HttpServletRequest request, HttpServletResponse response) throws Exception {
71  
72      String resourceName = ServletRequestUtils.getStringParameter(request, "resource");
73      String referer = request.getHeader("Referer");
74      String redirectUrl;
75      if (referer != null) {
76        redirectUrl = referer.replaceAll(replacePattern, "");
77      } else {
78        redirectUrl = request.getContextPath() + getViewName();
79      }
80  
81      if (resourceName != null && resourceName.length() > 0) {
82        boolean reset = false;
83        try {
84          reset = getContainerWrapper().getResourceResolver().resetResource(context, resourceName,
85              getContainerWrapper());
86        } catch (NamingException e) {
87          request.setAttribute("errorMessage", getMessageSourceAccessor()
88              .getMessage("probe.src.reset.datasource.notfound", new Object[] {resourceName}));
89          logger.trace("", e);
90        }
91        if (!reset) {
92          request.setAttribute("errorMessage",
93              getMessageSourceAccessor().getMessage("probe.src.reset.datasource"));
94        }
95  
96      }
97      logger.debug("Redirected to {}", redirectUrl);
98      return new ModelAndView(new RedirectView(redirectUrl));
99    }
100 
101   @Override
102   protected boolean isContextOptional() {
103     return !getContainerWrapper().getResourceResolver().supportsPrivateResources();
104   }
105 
106   @Value("/resources.htm")
107   @Override
108   public void setViewName(String viewName) {
109     super.setViewName(viewName);
110   }
111 
112 }