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.ServletException;
19  import javax.servlet.http.HttpServlet;
20  import javax.servlet.http.HttpServletRequest;
21  import javax.servlet.http.HttpServletResponse;
22  
23  
24  /* ------------------------------------------------------------ */
25  /**
26   * Servlet to upgrade connections to WebSocket
27   * <p>
28   * The request must have the correct upgrade headers, else it is
29   * handled as a normal servlet request.
30   * <p>
31   * The initParameter "bufferSize" can be used to set the buffer size,
32   * which is also the max frame byte size (default 8192).
33   * <p>
34   * The initParameter "maxIdleTime" can be used to set the time in ms
35   * that a websocket may be idle before closing (default 300,000).
36   * 
37   */
38  public abstract class WebSocketServlet extends HttpServlet implements WebSocketFactory.Acceptor
39  {
40      WebSocketFactory _webSocketFactory;
41         
42      /* ------------------------------------------------------------ */
43      /**
44       * @see javax.servlet.GenericServlet#init()
45       */
46      @Override
47      public void init() throws ServletException
48      {
49          String bs=getInitParameter("bufferSize");
50          _webSocketFactory = new WebSocketFactory(this,bs==null?8192:Integer.parseInt(bs));
51          String mit=getInitParameter("maxIdleTime");
52          if (mit!=null)
53              _webSocketFactory.setMaxIdleTime(Integer.parseInt(mit));
54      }
55  
56      /* ------------------------------------------------------------ */
57      /**
58       * @see javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
59       */
60      @Override
61      protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
62      {   
63          if (_webSocketFactory.acceptWebSocket(request,response) || response.isCommitted())
64              return;
65          super.service(request,response);
66      }
67  
68      public String checkOrigin(HttpServletRequest request, String host, String origin)
69      {
70          if (origin==null)
71              origin=host;
72          return origin;
73      }
74      
75      
76      
77  }