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.server;
20  
21  import java.io.IOException;
22  
23  import javax.servlet.ServletException;
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  
27  import org.eclipse.jetty.server.Request;
28  import org.eclipse.jetty.server.handler.HandlerWrapper;
29  import org.eclipse.jetty.websocket.api.WebSocketBehavior;
30  import org.eclipse.jetty.websocket.api.WebSocketPolicy;
31  import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;
32  
33  public abstract class WebSocketHandler extends HandlerWrapper
34  {
35      /**
36       * Create a simple WebSocketHandler that registers a single WebSocket POJO that is created on every upgrade request.
37       */
38      public static class Simple extends WebSocketHandler
39      {
40          private Class<?> websocketPojo;
41  
42          public Simple(Class<?> websocketClass)
43          {
44              super();
45              this.websocketPojo = websocketClass;
46          }
47  
48          @Override
49          public void configure(WebSocketServletFactory factory)
50          {
51              factory.register(websocketPojo);
52          }
53      }
54  
55      private final WebSocketServletFactory webSocketFactory;
56  
57      public WebSocketHandler()
58      {
59          WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
60          configurePolicy(policy);
61          webSocketFactory = new WebSocketServerFactory(policy);
62          addBean(webSocketFactory);
63      }
64  
65      public abstract void configure(WebSocketServletFactory factory);
66  
67      public void configurePolicy(WebSocketPolicy policy)
68      {
69          /* leave at default */
70      }
71  
72      @Override
73      protected void doStart() throws Exception
74      {
75          configure(webSocketFactory);
76          super.doStart();
77      }
78  
79      public WebSocketServletFactory getWebSocketFactory()
80      {
81          return webSocketFactory;
82      }
83  
84      @Override
85      public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
86      {
87          if (webSocketFactory.isUpgradeRequest(request,response))
88          {
89              // We have an upgrade request
90              if (webSocketFactory.acceptWebSocket(request,response))
91              {
92                  // We have a socket instance created
93                  baseRequest.setHandled(true);
94                  return;
95              }
96              // If we reach this point, it means we had an incoming request to upgrade
97              // but it was either not a proper websocket upgrade, or it was possibly rejected
98              // due to incoming request constraints (controlled by WebSocketCreator)
99              if (response.isCommitted())
100             {
101                 // not much we can do at this point.
102                 return;
103             }
104         }
105         super.handle(target,baseRequest,request,response);
106     }
107 }