View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 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 javax.servlet.ServletException;
23  import javax.servlet.http.HttpServlet;
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  
27  import org.eclipse.jetty.util.log.Log;
28  import org.eclipse.jetty.util.log.Logger;
29  
30  /* ------------------------------------------------------------ */
31  /**
32   * Servlet to upgrade connections to WebSocket
33   * <p/>
34   * The request must have the correct upgrade headers, else it is
35   * handled as a normal servlet request.
36   * <p/>
37   * The initParameter "bufferSize" can be used to set the buffer size,
38   * which is also the max frame byte size (default 8192).
39   * <p/>
40   * The initParameter "maxIdleTime" can be used to set the time in ms
41   * that a websocket may be idle before closing.
42   * <p/>
43   * The initParameter "maxTextMessagesSize" can be used to set the size in characters
44   * that a websocket may be accept before closing.
45   * <p/>
46   * The initParameter "maxBinaryMessagesSize" can be used to set the size in bytes
47   * that a websocket may be accept before closing.
48   * <p/>
49   * The initParameter "minVersion" can be used to set the minimum protocol version
50   * accepted. Default is the RFC6455 version (13)
51   */
52  @SuppressWarnings("serial")
53  public abstract class WebSocketServlet extends HttpServlet implements WebSocketFactory.Acceptor
54  {
55      private final Logger LOG = Log.getLogger(getClass());
56      private WebSocketFactory _webSocketFactory;
57  
58      /* ------------------------------------------------------------ */
59      /**
60       * @see javax.servlet.GenericServlet#init()
61       */
62      @Override
63      public void init() throws ServletException
64      {
65          try
66          {
67              String bs = getInitParameter("bufferSize");
68              _webSocketFactory = new WebSocketFactory(this, bs == null ? 8192 : Integer.parseInt(bs));
69              _webSocketFactory.start();
70  
71              String max = getInitParameter("maxIdleTime");
72              if (max != null)
73                  _webSocketFactory.setMaxIdleTime(Integer.parseInt(max));
74  
75              max = getInitParameter("maxTextMessageSize");
76              if (max != null)
77                  _webSocketFactory.setMaxTextMessageSize(Integer.parseInt(max));
78  
79              max = getInitParameter("maxBinaryMessageSize");
80              if (max != null)
81                  _webSocketFactory.setMaxBinaryMessageSize(Integer.parseInt(max));
82              
83              String min = getInitParameter("minVersion");
84              if (min != null)
85                  _webSocketFactory.setMinVersion(Integer.parseInt(min));
86          }
87          catch (ServletException x)
88          {
89              throw x;
90          }
91          catch (Exception x)
92          {
93              throw new ServletException(x);
94          }
95      }
96  
97      /* ------------------------------------------------------------ */
98      /**
99       * @see javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
100      */
101     @Override
102     protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
103     {
104         if (_webSocketFactory.acceptWebSocket(request, response) || response.isCommitted())
105             return;
106         super.service(request, response);
107     }
108 
109     /* ------------------------------------------------------------ */
110     public boolean checkOrigin(HttpServletRequest request, String origin)
111     {
112         return true;
113     }
114 
115     /* ------------------------------------------------------------ */
116     @Override
117     public void destroy()
118     {
119         try
120         {
121             _webSocketFactory.stop();
122         }
123         catch (Exception x)
124         {
125             LOG.ignore(x);
126         }
127     }
128 }