View Javadoc

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