View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2014 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.websocket.servlet;
20  
21  import java.io.IOException;
22  import java.util.Iterator;
23  import java.util.ServiceLoader;
24  
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpServletResponse;
27  
28  import org.eclipse.jetty.websocket.api.WebSocketPolicy;
29  import org.eclipse.jetty.websocket.api.extensions.ExtensionFactory;
30  
31  /**
32   * Basic WebSocketServletFactory for working with Jetty-based WebSocketServlets
33   */
34  public interface WebSocketServletFactory
35  {
36      public static class Loader
37      {
38          private static WebSocketServletFactory INSTANCE;
39  
40          public static WebSocketServletFactory create(WebSocketPolicy policy) throws ClassNotFoundException, InstantiationException, IllegalAccessException
41          {
42              return load(policy).createFactory(policy);
43          }
44  
45          public static WebSocketServletFactory load(WebSocketPolicy policy) throws ClassNotFoundException, InstantiationException, IllegalAccessException
46          {
47              if (INSTANCE != null)
48              {
49                  return INSTANCE;
50              }
51              WebSocketServletFactory baseFactory;
52              Iterator<WebSocketServletFactory> factories = ServiceLoader.load(WebSocketServletFactory.class).iterator();
53  
54              if (factories.hasNext())
55              {
56                  baseFactory = factories.next();
57              }
58              else
59              {
60                  // Load the default class if ServiceLoader mechanism isn't valid in this environment. (such as OSGi)
61                  ClassLoader loader = Thread.currentThread().getContextClassLoader();
62                  @SuppressWarnings("unchecked")
63                  Class<WebSocketServletFactory> wssf = (Class<WebSocketServletFactory>)loader
64                          .loadClass("org.eclipse.jetty.websocket.server.WebSocketServerFactory");
65                  baseFactory = wssf.newInstance();
66              }
67  
68              INSTANCE = baseFactory;
69              return INSTANCE;
70          }
71      }
72  
73      public boolean acceptWebSocket(HttpServletRequest request, HttpServletResponse response) throws IOException;
74  
75      public boolean acceptWebSocket(WebSocketCreator creator, HttpServletRequest request, HttpServletResponse response) throws IOException;
76  
77      public void cleanup();
78  
79      public WebSocketServletFactory createFactory(WebSocketPolicy policy);
80  
81      public abstract WebSocketCreator getCreator();
82  
83      public abstract ExtensionFactory getExtensionFactory();
84  
85      /**
86       * Get the base policy in use for WebSockets.
87       * <p>
88       * Note: individual WebSocket implementations can override some of the values in here by using the {@link WebSocket &#064;WebSocket} annotation.
89       * 
90       * @return the base policy
91       */
92      public WebSocketPolicy getPolicy();
93  
94      public void init() throws Exception;
95  
96      public boolean isUpgradeRequest(HttpServletRequest request, HttpServletResponse response);
97  
98      /**
99       * Register a websocket class pojo with the default {@link WebSocketCreator}.
100      * <p>
101      * Note: only required if using the default {@link WebSocketCreator} provided by this factory.
102      * 
103      * @param websocketPojo
104      *            the class to instantiate for each incoming websocket upgrade request.
105      */
106     public void register(Class<?> websocketPojo);
107 
108     public abstract void setCreator(WebSocketCreator creator);
109 }