View Javadoc

1   /*
2    * $Id: FilterDispatcherCompatWeblogic61.java 439747 2006-09-03 09:22:46Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.dispatcher;
19  
20  import javax.servlet.Filter;
21  import javax.servlet.FilterConfig;
22  import javax.servlet.ServletContext;
23  import javax.servlet.ServletException;
24  import javax.servlet.http.HttpSession;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.struts2.config.ServletContextSingleton;
29  
30  
31  /***
32   * When running Weblogic Server 6.1, this class should be
33   * specified in web.xml instead of {@link FilterDispatcher}.
34   * <p/>
35   * This class properly handles the weblogic.jar handling
36   * of servlet filters.  There is one serious incompatibility, and
37   * that is that while {@link FilterDispatcher#init(FilterConfig)}
38   * throws a {@link ServletException}, this class's method
39   * {@link #setFilterConfig(FilterConfig)} does not throw
40   * the exception.  Since {@link #setFilterConfig(FilterConfig)}
41   * invokes {@link FilterDispatcher#init(FilterConfig)}, the setter
42   * must "swallow" the exception.  This it does by logging the
43   * exception as an error.
44   *
45   */
46  public class FilterDispatcherCompatWeblogic61
47          extends FilterDispatcher
48          implements Filter {
49  
50      private static Log log =
51              LogFactory.getLog(FilterDispatcherCompatWeblogic61.class);
52  
53      /***
54       * dummy setter for {@link #filterConfig}; this method
55       * sets up the {@link org.apache.struts2.config.ServletContextSingleton} with
56       * the servlet context from the filter configuration.
57       * <p/>
58       * This is needed by Weblogic Server 6.1 because it
59       * uses a slightly obsolete Servlet 2.3-minus spec
60       * whose {@link Filter} interface requires this method.
61       * <p/>
62       *
63       * @param filterConfig the filter configuration.
64       */
65      public void setFilterConfig(FilterConfig filterConfig) {
66          try {
67              init(filterConfig);
68          } catch (ServletException se) {
69              log.error("Couldn't set the filter configuration in this filter", se);
70          }
71  
72          ServletContextSingleton singleton = ServletContextSingleton.getInstance();
73          singleton.setServletContext(filterConfig.getServletContext());
74      }
75  
76      /***
77       * answers the servlet context.
78       * <p/>
79       * Servlet 2.3 specifies that this can be retrieved from
80       * the session.  Unfortunately, weblogic.jar can only retrieve
81       * the servlet context from the filter config.  Hence, this
82       * returns the servlet context from the singleton that was
83       * setup by {@link #setFilterConfig(FilterConfig)}.
84       *
85       * @param session the HTTP session.  Not used
86       * @return the servlet context.
87       */
88      protected ServletContext getServletContext(HttpSession session) {
89          ServletContextSingleton singleton =
90                  ServletContextSingleton.getInstance();
91          return singleton.getServletContext();
92      }
93  
94      /***
95       * This method is required by Weblogic 6.1 SP4 because
96       * they defined this as a required method just before
97       * the Servlet 2.3 specification was finalized.
98       *
99       * @return the filter's filter configuration
100      */
101     public FilterConfig getFilterConfig() {
102         return super.getFilterConfig();
103     }
104 }