View Javadoc

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