View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.continuation;
20  
21  import java.lang.reflect.Constructor;
22  
23  import javax.servlet.ServletRequest;
24  import javax.servlet.ServletRequestWrapper;
25  import javax.servlet.ServletResponse;
26  
27  /** 
28   * ContinuationSupport.
29   *
30   * Factory class for accessing Continuation instances, which with either be
31   * a servlet 3.0 or a faux continuation.
32   */
33  public class ContinuationSupport
34  {
35      static final boolean __servlet3;
36      static final Class<?> __waitingContinuation;
37      static final Constructor<? extends Continuation> __newServlet3Continuation;
38      static
39      {
40          boolean servlet3Support=false;
41          Constructor<? extends Continuation>s3cc=null;
42          try
43          {
44              boolean servlet3=ServletRequest.class.getMethod("startAsync")!=null;
45              if (servlet3)
46              {
47                  Class<? extends Continuation> s3c = ContinuationSupport.class.getClassLoader().loadClass("org.eclipse.jetty.continuation.Servlet3Continuation").asSubclass(Continuation.class);
48                  s3cc=s3c.getConstructor(ServletRequest.class);
49                  servlet3Support=true;
50              }
51          }
52          catch (Exception e)
53          {}
54          finally
55          {
56              __servlet3=servlet3Support;
57              __newServlet3Continuation=s3cc;
58          }
59  
60          Class<?> waiting=null;
61          try
62          {
63              waiting=ContinuationSupport.class.getClassLoader().loadClass("org.mortbay.util.ajax.WaitingContinuation");
64          }
65          catch (Exception e)
66          {
67          }
68          finally
69          {
70              __waitingContinuation=waiting;
71          }
72      }
73  
74      /* ------------------------------------------------------------ */
75      /**
76       * Get a Continuation.  The type of the Continuation returned may
77       * vary depending on the container in which the application is
78       * deployed. It may be an implementation native to the container (eg
79       * org.eclipse.jetty.server.AsyncContinuation) or one of the utility
80       * implementations provided such as an internal <code>FauxContinuation</code>
81       * or a real implementation like {@link org.eclipse.jetty.continuation.Servlet3Continuation}.
82       * @param request The request
83       * @return a Continuation instance
84       */
85      public static Continuation getContinuation(ServletRequest request)
86      {
87          Continuation continuation = (Continuation) request.getAttribute(Continuation.ATTRIBUTE);
88          if (continuation!=null)
89              return continuation;
90  
91          while (request instanceof ServletRequestWrapper)
92              request=((ServletRequestWrapper)request).getRequest();
93  
94          if (__servlet3 )
95          {
96              try
97              {
98                  continuation=__newServlet3Continuation.newInstance(request);
99                  request.setAttribute(Continuation.ATTRIBUTE,continuation);
100                 return continuation;
101             }
102             catch(Exception e)
103             {
104                 throw new RuntimeException(e);
105             }
106         }
107 
108         throw new IllegalStateException("!(Jetty || Servlet 3.0 || ContinuationFilter)");
109     }
110 
111     /* ------------------------------------------------------------ */
112     /**
113      * @param request the servlet request
114      * @param response the servlet response
115      * @deprecated use {@link #getContinuation(ServletRequest)}
116      * @return the continuation
117      */
118     @Deprecated
119     public static Continuation getContinuation(final ServletRequest request, final ServletResponse response)
120     {
121         return getContinuation(request);
122     }
123 }