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  
77              Continuation c = (Continuation) request.getAttribute(Continuation.ATTRIBUTE);
78              FilteredContinuation fc;
79              if (_faux && (c==null || !(c instanceof FauxContinuation)))
80              {
81                  fc = new FauxContinuation(request);
82                  request.setAttribute(Continuation.ATTRIBUTE,fc);
83              }
84              else
85                  fc=(FilteredContinuation)c;
86  
87              boolean complete=false;
88              while (!complete)
89              {
90                  try
91                  {
92                      if (fc==null || (fc).enter(response))
93                          chain.doFilter(request,response);
94                  }
95                  catch (ContinuationThrowable e)
96                  {
97                      debug("faux",e);
98                  }
99                  finally
100                 {
101                     if (fc==null)
102                         fc = (FilteredContinuation) request.getAttribute(Continuation.ATTRIBUTE);
103 
104                     complete=fc==null || (fc).exit();
105                 }
106             }
107         }
108         else
109         {
110             try
111             {
112                 chain.doFilter(request,response);
113             }
114             catch (ContinuationThrowable e)
115             {
116                 debug("caught",e);
117             }
118         }
119     }
120 
121     private void debug(String string)
122     {
123         if (_debug)
124         {
125             _context.log(string);
126         }
127     }
128     
129     private void debug(String string, Throwable th)
130     {
131         if (_debug)
132         {
133             if (th instanceof ContinuationThrowable)
134                 _context.log(string+":"+th);
135             else
136                 _context.log(string,th);
137         }
138     }
139 
140     public void destroy()
141     {
142     }
143 
144     public interface FilteredContinuation extends Continuation
145     {
146         boolean enter(ServletResponse response);
147         boolean exit();
148     }
149 }