View Javadoc

1   // ========================================================================
2   // Copyright (c) 2010 Mort Bay Consulting Pty. Ltd.
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   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.websocket;
15  
16  import java.io.IOException;
17  
18  import javax.servlet.http.HttpServletRequest;
19  import javax.servlet.http.HttpServletResponse;
20  
21  import org.eclipse.jetty.http.HttpParser;
22  import org.eclipse.jetty.io.ConnectedEndPoint;
23  import org.eclipse.jetty.server.HttpConnection;
24  
25  
26  /* ------------------------------------------------------------ */
27  /** Factory to create WebSocket connections
28   */
29  public class WebSocketFactory
30  {
31      private WebSocketBuffers _buffers;
32      private int _maxIdleTime=300000;
33  
34      /* ------------------------------------------------------------ */
35      public WebSocketFactory()
36      {
37          _buffers=new WebSocketBuffers(8192);
38      }
39  
40      /* ------------------------------------------------------------ */
41      public WebSocketFactory(int bufferSize)
42      {
43          _buffers=new WebSocketBuffers(bufferSize);
44      }
45  
46      /* ------------------------------------------------------------ */
47      /** Get the maxIdleTime.
48       * @return the maxIdleTime
49       */
50      public long getMaxIdleTime()
51      {
52          return _maxIdleTime;
53      }
54  
55      /* ------------------------------------------------------------ */
56      /** Set the maxIdleTime.
57       * @param maxIdleTime the maxIdleTime to set
58       */
59      public void setMaxIdleTime(int maxIdleTime)
60      {
61          _maxIdleTime = maxIdleTime;
62      }
63  
64      /* ------------------------------------------------------------ */
65      /** Get the bufferSize.
66       * @return the bufferSize
67       */
68      public int getBufferSize()
69      {
70          return _buffers.getBufferSize();
71      }
72  
73      /* ------------------------------------------------------------ */
74      /** Set the bufferSize.
75       * @param bufferSize the bufferSize to set
76       */
77      public void setBufferSize(int bufferSize)
78      {
79          if (bufferSize!=getBufferSize())
80              _buffers=new WebSocketBuffers(bufferSize);
81      }
82  
83      /* ------------------------------------------------------------ */
84      /** Upgrade the request/response to a WebSocket Connection.
85       * <p>This method will not normally return, but will instead throw a
86       * UpgradeConnectionException, to exit HTTP handling and initiate
87       * WebSocket handling of the connection.
88       * @param request The request to upgrade
89       * @param response The response to upgrade
90       * @param websocket The websocket handler implementation to use
91       * @param origin The origin of the websocket connection
92       * @param subprotocol The protocol
93       * @throws IOException
94       */
95      public void upgrade(HttpServletRequest request,HttpServletResponse response, WebSocket websocket, String origin, String subprotocol)
96       throws IOException
97       {
98          if (!"WebSocket".equals(request.getHeader("Upgrade")))
99              throw new IllegalStateException("!Upgrade:websocket");
100         if (!"HTTP/1.1".equals(request.getProtocol()))
101             throw new IllegalStateException("!HTTP/1.1");
102                 
103         int draft=request.getIntHeader("Sec-WebSocket-Draft");
104         HttpConnection http = HttpConnection.getCurrentConnection();
105         ConnectedEndPoint endp = (ConnectedEndPoint)http.getEndPoint();
106         
107         final WebSocketConnection connection;
108         switch(draft)
109         {
110             default:
111                 connection=new WebSocketConnectionD00(websocket,endp,_buffers,http.getTimeStamp(), _maxIdleTime,draft);
112         }
113         
114         // Let the connection finish processing the handshake
115         connection.handshake(request,response, origin, subprotocol);
116         response.flushBuffer();
117 
118         // Give the connection any unused data from the HTTP connection.
119         connection.fillBuffersFrom(((HttpParser)http.getParser()).getHeaderBuffer());
120         connection.fillBuffersFrom(((HttpParser)http.getParser()).getBodyBuffer());
121 
122         // Tell jetty about the new connection 
123         request.setAttribute("org.eclipse.jetty.io.Connection",connection);
124      }
125 }