View Javadoc

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