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;
12  
13  import jakarta.servlet.ServletConfig;
14  import jakarta.servlet.ServletException;
15  import jakarta.servlet.annotation.WebServlet;
16  import jakarta.servlet.http.HttpServletRequest;
17  import jakarta.servlet.http.HttpServletResponse;
18  
19  import java.nio.charset.StandardCharsets;
20  
21  import org.apache.catalina.ContainerServlet;
22  import org.apache.catalina.Wrapper;
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  import org.springframework.web.servlet.DispatcherServlet;
26  
27  import psiprobe.beans.ContainerWrapperBean;
28  
29  /**
30   * Main dispatcher servlet. Spring default dispatcher servlet had to be superseded to handle
31   * "privileged" application context features. The actual requirement is to capture passed Wrapper
32   * instance into ContainerWrapperBean. Wrapper instance is our gateway to Tomcat.
33   */
34  @WebServlet
35  public class ProbeServlet extends DispatcherServlet implements ContainerServlet {
36  
37    /** The Constant serialVersionUID. */
38    private static final long serialVersionUID = 1L;
39  
40    /** The Constant logger. */
41    private static final Logger logger = LoggerFactory.getLogger(ProbeServlet.class);
42  
43    /** The wrapper. */
44    private transient Wrapper wrapper;
45  
46    @Override
47    public Wrapper getWrapper() {
48      return wrapper;
49    }
50  
51    @Override
52    public void setWrapper(Wrapper wrapper) {
53      this.wrapper = wrapper;
54      logger.info("setWrapper() called");
55    }
56  
57    /**
58     * Associates the {@link Wrapper} with the {@link ContainerWrapperBean}.
59     *
60     * @param config this servlet's configuration and initialization parameters
61     *
62     * @throws ServletException if the wrapper is null or another servlet-interrupting error occurs
63     */
64    @Override
65    public void init(ServletConfig config) throws ServletException {
66      super.init(config);
67      if (wrapper == null) {
68        throw new ServletException("Wrapper is null");
69      }
70      getContainerWrapperBean().setWrapper(wrapper);
71    }
72  
73    /**
74     * Dispatch.
75     *
76     * @param httpServletRequest the request
77     * @param httpServletResponse the response
78     *
79     * @throws Exception if setting UTF-8 encoding fails or if the super implementation throws an
80     *         exception
81     */
82    @Override
83    protected void doDispatch(HttpServletRequest httpServletRequest,
84        HttpServletResponse httpServletResponse) throws Exception {
85      httpServletRequest.setCharacterEncoding(StandardCharsets.UTF_8.name());
86      super.doDispatch(httpServletRequest, httpServletResponse);
87    }
88  
89    @Override
90    public void destroy() {
91      getContainerWrapperBean().setWrapper(null);
92      super.destroy();
93    }
94  
95    /**
96     * Gets the container wrapper bean.
97     *
98     * @return the container wrapper bean
99     */
100   protected ContainerWrapperBean getContainerWrapperBean() {
101     return (ContainerWrapperBean) getWebApplicationContext().getBean("containerWrapper");
102   }
103 
104 }