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