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 _initialized;
30      static boolean __debug; // shared debug status
31      private boolean _faux;
32      private boolean _jetty6;
33      private boolean _filtered;
34      ServletContext _context;
35      private boolean _debug;
36  
37      public void init(FilterConfig filterConfig) throws ServletException
38      {
39          boolean jetty_7_or_greater="org.eclipse.jetty.servlet".equals(filterConfig.getClass().getPackage().getName());
40          _context = filterConfig.getServletContext();
41          
42          String param=filterConfig.getInitParameter("debug");
43          _debug=param!=null&&Boolean.parseBoolean(param);
44          if (_debug)
45              __debug=true;
46          
47          param=filterConfig.getInitParameter("jetty6");
48          if (param==null)
49              param=filterConfig.getInitParameter("partial");
50          if (param!=null)
51              _jetty6=Boolean.parseBoolean(param);
52          else
53              _jetty6=ContinuationSupport.__jetty6 && !jetty_7_or_greater;
54  
55          param=filterConfig.getInitParameter("faux");
56          if (param!=null)
57              _faux=Boolean.parseBoolean(param);
58          else
59              _faux=!(jetty_7_or_greater || _jetty6 || _context.getMajorVersion()>=3);
60          
61          _filtered=_faux||_jetty6;
62          if (_debug)
63              _context.log("ContinuationFilter "+
64                      " jetty="+jetty_7_or_greater+
65                      " jetty6="+_jetty6+
66                      " faux="+_faux+
67                      " filtered="+_filtered+
68                      " servlet3="+ContinuationSupport.__servlet3);
69          _initialized=true;
70      }
71  
72      public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
73      {
74          if (_filtered)
75          {
76              Continuation c = (Continuation) request.getAttribute(Continuation.ATTRIBUTE);
77              FilteredContinuation fc;
78              if (_faux && (c==null || !(c instanceof FauxContinuation)))
79              {
80                  fc = new FauxContinuation(request);
81                  request.setAttribute(Continuation.ATTRIBUTE,fc);
82              }
83              else
84                  fc=(FilteredContinuation)c;
85  
86              boolean complete=false;
87              while (!complete)
88              {
89                  try
90                  {
91                      if (fc==null || (fc).enter(response))
92                          chain.doFilter(request,response);
93                  }
94                  catch (ContinuationThrowable e)
95                  {
96                      debug("faux",e);
97                  }
98                  finally
99                  {
100                     if (fc==null)
101                         fc = (FilteredContinuation) request.getAttribute(Continuation.ATTRIBUTE);
102 
103                     complete=fc==null || (fc).exit();
104                 }
105             }
106         }
107         else
108         {
109             try
110             {
111                 chain.doFilter(request,response);
112             }
113             catch (ContinuationThrowable e)
114             {
115                 debug("caught",e);
116             }
117         }
118     }
119 
120     private void debug(String string)
121     {
122         if (_debug)
123         {
124             _context.log(string);
125         }
126     }
127     
128     private void debug(String string, Throwable th)
129     {
130         if (_debug)
131         {
132             if (th instanceof ContinuationThrowable)
133                 _context.log(string+":"+th);
134             else
135                 _context.log(string,th);
136         }
137     }
138 
139     public void destroy()
140     {
141     }
142 
143     public interface FilteredContinuation extends Continuation
144     {
145         boolean enter(ServletResponse response);
146         boolean exit();
147     }
148 }