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.error;
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.annotation.RequestMapping;
19  import org.springframework.web.servlet.ModelAndView;
20  import org.springframework.web.servlet.mvc.AbstractController;
21  
22  /**
23   * The ErrorHandlerController will show two different views depending on whether the failed request
24   * was AJAX or not.
25   */
26  @Controller
27  public class Error403Controller extends AbstractController {
28  
29    /** The view name. */
30    private String viewName;
31  
32    /** The ajax view name. */
33    private String ajaxViewName;
34  
35    /** The ajax extension. */
36    private String ajaxExtension;
37  
38    /**
39     * Gets the view name.
40     *
41     * @return the view name
42     */
43    public String getViewName() {
44      return viewName;
45    }
46  
47    /**
48     * Sets the view name.
49     *
50     * @param viewName the new view name
51     */
52    @Value("errors/403")
53    public void setViewName(String viewName) {
54      this.viewName = viewName;
55    }
56  
57    /**
58     * Gets the ajax view name.
59     *
60     * @return the ajax view name
61     */
62    public String getAjaxViewName() {
63      return ajaxViewName;
64    }
65  
66    /**
67     * Sets the ajax view name.
68     *
69     * @param ajaxViewName the new ajax view name
70     */
71    @Value("errors/403_ajax")
72    public void setAjaxViewName(String ajaxViewName) {
73      this.ajaxViewName = ajaxViewName;
74    }
75  
76    /**
77     * Gets the ajax extension.
78     *
79     * @return the ajax extension
80     */
81    public String getAjaxExtension() {
82      return ajaxExtension;
83    }
84  
85    /**
86     * Sets the ajax extension.
87     *
88     * @param ajaxExtension the new ajax extension
89     */
90    @Value(".ajax")
91    public void setAjaxExtension(String ajaxExtension) {
92      this.ajaxExtension = ajaxExtension;
93    }
94  
95    @RequestMapping(path = "/403.htm")
96    @Override
97    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
98        throws Exception {
99      return super.handleRequest(request, response);
100   }
101 
102   @Override
103   protected ModelAndView handleRequestInternal(HttpServletRequest request,
104       HttpServletResponse response) throws Exception {
105 
106     String originalUri = (String) request.getAttribute("javax.servlet.error.request_uri");
107     if (originalUri != null && originalUri.endsWith(ajaxExtension)) {
108       return new ModelAndView(ajaxViewName);
109     }
110     return new ModelAndView(viewName);
111   }
112 
113 }