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;
20  
21  import java.io.IOException;
22  import java.util.List;
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  
26  import org.eclipse.jetty.io.EndPoint;
27  
28  public class WebSocketServletConnectionD08 extends WebSocketConnectionD08 implements WebSocketServletConnection
29  {
30      private final WebSocketFactory factory;
31  
32      public WebSocketServletConnectionD08(WebSocketFactory factory, WebSocket websocket, EndPoint endpoint, WebSocketBuffers buffers, long timestamp, int maxIdleTime, String protocol,
33              List<Extension> extensions, int draft) throws IOException
34      {
35          super(websocket,endpoint,buffers,timestamp,maxIdleTime,protocol,extensions,draft);
36          this.factory = factory;
37      }
38  
39      /* ------------------------------------------------------------ */
40      public void handshake(HttpServletRequest request, HttpServletResponse response, String subprotocol) throws IOException
41      {
42          String key = request.getHeader("Sec-WebSocket-Key");
43  
44          response.setHeader("Upgrade","WebSocket");
45          response.addHeader("Connection","Upgrade");
46          response.addHeader("Sec-WebSocket-Accept",hashKey(key));
47          if (subprotocol != null)
48          {
49              response.addHeader("Sec-WebSocket-Protocol",subprotocol);
50          }
51  
52          for (Extension ext : getExtensions())
53          {
54              response.addHeader("Sec-WebSocket-Extensions",ext.getParameterizedName());
55          }
56  
57          response.sendError(101);
58  
59          onFrameHandshake();
60          onWebSocketOpen();
61      }
62  
63      @Override
64      public void onClose()
65      {
66          super.onClose();
67          factory.removeConnection(this);
68      }
69  }