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.threads;
12  
13  import javax.servlet.http.HttpServletRequest;
14  import javax.servlet.http.HttpServletResponse;
15  
16  import org.springframework.beans.factory.annotation.Value;
17  import org.springframework.stereotype.Controller;
18  import org.springframework.web.bind.ServletRequestUtils;
19  import org.springframework.web.bind.annotation.RequestMapping;
20  import org.springframework.web.servlet.ModelAndView;
21  import org.springframework.web.servlet.mvc.ParameterizableViewController;
22  import org.springframework.web.servlet.view.RedirectView;
23  
24  import psiprobe.Utils;
25  
26  /**
27   * The Class KillThreadController.
28   */
29  @Controller
30  public class KillThreadController extends ParameterizableViewController {
31  
32    /** The replace pattern. */
33    private String replacePattern;
34  
35    /**
36     * Gets the replace pattern.
37     *
38     * @return the replace pattern
39     */
40    public String getReplacePattern() {
41      return replacePattern;
42    }
43  
44    /**
45     * Sets the replace pattern.
46     *
47     * @param replacePattern the new replace pattern
48     */
49    @Value("^http(s)?://[a-zA-Z\\-\\.0-9]+(:[0-9]+)?")
50    public void setReplacePattern(String replacePattern) {
51      this.replacePattern = replacePattern;
52    }
53  
54    @RequestMapping(path = "/adm/kill.htm")
55    @Override
56    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
57        throws Exception {
58      return super.handleRequest(request, response);
59    }
60  
61    @Override
62    protected ModelAndView handleRequestInternal(HttpServletRequest request,
63        HttpServletResponse response) throws Exception {
64  
65      String threadName = ServletRequestUtils.getStringParameter(request, "thread");
66  
67      Thread thread = null;
68      if (threadName != null) {
69        thread = Utils.getThreadByName(threadName);
70      }
71  
72      if (thread != null) {
73        thread.stop();
74      }
75  
76      String referer = request.getHeader("Referer");
77      String redirectUrl;
78      if (referer != null) {
79        redirectUrl = referer.replaceAll(replacePattern, "");
80      } else {
81        redirectUrl = request.getContextPath() + getViewName();
82      }
83      return new ModelAndView(new RedirectView(redirectUrl));
84    }
85  
86    @Value("redirect:/threads.htm")
87    @Override
88    public void setViewName(String viewName) {
89      super.setViewName(viewName);
90    }
91  
92  }