View Javadoc

1   package org.eclipse.jetty.websocket;
2   
3   import java.io.IOException;
4   
5   import javax.servlet.http.HttpServletRequest;
6   import javax.servlet.http.HttpServletResponse;
7   
8   import org.eclipse.jetty.http.HttpParser;
9   import org.eclipse.jetty.io.ConnectedEndPoint;
10  import org.eclipse.jetty.server.HttpConnection;
11  
12  
13  /* ------------------------------------------------------------ */
14  /** Factory to create WebSocket connections
15   */
16  public class WebSocketFactory
17  {
18      private WebSocketBuffers _buffers;
19      private long _maxIdleTime=300000;
20  
21      /* ------------------------------------------------------------ */
22      public WebSocketFactory()
23      {
24          _buffers=new WebSocketBuffers(8192);
25      }
26  
27      /* ------------------------------------------------------------ */
28      public WebSocketFactory(int bufferSize)
29      {
30          _buffers=new WebSocketBuffers(bufferSize);
31      }
32  
33      /* ------------------------------------------------------------ */
34      /** Get the maxIdleTime.
35       * @return the maxIdleTime
36       */
37      public long getMaxIdleTime()
38      {
39          return _maxIdleTime;
40      }
41  
42      /* ------------------------------------------------------------ */
43      /** Set the maxIdleTime.
44       * @param maxIdleTime the maxIdleTime to set
45       */
46      public void setMaxIdleTime(long maxIdleTime)
47      {
48          _maxIdleTime = maxIdleTime;
49      }
50  
51      /* ------------------------------------------------------------ */
52      /** Get the bufferSize.
53       * @return the bufferSize
54       */
55      public int getBufferSize()
56      {
57          return _buffers.getBufferSize();
58      }
59  
60      /* ------------------------------------------------------------ */
61      /** Set the bufferSize.
62       * @param bufferSize the bufferSize to set
63       */
64      public void setBufferSize(int bufferSize)
65      {
66          if (bufferSize!=getBufferSize())
67              _buffers=new WebSocketBuffers(bufferSize);
68      }
69  
70      /* ------------------------------------------------------------ */
71      /** Upgrade the request/response to a WebSocket Connection.
72       * <p>This method will not normally return, but will instead throw a
73       * UpgradeConnectionException, to exit HTTP handling and initiate
74       * WebSocket handling of the connection.
75       * @param request The request to upgrade
76       * @param response The response to upgrade
77       * @param websocket The websocket handler implementation to use
78       * @param origin The origin of the websocket connection
79       * @param protocol The protocol
80       * @throws UpgradeConnectionException Thrown to upgrade the connection
81       * @throws IOException
82       */
83      public void upgrade(HttpServletRequest request,HttpServletResponse response, WebSocket websocket, String origin, String protocol)
84       throws IOException
85       {
86          if (!"WebSocket".equals(request.getHeader("Upgrade")))
87              throw new IllegalStateException("!Upgrade:websocket");
88          if (!"HTTP/1.1".equals(request.getProtocol()))
89              throw new IllegalStateException("!HTTP/1.1");
90                  
91          HttpConnection http = HttpConnection.getCurrentConnection();
92          ConnectedEndPoint endp = (ConnectedEndPoint)http.getEndPoint();
93          WebSocketConnection connection = new WebSocketConnection(websocket,endp,_buffers,http.getTimeStamp(), _maxIdleTime);
94  
95          String uri=request.getRequestURI();
96          String host=request.getHeader("Host");
97  
98          response.setHeader("Upgrade","WebSocket");
99          response.addHeader("Connection","Upgrade");
100         response.addHeader("WebSocket-Origin",origin);
101         response.addHeader("WebSocket-Location","ws://"+host+uri);
102         if (protocol!=null)
103             response.addHeader("WebSocket-Protocol",protocol);
104         response.sendError(101,"Web Socket Protocol Handshake");
105         response.flushBuffer();
106 
107         connection.fill(((HttpParser)http.getParser()).getHeaderBuffer());
108         connection.fill(((HttpParser)http.getParser()).getBodyBuffer());
109 
110         websocket.onConnect(connection);
111         request.setAttribute("org.eclipse.jetty.io.Connection",connection);
112         response.flushBuffer();
113      }
114 }