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