View Javadoc

1   package org.eclipse.jetty.continuation;
2   
3   import java.io.IOException;
4   
5   import javax.servlet.Filter;
6   import javax.servlet.FilterChain;
7   import javax.servlet.FilterConfig;
8   import javax.servlet.ServletContext;
9   import javax.servlet.ServletException;
10  import javax.servlet.ServletRequest;
11  import javax.servlet.ServletResponse;
12  
13  
14  
15  /* ------------------------------------------------------------ */
16  /** ContinuationFilter
17   * <p>
18   * This filter may be applied to webapplication that use the asynchronous 
19   * features of the {@link Continuation} API, but that are deployed in container 
20   * that is neither Jetty (>=6.1) nor a Servlet3.0 container.
21   * The following init parameters may be used to configure the filter (these are mostly for testing):<dl>
22   * <dt>debug</dt><dd>Boolean controlling debug output</dd>
23   * <dt>jetty6</dt><dd>Boolean to force support for jetty 6 continuations)</dd>
24   * <dt>faux</dt><dd>Boolean to force support for faux continuations</dd>
25   * </dl>
26   */
27  public class ContinuationFilter implements Filter
28  {
29      static boolean __debug; // shared debug status
30      private boolean _faux;
31      private boolean _jetty6;
32      private boolean _filtered;
33      ServletContext _context;
34      private boolean _debug;
35  
36      public void init(FilterConfig filterConfig) throws ServletException
37      {
38          boolean jetty_7_or_greater="org.eclipse.jetty.servlet".equals(filterConfig.getClass().getPackage().getName());
39          _context = filterConfig.getServletContext();
40          
41          String param=filterConfig.getInitParameter("debug");
42          _debug=param!=null&&Boolean.parseBoolean(param);
43          if (_debug)
44              __debug=true;
45          
46          param=filterConfig.getInitParameter("jetty6");
47          if (param==null)
48              param=filterConfig.getInitParameter("partial");
49          if (param!=null)
50              _jetty6=Boolean.parseBoolean(param);
51          else
52              _jetty6=ContinuationSupport.__jetty6 && !jetty_7_or_greater;
53  
54          param=filterConfig.getInitParameter("faux");
55          if (param!=null)
56              _faux=Boolean.parseBoolean(param);
57          else
58              _faux=!(jetty_7_or_greater || _jetty6 || _context.getMajorVersion()>=3);
59          
60          _filtered=_faux||_jetty6;
61          if (_debug)
62              _context.log("ContinuationFilter "+
63                      " jetty="+jetty_7_or_greater+
64                      " jetty6="+_jetty6+
65                      " faux="+_faux+
66                      " filtered="+_filtered+
67                      " servlet3="+ContinuationSupport.__servlet3);
68      }
69  
70      public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
71      {
72          if (_filtered)
73          {
74  
75              Continuation c = (Continuation) request.getAttribute(Continuation.ATTRIBUTE);
76              FilteredContinuation fc;
77              if (_faux && (c==null || !(c instanceof FauxContinuation)))
78              {
79                  fc = new FauxContinuation(request);
80                  request.setAttribute(Continuation.ATTRIBUTE,fc);
81              }
82              else
83                  fc=(FilteredContinuation)c;
84  
85              boolean complete=false;
86              while (!complete)
87              {
88                  try
89                  {
90                      if (fc==null || ((FilteredContinuation)fc).enter(response))
91                          chain.doFilter(request,response);
92                  }
93                  catch (ContinuationThrowable e)
94                  {
95                      debug("faux",e);
96                  }
97                  finally
98                  {
99                      if (fc==null)
100                         fc = (FilteredContinuation) request.getAttribute(Continuation.ATTRIBUTE);
101 
102                     complete=fc==null || ((FilteredContinuation)fc).exit();
103                 }
104             }
105         }
106         else
107         {
108             try
109             {
110                 chain.doFilter(request,response);
111             }
112             catch (ContinuationThrowable e)
113             {
114                 debug("caught",e);
115             }
116         }
117     }
118 
119     private void debug(String string)
120     {
121         if (_debug)
122         {
123             _context.log(string);
124         }
125     }
126     
127     private void debug(String string, Throwable th)
128     {
129         if (_debug)
130         {
131             if (th instanceof ContinuationThrowable)
132                 _context.log(string+":"+th);
133             else
134                 _context.log(string,th);
135         }
136     }
137 
138     public void destroy()
139     {
140     }
141 
142     public interface FilteredContinuation extends Continuation
143     {
144         boolean enter(ServletResponse response);
145         boolean exit();
146     }
147 }