View Javadoc

1   /*******************************************************************************
2    * Copyright (c) 2011 Intalio, Inc.
3    * ======================================================================
4    * All rights reserved. This program and the accompanying materials
5    * are made available under the terms of the Eclipse Public License v1.0
6    * and Apache License v2.0 which accompanies this distribution.
7    *
8    *   The Eclipse Public License is available at
9    *   http://www.eclipse.org/legal/epl-v10.html
10   *
11   *   The Apache License v2.0 is available at
12   *   http://www.opensource.org/licenses/apache2.0.php
13   *
14   * You may elect to redistribute this code under either of these licenses.
15   *******************************************************************************/
16  package org.eclipse.jetty.websocket;
17  
18  import java.io.IOException;
19  import java.util.List;
20  import javax.servlet.http.HttpServletRequest;
21  import javax.servlet.http.HttpServletResponse;
22  
23  import org.eclipse.jetty.io.EndPoint;
24  
25  public class WebSocketServletConnectionD08 extends WebSocketConnectionD08 implements WebSocketServletConnection
26  {
27      private final WebSocketFactory factory;
28  
29      public WebSocketServletConnectionD08(WebSocketFactory factory, WebSocket websocket, EndPoint endpoint, WebSocketBuffers buffers, long timestamp, int maxIdleTime, String protocol,
30              List<Extension> extensions, int draft) throws IOException
31      {
32          super(websocket,endpoint,buffers,timestamp,maxIdleTime,protocol,extensions,draft);
33          this.factory = factory;
34      }
35  
36      /* ------------------------------------------------------------ */
37      public void handshake(HttpServletRequest request, HttpServletResponse response, String subprotocol) throws IOException
38      {
39          String key = request.getHeader("Sec-WebSocket-Key");
40  
41          response.setHeader("Upgrade","WebSocket");
42          response.addHeader("Connection","Upgrade");
43          response.addHeader("Sec-WebSocket-Accept",hashKey(key));
44          if (subprotocol != null)
45          {
46              response.addHeader("Sec-WebSocket-Protocol",subprotocol);
47          }
48  
49          for (Extension ext : getExtensions())
50          {
51              response.addHeader("Sec-WebSocket-Extensions",ext.getParameterizedName());
52          }
53  
54          response.sendError(101);
55  
56          onFrameHandshake();
57          onWebSocketOpen();
58      }
59  
60      @Override
61      public void onClose()
62      {
63          super.onClose();
64          factory.removeConnection(this);
65      }
66  }