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>partial</dt><dd>Boolean to force support for partial continuation implementations (eg jetty 6)</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 _partial;
32      ServletContext _context;
33      private boolean _debug;
34  
35      public void init(FilterConfig filterConfig) throws ServletException
36      {
37          boolean jetty_7_or_greater="org.eclipse.jetty.servlet".equals(filterConfig.getClass().getPackage().getName());
38          _context = filterConfig.getServletContext();
39          
40          String param=filterConfig.getInitParameter("debug");
41          _debug=param!=null&&Boolean.parseBoolean(param);
42          if (_debug)
43              __debug=true;
44          
45          param=filterConfig.getInitParameter("partial");
46          if (param!=null)
47              _partial=Boolean.parseBoolean(param);
48          else
49              _partial=ContinuationSupport.__jetty6 && !jetty_7_or_greater;
50  
51          param=filterConfig.getInitParameter("faux");
52          if (param!=null)
53              _faux=Boolean.parseBoolean(param);
54          else
55              _faux=!(jetty_7_or_greater || _partial || _context.getMajorVersion()>=3);
56          
57          if (_debug)
58              _context.log("ContinuationFilter "+
59                      " jetty="+jetty_7_or_greater+
60                      " partial="+_partial+
61                      " jetty6="+ContinuationSupport.__jetty6+
62                      " faux="+_faux+
63                      " servlet3="+ContinuationSupport.__servlet3);
64      }
65  
66      public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
67      {
68          if (_faux)
69          {
70              final FauxContinuation fc = new FauxContinuation(request);
71              request.setAttribute(Continuation.ATTRIBUTE,fc);
72              boolean complete=false;
73        
74              while (!complete)
75              {
76                  try
77                  {
78                      chain.doFilter(request,response);
79                  }
80                  catch (ContinuationThrowable e)
81                  {
82                      debug("faux",e);
83                  }
84                  finally
85                  {
86                      complete=fc.handleSuspension();
87                  }
88              }
89              fc.onComplete();
90          }
91          else if (_partial)
92          {
93              Continuation c = (Continuation) request.getAttribute(Continuation.ATTRIBUTE);
94              try
95              {
96                  if (c==null || !(c instanceof PartialContinuation) || ((PartialContinuation)c).enter())
97                      chain.doFilter(request,response);
98              }
99              finally
100             {
101                 if (c==null)
102                     c = (Continuation) request.getAttribute(Continuation.ATTRIBUTE);
103                 if (c!=null && c instanceof PartialContinuation)
104                     ((PartialContinuation)c).exit();
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 PartialContinuation extends Continuation
144     {
145         boolean enter();
146         void exit();
147     }
148 }